dd3aad003ecaa124256b7771d59304d0eac5d535
[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         InputStream is;
58
59         is = new FileInputStream("test.fits");
60         assertEquals("Stream1", 300, streamRead(is, false, false));
61
62         is = new FileInputStream("test.fits.Z");
63         assertEquals("Stream2", 300, streamRead(is, false, false));
64
65         is = new FileInputStream("test.fits.gz");
66         assertEquals("Stream3", 300, streamRead(is, false, false));
67
68         is = new FileInputStream("test.fits");
69         assertEquals("Stream4", 300, streamRead(is, false, true));
70
71         is = new FileInputStream("test.fits.Z");
72         assertEquals("Stream5", 300, streamRead(is, false, true));
73
74         is = new FileInputStream("test.fits.gz");
75         assertEquals("Stream6", 300, streamRead(is, false, true));
76
77
78         is = new FileInputStream("test.fits.Z");
79         assertEquals("Stream7", 300, streamRead(is, true, true));
80
81         is = new FileInputStream("test.fits.gz");
82         assertEquals("Stream8", 300, streamRead(is, true, true));
83
84         is = new FileInputStream("test.fits.bz2");
85         assertEquals("Stream9", 300, streamRead(is, true, true));
86     }
87
88     @Test
89     public void testFile() throws Exception {
90         File is = new File("test.fits");
91         assertEquals("File1", 300, fileRead(is, false, false));
92
93         is = new File("test.fits.Z");
94         assertEquals("File2", 300, fileRead(is, false, false));
95
96         is = new File("test.fits.gz");
97         assertEquals("File3", 300, fileRead(is, false, false));
98
99         is = new File("test.fits");
100         assertEquals("File4", 300, fileRead(is, false, true));
101
102         is = new File("test.fits.Z");
103         assertEquals("File7", 300, fileRead(is, true, true));
104
105         is = new File("test.fits.gz");
106         assertEquals("File8", 300, fileRead(is, true, true));
107
108         is = new File("test.fits.bz2");
109         assertEquals("File9", 300, fileRead(is, true, true));
110     }
111
112     @Test
113     public void testString() throws Exception {
114         String is = "test.fits";
115         assertEquals("String1", 300, stringRead(is, false, false));
116
117         is = "test.fits.Z";
118         assertEquals("String2", 300, stringRead(is, false, false));
119
120         is = "test.fits.gz";
121         assertEquals("String3", 300, stringRead(is, false, false));
122
123         is = "test.fits";
124         assertEquals("String4", 300, stringRead(is, false, true));
125
126         is = "test.fits.Z";
127         assertEquals("String7", 300, stringRead(is, true, true));
128
129         is = "test.fits.gz";
130         assertEquals("String8", 300, stringRead(is, true, true));
131
132         is = "test.fits.bz2";
133         assertEquals("String8", 300, stringRead(is, true, true));
134
135     }
136
137     @Test
138     public void testURL() throws Exception {
139         String is = "test.fits";
140         assertEquals("String1", 300, urlRead(is, false, false));
141
142         is = "test.fits.Z";
143         assertEquals("String2", 300, urlRead(is, false, false));
144
145         is = "test.fits.gz";
146         assertEquals("String3", 300, urlRead(is, false, false));
147
148         is = "test.fits";
149         assertEquals("String4", 300, urlRead(is, false, true));
150
151         is = "test.fits.Z";
152         assertEquals("String7", 300, urlRead(is, true, true));
153
154         is = "test.fits.gz";
155         assertEquals("String8", 300, urlRead(is, true, true));
156
157         is = "test.fits.bz2";
158         assertEquals("String8", 300, urlRead(is, true, true));
159     }
160
161     int urlRead(String is, boolean comp, boolean useComp)
162             throws Exception {
163         File fil = new File(is);
164
165         String path = fil.getCanonicalPath();
166         URL u = new URL("file://" + path);
167
168         Fits f;
169         if (useComp) {
170             f = new Fits(u, comp);
171         } else {
172             f = new Fits(u);
173         }
174         short[][] data = (short[][]) f.readHDU().getKernel();
175
176         return total(data);
177     }
178
179     int streamRead(InputStream is, boolean comp, boolean useComp)
180             throws Exception {
181         Fits f;
182         if (useComp) {
183             f = new Fits(is, comp);
184         } else {
185             f = new Fits(is);
186         }
187         short[][] data = (short[][]) f.readHDU().getKernel();
188         is.close();
189
190         return total(data);
191     }
192
193     int fileRead(File is, boolean comp, boolean useComp)
194             throws Exception {
195         Fits f;
196         if (useComp) {
197             f = new Fits(is, comp);
198         } else {
199             f = new Fits(is);
200         }
201         short[][] data = (short[][]) f.readHDU().getKernel();
202
203         return total(data);
204     }
205
206     int stringRead(String is, boolean comp, boolean useComp)
207             throws Exception {
208         Fits f;
209         if (useComp) {
210             f = new Fits(is, comp);
211         } else {
212             f = new Fits(is);
213         }
214         short[][] data = (short[][]) f.readHDU().getKernel();
215
216         return total(data);
217     }
218
219     int total(short[][] data) {
220         int total = 0;
221         for (int i = 0; i < data.length; i += 1) {
222             for (int j = 0; j < data[i].length; j += 1) {
223                 total += data[i][j];
224             }
225         }
226         return total;
227     }
228 }