1 package nom.tam.fits.test;
4 import static org.junit.Assert.assertEquals;
5 import junit.framework.JUnit4TestAdapter;
7 import nom.tam.image.*;
13 /** Test that we can read files that fail due to lack of padding in the final HDU.
15 public class PaddingTest {
18 public void test1() throws Exception {
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);
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");
34 bf.writeArray(bimg); // The data but no following padding.
38 // Now try reading this back.
39 f = new Fits("padding1.fits");
43 } catch (PaddingException e) {
44 assertEquals("HDUCount", 0, f.getNumberOfHDUs());
45 f.addHDU(e.getTruncatedHDU());
46 assertEquals("HDUCount2", 1, f.getNumberOfHDUs());
50 ImageHDU hdu0 = (ImageHDU) (f.getHDU(0));
51 byte[][] aa = (byte[][]) hdu0.getKernel();
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)) {
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");
69 nom.tam.image.ImageTiler it = hdu0.getTiler();
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});
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);
84 public void test2() throws Exception {
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);
95 BasicHDU hdu = Fits.makeHDU(bimg);
98 // First create a FITS file with a truncated second HDU.
99 BufferedFile bf = new BufferedFile("padding2.fits", "rw");
102 hdu.getHeader().setXtension("IMAGE");
103 Cursor curs = hdu.getHeader().iterator();
106 while (curs.hasNext()) {
107 bf.write(((HeaderCard) curs.next()).toString().getBytes());
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
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));
122 bf.writeArray(bimg); // The data but no following padding.
126 // Now try reading this back.
127 f = new Fits("padding2.fits");
131 } catch (PaddingException e) {
132 assertEquals("HDUCount", 1, f.getNumberOfHDUs());
133 f.addHDU(e.getTruncatedHDU());
134 assertEquals("HDUCount2", 2, f.getNumberOfHDUs());
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();
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])) {
152 assertEquals("PadMiss2:", miss, 0);
153 assertEquals("PadMatch2:", match, 400);