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