Use getResource to access CompressTest data for unit tests.
[fits.git] / src / nom / tam / fits / test / CompressTest.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.net.URL;
12
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
16
17 /** Test reading .Z and .gz compressed files.
18  */
19 public class CompressTest {
20
21     @Test
22     public void testgz() throws Exception {
23
24         File fil = new File(".");
25         System.out.println("File is:" + fil.getCanonicalPath());
26         Fits f = new Fits("http://heasarc.gsfc.nasa.gov/FTP/asca/data/rev2/43021000/images/ad43021000gis25670_lo.totsky.gz");
27
28         BasicHDU h = f.readHDU();
29         int[][] data = (int[][]) h.getKernel();
30         double sum = 0;
31         for (int i = 0; i < data.length; i += 1) {
32             for (int j = 0; j < data[i].length; j += 1) {
33                 sum += data[i][j];
34             }
35         }
36         assertEquals("ZCompress", sum, 296915., 0);
37     }
38
39     @Test
40     public void testZ() throws Exception {
41
42         Fits f = new Fits("http://heasarc.gsfc.nasa.gov/FTP/rosat/data/pspc/processed_data/600000/rp600245n00/rp600245n00_im1.fits.Z");
43
44         BasicHDU h = f.readHDU();
45         short[][] data = (short[][]) h.getKernel();
46         double sum = 0;
47         for (int i = 0; i < data.length; i += 1) {
48             for (int j = 0; j < data[i].length; j += 1) {
49                 sum += data[i][j];
50             }
51         }
52         assertEquals("ZCompress", sum, 91806., 0);
53     }
54
55     @Test
56     public void testStream() throws Exception {
57         String is;
58
59         is = "test.fits";
60         assertEquals("Stream1", 300, streamRead(is, false, false));
61
62         is = "test.fits.Z";
63         assertEquals("Stream2", 300, streamRead(is, false, false));
64
65         is = "test.fits.gz";
66         assertEquals("Stream3", 300, streamRead(is, false, false));
67
68         is = "test.fits";
69         assertEquals("Stream4", 300, streamRead(is, false, true));
70
71         is = "test.fits.Z";
72         assertEquals("Stream5", 300, streamRead(is, false, true));
73
74         is = "test.fits.gz";
75         assertEquals("Stream6", 300, streamRead(is, false, true));
76
77         is = "test.fits.Z";
78         assertEquals("Stream7", 300, streamRead(is, true, true));
79
80         is = "test.fits.gz";
81         assertEquals("Stream8", 300, streamRead(is, true, true));
82
83         is = "test.fits.bz2";
84         assertEquals("Stream9", 300, streamRead(is, true, true));
85     }
86
87     @Test
88     public void testFile() throws Exception {
89         String is = "test.fits";
90         assertEquals("File1", 300, fileRead(is, false, false));
91
92         is = "test.fits.Z";
93         assertEquals("File2", 300, fileRead(is, false, false));
94
95         is = "test.fits.gz";
96         assertEquals("File3", 300, fileRead(is, false, false));
97
98         is = "test.fits";
99         assertEquals("File4", 300, fileRead(is, false, true));
100
101         is = "test.fits.Z";
102         assertEquals("File7", 300, fileRead(is, true, true));
103
104         is = "test.fits.gz";
105         assertEquals("File8", 300, fileRead(is, true, true));
106
107         is = "test.fits.bz2";
108         assertEquals("File9", 300, fileRead(is, true, true));
109     }
110
111     @Test
112     public void testString() throws Exception {
113         String is = "test.fits";
114         assertEquals("String1", 300, stringRead(is, false, false));
115
116         is = "test.fits.Z";
117         assertEquals("String2", 300, stringRead(is, false, false));
118
119         is = "test.fits.gz";
120         assertEquals("String3", 300, stringRead(is, false, false));
121
122         is = "test.fits";
123         assertEquals("String4", 300, stringRead(is, false, true));
124
125         is = "test.fits.Z";
126         assertEquals("String7", 300, stringRead(is, true, true));
127
128         is = "test.fits.gz";
129         assertEquals("String8", 300, stringRead(is, true, true));
130
131         is = "test.fits.bz2";
132         assertEquals("String8", 300, stringRead(is, true, true));
133     }
134
135     @Test
136     public void testURL() throws Exception {
137         String is = "test.fits";
138         assertEquals("String1", 300, urlRead(is, false, false));
139
140         is = "test.fits.Z";
141         assertEquals("String2", 300, urlRead(is, false, false));
142
143         is = "test.fits.gz";
144         assertEquals("String3", 300, urlRead(is, false, false));
145
146         is = "test.fits";
147         assertEquals("String4", 300, urlRead(is, false, true));
148
149         is = "test.fits.Z";
150         assertEquals("String7", 300, urlRead(is, true, true));
151
152         is = "test.fits.gz";
153         assertEquals("String8", 300, urlRead(is, true, true));
154
155         is = "test.fits.bz2";
156         assertEquals("String8", 300, urlRead(is, true, true));
157     }
158
159     int urlRead(String filename, boolean comp, boolean useComp)
160             throws Exception {
161         URL u = CompressTest.class.getResource(filename);
162         Fits f;
163         if (useComp) {
164             f = new Fits(u, comp);
165         } else {
166             f = new Fits(u);
167         }
168         short[][] data = (short[][]) f.readHDU().getKernel();
169
170         return total(data);
171     }
172
173     int streamRead(String filename, boolean comp, boolean useComp)
174             throws Exception {
175         InputStream is = CompressTest.class.getResourceAsStream(filename);
176         Fits f;
177         if (useComp) {
178             f = new Fits(is, comp);
179         } else {
180             f = new Fits(is);
181         }
182         short[][] data = (short[][]) f.readHDU().getKernel();
183         is.close();
184
185         return total(data);
186     }
187
188     int fileRead(String filename, boolean comp, boolean useComp)
189             throws Exception {
190         File is = new File(CompressTest.class.getResource(filename).getPath());
191         Fits f;
192         if (useComp) {
193             f = new Fits(is, comp);
194         } else {
195             f = new Fits(is);
196         }
197         short[][] data = (short[][]) f.readHDU().getKernel();
198
199         return total(data);
200     }
201
202     int stringRead(String filename, boolean comp, boolean useComp)
203             throws Exception {
204         String is = CompressTest.class.getResource(filename).getPath();
205         Fits f;
206         if (useComp) {
207             f = new Fits(is, comp);
208         } else {
209             f = new Fits(is);
210         }
211         short[][] data = (short[][]) f.readHDU().getKernel();
212
213         return total(data);
214     }
215
216     int total(short[][] data) {
217         int total = 0;
218         for (int i = 0; i < data.length; i += 1) {
219             for (int j = 0; j < data[i].length; j += 1) {
220                 total += data[i][j];
221             }
222         }
223         return total;
224     }
225 }