Begin versioning.
[fits.git] / src / nom / tam / fits / ImageHDU.java
1 package nom.tam.fits;
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
6  * derived software.
7  * Many thanks to David Glowacki (U. Wisconsin) for substantial
8  * improvements, enhancements and bug fixes.
9  */
10
11 import nom.tam.util.ArrayFuncs;
12 import nom.tam.util.BufferedDataInputStream;
13 import nom.tam.image.ImageTiler;
14
15 /** FITS image header/data unit */
16 public class ImageHDU
17         extends BasicHDU {
18
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.
22      */
23     public ImageHDU(Header h, Data d)
24             throws FitsException {
25         myData = d;
26         myHeader = h;
27
28     }
29
30     /** Indicate that Images can appear at the beginning of a FITS dataset */
31     protected boolean canBePrimary() {
32         return true;
33     }
34
35     /** Change the Image from/to primary */
36     protected void setPrimaryHDU(boolean status) {
37
38         try {
39             super.setPrimaryHDU(status);
40         } catch (FitsException e) {
41             System.err.println("Impossible exception in ImageData");
42         }
43
44         if (status) {
45             myHeader.setSimple(true);
46         } else {
47             myHeader.setXtension("IMAGE");
48         }
49     }
50
51     /** Check that this HDU has a valid header for this type.
52      * @return <CODE>true</CODE> if this HDU has a valid header.
53      */
54     public static boolean isHeader(Header hdr) {
55         boolean found = false;
56         found = hdr.getBooleanValue("SIMPLE");
57         if (!found) {
58             String s = hdr.getStringValue("XTENSION");
59             if (s != null) {
60                 if (s.trim().equals("IMAGE") || s.trim().equals("IUEIMAGE")) {
61                     found = true;
62                 }
63             }
64         }
65         if (!found) {
66             return false;
67         }
68         return !hdr.getBooleanValue("GROUPS");
69     }
70
71     /** Check if this object can be described as a FITS image.
72      *  @param o    The Object being tested.
73      */
74     public static boolean isData(Object o) {
75         String s = o.getClass().getName();
76
77         int i;
78         for (i = 0; i < s.length(); i += 1) {
79             if (s.charAt(i) != '[') {
80                 break;
81             }
82         }
83
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') {
87             return false;
88         } else {
89             return true;
90         }
91     }
92
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.
97      */
98     public Data manufactureData()
99             throws FitsException {
100         return manufactureData(myHeader);
101     }
102
103     public static Data manufactureData(Header hdr)
104             throws FitsException {
105         return new ImageData(hdr);
106     }
107
108     /** Create a  header that describes the given
109      * image data.
110      * @param o The image to be described.
111      * @exception FitsException if the object does not contain
112      *          valid image data.
113      */
114     public static Header manufactureHeader(Data d)
115             throws FitsException {
116
117         if (d == null) {
118             return null;
119         }
120
121         Header h = new Header();
122         d.fillHeader(h);
123
124         return h;
125     }
126
127     /** Encapsulate an object as an ImageHDU. */
128     public static Data encapsulate(Object o) throws FitsException {
129         return new ImageData(o);
130     }
131
132     public ImageTiler getTiler() {
133         return ((ImageData) myData).getTiler();
134     }
135
136     /** Print out some information about this HDU.
137      */
138     public void info() {
139         if (isHeader(myHeader)) {
140             System.out.println("  Image");
141         } else {
142             System.out.println("  Image (bad header)");
143         }
144
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));
152         }
153
154         System.out.println("      Data information:");
155         try {
156             if (myData.getData() == null) {
157                 System.out.println("        No Data");
158             } else {
159                 System.out.println("         "
160                         + ArrayFuncs.arrayDescription(myData.getData()));
161             }
162         } catch (Exception e) {
163             System.out.println("      Unable to get data");
164         }
165     }
166 }