Don't treat inner classes as test classes with ant.
[fits.git] / src / nom / tam / fits / test / HeaderTest.java
1 package nom.tam.fits.test;
2
3 import nom.tam.fits.*;
4 import nom.tam.util.*;
5
6 import org.junit.Test;
7 import static org.junit.Assert.assertEquals;
8 import junit.framework.JUnit4TestAdapter;
9
10 public class HeaderTest {
11
12     /** Check out header manipulation.
13      */
14     @Test
15     public void simpleImages() throws Exception {
16         float[][] img = new float[300][300];
17
18         Fits f = new Fits();
19
20         ImageHDU hdu = (ImageHDU) Fits.makeHDU(img);
21         BufferedFile bf = new BufferedFile("ht1.fits", "rw");
22         f.addHDU(hdu);
23         f.write(bf);
24         bf.close();
25
26         f = new Fits("ht1.fits");
27         hdu = (ImageHDU) f.getHDU(0);
28         Header hdr = hdu.getHeader();
29
30         assertEquals("NAXIS", 2, hdr.getIntValue("NAXIS"));
31         assertEquals("NAXIS1", 300, hdr.getIntValue("NAXIS1"));
32         assertEquals("NAXIS2", 300, hdr.getIntValue("NAXIS2"));
33         assertEquals("NAXIS2a", 300, hdr.getIntValue("NAXIS2", -1));
34         assertEquals("NAXIS3", -1, hdr.getIntValue("NAXIS3", -1));
35
36         assertEquals("BITPIX", -32, hdr.getIntValue("BITPIX"));
37
38
39         Cursor c = hdr.iterator();
40         HeaderCard hc = (HeaderCard) c.next();
41         assertEquals("SIMPLE_1", "SIMPLE", hc.getKey());
42
43         hc = (HeaderCard) c.next();
44         assertEquals("BITPIX_2", "BITPIX", hc.getKey());
45
46         hc = (HeaderCard) c.next();
47         assertEquals("NAXIS_3", "NAXIS", hc.getKey());
48
49         hc = (HeaderCard) c.next();
50         assertEquals("NAXIS1_4", "NAXIS1", hc.getKey());
51
52         hc = (HeaderCard) c.next();
53         assertEquals("NAXIS2_5", "NAXIS2", hc.getKey());
54     }
55
56     /** Confirm initial location versus EXTEND keyword (V. Forchi). */
57     @Test
58     public void extendTest() throws Exception {
59         Fits f = new Fits("ht1.fits");
60         Header h = f.getHDU(0).getHeader();
61         h.addValue("TESTKEY", "TESTVAL", "TESTCOMM");
62         h.rewrite();
63         f.getStream().close();
64         f = new Fits("ht1.fits");
65         h = f.getHDU(0).getHeader();
66
67         // We should be pointed after the EXTEND and before TESTKEY
68         h.addValue("TESTKEY2", "TESTVAL2", null); // Should precede TESTKEY
69
70         Cursor c = h.iterator();
71         assertEquals("E1", ((HeaderCard) c.next()).getKey(), "SIMPLE");
72         assertEquals("E2", ((HeaderCard) c.next()).getKey(), "BITPIX");
73         assertEquals("E3", ((HeaderCard) c.next()).getKey(), "NAXIS");
74         assertEquals("E4", ((HeaderCard) c.next()).getKey(), "NAXIS1");
75         assertEquals("E5", ((HeaderCard) c.next()).getKey(), "NAXIS2");
76         assertEquals("E6", ((HeaderCard) c.next()).getKey(), "EXTEND");
77         assertEquals("E7", ((HeaderCard) c.next()).getKey(), "TESTKEY2");
78         assertEquals("E8", ((HeaderCard) c.next()).getKey(), "TESTKEY");
79
80     }
81
82     @Test
83     public void cursorTest() throws Exception {
84
85         Fits f = new Fits("ht1.fits");
86         ImageHDU hdu = (ImageHDU) f.getHDU(0);
87         Header hdr = hdu.getHeader();
88         Cursor c = hdr.iterator();
89
90
91         c.setKey("XXX");
92         c.add("CTYPE1", new HeaderCard("CTYPE1", "GLON-CAR", "Galactic Longitude"));
93         c.add("CTYPE2", new HeaderCard("CTYPE2", "GLAT-CAR", "Galactic Latitude"));
94         c.setKey("CTYPE1");  // Move before CTYPE1
95         c.add("CRVAL1", new HeaderCard("CRVAL1", 0., "Longitude at reference"));
96         c.setKey("CTYPE2"); // Move before CTYPE2
97         c.add("CRVAL2", new HeaderCard("CRVAL2", -90., "Latitude at reference"));
98         c.setKey("CTYPE1");  // Just practicing moving around!!
99         c.add("CRPIX1", new HeaderCard("CRPIX1", 150.0, "Reference Pixel X"));
100         c.setKey("CTYPE2");
101         c.add("CRPIX2", new HeaderCard("CRPIX2", 0., "Reference pixel Y"));
102         c.add("INV2", new HeaderCard("INV2", true, "Invertible axis"));
103         c.add("SYM2", new HeaderCard("SYM2", "YZ SYMMETRIC", "Symmetries..."));
104
105         assertEquals("CTYPE1", "GLON-CAR", hdr.getStringValue("CTYPE1"));
106         assertEquals("CRPIX2", 0., hdr.getDoubleValue("CRPIX2", -2.), 0);
107
108         c.setKey("CRVAL1");
109         HeaderCard hc = (HeaderCard) c.next();
110         assertEquals("CRVAL1_c", "CRVAL1", hc.getKey());
111         hc = (HeaderCard) c.next();
112         assertEquals("CRPIX1_c", "CRPIX1", hc.getKey());
113         hc = (HeaderCard) c.next();
114         assertEquals("CTYPE1_c", "CTYPE1", hc.getKey());
115         hc = (HeaderCard) c.next();
116         assertEquals("CRVAL2_c", "CRVAL2", hc.getKey());
117         hc = (HeaderCard) c.next();
118         assertEquals("CRPIX2_c", "CRPIX2", hc.getKey());
119         hc = (HeaderCard) c.next();
120         assertEquals("INV2_c", "INV2", hc.getKey());
121         hc = (HeaderCard) c.next();
122         assertEquals("SYM2_c", "SYM2", hc.getKey());
123         hc = (HeaderCard) c.next();
124         assertEquals("CTYPE2_c", "CTYPE2", hc.getKey());
125
126         hdr.findCard("CRPIX1");
127         hdr.addValue("INTVAL1", 1, "An integer value");
128         hdr.addValue("LOG1", true, "A true value");
129         hdr.addValue("LOGB1", false, "A false value");
130         hdr.addValue("FLT1", 1.34, "A float value");
131         hdr.addValue("FLT2", -1.234567890e-134, "A very long float");
132         hdr.insertComment("Comment after flt2");
133
134         c.setKey("INTVAL1");
135         hc = (HeaderCard) c.next();
136         assertEquals("INTVAL1", "INTVAL1", hc.getKey());
137         hc = (HeaderCard) c.next();
138         assertEquals("LOG1", "LOG1", hc.getKey());
139         hc = (HeaderCard) c.next();
140         assertEquals("LOGB1", "LOGB1", hc.getKey());
141         hc = (HeaderCard) c.next();
142         assertEquals("FLT1", "FLT1", hc.getKey());
143         hc = (HeaderCard) c.next();
144         assertEquals("FLT2", "FLT2", hc.getKey());
145         c.next(); // Skip comment
146         hc = (HeaderCard) c.next();
147         assertEquals("CRPIX1x", "CRPIX1", hc.getKey());
148
149         assertEquals("FLT1", 1.34, hdr.getDoubleValue("FLT1", 0), 0);
150         c.setKey("FLT1");
151         c.next();
152         c.remove();
153         assertEquals("FLT1", 0., hdr.getDoubleValue("FLT1", 0), 0);
154         c.setKey("LOGB1");
155         hc = (HeaderCard) c.next();
156         assertEquals("AftDel1", "LOGB1", hc.getKey());
157         hc = (HeaderCard) c.next();
158         assertEquals("AftDel2", "FLT2", hc.getKey());
159         hc = (HeaderCard) c.next();
160         assertEquals("AftDel3", "Comment after flt2", hc.getComment());
161     }
162
163     @Test
164     public void testBadHeader() throws Exception {
165
166         Fits f = new Fits("ht1.fits");
167         ImageHDU hdu = (ImageHDU) f.getHDU(0);
168         Header hdr = hdu.getHeader();
169         Cursor c = hdr.iterator();
170
171         c = hdr.iterator();
172         c.next();
173         c.next();
174         c.remove();
175         boolean thrown = false;
176         try {
177             hdr.rewrite();
178         } catch (Exception e) {
179             thrown = true;
180         }
181         assertEquals("BITPIX delete", true, thrown);
182     }
183
184     @Test
185     public void testUpdateHeaderComments() throws Exception {
186         byte[][] z = new byte[4][4];
187         Fits f = new Fits();
188         f.addHDU(FitsFactory.HDUFactory(z));
189         BufferedFile bf = new BufferedFile("hx1.fits", "rw");
190         f.write(bf);
191         bf.close();
192         f = new Fits("hx1.fits");
193         HeaderCard c1 = f.getHDU(0).getHeader().findCard("SIMPLE");
194         assertEquals("tuhc1", c1.getComment(), HeaderCommentsMap.getComment("header:simple:1"));
195         c1 = f.getHDU(0).getHeader().findCard("BITPIX");
196         assertEquals("tuhc2", c1.getComment(), HeaderCommentsMap.getComment("header:bitpix:1"));
197         HeaderCommentsMap.updateComment("header:bitpix:1", "A byte array");
198         HeaderCommentsMap.deleteComment("header:simple:1");
199         f = new Fits();
200         f.addHDU(FitsFactory.HDUFactory(z));
201         bf = new BufferedFile("hx2.fits", "rw");
202         f.write(bf);
203         bf.close();
204         f = new Fits("hx2.fits");
205         c1 = f.getHDU(0).getHeader().findCard("SIMPLE");
206         assertEquals("tuhc1", c1.getComment(), null);
207         c1 = f.getHDU(0).getHeader().findCard("BITPIX");
208         assertEquals("tuhc2", c1.getComment(), "A byte array");
209     }
210
211     @Test
212     public void testRewrite() throws Exception {
213
214         // Should be rewriteable until we add enough cards to
215         // start a new block.
216
217         Fits f = new Fits("ht1.fits");
218         ImageHDU hdu = (ImageHDU) f.getHDU(0);
219         Header hdr = hdu.getHeader();
220         Cursor c = hdr.iterator();
221
222         int nc = hdr.getNumberOfCards();
223         int nb = (nc - 1) / 36;
224
225         while (hdr.rewriteable()) {
226             int nbx = (hdr.getNumberOfCards() - 1) / 36;
227             assertEquals("Rewrite:" + nbx, nb == nbx, hdr.rewriteable());
228             c.add(new HeaderCard("DUMMY" + nbx, null, null));
229         }
230     }
231
232     @Test
233     public void longStringTest() throws Exception {
234
235         Header hdr = new Fits("ht1.fits").getHDU(0).getHeader();
236
237         String seq = "0123456789";
238         String lng = "";
239         for (int i = 0; i < 20; i += 1) {
240             lng += seq;
241         }
242         assertEquals("Initial state:", false, Header.getLongStringsEnabled());
243         Header.setLongStringsEnabled(true);
244         assertEquals("Set state:", true, Header.getLongStringsEnabled());
245         hdr.addValue("LONG1", lng, "Here is a comment");
246         hdr.addValue("LONG2", "xx'yy'zz" + lng, "Another comment");
247         hdr.addValue("SHORT", "A STRING ENDING IN A &", null);
248         hdr.addValue("LONGISH", lng + "&", null);
249         hdr.addValue("LONGSTRN", "OGIP 1.0", "Uses long strings");
250
251         String sixty = seq + seq + seq + seq + seq + seq;
252         hdr.addValue("APOS1", sixty + "''''''''''", "Should be 70 chars long");
253         hdr.addValue("APOS2", sixty + " ''''''''''", "Should be 71 chars long");
254
255         // Now try to read the values back.
256         BufferedFile bf = new BufferedFile("ht4.hdr", "rw");
257         hdr.write(bf);
258         bf.close();
259         String val = hdr.getStringValue("LONG1");
260         assertEquals("LongT1", val, lng);
261         val = hdr.getStringValue("LONG2");
262         assertEquals("LongT2", val, "xx'yy'zz" + lng);
263         assertEquals("APOS1", hdr.getStringValue("APOS1").length(), 70);
264         assertEquals("APOS2", hdr.getStringValue("APOS2").length(), 71);
265         Header.setLongStringsEnabled(false);
266         val = hdr.getStringValue("LONG1");
267         assertEquals("LongT3", true, !val.equals(lng));
268         assertEquals("Longt4", true, val.length() <= 70);
269         assertEquals("longamp1", hdr.getStringValue("SHORT"), "A STRING ENDING IN A &");
270         bf = new BufferedFile("ht4.hdr", "r");
271         hdr = new Header(bf);
272         assertEquals("Set state2:", true, Header.getLongStringsEnabled());
273         val = hdr.getStringValue("LONG1");
274         assertEquals("LongT5", val, lng);
275         val = hdr.getStringValue("LONG2");
276         assertEquals("LongT6", val, "xx'yy'zz" + lng);
277         assertEquals("longamp2", hdr.getStringValue("LONGISH"), lng + "&");
278         assertEquals("APOS1b", hdr.getStringValue("APOS1").length(), 70);
279         assertEquals("APOS2b", hdr.getStringValue("APOS2").length(), 71);
280         assertEquals("APOS2c", hdr.getStringValue("APOS2"), sixty + " ''''''''''");
281         assertEquals("longamp1b", hdr.getStringValue("SHORT"), "A STRING ENDING IN A &");
282         assertEquals("longamp2b", hdr.getStringValue("LONGISH"), lng + "&");
283
284         int cnt = hdr.getNumberOfCards();
285         // This should remove all three cards associated with
286         // LONG1
287         hdr.removeCard("LONG1");
288         assertEquals("deltest", cnt - 3, hdr.getNumberOfCards());
289         Header.setLongStringsEnabled(false);
290         // With long strings disabled this should only remove one more card.
291         hdr.removeCard("LONG2");
292         assertEquals("deltest2", cnt - 4, hdr.getNumberOfCards());
293
294     }
295 }
296