1 package nom.tam.fits.test;
4 import static org.junit.Assert.assertEquals;
5 import junit.framework.JUnit4TestAdapter;
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.
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.
25 public class AsciiTableTest {
27 Object[] getSampleCols() {
29 float[] realCol = new float[50];
31 for (int i = 0; i < realCol.length; i += 1) {
32 realCol[i] = 10000.F * (i) * (i) * (i) + 1;
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);
39 String[] strCol = new String[realCol.length];
41 for (int i = 0; i < realCol.length; i += 1) {
42 strCol[i] = "ABC" + String.valueOf(realCol[i]) + "CDE";
44 return new Object[]{realCol, intCol, longCol, doubleCol, strCol};
47 Fits makeAsciiTable() throws Exception {
48 Object[] cols = getSampleCols();
49 // Create the new ASCII table.
51 f.addHDU(Fits.makeHDU(cols));
55 public void writeFile(Fits f, String name) throws Exception {
56 BufferedFile bf = new BufferedFile(name, "rw");
63 public void test() throws Exception {
73 public void createByColumn() throws Exception {
74 Fits f = makeAsciiTable();
75 writeFile(f, "at1.fits");
77 // Read back the data from the file.
78 f = new Fits("at1.fits");
79 AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
81 Object[] inputs = getSampleCols();
82 Object[] outputs = (Object[]) hdu.getKernel();
84 for (int i = 0; i < 50; i += 1) {
85 ((String[]) outputs[4])[i] = ((String[]) outputs[4])[i].trim();
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));
94 Object[] getRow(int i) {
98 new String[]{"Str" + i}
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;
112 public void createByRow() throws Exception {
114 // Create a table row by row .
116 AsciiTable data = new AsciiTable();
117 Object[] row = new Object[4];
119 for (int i = 0; i < 50; i += 1) {
120 data.addRow(getRow(i));
123 f.addHDU(Fits.makeHDU(data));
125 writeFile(f, "at2.fits");
128 f = new Fits("at2.fits");
130 Object[] output = (Object[]) f.getHDU(1).getKernel();
131 Object[] input = getRowBlock(50);
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
140 if (istr[i].length() > len1) {
141 istr[i] = istr[i].substring(0, len1);
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));
150 public void readByRow() throws Exception {
152 Fits f = new Fits("at1.fits");
153 Object[] cols = getSampleCols();
155 AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
156 AsciiTable data = (AsciiTable) hdu.getData();
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]);
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();
177 assertEquals("Number of rows", data.getNRows(), 50);
178 assertEquals("Number of columns", data.getNCols(), 5);
180 for (int j = 0; j < data.getNCols(); j += 1) {
181 Object col = data.getColumn(j);
183 String[] st = (String[]) col;
184 for (int i = 0; i < st.length; i += 1) {
185 st[i] = st[i].trim();
188 assertEquals("Ascii Columns:" + j, true, ArrayFuncs.arrayEquals(cols[j], col, 1.e-6, 1.e-14));
192 public void readByElement() throws Exception {
194 Fits f = new Fits("at2.fits");
195 AsciiTableHDU hdu = (AsciiTableHDU) f.getHDU(1);
196 AsciiTable data = (AsciiTable) hdu.getData();
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]));
208 public void modifyTable() throws Exception {
210 Fits f = new Fits("at1.fits");
211 Object[] samp = getSampleCols();
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) {
221 data.setColumn(0, f2);
222 f1 = new float[]{3.14159f};
223 data.setElement(3, 0, f1);
225 hdu.setNullString(0, "**INVALID**");
226 data.setNull(5, 0, true);
227 data.setNull(6, 0, true);
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"};
238 data.setElement(4, 2, new long[]{54321});
240 BufferedFile bf = new BufferedFile("at1x.fits", "rw");
244 f = new Fits("at1x.fits");
245 AsciiTable tab = (AsciiTable) f.getHDU(1).getData();
246 Object[] kern = (Object[]) tab.getKernel();
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];
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];
260 assertEquals("Null", true, tab.isNull(6, 0));
261 assertEquals("Null2", false, tab.isNull(5, 0));
263 for (int i = 0; i < data.getNRows(); i += 1) {
265 if (i != 6) { // Null
266 assertEquals("f" + i, 1., f2[i] / fx[i], 1.e-6);
268 assertEquals("i" + i, iy[i], ix[i]);
270 assertEquals("l4", 54321L, lx[i]);
272 assertEquals("l" + i, ly[i], lx[i]);
274 assertEquals("d" + i, 1., dy[i] / dx[i], 1.e-14);
275 assertEquals("s" + i, sy[i], sx[i].trim());
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));
284 public void delete() throws Exception {
286 Fits f = new Fits("at1.fits");
288 TableHDU th = (TableHDU) f.getHDU(1);
289 assertEquals("delrBef", 50, th.getNRows());
291 assertEquals("delrAft", 48, th.getNRows());
292 BufferedFile bf = new BufferedFile("at1y.fits", "rw");
296 f = new Fits("at1y.fits");
297 th = (TableHDU) f.getHDU(1);
298 assertEquals("delrAft2", 48, th.getNRows());
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");
309 f = new Fits("at1z.fits");
310 th = (TableHDU) f.getHDU(1);
311 assertEquals("delcAft3", 1, th.getNCols());