-
Notifications
You must be signed in to change notification settings - Fork 3
/
header_test.go
299 lines (274 loc) · 6.08 KB
/
header_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package cfitsio
import (
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestHeaderRW(t *testing.T) {
curdir, err := os.Getwd()
if err != nil {
t.Fatalf(err.Error())
}
defer os.Chdir(curdir)
workdir, err := ioutil.TempDir("", "go-cfitsio-test-")
if err != nil {
t.Fatalf(err.Error())
}
defer os.RemoveAll(workdir)
err = os.Chdir(workdir)
if err != nil {
t.Fatalf(err.Error())
}
table := struct {
name string
version int
cards []Card
bitpix int64
axes []int64
image interface{}
}{
name: "new.fits",
version: 2,
cards: []Card{
{
"EXTNAME",
"primary hdu",
"the primary HDU",
},
{
"EXTVER",
2,
"the primary hdu version",
},
{
"CARD_UINT8",
byte(42),
"an uint8",
},
{
"CARD_UINT16",
uint16(42),
"an uint16",
},
{
"CARD_UINT32",
uint32(42),
"an uint32",
},
{
"CARD_UINT64",
uint64(42),
"an uint64",
},
{
"CARD_INT8",
int8(42),
"an int8",
},
{
"CARD_INT16",
int16(42),
"an int16",
},
{
"CARD_INT32",
int32(42),
"an int32",
},
{
"CARD_INT64",
int64(42),
"an int64",
},
{
"CARD_INT3264",
int(42),
"an int",
},
{
"CARD_UINT",
uint(42),
"an uint",
},
{
"CARD_FLOAT32",
float32(666),
"a float32",
},
{
"CARD_FLOAT64",
float64(666),
"a float64",
},
{
"CARD_COMPLEX64",
complex(float32(42), float32(66)),
"a complex64",
},
{
"CARD_COMPLEX128",
complex(float64(42), float64(66)),
"a complex128",
},
},
bitpix: 8,
axes: []int64{3, 4},
}
fname := "new.fits"
for _, fct := range []func(){
// create
func() {
f, err := Create(fname)
if err != nil {
t.Fatalf("error creating new file [%v]: %v", fname, err)
}
defer f.Close()
phdr := NewHeader(
table.cards,
IMAGE_HDU,
table.bitpix,
table.axes,
)
phdu, err := NewPrimaryHDU(&f, phdr)
if err != nil {
t.Fatalf("error creating PHDU: %v", err)
}
defer phdu.Close()
hdr := phdu.Header()
if hdr.bitpix != table.bitpix {
t.Fatalf("expected BITPIX=%v. got %v", table.bitpix, hdr.bitpix)
}
name := phdu.Name()
if name != "primary hdu" {
t.Fatalf("expected EXTNAME==%q. got %q", "primary hdu", name)
}
vers := phdu.Version()
if vers != table.version {
t.Fatalf("expected EXTVER==%v. got %v", table.version, vers)
}
card := hdr.Get("EXTNAME")
if card == nil {
t.Fatalf("error retrieving card [EXTNAME]")
}
if card.Comment != "the primary HDU" {
t.Fatalf("expected EXTNAME.Comment==%q. got %q", "the primary HDU", card.Comment)
}
card = hdr.Get("EXTVER")
if card == nil {
t.Fatalf("error retrieving card [EXTVER]")
}
if card.Comment != "the primary hdu version" {
t.Fatalf("expected EXTVER.Comment==%q. got %q", "the primary hdu version", card.Comment)
}
for _, ref := range table.cards {
card := hdr.Get(ref.Name)
if card == nil {
t.Fatalf("error retrieving card [%v]", ref.Name)
}
rv := reflect.ValueOf(ref.Value)
var val interface{}
switch rv.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val = rv.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val = int64(rv.Uint())
case reflect.Float32, reflect.Float64:
val = rv.Float()
case reflect.Complex64, reflect.Complex128:
val = rv.Complex()
case reflect.String:
val = ref.Value.(string)
}
if !reflect.DeepEqual(card.Value, val) {
t.Fatalf(
"card %q. expected [%v](%T). got [%v](%T)",
ref.Name,
val, val,
card.Value, card.Value,
)
}
if card.Comment != ref.Comment {
t.Fatalf("card %q. comment differ. expected %q. got %q", ref.Name, ref.Comment, card.Comment)
}
}
card = hdr.Get("NOT THERE")
if card != nil {
t.Fatalf("expected no card. got [%v]", card)
}
},
// read-back
func() {
f, err := Open(fname, ReadOnly)
if err != nil {
t.Fatalf("error opening file [%v]: %v", fname, err)
}
defer f.Close()
hdu := f.HDU(0)
hdr := hdu.Header()
if hdr.bitpix != table.bitpix {
t.Fatalf("expected BITPIX=%v. got %v", 8, hdr.bitpix)
}
name := hdu.Name()
if name != "primary hdu" {
t.Fatalf("expected EXTNAME==%q. got %q", "primary hdu", name)
}
vers := hdu.Version()
if vers != table.version {
t.Fatalf("expected EXTVER==%v. got %v", 2, vers)
}
card := hdr.Get("EXTNAME")
if card == nil {
t.Fatalf("error retrieving card [EXTNAME]")
}
if card.Comment != "the primary HDU" {
t.Fatalf("expected EXTNAME.Comment==%q. got %q", "the primary HDU", card.Comment)
}
card = hdr.Get("EXTVER")
if card == nil {
t.Fatalf("error retrieving card [EXTVER]")
}
if card.Comment != "the primary hdu version" {
t.Fatalf("expected EXTVER.Comment==%q. got %q", "the primary hdu version", card.Comment)
}
for _, ref := range table.cards {
card := hdr.Get(ref.Name)
if card == nil {
t.Fatalf("error retrieving card [%v]", ref.Name)
}
rv := reflect.ValueOf(ref.Value)
var val interface{}
switch rv.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val = rv.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val = int64(rv.Uint())
case reflect.Float32, reflect.Float64:
val = rv.Float()
case reflect.Complex64, reflect.Complex128:
val = rv.Complex()
case reflect.String:
val = ref.Value.(string)
}
if !reflect.DeepEqual(card.Value, val) {
t.Fatalf(
"card %q. expected [%v](%T). got [%v](%T)",
ref.Name,
val, val,
card.Value, card.Value,
)
}
if card.Comment != ref.Comment {
t.Fatalf("card %q. comment differ. expected %q. got %q", ref.Name, ref.Comment, card.Comment)
}
}
card = hdr.Get("NOT THERE")
if card != nil {
t.Fatalf("expected no card. got [%v]", card)
}
},
} {
fct()
}
}
// EOF