View Javadoc
1   package nom.tam.image.compression.hdu;
2   
3   import java.io.File;
4   
5   /*
6    * #%L
7    * nom.tam FITS library
8    * %%
9    * Copyright (C) 1996 - 2024 nom-tam-fits
10   * %%
11   * This is free and unencumbered software released into the public domain.
12   *
13   * Anyone is free to copy, modify, publish, use, compile, sell, or
14   * distribute this software, either in source code form or as a compiled
15   * binary, for any purpose, commercial or non-commercial, and by any
16   * means.
17   *
18   * In jurisdictions that recognize copyright laws, the author or authors
19   * of this software dedicate any and all copyright interest in the
20   * software to the public domain. We make this dedication for the benefit
21   * of the public at large and to the detriment of our heirs and
22   * successors. We intend this dedication to be an overt act of
23   * relinquishment in perpetuity of all present and future rights to this
24   * software under copyright law.
25   *
26   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32   * OTHER DEALINGS IN THE SOFTWARE.
33   * #L%
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          // compressThenUncompressTableAndAssert(originalFileName, Compression.ZCMPTYPE_RICE_1);
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(); // skip image
115             CompressedTableHDU compressedTable = (CompressedTableHDU) fitsComp.readHDU();
116             BinaryTableHDU uncompressedTable = compressedTable.asBinaryTableHDU();
117 
118             fitsOrg = new Fits(tableOrgFile);
119             fitsOrg.readHDU(); // skip image
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         // Assert.assertEquals(numberOfCards, testTable.getHeader().getNumberOfCards());
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 // TODO also cfitsio can not uncompress this, mail to bill 22.7.2016
174     public void testUncompress_tst0010() throws FitsException, IOException {
175         uncompressTableAndAssert("bintable/tst0010.fits.fz", "bintable/tst0010.fits");
176     }
177 
178     @Test
179     @Ignore // TODO also cfitsio can not uncompress this, mail to bill 22.7.2016
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     // TODO add fpack files to blackbox images, and enable tests once fpack fixes its
190     // critical bugs.
191     // @Test
192     // public void testUncompress_vtab_p() throws FitsException, IOException {
193     // uncompressTableAndAssert("bintable/vtab.p.fits.fz", "bintable/vtab.p.fits");
194     // }
195     //
196     // @Test
197     // public void testUncompress_vtab_q() throws FitsException, IOException {
198     // uncompressTableAndAssert("bintable/vtab.q.fits.fz", "bintable/vtab.q.fits");
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 }