-
Notifications
You must be signed in to change notification settings - Fork 22
/
column.go
737 lines (518 loc) · 15.4 KB
/
column.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Copyright 2015 The astrogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fitsio
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
)
// Column represents a column in a FITS table
type Column struct {
Name string // column name, corresponding to ``TTYPE`` keyword
Format string // column format, corresponding to ``TFORM`` keyword
Unit string // column unit, corresponding to ``TUNIT`` keyword
Null string // null value, corresponding to ``TNULL`` keyword
Bscale float64 // bscale value, corresponding to ``TSCAL`` keyword
Bzero float64 // bzero value, corresponding to ``TZERO`` keyword
Display string // display format, corresponding to ``TDISP`` keyword
Dim []int64 // column dimension corresponding to ``TDIM`` keyword
Start int64 // column starting position, corresponding to ``TBCOL`` keyword
dtype Type // type of the value held by the column
offset int // offset in bytes (from start of data-pad or heap-pad) to get at the column data
txtfmt string // go-based fmt string for ASCII table
// read function (binary/ascii)
read func(table *Table, icol int, irow int64, ptr interface{}) error
// write function (binary/ascii)
write func(table *Table, icol int, irow int64, ptr interface{}) error
}
// NewColumn creates a new Column with name `name` and Format inferred from the type of value
func NewColumn(name string, v interface{}) (Column, error) {
panic("not implemented")
}
// Type returns the Go reflect.Type associated with this Column
func (col *Column) Type() reflect.Type {
return col.dtype.gotype
}
// readBin reads the value at column number icol and row irow, into ptr.
func (col *Column) readBin(table *Table, icol int, irow int64, ptr interface{}) error {
var err error
rv := reflect.Indirect(reflect.ValueOf(ptr))
rt := reflect.TypeOf(rv.Interface())
switch rt.Kind() {
case reflect.Slice:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
row := table.data[beg:end]
r := newReader(row)
slice := reflect.ValueOf(ptr).Elem()
nmax := 0
switch col.dtype.dsize {
case 8:
var n int32
var offset int32
r.readI32(&n)
r.readI32(&offset)
beg = int(offset)
end = beg + int(n)*int(col.dtype.gotype.Elem().Size())
nmax = int(n)
case 16:
var n int64
var offset int64
r.readI64(&n)
r.readI64(&offset)
beg = int(offset)
end = beg + int(n)*int(col.dtype.gotype.Elem().Size())
nmax = int(n)
}
if slice.Len() < nmax {
slice = reflect.MakeSlice(rt, nmax, nmax)
}
r = newReader(table.heap[beg:end])
switch slice := slice.Interface().(type) {
case []bool:
r.readBools(slice[:nmax])
case []byte:
copy(slice[:nmax], r.p)
case []int8:
r.readI8s(slice[:nmax])
case []int16:
r.readI16s(slice[:nmax])
case []int32:
r.readI32s(slice[:nmax])
case []int64:
r.readI64s(slice[:nmax])
case []int:
r.readInts(slice[:nmax])
case []uint16:
r.readU16s(slice[:nmax])
case []uint32:
r.readU32s(slice[:nmax])
case []uint64:
r.readU64s(slice[:nmax])
case []uint:
r.readUints(slice[:nmax])
case []float32:
r.readF32s(slice[:nmax])
case []float64:
r.readF64s(slice[:nmax])
case []complex64:
r.readC64s(slice[:nmax])
case []complex128:
r.readC128s(slice[:nmax])
default:
panic(fmt.Errorf("fitsio: not implemented %T", slice))
}
rv.Set(slice)
case reflect.Array:
beg := table.rowsz*int(irow) + col.offset
end := beg + (col.dtype.dsize * col.dtype.len)
row := table.data[beg:end]
r := newReader(row)
switch slice := rv.Slice(0, rv.Len()).Interface().(type) {
case []bool:
r.readBools(slice)
case []byte:
copy(slice, r.p)
case []int8:
r.readI8s(slice)
case []int16:
r.readI16s(slice)
case []int32:
r.readI32s(slice)
case []int64:
r.readI64s(slice)
case []int:
r.readInts(slice)
case []uint16:
r.readU16s(slice)
case []uint32:
r.readU32s(slice)
case []uint64:
r.readU64s(slice)
case []uint:
r.readUints(slice)
case []float32:
r.readF32s(slice)
case []float64:
r.readF64s(slice)
case []complex64:
r.readC64s(slice)
case []complex128:
r.readC128s(slice)
default:
panic(fmt.Errorf("fitsio: not implemented %T", slice))
}
case reflect.Bool:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readBool(ptr.(*bool))
case reflect.Int8:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readI8(ptr.(*int8))
case reflect.Int16:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readI16(ptr.(*int16))
case reflect.Int32:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readI32(ptr.(*int32))
case reflect.Int64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readI64(ptr.(*int64))
case reflect.Int:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readInt(ptr.(*int))
case reflect.Uint8:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readU8(ptr.(*uint8))
case reflect.Uint16:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readU16(ptr.(*uint16))
case reflect.Uint32:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readU32(ptr.(*uint32))
case reflect.Uint64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readU64(ptr.(*uint64))
case reflect.Uint:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readUint(ptr.(*uint))
case reflect.Float32:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readF32(ptr.(*float32))
case reflect.Float64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readF64(ptr.(*float64))
case reflect.Complex64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readC64(ptr.(*complex64))
case reflect.Complex128:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
r := newReader(table.data[beg:end])
r.readC128(ptr.(*complex128))
case reflect.String:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
row := table.data[beg:end]
str := ""
if row[0] == '\x00' {
str = string(row[1:])
str = strings.TrimRight(str, string([]byte("\x00")))
} else {
str = string(row)
}
rv.SetString(str)
default:
return fmt.Errorf("fitsio: binary-table can not read/write %v", rt.Kind())
}
return err
}
// writeBin writes the value at column number icol and row irow, from ptr.
func (col *Column) writeBin(table *Table, icol int, irow int64, ptr interface{}) error {
var (
err error
rv = reflect.Indirect(reflect.ValueOf(ptr))
rvi = rv.Interface()
rt = reflect.TypeOf(rvi)
)
switch rt.Kind() {
case reflect.Slice:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
nmax := rv.Len()
switch col.dtype.dsize {
case 8:
w.writeI32(int32(nmax))
w.writeI32(int32(len(table.heap)))
case 16:
w.writeI64(int64(nmax))
w.writeI64(int64(len(table.heap)))
}
{
w := newWriter(make([]byte, nmax*col.dtype.hsize))
switch slice := rvi.(type) {
case []bool:
w.writeBools(slice)
case []byte:
copy(w.p, slice)
case []int8:
w.writeI8s(slice)
case []int16:
w.writeI16s(slice)
case []int32:
w.writeI32s(slice)
case []int64:
w.writeI64s(slice)
case []int:
w.writeInts(slice)
case []uint16:
w.writeU16s(slice)
case []uint32:
w.writeU32s(slice)
case []uint64:
w.writeU64s(slice)
case []uint:
w.writeUints(slice)
case []float32:
w.writeF32s(slice)
case []float64:
w.writeF64s(slice)
case []complex64:
w.writeC64s(slice)
case []complex128:
w.writeC128s(slice)
default:
panic(fmt.Errorf("fitsio: not implemented %T", slice))
}
table.heap = append(table.heap, w.bytes()...)
}
case reflect.Array:
beg := table.rowsz*int(irow) + col.offset
end := beg + (col.dtype.dsize * col.dtype.len)
w := newWriter(table.data[beg:end])
switch slice := rv.Slice(0, rv.Len()).Interface().(type) {
case []bool:
w.writeBools(slice)
case []byte:
copy(w.p, slice)
case []int8:
w.writeI8s(slice)
case []int16:
w.writeI16s(slice)
case []int32:
w.writeI32s(slice)
case []int64:
w.writeI64s(slice)
case []int:
w.writeInts(slice)
case []uint16:
w.writeU16s(slice)
case []uint32:
w.writeU32s(slice)
case []uint64:
w.writeU64s(slice)
case []uint:
w.writeUints(slice)
case []float32:
w.writeF32s(slice)
case []float64:
w.writeF64s(slice)
case []complex64:
w.writeC64s(slice)
case []complex128:
w.writeC128s(slice)
default:
panic(fmt.Errorf("fitsio: not implemented %T", slice))
}
case reflect.Bool:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeBool(rvi.(bool))
case reflect.Int8:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeI8(rvi.(int8))
case reflect.Int16:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeI16(rvi.(int16))
case reflect.Int32:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeI32(rvi.(int32))
case reflect.Int64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeI64(rvi.(int64))
case reflect.Int:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeInt(rvi.(int))
case reflect.Uint8:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeU8(rvi.(uint8))
case reflect.Uint16:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeU16(rvi.(uint16))
case reflect.Uint32:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeU32(rvi.(uint32))
case reflect.Uint64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeU64(rvi.(uint64))
case reflect.Uint:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeUint(rvi.(uint))
case reflect.Float32:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeF32(rvi.(float32))
case reflect.Float64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeF64(rvi.(float64))
case reflect.Complex64:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeC64(rvi.(complex64))
case reflect.Complex128:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
w.writeC128(rvi.(complex128))
case reflect.String:
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
buf := newWriter(table.data[beg:end])
str := rv.String()
data := make([]byte, 0, len(str)+1)
data = append(data, '\x00')
data = append(data, []byte(str)...)
n := len(data)
if n > end-beg {
n = end - beg
}
if n < end-beg {
data = append(data, bytes.Repeat([]byte("\x00"), end-beg-n)...)
n = len(data)
}
_, err = buf.Write(data[:n])
if err != nil {
return fmt.Errorf("fitsio: %v\n", err)
}
default:
return fmt.Errorf("fitsio: binary-table can not read/write %v", rt.Kind())
}
return err
}
// readTxt reads the value at column number icol and row irow, into ptr.
func (col *Column) readTxt(table *Table, icol int, irow int64, ptr interface{}) error {
var err error
rv := reflect.Indirect(reflect.ValueOf(ptr))
rt := reflect.TypeOf(rv.Interface())
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
buf := table.data[beg:end]
str := strings.TrimSpace(string(buf))
switch rt.Kind() {
case reflect.Slice:
return fmt.Errorf("fitsio: ASCII-table can not read/write slices")
case reflect.Array:
return fmt.Errorf("fitsio: ASCII-table can not read/write arrays")
case reflect.Bool:
return fmt.Errorf("fitsio: ASCII-table can not read/write booleans")
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return fmt.Errorf("fitsio: error parsing %q into a uint: %v", str, err)
}
rv.SetUint(v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return fmt.Errorf("fitsio: error parsing %q into an int: %v", str, err)
}
rv.SetInt(v)
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(str, 64)
if err != nil {
return fmt.Errorf("fitsio: error parsing %q into a float: %v", str, err)
}
rv.SetFloat(v)
case reflect.Complex64, reflect.Complex128:
return fmt.Errorf("fitsio: ASCII-table can not read/write complexes")
case reflect.String:
rv.SetString(str)
default:
return fmt.Errorf("fitsio: ASCII-table can not read/write %v", rt.Kind())
}
return err
}
// writeTxt writes the value at column number icol and row irow, from ptr.
func (col *Column) writeTxt(table *Table, icol int, irow int64, ptr interface{}) error {
var err error
beg := table.rowsz*int(irow) + col.offset
end := beg + col.dtype.dsize
w := newWriter(table.data[beg:end])
rv := reflect.Indirect(reflect.ValueOf(ptr))
rt := reflect.TypeOf(rv.Interface())
switch rt.Kind() {
case reflect.Slice:
return fmt.Errorf("fitsio: ASCII-table can not read/write slices")
case reflect.Array:
return fmt.Errorf("fitsio: ASCII-table can not read/write arrays")
case reflect.Bool:
return fmt.Errorf("fitsio: ASCII-table can not read/write booleans")
case reflect.Complex64, reflect.Complex128:
return fmt.Errorf("fitsio: ASCII-table can not read/write complexes")
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64,
reflect.String:
str := fmt.Sprintf(col.txtfmt, rv.Interface())
n := 0
n, err = fmt.Fprintf(w, str)
if err != nil {
return fmt.Errorf("fitsio: error writing '%#v': %v", rv.Interface(), err)
}
if n != len(w.p) {
return fmt.Errorf(
"fitsio: error writing '%#v'. expected %d bytes. wrote %d",
rv.Interface(), len(w.p), n,
)
}
default:
return fmt.Errorf("fitsio: ASCII-table can not read/write %v", rt.Kind())
}
return err
}