github.com/jjjabc/fitsio@v0.0.0-20161215022839-d1807e9e818e/phdu.go (about) 1 // Copyright 2015 The astrogo Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fitsio 6 7 import "reflect" 8 9 type primaryHDU struct { 10 imageHDU 11 } 12 13 // Name returns the value of the 'EXTNAME' Card (or "PRIMARY" if none) 14 func (hdu *primaryHDU) Name() string { 15 card := hdu.hdr.Get("EXTNAME") 16 if card == nil { 17 return "PRIMARY" 18 } 19 return card.Value.(string) 20 } 21 22 // Version returns the value of the 'EXTVER' Card (or 1 if none) 23 func (hdu *primaryHDU) Version() int { 24 card := hdu.hdr.Get("EXTVER") 25 if card == nil { 26 return 1 27 } 28 rv := reflect.ValueOf(card.Value) 29 return int(rv.Int()) 30 } 31 32 // NewPrimaryHDU creates a new PrimaryHDU with Header hdr. 33 // If hdr is nil, a default Header will be created. 34 func NewPrimaryHDU(hdr *Header) (Image, error) { 35 var err error 36 37 if hdr == nil { 38 hdr = NewDefaultHeader() 39 } 40 41 // add default cards (SIMPLE, BITPIX, NAXES, AXIS1, AXIS2) 42 keys := make(map[string]struct{}, len(hdr.cards)) 43 for i := range hdr.cards { 44 card := &hdr.cards[i] 45 k := card.Name 46 keys[k] = struct{}{} 47 } 48 49 cards := make([]Card, 0, 3) 50 if _, ok := keys["SIMPLE"]; !ok { 51 cards = append(cards, Card{ 52 Name: "SIMPLE", 53 Value: true, 54 Comment: "primary HDU", 55 }) 56 } 57 58 if _, ok := keys["BITPIX"]; !ok { 59 cards = append(cards, Card{ 60 Name: "BITPIX", 61 Value: hdr.Bitpix(), 62 Comment: "number of bits per data pixel", 63 }) 64 } 65 66 if _, ok := keys["NAXIS"]; !ok { 67 cards = append(cards, Card{ 68 Name: "NAXIS", 69 Value: len(hdr.Axes()), 70 Comment: "number of data axes", 71 }) 72 } 73 74 if len(hdr.Axes()) >= 1 { 75 if _, ok := keys["NAXIS1"]; !ok { 76 cards = append(cards, Card{ 77 Name: "NAXIS1", 78 Value: hdr.Axes()[0], 79 Comment: "length of data axis 1", 80 }) 81 } 82 } 83 84 if len(hdr.Axes()) >= 2 { 85 if _, ok := keys["NAXIS2"]; !ok { 86 cards = append(cards, Card{ 87 Name: "NAXIS2", 88 Value: hdr.Axes()[1], 89 Comment: "length of data axis 2", 90 }) 91 } 92 } 93 94 phdr := *hdr 95 phdr.cards = append(cards, hdr.cards...) 96 hdu := &primaryHDU{ 97 imageHDU{ 98 hdr: phdr, 99 raw: make([]byte, 0), 100 }, 101 } 102 103 return hdu, err 104 }