Begin versioning.
[fits.git] / src / nom / tam / fits / test / PaddingTest.java
1 package nom.tam.fits.test;
2
3 import org.junit.Test;
4 import static org.junit.Assert.assertEquals;
5 import junit.framework.JUnit4TestAdapter;
6
7 import nom.tam.image.*;
8 import nom.tam.util.*;
9 import nom.tam.fits.*;
10
11 import java.io.File;
12
13 /** Test that we can read files that fail due to lack of padding in the final HDU.
14  */
15 public class PaddingTest {
16
17     @Test
18     public void test1() throws Exception {
19
20         Fits f = new Fits();
21
22         byte[][] bimg = new byte[20][20];
23         for (int i = 0; i < 20; i += 1) {
24             for (int j = 0; j < 20; j += 1) {
25                 bimg[i][j] = (byte) (i + j);
26             }
27         }
28
29         BasicHDU hdu = Fits.makeHDU(bimg);
30         Header hdr = hdu.getHeader();
31         hdr.addValue("NEWKEY", "TESTVALUE", "Test keyword");
32         BufferedFile bf = new BufferedFile("padding1.fits", "rw");
33         hdr.write(bf);
34         bf.writeArray(bimg);  // The data but no following padding.
35         bf.flush();
36         bf.close();
37
38         // Now try reading this back.
39         f = new Fits("padding1.fits");
40
41         try {
42             f.read();
43         } catch (PaddingException e) {
44             assertEquals("HDUCount", 0, f.getNumberOfHDUs());
45             f.addHDU(e.getTruncatedHDU());
46             assertEquals("HDUCount2", 1, f.getNumberOfHDUs());
47         }
48
49
50         ImageHDU hdu0 = (ImageHDU) (f.getHDU(0));
51         byte[][] aa = (byte[][]) hdu0.getKernel();
52         int miss = 0;
53         int match = 0;
54         for (int i = 0; i < 20; i += 1) {
55             for (int j = 0; j < 20; j += 1) {
56                 if (aa[i][j] != (byte) (i + j)) {
57                     miss += 1;
58                 } else {
59                     match += 1;
60                 }
61             }
62         }
63         assertEquals("PadMiss1:", miss, 0);
64         assertEquals("PadMatch1:", match, 400);
65         // Make sure we got the real header and not the one generated strictly from the data.
66         assertEquals("Update header:", hdu0.getHeader().getStringValue("NEWKEY"), "TESTVALUE");
67
68
69         nom.tam.image.ImageTiler it = hdu0.getTiler();
70
71         // Remember that the tile is always a flattened
72         // 1-D representation of the data.
73         byte[] data = (byte[]) it.getTile(new int[]{2, 2}, new int[]{2, 2});
74
75         assertEquals("tilet1:", data.length, 4);
76         assertEquals("tilet2:", data[0] + 0, 4);
77         assertEquals("tilet3:", data[1] + 0, 5);
78         assertEquals("tilet4:", data[2] + 0, 5);
79         assertEquals("tilet5:", data[3] + 0, 6);
80
81     }
82
83     @Test
84     public void test2() throws Exception {
85
86         Fits f = new Fits();
87
88         byte[][] bimg = new byte[20][20];
89         for (int i = 0; i < 20; i += 1) {
90             for (int j = 0; j < 20; j += 1) {
91                 bimg[i][j] = (byte) (i + j);
92             }
93         }
94
95         BasicHDU hdu = Fits.makeHDU(bimg);
96         f.addHDU(hdu);
97
98         // First create a FITS file with a truncated second HDU.
99         BufferedFile bf = new BufferedFile("padding2.fits", "rw");
100         f.write(bf);
101
102         hdu.getHeader().setXtension("IMAGE");
103         Cursor curs = hdu.getHeader().iterator();
104         int cnt = 0;
105         // Write the header
106         while (curs.hasNext()) {
107             bf.write(((HeaderCard) curs.next()).toString().getBytes());
108             cnt += 1;
109         }
110
111         // The padding between header and data
112         byte[] b = new byte[(36 - cnt) * 80];  // Assuming fewer than 36 cards.
113         for (int i = 0; i < b.length; i += 1) {
114             b[i] = 32; // i.e., a blank
115         }
116         bf.write(b);
117         for (int i = 0; i < 20; i += 1) {
118             for (int j = 0; j < 20; j += 1) {
119                 bimg[i][j] = (byte) (2 * (i + j));
120             }
121         }
122         bf.writeArray(bimg);  // The data but no following padding.
123         bf.flush();
124         bf.close();
125
126         // Now try reading this back.
127         f = new Fits("padding2.fits");
128
129         try {
130             f.read();
131         } catch (PaddingException e) {
132             assertEquals("HDUCount", 1, f.getNumberOfHDUs());
133             f.addHDU(e.getTruncatedHDU());
134             assertEquals("HDUCount2", 2, f.getNumberOfHDUs());
135         }
136
137         ImageHDU hdu0 = (ImageHDU) (f.getHDU(0));
138         ImageHDU hdu1 = (ImageHDU) (f.getHDU(1));
139         byte[][] aa = (byte[][]) hdu0.getKernel();
140         byte[][] bb = (byte[][]) hdu1.getKernel();
141         int miss = 0;
142         int match = 0;
143         for (int i = 0; i < 20; i += 1) {
144             for (int j = 0; j < 20; j += 1) {
145                 if (bb[i][j] != (byte) (2 * aa[i][j])) {
146                     miss += 1;
147                 } else {
148                     match += 1;
149                 }
150             }
151         }
152         assertEquals("PadMiss2:", miss, 0);
153         assertEquals("PadMatch2:", match, 400);
154     }
155 }