1 package nom.tam.image.compression.hdu;
2
3 import java.io.File;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import java.io.IOException;
37 import java.lang.reflect.Array;
38 import java.util.Arrays;
39
40 import org.junit.Assert;
41 import org.junit.Ignore;
42 import org.junit.Test;
43
44 import nom.tam.fits.BinaryTableHDU;
45 import nom.tam.fits.Fits;
46 import nom.tam.fits.FitsException;
47 import nom.tam.fits.HeaderCard;
48 import nom.tam.fits.header.Compression;
49 import nom.tam.fits.util.BlackBoxImages;
50 import nom.tam.util.Cursor;
51 import nom.tam.util.SafeClose;
52
53 public class CompressedTableBlackBoxTest {
54
55 @Test
56 public void testUncompress_mddtsapcln() throws Exception {
57 uncompressTableAndAssert("bintable/mddtsapcln.fits.fz", "bintable/mddtsapcln.fits");
58 }
59
60 private void compressThenUncompressTableAndAssert(String originalFileName) throws FitsException, IOException {
61 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_GZIP_1);
62 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_GZIP_2);
63
64 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_NOCOMPRESS);
65 }
66
67 private void compressIntThenUncompressTableAndAssert(String originalFileName) throws FitsException, IOException {
68 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_GZIP_1);
69 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_GZIP_2);
70 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_RICE_1);
71 compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_NOCOMPRESS);
72 }
73
74 private void compressThenUncompressTableAndAssert(String originalFileName, String algo)
75 throws FitsException, IOException {
76 String tableOrgFile = BlackBoxImages.getBlackBoxImage(originalFileName);
77 String[] algos = new String[100];
78 Arrays.fill(algos, algo);
79
80 File file = new File("target/" + originalFileName + ".fz");
81 file.getParentFile().mkdirs();
82
83 try (Fits fitsCompressed = new Fits(); Fits fitsOrg = new Fits(tableOrgFile)) {
84 CompressedTableHDU cHDU = CompressedTableHDU.fromBinaryTableHDU((BinaryTableHDU) fitsOrg.getHDU(1), 0, algos)
85 .compress();
86
87 for (int col = 0; col < cHDU.getNCols(); col++) {
88 Assert.assertEquals(algo, cHDU.getHeader().getStringValue(Compression.ZCTYPn.n(col + 1)));
89 }
90
91 fitsCompressed.addHDU(cHDU);
92
93 fitsCompressed.write(file);
94 fitsCompressed.close();
95 fitsOrg.close();
96 }
97
98 uncompressTableAndAssert(file.getAbsolutePath(), originalFileName);
99 }
100
101 private void uncompressTableAndAssert(String compressedfileName, String originalFileName)
102 throws FitsException, IOException {
103 String tableFile;
104 if (new File(compressedfileName).exists()) {
105 tableFile = compressedfileName;
106 } else {
107 tableFile = BlackBoxImages.getBlackBoxImage(compressedfileName);
108 }
109 Fits fitsComp = null;
110 String tableOrgFile = BlackBoxImages.getBlackBoxImage(originalFileName);
111 Fits fitsOrg = null;
112 try {
113 fitsComp = new Fits(tableFile);
114 fitsComp.readHDU();
115 CompressedTableHDU compressedTable = (CompressedTableHDU) fitsComp.readHDU();
116 BinaryTableHDU uncompressedTable = compressedTable.asBinaryTableHDU();
117
118 fitsOrg = new Fits(tableOrgFile);
119 fitsOrg.readHDU();
120 BinaryTableHDU orgTable = compressedTable.asBinaryTableHDU();
121
122 assertEquals(orgTable, uncompressedTable);
123
124 fitsOrg.close();
125 } finally {
126 SafeClose.close(fitsComp);
127 SafeClose.close(fitsOrg);
128 }
129 }
130
131 private void assertEquals(BinaryTableHDU orgTable, BinaryTableHDU testTable) throws FitsException {
132 int numberOfCards = orgTable.getHeader().getNumberOfCards();
133
134 Cursor<String, HeaderCard> orgIterator = orgTable.getHeader().iterator();
135 for (int index = 0; index < numberOfCards; index++) {
136 HeaderCard orgCard = orgIterator.next();
137 HeaderCard testCard = testTable.getHeader().findCard(orgCard.getKey());
138 Assert.assertEquals("header " + orgCard.getKey(), orgCard.getValue(), testCard.getValue());
139 }
140 for (int column = 0; column < orgTable.getNCols(); column++) {
141 for (int row = 0; row < orgTable.getNRows(); row++) {
142 Object orgValue = orgTable.getElement(row, column);
143 Object testValue = testTable.getElement(row, column);
144 assertValues("col=" + column + ", row=" + row, orgValue, testValue);
145 }
146 }
147 }
148
149 private void assertValues(String label, Object orgValue, Object testValue) {
150 if (orgValue.getClass().isArray()) {
151 int arraySize = Array.getLength(orgValue);
152 for (int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++) {
153 Object orgValueElement = Array.get(orgValue, arrayIndex);
154 Object testValueElement = Array.get(testValue, arrayIndex);
155 assertValues(label + ":" + arrayIndex, orgValueElement, testValueElement);
156 }
157 } else {
158 Assert.assertEquals(orgValue, testValue);
159 }
160 }
161
162 @Test
163 public void testUncompress_swp06542llg() throws FitsException, IOException {
164 uncompressTableAndAssert("bintable/swp06542llg.fits.fz", "bintable/swp06542llg.fits");
165 }
166
167 @Test
168 public void testUncompress_testdata() throws FitsException, IOException {
169 uncompressTableAndAssert("bintable/testdata.fits.fz", "bintable/testdata.fits");
170 }
171
172 @Test
173 @Ignore
174 public void testUncompress_tst0010() throws FitsException, IOException {
175 uncompressTableAndAssert("bintable/tst0010.fits.fz", "bintable/tst0010.fits");
176 }
177
178 @Test
179 @Ignore
180 public void testUncompress_tst0012() throws FitsException, IOException {
181 uncompressTableAndAssert("bintable/tst0012.fits.fz", "bintable/tst0012.fits");
182 }
183
184 @Test
185 public void testUncompress_tst0014() throws FitsException, IOException {
186 uncompressTableAndAssert("bintable/tst0014.fits.fz", "bintable/tst0014.fits");
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201 @Test
202 public void testCompressAndUncompress_dddtsuvdata() throws FitsException, IOException {
203 compressThenUncompressTableAndAssert("bintable/dddtsuvdata.fits");
204 }
205
206 @Test
207 public void testCompressAndUncompress_mddtsapcln() throws FitsException, IOException {
208 compressThenUncompressTableAndAssert("bintable/mddtsapcln.fits");
209 }
210
211 @Test
212 public void testCompressAndUncompress_swp06542llg() throws FitsException, IOException {
213 compressThenUncompressTableAndAssert("bintable/swp06542llg.fits");
214 }
215
216 @Test
217 public void testCompressAndUncompress_testdata() throws FitsException, IOException {
218 compressThenUncompressTableAndAssert("bintable/testdata.fits");
219 }
220
221 @Test
222 public void testCompressAndUncompress_tst0010() throws FitsException, IOException {
223 compressThenUncompressTableAndAssert("bintable/tst0010.fits");
224 }
225
226 @Test
227 public void testCompressAndUncompress_tst0012() throws FitsException, IOException {
228 compressThenUncompressTableAndAssert("bintable/tst0012.fits");
229 }
230
231 @Test
232 public void testCompressAndUncompress_tst0014() throws FitsException, IOException {
233 compressThenUncompressTableAndAssert("bintable/tst0014.fits");
234 }
235
236 @Test
237 public void testCompressAndUncompress_vtab_p() throws FitsException, IOException {
238 compressIntThenUncompressTableAndAssert("bintable/vtab.p.fits");
239 }
240
241 @Test
242 public void testCompressAndUncompress_vtab_q() throws FitsException, IOException {
243 compressIntThenUncompressTableAndAssert("bintable/vtab.q.fits");
244 }
245
246 @Test(expected = Exception.class)
247 public void test_vtab_q_reversed() throws Exception {
248 try {
249 CompressedTableHDU.useOldStandardVLAIndexing(true);
250 compressIntThenUncompressTableAndAssert("bintable/vtab.q.fits");
251 } finally {
252 CompressedTableHDU.useOldStandardVLAIndexing(false);
253 }
254 }
255
256 @Test(expected = Exception.class)
257 public void test_vtab_p_reversed() throws Exception {
258 try {
259 CompressedTableHDU.useOldStandardVLAIndexing(true);
260 compressIntThenUncompressTableAndAssert("bintable/vtab.p.fits");
261 } finally {
262 CompressedTableHDU.useOldStandardVLAIndexing(false);
263 }
264 }
265 }