001 /*
002 * @(#)FitsTform.java $Revision: 1.4 $ $Date: 2003/04/11 08:41:15 $
003 *
004 * Copyright (C) 1999 European Southern Observatory
005 * License: GNU General Public License version 2 or later
006 */
007 package org.eso.fits;
008
009 import java.lang.*;
010 import java.util.*;
011
012 /** FitsTform class decodes information in value fields of FITS
013 * table TFORM and TDISP keywords.
014 *
015 * @version $Revision: 1.4 $ $Date: 2003/04/11 08:41:15 $
016 * @author P.Grosbol DMD/ESO, <pgrosbol@eso.org>
017 */
018 public class FitsTform {
019
020 private char dataType = '\0';
021 private String format;
022 private char eFormat = '\0';
023 private int repeat = 1;
024 private int width = 0;
025 private int decimals = 0;
026 private int exponent = 0;
027 private String additional = "";
028
029 /** Constructur for FitsTform class from a FITS table format string.
030 *
031 * @param form String with TFORM or TDISP format
032 * @exception FitsException */
033 public FitsTform(String form) throws FitsException {
034 char ch;
035 String tok = "";
036
037 format = form.toUpperCase();
038 StringTokenizer stok = new StringTokenizer(format,
039 "LXBIJAFEDCMP.OZNSG",
040 true);
041
042 if (stok.hasMoreTokens()) {
043 tok = stok.nextToken();
044 if (Character.isDigit(tok.charAt(0))) { // decode format repeat
045 try {
046 repeat = (Integer.valueOf(tok)).intValue();
047 } catch (NumberFormatException e) {
048 throw new FitsException("Wrong TFORM format",
049 FitsException.HEADER);
050 }
051 if (stok.hasMoreTokens()) {
052 tok = stok.nextToken();
053 }
054 }
055 ch = tok.charAt(0); // get format data type
056 if (Character.isLetter(ch)) {
057 this.dataType = ch;
058 } else {
059 throw new FitsException("Wrong TFORM format",
060 FitsException.HEADER);
061 }
062 }
063
064 // save additional information after data type character
065 int idx = format.indexOf(this.dataType) + 1;
066 if (idx<format.length()) {
067 additional = format.substring(idx);
068 }
069
070 if (dataType != 'P') { // find and decode width etc.
071 while (stok.hasMoreTokens()) {
072 tok = stok.nextToken();
073 ch = tok.charAt(0);
074 if (Character.isDigit(ch)) break;
075 if (eFormat == '\0') {
076 eFormat = ch;
077 }
078 }
079 if (Character.isDigit(tok.charAt(0))) {
080 try {
081 width = (Integer.valueOf(tok)).intValue();
082 } catch (NumberFormatException e) {
083 throw new FitsException("Wrong TFORM format",
084 FitsException.HEADER);
085 }
086 }
087 else {
088 switch (dataType) {
089 case 'L' :
090 case 'B' :
091 case 'A' :
092 width = 1;
093 break;
094 case 'X' :
095 width = (0<repeat) ? (repeat-1)%8 + 1 : 0;
096 break;
097 case 'I' :
098 width = 2;
099 break;
100 case 'J' :
101 case 'E' :
102 width = 4;
103 break;
104 case 'D' :
105 case 'C' :
106 case 'P' :
107 width = 8;
108 break;
109 case 'M' :
110 width = 16;
111 break;
112 default :
113 throw new FitsException("Wrong TFORM format",
114 FitsException.HEADER);
115 }
116 }
117 if (stok.hasMoreTokens()) { // get no. of dicimals
118 tok = stok.nextToken();
119 if (tok.charAt(0) == '.') {
120 if (stok.hasMoreTokens()) tok = stok.nextToken();
121 try {
122 decimals = (Integer.valueOf(tok)).intValue();
123 } catch (NumberFormatException e) {
124 throw new FitsException("Wrong TFORM format",
125 FitsException.HEADER);
126 }
127 } else {
128 throw new FitsException("Wrong TFORM format",
129 FitsException.HEADER);
130 }
131
132 if (stok.hasMoreTokens()) { // get size of exponent
133 tok = stok.nextToken();
134 if (tok.charAt(0) == 'E') {
135 if (stok.hasMoreTokens()) tok = stok.nextToken();
136 try {
137 decimals = (Integer.valueOf(tok)).intValue();
138 } catch (NumberFormatException e) {
139 throw new FitsException("Wrong TFORM format",
140 FitsException.HEADER);
141 }
142 } else {
143 throw new FitsException("Wrong TFORM format",
144 FitsException.HEADER);
145 }
146 }
147 }
148 }
149 }
150
151 /** Get method to retrieve the original format string. */
152 public String getFormat(){
153 return format;
154 }
155
156 /** Get method to obtain the data type indicated by the format. */
157 public char getDataType(){
158 return this.dataType;
159 }
160
161 /** Get method to give the extended E-format display format.
162 * 'E' indicates engineering format while 'S' is scientific.
163 * If none is given a null character is returned. */
164 public char getEFormat(){
165 return this.eFormat;
166 }
167
168 /** Get method to obtain the repeat factor of the format for
169 * Binary Tables (by default it is 1). */
170 public int getRepeat(){
171 return repeat;
172 }
173
174 /** Get method to retrieve the field width of a single data value
175 * in bytes. */
176 public int getWidth(){
177 return width;
178 }
179
180 /** Get method to give the number of decimals for display formats.
181 * For B/O/X display formats it gives maximum number. */
182 public int getDecimals(){
183 return decimals;
184 }
185
186 /** Get method to obtain the number of chararters to be displayd
187 * in an exponential display format. */
188 public int getExponent(){
189 return exponent;
190 }
191
192 /** Get method to retreive the additional information string.
193 * This may be appended the prime data type in Binary Table
194 * TFORM keywords e.g. P type. */
195 public String getAdditional(){
196 return additional;
197 }
198 }
199
200
201
202