1 package nom.tam.fits;
2
3 /*-
4 * #%L
5 * nom.tam.fits
6 * %%
7 * Copyright (C) 1996 - 2024 nom-tam-fits
8 * %%
9 * This is free and unencumbered software released into the public domain.
10 *
11 * Anyone is free to copy, modify, publish, use, compile, sell, or
12 * distribute this software, either in source code form or as a compiled
13 * binary, for any purpose, commercial or non-commercial, and by any
14 * means.
15 *
16 * In jurisdictions that recognize copyright laws, the author or authors
17 * of this software dedicate any and all copyright interest in the
18 * software to the public domain. We make this dedication for the benefit
19 * of the public at large and to the detriment of our heirs and
20 * successors. We intend this dedication to be an overt act of
21 * relinquishment in perpetuity of all present and future rights to this
22 * software under copyright law.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 * #L%
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 * A container for unknown binary data types. We can still retrieve the data as a <code>byte[]</code> array, we just
46 * don't know how to interpret it ourselves. This class makes sure we don't break when we encouter HDUs that we don't
47 * (yet) support, such as HDU types defined by future FITS standards.
48 *
49 * @see UndefinedHDU
50 */
51 public class UndefinedData extends Data {
52
53 private static final String XTENSION_UNKNOWN = "UNKNOWN";
54 // private static final Logger LOG = getLogger(UndefinedData.class);
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 * Creates a new empty container for data of unknown type based on the provided FITS header information.
67 *
68 * @param h The FITS header corresponding to the data segment in the HDU
69 *
70 * @throws FitsException if there wan an error accessing or interpreting the provided header information.
71 *
72 * @deprecated (<i>for internal use</i>). Visibility will be reduced to the package level in the
73 * future.
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 * @deprecated (<i>for internal use</i>). Users should always construct known data types.
104 * Reduce visibility to the package level.
105 *
106 * @param x object to create the hdu from
107 *
108 * @throws IllegalArgumentException If the object is not an array or contains elements that do not have a known
109 * binary size.
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 // We'll assume it's a primary image, until we know better...
122 // Just in case, we don't want an XTENSION key lingering around...
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 * Returns the FITS extension type as stored by the XTENSION keyword in the FITS header.
156 *
157 * @return The value used for the XTENSION keyword in the FITS header
158 *
159 * @since 1.19
160 */
161 public final String getXtension() {
162 return extensionType;
163 }
164
165 /**
166 * Returns the FITS element type as a Bitpux value.
167 *
168 * @return The FITS Bitpix value for the type of primitive data element used by this data
169 *
170 * @since 1.19
171 */
172 public final Bitpix getBitpix() {
173 return bitpix;
174 }
175
176 /**
177 * Returns the size of the optional parameter space as stored by the PCOUNT keyword in the FITS header.
178 *
179 * @return The element count of the optional parameter space accompanying the main data, as stored by the PCOUNT
180 * header value.
181 *
182 * @since 1.19
183 */
184 public final int getParameterCount() {
185 return pCount;
186 }
187
188 /**
189 * Returns the number of repeated (data + parameter) groups in this data object
190 *
191 * @return The number of repeated data + parameter blocks, as stored by the GCOUNT header value.
192 *
193 * @since 1.19
194 */
195 public final int getGroupCount() {
196 return gCount;
197 }
198
199 /**
200 * Returns the dimensionality of the data (if any), in Java array index order. That is, The value for NAXIS1 is the
201 * last value in the returned array
202 *
203 * @return the regular dimensions of the data in Java index order (that is NAXIS1 is the last entry in the array),
204 * or possibly <code>null</code> if no dimensions have been defined.
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 }