-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfile.go
213 lines (182 loc) · 3.8 KB
/
file.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
// 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 (
"fmt"
"io"
"os"
)
// Mode defines a FITS file access mode (r/w)
type Mode int
const (
ReadOnly Mode = Mode(os.O_RDONLY) // open the file read-only
WriteOnly = Mode(os.O_WRONLY) // open the file write-only
ReadWrite = Mode(os.O_RDWR) // open the file read-write
)
// File represents a FITS file.
type File struct {
dec Decoder
enc Encoder
name string
mode Mode
hdus []HDU
}
// Open opens a FITS file in read-only mode.
func Open(r io.Reader) (*File, error) {
var err error
type namer interface {
Name() string
}
name := ""
if r, ok := r.(namer); ok {
name = r.Name()
}
f := &File{
dec: NewDecoder(r),
name: name,
mode: ReadOnly,
hdus: make([]HDU, 0, 1),
}
for {
var hdu HDU
hdu, err = f.dec.DecodeHDU()
if err != nil {
if err != io.EOF {
return nil, err
}
err = nil
break
}
f.hdus = append(f.hdus, hdu)
}
return f, err
}
// Create creates a new FITS file in write-only mode
func Create(w io.Writer) (*File, error) {
var err error
type namer interface {
Name() string
}
name := ""
if w, ok := w.(namer); ok {
name = w.Name()
}
f := &File{
enc: NewEncoder(w),
name: name,
mode: WriteOnly,
hdus: make([]HDU, 0, 1),
}
return f, err
}
// Close releases resources held by a FITS file.
//
// It does not close the underlying io.Reader or io.Writer.
func (f *File) Close() error {
f.enc = nil
f.dec = nil
f.hdus = nil
return nil
}
// Mode returns the access-mode of this FITS file
func (f *File) Mode() Mode {
return f.mode
}
// Name returns the name of the FITS file
func (f *File) Name() string {
return f.name
}
// HDUs returns the list of all Header-Data Unit blocks in the file
func (f *File) HDUs() []HDU {
return f.hdus
}
// HDU returns the i-th HDU
func (f *File) HDU(i int) HDU {
return f.hdus[i]
}
// Get returns the HDU with name `name` or nil
func (f *File) Get(name string) HDU {
i, hdu := f.gethdu(name)
if i < 0 {
return nil
}
return hdu
}
// Has returns whether the File has a HDU with name `name`.
func (f *File) Has(name string) bool {
i, _ := f.gethdu(name)
if i < 0 {
return false
}
return true
}
// get returns the index and HDU of HDU with name `name`.
func (f *File) gethdu(name string) (int, HDU) {
for i, hdu := range f.hdus {
if hdu.Name() == name {
return i, hdu
}
}
return -1, nil
}
// Write writes a HDU to file
func (f *File) Write(hdu HDU) error {
var err error
if f.mode != WriteOnly && f.mode != ReadWrite {
return fmt.Errorf("fitsio: file not open for write")
}
if len(f.hdus) == 0 {
if hdu.Type() != IMAGE_HDU {
return fmt.Errorf("fitsio: file has no primary header. create one first")
}
hdr := hdu.Header()
if hdr.Get("SIMPLE") == nil {
err = hdr.prepend(Card{
Name: "SIMPLE",
Value: true,
Comment: "primary HDU",
})
if err != nil {
return err
}
}
} else {
switch hdu.Type() {
case IMAGE_HDU:
img := hdu.(Image)
err = img.freeze()
if err != nil {
return err
}
case ASCII_TBL, BINARY_TBL:
tbl := hdu.(*Table)
err = tbl.freeze()
if err != nil {
return err
}
}
}
err = f.enc.EncodeHDU(hdu)
if err != nil {
return err
}
err = f.append(hdu)
if err != nil {
return err
}
return err
}
// append appends an HDU to the list of Header-Data Unit blocks.
func (f *File) append(hdu HDU) error {
var err error
if f.mode != WriteOnly && f.mode != ReadWrite {
return fmt.Errorf("fitsio: file not open for write")
}
// mare sure there is only one primary-hdu
if _, ok := hdu.(*primaryHDU); ok && len(f.hdus) != 0 {
return fmt.Errorf("fitsio: file has already a Primary HDU")
}
f.hdus = append(f.hdus, hdu)
return err
}