3 import java.io.IOException;
5 import java.util.Iterator;
9 * Copyright: Thomas McGlynn 1997-1999.
10 * This code may be used for any purpose, non-commercial
11 * or commercial so long as this copyright notice is retained
12 * in the source code or included in or referred to in any
15 * Many thanks to David Glowacki (U. Wisconsin) for substantial
16 * improvements, enhancements and bug fixes.
19 * FITS ASCII table header/data unit
21 public class AsciiTableHDU extends TableHDU {
23 /** Just a copy of myData with the correct type */
25 /** The standard column stems for an ASCII table.
26 * Note that TBCOL is not included here -- it needs to
27 * be handled specially since it does not simply shift.
29 private String[] keyStems = {"TFORM", "TZERO", "TNULL", "TTYPE", "TUNIT"};
32 * Create an ascii table header/data unit.
33 * @param header the template specifying the ascii table.
34 * @param data the FITS data structure containing the table data.
35 * @exception FitsException if there was a problem with the header.
37 public AsciiTableHDU(Header h, Data d) {
40 data = (AsciiTable) d;
45 * Check that this is a valid ascii table header.
46 * @param header to validate.
47 * @return <CODE>true</CODE> if this is an ascii table header.
49 public static boolean isHeader(Header header) {
50 return header.getStringValue("XTENSION").trim().equals("TABLE");
54 * Check that this HDU has a valid header.
55 * @return <CODE>true</CODE> if this HDU has a valid header.
57 public boolean isHeader() {
58 return isHeader(myHeader);
61 /** Check if this data is usable as an ASCII table.
63 public static boolean isData(Object o) {
65 if (o instanceof Object[]) {
66 Object[] oo = (Object[]) o;
67 for (int i = 0; i < oo.length; i += 1) {
68 if (oo[i] instanceof String[]
69 || oo[i] instanceof int[]
70 || oo[i] instanceof long[]
71 || oo[i] instanceof float[]
72 || oo[i] instanceof double[]) {
84 * Create a Data object to correspond to the header description.
85 * @return An unfilled Data object which can be used to read
86 * in the data for this HDU.
87 * @exception FitsException if the Data object could not be created
88 * from this HDU's Header
90 public static Data manufactureData(Header hdr) throws FitsException {
91 return new AsciiTable(hdr);
94 /** Create an empty data structure corresponding to the input header.
96 public Data manufactureData() throws FitsException {
97 return manufactureData(myHeader);
100 /** Create a header to match the input data. */
101 public static Header manufactureHeader(Data d) throws FitsException {
102 Header hdr = new Header();
104 Iterator iter = hdr.iterator();
108 /** Create a ASCII table data structure from an array of objects
109 * representing the columns.
111 public static Data encapsulate(Object o) throws FitsException {
113 Object[] oo = (Object[]) o;
114 AsciiTable d = new AsciiTable();
115 for (int i = 0; i < oo.length; i += 1) {
122 * Skip the ASCII table and throw an exception.
123 * @param stream the stream from which the data is read.
125 public void readData(ArrayDataInput stream)
126 throws FitsException {
130 /** Mark an entry as null.
132 public void setNull(int row, int col, boolean flag) {
135 String nullStr = myHeader.getStringValue("TNULL" + (col + 1));
136 if (nullStr == null) {
137 setNullString(col, "NULL");
140 data.setNull(row, col, flag);
143 /** See if an element is null */
144 public boolean isNull(int row, int col) {
145 return data.isNull(row, col);
148 /** Set the null string for a column */
149 public void setNullString(int col, String newNull) {
150 myHeader.positionAfterIndex("TBCOL", col + 1);
152 myHeader.addValue("TNULL" + (col + 1), newNull, "ntf::asciitablehdu:tnullN:1");
153 } catch (HeaderCardException e) {
154 System.err.println("Impossible exception in setNullString" + e);
156 data.setNullString(col, newNull);
160 public int addColumn(Object newCol) throws FitsException {
162 data.addColumn(newCol);
164 // Move the iterator to point after all the data describing
165 // the previous column.
168 myHeader.positionAfterIndex("TBCOL", data.getNCols());
170 int rowlen = data.addColInfo(getNCols(), iter);
171 int oldRowlen = myHeader.getIntValue("NAXIS1");
172 myHeader.setNaxis(1, rowlen + oldRowlen);
174 int oldTfields = myHeader.getIntValue("TFIELDS");
176 myHeader.addValue("TFIELDS", oldTfields + 1, "ntf::asciitablehdu:tfields:1");
177 } catch (Exception e) {
178 System.err.println("Impossible exception at addColumn:" + e);
184 * Print a little information about the data set.
187 System.out.println("ASCII Table:");
188 System.out.println(" Header:");
189 System.out.println(" Number of fields:" + myHeader.getIntValue("TFIELDS"));
190 System.out.println(" Number of rows: " + myHeader.getIntValue("NAXIS2"));
191 System.out.println(" Length of row: " + myHeader.getIntValue("NAXIS1"));
192 System.out.println(" Data:");
193 Object[] data = (Object[]) getKernel();
194 for (int i = 0; i < getNCols(); i += 1) {
195 System.out.println(" " + i + ":" + ArrayFuncs.arrayDescription(data[i]));
199 /** Return the FITS data structure associated with this HDU.
201 public Data getData() {
205 /** Return the keyword column stems for an ASCII table.
207 public String[] columnKeyStems() {