Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
212 lines (178 loc) · 8.86 KB
/
Copy patherrors.go
File metadata and controls
212 lines (178 loc) · 8.86 KB
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
// Package fits implements a pure-Go reader/writer for the FITS
// (Flexible Image Transport System) file format (FITS Standard v4.0).
// Random-groups HDUs (§6) are located, so the HDUs after them are read,
// but not decoded: (*File).HDU returns ErrRandomGroups for them.
//
// This file defines the public error sentinels and typed errors.
package fits
import (
"errors"
"fmt"
)
// Sentinel errors — match via errors.Is.
var (
// ErrEmptyFile is returned when an input stream is zero bytes.
ErrEmptyFile = errors.New("fits: empty file")
// ErrNotFITS is returned when the first block does not begin with
// "SIMPLE =".
ErrNotFITS = errors.New("fits: not a FITS file")
// ErrKeyNotFound is reserved for keyword lookups at the File level. No
// function in this package returns it today: the typed getters of
// package header return header.ErrKeyNotFound.
ErrKeyNotFound = errors.New("fits: key not found")
// ErrNotImageHDU is reserved for image operations on non-image HDUs.
// No function returns it today (image functions take *ImageHDU, so the
// mismatch is a compile-time error).
ErrNotImageHDU = errors.New("fits: not an image HDU")
// ErrNotImageCompatible is returned by AsImage, Decode and DecodeConfig
// for HDUs that cannot be losslessly represented as an image.Image:
// NAXIS != 2, signed or scaled integer data (other than BITPIX 16 with
// BZERO 32768), BLANK pixels, float values float32 cannot hold exactly,
// BSCALE = 0, and tile-compressed files (the NAXIS = 0 primary).
ErrNotImageCompatible = errors.New("fits: not image.Image-compatible")
// ErrTypeMismatch is returned when a generic read is asked for a target
// type that cannot losslessly represent the on-disk type.
ErrTypeMismatch = errors.New("fits: type mismatch")
// ErrReadOnly is returned when a write or edit operation is attempted on
// a read-only *File.
ErrReadOnly = errors.New("fits: file is read-only")
// ErrShapeMismatch is returned (wrapped) when OverwritePixels,
// WriteImage or AppendImage receive data whose length is not the
// product of the image shape.
ErrShapeMismatch = errors.New("fits: shape mismatch")
// ErrRandomGroups is returned for random-groups (§6) HDUs, which
// this library does not support by design — random groups are a
// legacy radio-interferometry format superseded by binary tables.
ErrRandomGroups = errors.New("fits: random-groups HDUs not supported")
// ErrTiledTableCompression is returned by (*File).HDU for a binary
// table with ZTABLE = T (tiled table compression, FITS 4.0 §10.3, as
// written by fpack -table), which this library does not decode: read
// as a plain table its rows and columns would be the compressed
// bytes, not the data.
ErrTiledTableCompression = errors.New("fits: tiled table compression (ZTABLE = T) not supported")
// ErrUnsupportedExtension is returned (as an *UnsupportedExtensionError)
// by (*File).HDU for an extension whose XTENSION value is not one this
// library decodes (IMAGE, TABLE, BINTABLE and the legacy aliases
// IUEIMAGE, A3DTABLE, 3DTABLE), e.g. 'FOREIGN' or 'DUMP'. The HDU is
// still framed (its size follows FITS 4.0 eq. 2, as for any
// extension), so the HDUs after it are read normally. cfitsio reads
// such an HDU as an IMAGE_HDU; this library does not treat its bytes
// as pixels.
ErrUnsupportedExtension = errors.New("fits: unsupported extension type")
)
// UnsupportedExtensionError reports an extension of a type this library
// does not decode. errors.Is(err, ErrUnsupportedExtension) is true for it.
type UnsupportedExtensionError struct {
Index int // HDU index (0 = primary)
XTension string // the XTENSION value, trailing blanks removed
}
func (e *UnsupportedExtensionError) Error() string {
return fmt.Sprintf("fits: HDU %d: unsupported extension type XTENSION = '%s'", e.Index, e.XTension)
}
// Is reports whether target is ErrUnsupportedExtension.
func (e *UnsupportedExtensionError) Is(target error) bool { return target == ErrUnsupportedExtension }
// ErrUnterminatedHeader describes a header that does not contain an END card
// within a generous block limit.
type ErrUnterminatedHeader struct {
HDU int
BytesScanned int64
}
func (e *ErrUnterminatedHeader) Error() string {
return fmt.Sprintf("fits: HDU %d: unterminated header (scanned %d bytes)", e.HDU, e.BytesScanned)
}
// ErrTruncatedData describes a data section that ends before the declared
// data size.
type ErrTruncatedData struct {
HDU int
Expected int64
Got int64
}
func (e *ErrTruncatedData) Error() string {
return fmt.Sprintf("fits: HDU %d: data truncated: expected %d bytes, got %d", e.HDU, e.Expected, e.Got)
}
// ErrDuplicateKeyword is matched (errors.Is) by *DuplicateKeywordError.
var ErrDuplicateKeyword = errors.New("fits: duplicate structural keyword")
// DuplicateKeywordError reports a header that carries a structural keyword
// (SIMPLE, XTENSION, BITPIX, NAXIS, NAXISn for n <= NAXIS, PCOUNT, GCOUNT,
// TFIELDS) more than once with different values. FITS 4.0 §4.1.2.3: the
// mandatory keywords must not appear more than once, and "if a keyword
// does appear multiple times with different values, then the value is
// indeterminate"; cfitsio reads BITPIX, NAXIS and NAXISn by card position
// and takes the last PCOUNT/GCOUNT of an image, so such a file has no
// single reading, and Open refuses it instead of guessing the element
// size or the HDU length. A keyword repeated with the same value reads
// the same either way and is accepted.
type DuplicateKeywordError struct {
HDU int
Keyword string
}
func (e *DuplicateKeywordError) Error() string {
return fmt.Sprintf("fits: HDU %d: structural keyword %q appears more than once", e.HDU, e.Keyword)
}
func (e *DuplicateKeywordError) Is(target error) bool { return target == ErrDuplicateKeyword }
// ErrInvalidScaling is returned (wrapped, with the reason) when the
// BSCALE/BZERO/BLANK cards of an image are refused by the scaling-card
// rule (scalingCards in scaling.go: a card cfitsio and astropy read
// differently, e.g. a value that is not a FITS number, a HIERARCH BSCALE,
// a BLANK outside the BITPIX range or equal to 1234554321), by every read
// of the pixels (ReadPixels, ReadPixelsMasked, ReadFloat32, AsImage, the
// tile-compressed readers), by ImageHDU.BSCALE / BZERO / BLANK and by
// OverwritePixels.
//
// WriteImage also returns it for the caller's header when, beyond that
// rule, BSCALE = 0 (cfitsio's fits_set_bscale refuses it with
// ZERO_SCALE), a keyword is given twice, or a card cannot be encoded;
// OverwritePixels when the HDU's BSCALE is 0.
var ErrInvalidScaling = errors.New("fits: invalid image scaling keywords")
// ErrUndefinedChecksum is returned by ChecksumStatus and VerifyChecksum
// for an HDU whose DATASUM or CHECKSUM card has no value at all (cfitsio's
// fits_verify_chksum fails with VALUE_UNDEFINED there).
var ErrUndefinedChecksum = errors.New("fits: DATASUM or CHECKSUM card has no value")
// ErrMissingRequiredKeyword describes a missing mandatory keyword.
type ErrMissingRequiredKeyword struct {
HDU int
Keyword string
}
func (e *ErrMissingRequiredKeyword) Error() string {
return fmt.Sprintf("fits: HDU %d: missing required keyword %q", e.HDU, e.Keyword)
}
// TypeMismatchError carries detail about an ErrTypeMismatch failure.
// BSCALE and BZERO are the image's scaling (1 and 0 when absent or not
// applicable).
type TypeMismatchError struct {
Requested string
BITPIX int
Lossy bool
BSCALE float64
BZERO float64
}
func (e *TypeMismatchError) Error() string {
if e.BSCALE != 0 && (e.BSCALE != 1 || e.BZERO != 0) {
return fmt.Sprintf("fits: cannot represent BITPIX=%d data with BSCALE=%v BZERO=%v as %s (lossy=%v)", e.BITPIX, e.BSCALE, e.BZERO, e.Requested, e.Lossy)
}
return fmt.Sprintf("fits: cannot represent BITPIX=%d data as %s (lossy=%v)", e.BITPIX, e.Requested, e.Lossy)
}
func (e *TypeMismatchError) Is(target error) bool {
return target == ErrTypeMismatch
}
// KeyNotFoundError carries detail about an ErrKeyNotFound failure (not
// returned by any function today; see ErrKeyNotFound).
type KeyNotFoundError struct {
Key string
HDU int
}
func (e *KeyNotFoundError) Error() string {
return fmt.Sprintf("fits: HDU %d: key %q not found", e.HDU, e.Key)
}
func (e *KeyNotFoundError) Is(target error) bool {
return target == ErrKeyNotFound
}
// ErrNumOverflow is returned by WriteImage and OverwritePixels, which then
// write nothing, when a value cannot be stored under the image's BITPIX
// and BSCALE/BZERO: for integer BITPIX, (value - BZERO) / BSCALE lies
// outside the stored range (BITPIX 8: 0..255, 16/32/64: the signed type)
// by more than cfitsio's 0.49 rounding margin, which is where cfitsio's
// fits_write_img reports NUM_OVERFLOW (412) after storing the range limit;
// for float BITPIX, a finite value whose scaled form is not finite in the
// stored type (cfitsio stores the infinity without an error).
var ErrNumOverflow = errors.New("fits: value out of range for the stored type")