1 package nom.tam.fits;
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 import java.io.IOException;
35
36 import nom.tam.fits.header.Bitpix;
37 import nom.tam.fits.header.Standard;
38 import nom.tam.util.ArrayDataInput;
39 import nom.tam.util.ArrayDataOutput;
40 import nom.tam.util.ArrayFuncs;
41 import nom.tam.util.Cursor;
42 import nom.tam.util.FitsEncoder;
43
44 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
45
46
47
48
49
50
51
52
53 public class UndefinedData extends Data {
54
55 private static final String XTENSION_UNKNOWN = "UNKNOWN";
56
57
58 private Bitpix bitpix = Bitpix.BYTE;
59 private int[] dims;
60 private int byteSize = 0;
61 private byte[] data;
62 private int pCount = 0;
63 private int gCount = 1;
64
65 private String extensionType = XTENSION_UNKNOWN;
66
67
68
69
70
71
72
73
74
75
76
77 public UndefinedData(Header h) throws FitsException {
78 extensionType = h.getStringValue(Standard.XTENSION, XTENSION_UNKNOWN);
79
80 int naxis = h.getIntValue(Standard.NAXIS);
81
82 dims = new int[naxis];
83
84 int size = naxis > 0 ? 1 : 0;
85 for (int i = 1; i <= naxis; i++) {
86 dims[naxis - i] = h.getIntValue(Standard.NAXISn.n(i));
87 size *= dims[naxis - i];
88 }
89
90 pCount = h.getIntValue(Standard.PCOUNT);
91 size += pCount;
92
93 gCount = h.getIntValue(Standard.GCOUNT);
94 if (gCount > 1) {
95 size *= h.getIntValue(Standard.GCOUNT);
96 }
97
98 bitpix = Bitpix.fromHeader(h);
99 size *= bitpix.byteSize();
100
101 byteSize = size;
102 }
103
104
105
106
107
108
109
110
111
112
113 public UndefinedData(Object x) throws IllegalArgumentException {
114 byteSize = (int) FitsEncoder.computeSize(x);
115 dims = ArrayFuncs.getDimensions(x);
116 data = new byte[byteSize];
117 ArrayFuncs.copyInto(x, data);
118 }
119
120 @SuppressWarnings("deprecation")
121 @Override
122 protected void fillHeader(Header head) {
123
124
125 head.deleteKey(Standard.SIMPLE);
126 head.deleteKey(Standard.EXTEND);
127
128 Standard.context(UndefinedData.class);
129
130 Cursor<String, HeaderCard> c = head.iterator();
131 c.add(HeaderCard.create(Standard.XTENSION, extensionType));
132 c.add(HeaderCard.create(Standard.BITPIX, bitpix.getHeaderValue()));
133
134 c.add(HeaderCard.create(Standard.NAXIS, dims.length));
135
136 for (int i = 1; i <= dims.length; i++) {
137 c.add(HeaderCard.create(Standard.NAXISn.n(i), dims[dims.length - i]));
138 }
139
140 c.add(HeaderCard.create(Standard.PCOUNT, pCount));
141 c.add(HeaderCard.create(Standard.GCOUNT, gCount));
142
143 Standard.context(null);
144 }
145
146 @Override
147 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "intended exposure of mutable data")
148 protected byte[] getCurrentData() {
149 return data;
150 }
151
152 @Override
153 protected long getTrueSize() {
154 return byteSize;
155 }
156
157
158
159
160
161
162
163
164 public final String getXtension() {
165 return extensionType;
166 }
167
168
169
170
171
172
173
174
175 public final Bitpix getBitpix() {
176 return bitpix;
177 }
178
179
180
181
182
183
184
185
186
187 public final int getParameterCount() {
188 return pCount;
189 }
190
191
192
193
194
195
196
197
198 public final int getGroupCount() {
199 return gCount;
200 }
201
202
203
204
205
206
207
208
209 public final int[] getDimensions() {
210 return dims;
211 }
212
213 @Override
214 public byte[] getData() throws FitsException {
215 byte[] bytes = (byte[]) super.getData();
216 if (bytes != null) {
217 return bytes;
218 }
219
220 data = new byte[byteSize];
221 return data;
222 }
223
224 @Override
225 protected void loadData(ArrayDataInput in) throws IOException {
226 data = new byte[byteSize];
227 in.readFully(data);
228 }
229
230 @SuppressWarnings({"resource", "deprecation"})
231 @Override
232 public void write(ArrayDataOutput o) throws FitsException {
233 if (o != getRandomAccessInput()) {
234 ensureData();
235 }
236 try {
237 o.write(data);
238 } catch (IOException e) {
239 throw new FitsException("IO Error on unknown data write", e);
240 }
241 FitsUtil.pad(o, getTrueSize());
242 }
243
244 @Override
245 @SuppressWarnings("deprecation")
246 public UndefinedHDU toHDU() {
247 Header h = new Header();
248 fillHeader(h);
249 return new UndefinedHDU(h, this);
250 }
251 }