Begin versioning.
[fits.git] / src / nom / tam / fits / test / AsciiTableTest.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.util.*;
8 import nom.tam.fits.*;
9
10 /** This class tests the AsciiTableHDU and AsciiTable FITS
11  *  classes and implicitly the ByteFormatter and ByteParser
12  *  classes in the nam.tam.util library.
13  *  Tests include:
14  *     Create columns of every type
15  *     Read columns of every type
16  *     Create a table column by column
17  *     Create a table row by row
18  *     Use deferred input on rows
19  *     Use deferred input on elements
20  *     Read rows, columns and elements from in-memory kernel.
21  *     Specify width of columns.
22  *     Rewrite data/header  in place.
23  *     Set and read null elements.
24  */
25 public class AsciiTableTest {
26
27     Object[] getSampleCols() {
28
29         float[] realCol = new float[50];
30
31         for (int i = 0; i < realCol.length; i += 1) {
32             realCol[i] = 10000.F * (i) * (i) * (i) + 1;
33         }
34
35         int[] intCol = (int[]) ArrayFuncs.convertArray(realCol, int.class);
36         long[] longCol = (long[]) ArrayFuncs.convertArray(realCol, long.class);
37         double[] doubleCol = (double[]) ArrayFuncs.convertArray(realCol, double.class);
38
39         String[] strCol = new String[realCol.length];
40
41         for (int i = 0; i < realCol.length; i += 1) {
42             strCol[i] = "ABC" + String.valueOf(realCol[i]) + "CDE";
43         }
44         return new Object[]{realCol, intCol, longCol, doubleCol, strCol};
45     }
46
47     Fits makeAsciiTable() throws Exception {
48         Object[] cols = getSampleCols();
49         // Create the new ASCII table.
50         Fits f = new Fits();
51         f.addHDU(Fits.makeHDU(cols));
52         return f;
53     }
54
55     public void writeFile(Fits f, String name) throws Exception {
56         BufferedFile bf = new BufferedFile(name, "rw");
57         f.write(bf);
58         bf.flush();
59         bf.close();
60     }
61
62     @Test
63     public void test() throws Exception {
64         createByColumn();
65         createByRow();
66         readByRow();
67         readByColumn();
68         readByElement();
69         modifyTable();
70         delete();
71     }
72
73     public void createByColumn() throws Exception {
74         Fits f = makeAsciiTable();
75         writeFile(f, "at1.fits");
76
77         // Read back the data from the file.
78         f = new Fits("at1.fits");
79         AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
80
81         Object[] inputs = getSampleCols();
82         Object[] outputs = (Object[]) hdu.getKernel();
83
84         for (int i = 0; i < 50; i += 1) {
85             ((String[]) outputs[4])[i] = ((String[]) outputs[4])[i].trim();
86         }
87
88         for (int j = 0; j < 5; j += 1) {
89             assertEquals("ByCol:" + j, true, ArrayFuncs.arrayEquals(inputs[j], outputs[j], 1.e-6, 1.e-14));
90         }
91
92     }
93
94     Object[] getRow(int i) {
95         return new Object[]{
96                     new int[]{i},
97                     new float[]{i},
98                     new String[]{"Str" + i}
99                 };
100     }
101
102     Object[] getRowBlock(int max) {
103         Object[] o = new Object[]{new int[max], new float[max], new String[max]};
104         for (int i = 0; i < max; i += 1) {
105             ((int[]) o[0])[i] = i;
106             ((float[]) o[1])[i] = i;
107             ((String[]) o[2])[i] = "Str" + i;
108         }
109         return o;
110     }
111
112     public void createByRow() throws Exception {
113
114         // Create a table row by row .
115         Fits f = new Fits();
116         AsciiTable data = new AsciiTable();
117         Object[] row = new Object[4];
118
119         for (int i = 0; i < 50; i += 1) {
120             data.addRow(getRow(i));
121         }
122
123         f.addHDU(Fits.makeHDU(data));
124
125         writeFile(f, "at2.fits");
126
127         // Read it back.
128         f = new Fits("at2.fits");
129
130         Object[] output = (Object[]) f.getHDU(1).getKernel();
131         Object[] input = getRowBlock(50);
132
133         for (int i = 0; i < 50; i += 1) {
134             String[] str = (String[]) output[2];
135             String[] istr = (String[]) input[2];
136             int len1 = str[1].length();
137             str[i] = str[i].trim();
138             // The first row would have set the length for all the
139             // remaining rows...
140             if (istr[i].length() > len1) {
141                 istr[i] = istr[i].substring(0, len1);
142             }
143         }
144
145         for (int j = 0; j < 3; j += 1) {
146             assertEquals("ByRow:" + j, true, ArrayFuncs.arrayEquals(input[j], output[j], 1.e-6, 1.e-14));
147         }
148     }
149
150     public void readByRow() throws Exception {
151
152         Fits f = new Fits("at1.fits");
153         Object[] cols = getSampleCols();
154
155         AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
156         AsciiTable data = (AsciiTable) hdu.getData();
157
158         for (int i = 0; i < data.getNRows(); i += 1) {
159             assertEquals("Rows:" + i, 50, data.getNRows());
160             Object[] row = data.getRow(i);
161             assertEquals("Ascii Rows: float" + i, 1.F, ((float[]) cols[0])[i] / ((float[]) row[0])[0], 1.e-6);
162             assertEquals("Ascii Rows: int" + i, ((int[]) cols[1])[i], ((int[]) row[1])[0]);
163             assertEquals("Ascii Rows: long" + i, ((long[]) cols[2])[i], ((long[]) row[2])[0]);
164             assertEquals("Ascii Rows: double" + i, 1., ((double[]) cols[3])[i] / ((double[]) row[3])[0], 1.e-14);
165             String[] st = (String[]) row[4];
166             st[0] = st[0].trim();
167             assertEquals("Ascii Rows: Str" + i, ((String[]) cols[4])[i], ((String[]) row[4])[0]);
168         }
169     }
170
171     public void readByColumn() throws Exception {
172         Fits f = new Fits("at1.fits");
173         AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
174         AsciiTable data = (AsciiTable) hdu.getData();
175         Object[] cols = getSampleCols();
176
177         assertEquals("Number of rows", data.getNRows(), 50);
178         assertEquals("Number of columns", data.getNCols(), 5);
179
180         for (int j = 0; j < data.getNCols(); j += 1) {
181             Object col = data.getColumn(j);
182             if (j == 4) {
183                 String[] st = (String[]) col;
184                 for (int i = 0; i < st.length; i += 1) {
185                     st[i] = st[i].trim();
186                 }
187             }
188             assertEquals("Ascii Columns:" + j, true, ArrayFuncs.arrayEquals(cols[j], col, 1.e-6, 1.e-14));
189         }
190     }
191
192     public void readByElement() throws Exception {
193
194         Fits f = new Fits("at2.fits");
195         AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
196         AsciiTable data = (AsciiTable) hdu.getData();
197
198
199         for (int i = 0; i < data.getNRows(); i += 1) {
200             Object[] row = (Object[]) data.getRow(i);
201             for (int j = 0; j < data.getNCols(); j += 1) {
202                 Object val = data.getElement(i, j);
203                 assertEquals("Ascii readElement", true, ArrayFuncs.arrayEquals(val, row[j]));
204             }
205         }
206     }
207
208     public void modifyTable() throws Exception {
209
210         Fits f = new Fits("at1.fits");
211         Object[] samp = getSampleCols();
212
213         AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
214         AsciiTable data = (AsciiTable) hdu.getData();
215         float[] f1 = (float[]) data.getColumn(0);
216         float[] f2 = (float[]) f1.clone();
217         for (int i = 0; i < f2.length; i += 1) {
218             f2[i] = 2 * f2[i];
219         }
220
221         data.setColumn(0, f2);
222         f1 = new float[]{3.14159f};
223         data.setElement(3, 0, f1);
224
225         hdu.setNullString(0, "**INVALID**");
226         data.setNull(5, 0, true);
227         data.setNull(6, 0, true);
228
229         Object[] row = new Object[5];
230         row[0] = new float[]{6.28f};
231         row[1] = new int[]{22};
232         row[2] = new long[]{0};
233         row[3] = new double[]{-3};
234         row[4] = new String[]{"A string"};
235
236         data.setRow(5, row);
237
238         data.setElement(4, 2, new long[]{54321});
239
240         BufferedFile bf = new BufferedFile("at1x.fits", "rw");
241         f.write(bf);
242
243
244         f = new Fits("at1x.fits");
245         AsciiTable tab = (AsciiTable) f.getHDU(1).getData();
246         Object[] kern = (Object[]) tab.getKernel();
247
248         float[] fx = (float[]) kern[0];
249         int[] ix = (int[]) kern[1];
250         long[] lx = (long[]) kern[2];
251         double[] dx = (double[]) kern[3];
252         String[] sx = (String[]) kern[4];
253
254         float[] fy = (float[]) samp[0];
255         int[] iy = (int[]) samp[1];
256         long[] ly = (long[]) samp[2];
257         double[] dy = (double[]) samp[3];
258         String[] sy = (String[]) samp[4];
259
260         assertEquals("Null", true, tab.isNull(6, 0));
261         assertEquals("Null2", false, tab.isNull(5, 0));
262
263         for (int i = 0; i < data.getNRows(); i += 1) {
264             if (i != 5) {
265                 if (i != 6) {  // Null
266                     assertEquals("f" + i, 1., f2[i] / fx[i], 1.e-6);
267                 }
268                 assertEquals("i" + i, iy[i], ix[i]);
269                 if (i == 4) {
270                     assertEquals("l4", 54321L, lx[i]);
271                 } else {
272                     assertEquals("l" + i, ly[i], lx[i]);
273                 }
274                 assertEquals("d" + i, 1., dy[i] / dx[i], 1.e-14);
275                 assertEquals("s" + i, sy[i], sx[i].trim());
276             }
277         }
278         Object[] r5 = (Object[]) data.getRow(5);
279         String[] st = (String[]) r5[4];
280         st[0] = st[0].trim();
281         assertEquals("row5", true, ArrayFuncs.arrayEquals(row, r5, 1.e-6, 1.e-14));
282     }
283
284     public void delete() throws Exception {
285
286         Fits f = new Fits("at1.fits");
287
288         TableHDU th = (TableHDU) f.getHDU(1);
289         assertEquals("delrBef", 50, th.getNRows());
290         th.deleteRows(2, 2);
291         assertEquals("delrAft", 48, th.getNRows());
292         BufferedFile bf = new BufferedFile("at1y.fits", "rw");
293         f.write(bf);
294         bf.close();
295
296         f = new Fits("at1y.fits");
297         th = (TableHDU) f.getHDU(1);
298         assertEquals("delrAft2", 48, th.getNRows());
299
300         assertEquals("delcBef", 5, th.getNCols());
301         th.deleteColumnsIndexZero(3, 2);
302         assertEquals("delcAft1", 3, th.getNCols());
303         th.deleteColumnsIndexZero(0, 2);
304         assertEquals("delcAft2", 1, th.getNCols());
305         bf = new BufferedFile("at1z.fits", "rw");
306         f.write(bf);
307         bf.close();
308
309         f = new Fits("at1z.fits");
310         th = (TableHDU) f.getHDU(1);
311         assertEquals("delcAft3", 1, th.getNCols());
312     }
313 }