2 /* Copyright: Thomas McGlynn 1997-1998.
3 * This code may be used for any purpose, non-commercial
4 * or commercial so long as this copyright notice is retained
5 * in the source code or included in or referred to in any
7 * Many thanks to David Glowacki (U. Wisconsin) for substantial
8 * improvements, enhancements and bug fixes.
11 import nom.tam.util.ArrayFuncs;
12 import nom.tam.util.BufferedDataInputStream;
13 import nom.tam.image.ImageTiler;
15 /** FITS image header/data unit */
19 /** Build an image HDU using the supplied data.
20 * @param obj the data used to build the image.
21 * @exception FitsException if there was a problem with the data.
23 public ImageHDU(Header h, Data d)
24 throws FitsException {
30 /** Indicate that Images can appear at the beginning of a FITS dataset */
31 protected boolean canBePrimary() {
35 /** Change the Image from/to primary */
36 protected void setPrimaryHDU(boolean status) {
39 super.setPrimaryHDU(status);
40 } catch (FitsException e) {
41 System.err.println("Impossible exception in ImageData");
45 myHeader.setSimple(true);
47 myHeader.setXtension("IMAGE");
51 /** Check that this HDU has a valid header for this type.
52 * @return <CODE>true</CODE> if this HDU has a valid header.
54 public static boolean isHeader(Header hdr) {
55 boolean found = false;
56 found = hdr.getBooleanValue("SIMPLE");
58 String s = hdr.getStringValue("XTENSION");
60 if (s.trim().equals("IMAGE") || s.trim().equals("IUEIMAGE")) {
68 return !hdr.getBooleanValue("GROUPS");
71 /** Check if this object can be described as a FITS image.
72 * @param o The Object being tested.
74 public static boolean isData(Object o) {
75 String s = o.getClass().getName();
78 for (i = 0; i < s.length(); i += 1) {
79 if (s.charAt(i) != '[') {
84 // Allow all non-boolean/Object arrays.
85 // This does not check the rectangularity of the array though.
86 if (i <= 0 || s.charAt(i) == 'L' || s.charAt(i) == 'Z') {
93 /** Create a Data object to correspond to the header description.
94 * @return An unfilled Data object which can be used to read
95 * in the data for this HDU.
96 * @exception FitsException if the image extension could not be created.
98 public Data manufactureData()
99 throws FitsException {
100 return manufactureData(myHeader);
103 public static Data manufactureData(Header hdr)
104 throws FitsException {
105 return new ImageData(hdr);
108 /** Create a header that describes the given
110 * @param o The image to be described.
111 * @exception FitsException if the object does not contain
114 public static Header manufactureHeader(Data d)
115 throws FitsException {
121 Header h = new Header();
127 /** Encapsulate an object as an ImageHDU. */
128 public static Data encapsulate(Object o) throws FitsException {
129 return new ImageData(o);
132 public ImageTiler getTiler() {
133 return ((ImageData) myData).getTiler();
136 /** Print out some information about this HDU.
139 if (isHeader(myHeader)) {
140 System.out.println(" Image");
142 System.out.println(" Image (bad header)");
145 System.out.println(" Header Information:");
146 System.out.println(" BITPIX=" + myHeader.getIntValue("BITPIX", -1));
147 int naxis = myHeader.getIntValue("NAXIS", -1);
148 System.out.println(" NAXIS=" + naxis);
149 for (int i = 1; i <= naxis; i += 1) {
150 System.out.println(" NAXIS" + i + "="
151 + myHeader.getIntValue("NAXIS" + i, -1));
154 System.out.println(" Data information:");
156 if (myData.getData() == null) {
157 System.out.println(" No Data");
159 System.out.println(" "
160 + ArrayFuncs.arrayDescription(myData.getData()));
162 } catch (Exception e) {
163 System.out.println(" Unable to get data");