-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsciiTable.cxx
365 lines (302 loc) · 10.9 KB
/
AsciiTable.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Astrophysics Science Division,
// NASA/ Goddard Space Flight Center
// HEASARC
// http://heasarc.gsfc.nasa.gov
// e-mail: [email protected]
//
// Original author: Ben Dorman
#ifdef _MSC_VER
#include "MSconfig.h" // for truncation warning
#endif
// ColumnCreator
#include "ColumnCreator.h"
// Column
#include "Column.h"
// AsciiTable
#include "AsciiTable.h"
#ifdef SSTREAM_DEFECT
#include <strstream>
#else
#include <sstream>
#endif
namespace CCfits {
// Class CCfits::AsciiTable
AsciiTable::AsciiTable(const AsciiTable &right)
: Table(right)
{
}
AsciiTable::AsciiTable (FITS* p, const String &hduName, bool readFlag, const std::vector<String>& keys, int version)
: Table(p, AsciiTbl,hduName,version)
{
init(readFlag,keys);
}
AsciiTable::AsciiTable (FITS* p, const String &hduName, int rows, const std::vector<String>& columnName, const std::vector<String>& columnFmt, const std::vector<String>& columnUnit, int version)
: Table(p, AsciiTbl , hduName, rows, columnName, columnFmt, columnUnit, version)
{
long width=0;
int decimals=0;
int status=0;
int colType=0;
ColumnCreator create(this);
for (int i=0; i< numCols(); i++)
{
status = fits_ascii_tform(const_cast<char *>(columnFmt[i].c_str()),
&colType, &width, &decimals, &status);
if (status != 0) throw FitsError(status);
Column *newCol = create.createColumn(i+1,ValueType(colType),
columnName[i], columnFmt[i], columnUnit[i],1,width);
setColumn(columnName[i],newCol);
newCol->setLimits(ValueType(colType));
}
}
AsciiTable::AsciiTable (FITS* p, int number)
: Table(p,AsciiTbl,number)
{
init();
}
AsciiTable::~AsciiTable()
{
}
void AsciiTable::readTableHeader (int ncols, std::vector<String>& colName, std::vector<String>& colFmt, std::vector<String>& colUnit)
{
long rowlen=0;
long *tbcol = new long[ncols];
int status=0;
// hduName has already been set in ExtHDU so it is allocated and
// read here only because cfitsio requires it.
char hduName[FLEN_KEYWORD];
char** columnName = new char*[ncols];
char** columnFmt = new char*[ncols];
char** columnUnit = new char*[ncols];
int i = 0; // for MS VC++
for(; i < ncols; i++)
{
columnName[i] = new char[FLEN_KEYWORD];
columnFmt[i] = new char[FLEN_KEYWORD];
columnUnit[i] = new char[FLEN_KEYWORD];
}
long nRows = 0;
int fields = 0;
status = fits_read_atblhdr(fitsPointer(), ncols, &rowlen, &nRows, &fields,
columnName, tbcol, columnFmt, columnUnit,
hduName, &status);
rows(nRows);
numCols(fields);
for( i = 0; i < ncols; i++)
{
colName[i] = String(columnName[i]);
colFmt[i] = String(columnFmt[i]);
colUnit[i] = String(columnUnit[i]);
delete [] columnName[i];
delete [] columnFmt[i];
delete [] columnUnit[i];
}
delete [] columnName;
delete [] columnFmt;
delete [] columnUnit;
if (status != 0) throw FitsError(status);
delete [] tbcol;
}
AsciiTable * AsciiTable::clone (FITS* p) const
{
AsciiTable* cloned = new AsciiTable(*this);
cloned->parent() = p;
return cloned;
}
void AsciiTable::readData (bool readFlag, const std::vector<String>& keys)
{
int rowsRead=0;
int status = 0;
long rowSize = 0;
// grab the optimal rowsize using the get_rowsize call. It just
// might have changed since the Table HDU was constructed so it should
// be set just before reading takes place.
if (fits_get_rowsize(fitsPointer(), &rowSize, &status)) throw FitsError(status);
size_t keysRead = 0;
size_t nkey=keys.size();
ColMap::iterator endColumn = column().end();
// if a set of strings were supplied, interpret these as
// keywords and columns to be read.
// get a container for keys which correspond to columns.
std::vector<String> colKeys;
colKeys.reserve(nkey);
if (nkey > 0)
{
for (keysRead = 0; keysRead < nkey; keysRead++)
{
try
{
// after the read function is called by the ctor,
// the columns in the table have been indexed.
// check that the key in question is not a column
// name.
if (column().find(keys[keysRead]) == endColumn) readKeyword(keys[keysRead]);
else colKeys.push_back(keys[keysRead]);
}
catch (HDU::NoSuchKeyword)
{
continue;
}
catch (...)
{
throw;
}
}
}
// if readFlag is false, don't get any data.
if (!readFlag) return;
// the data consist of scalars in each row. For an ASCII table,
// rowSize is probably quite big because there's just one element per row.
for (rowsRead=0; rowsRead< rows() ; rowsRead+=rowSize)
{
ColMap::iterator col;
if (colKeys.size() > 0)
{
for (size_t i=0; i < colKeys.size() ; i++)
{
// if a set of keys was entered, read the data in the ones
// that correspond to columns, as checked earlier
col = column().find(colKeys[i]);
Column& current = *((*col).second);
current.readData(rowsRead+1,
current.repeat()*std::min<size_t>(rowSize,rows()-rowsRead),1);
}
}
else
{
// if no keys that correspond to column names were supplied, read all the data.
for(col = column().begin(); col != column().end(); col++ )
{
Column& current = *((*col).second);
current.readData(rowsRead+1,
current.repeat()*std::min<size_t>(rowSize,rows() - rowsRead),1);
}
}
}
if (colKeys.size() == 0)
{
// mark all columns read
for (ColMap::iterator col = column().begin();
col != column().end(); ++col)
{
(*col).second->isRead(true);
}
}
else
{
for (size_t i=0; i < colKeys.size() ; i++)
{
// if a set of keys was entered, read the data in the ones
// that correspond to columns, as checked earlier
ColMap::iterator col = column().find(colKeys[i]);
(*col).second->isRead(true);
}
}
}
void AsciiTable::addColumn (ValueType type, const String& columnName, long repeatWidth, const String& colUnit, long decimals, size_t columnNumber)
{
String diag("");
// repeatWidth has the following semantics:
// for non-string represents the multiplicity of entries in the column cell, and
// is therefore always 1 for numeric datatypes.
// for strings it represents the string width.
// In cfitsio, the information about datatypes is carried in the width field,
// but here this is carried by the type value.
if (type < 0 )
{
diag += " writing vector-valued column to ASCII table: ";
diag += name(); // name of ascii table extension
throw InvalidColumnSpecification(diag);
}
if (type == Tstring && repeatWidth < 1)
{
diag += " length of string values unspecified for Column: ";
diag += columnName;
throw InvalidColumnSpecification(diag);
}
if ( ( type == Tfloat || type == Tdouble ) )
{
if ( decimals < 0 || decimals > repeatWidth )
{
diag += " invalid specification for floating point data format in Column " ;
diag += columnName;
throw InvalidColumnSpecification(diag);
}
}
#ifdef SSTREAM_DEFECT
std::ostrstream tformStr;
#else
std::ostringstream tformStr;
#endif
switch (type)
{
case Tstring:
tformStr << 'A' << repeatWidth;
break;
case Tshort:
case Tint:
tformStr << 'I' << 6;
break;
case Tlong:
tformStr << 'I' << 12;
break;
case Tfloat:
if (repeatWidth <= 7)
{
tformStr << 'E' << repeatWidth << '.' << decimals;
}
else
{
tformStr << 'D' << repeatWidth << '.' << decimals;
}
break;
case Tdouble:
tformStr << 'D' << repeatWidth << '.' << decimals;
break;
default:
diag += "Invalid data type for ASCII table ";
diag += name();
throw WrongExtensionType(diag);
}
# ifdef SSTREAM_DEFECT
tformStr << std::ends;
# endif
makeThisCurrent();
// the user is allowed to specify which column number to create although
// this is frowned down upon because the user shouldn't care how the data
// are written internally.
int colNum(0);
int status(0);
if ( columnNumber == 0)
{
if (fits_get_num_cols(fitsPointer(),&colNum,&status)) throw FitsError(status);
colNum +=1;
}
else colNum = columnNumber;
//string ColumnName(FITSUtil::upperCase(columnName));
String tfString(tformStr.str());
char* tform = const_cast<char*>(tfString.c_str());
char* ttype = const_cast<char*>(columnName.c_str());
if (fits_insert_col(fitsPointer(),colNum,ttype,tform,&status)) throw FitsError(status);
if (colUnit.size())
{
std::ostringstream ustream;
char unitComment[] = "";
ustream << "TUNIT" << colNum;
if (fits_write_key (fitsPointer(), Tstring,
const_cast<char*>(ustream.str().c_str()),
const_cast<char*>(colUnit.c_str()),
unitComment, &status))
{
throw FitsError (status);
}
}
ColumnCreator create(this);
Column *newCol=create.createColumn(colNum,type,columnName,tfString,
colUnit,1,repeatWidth);
if (type != Tstring) newCol->setLimits(type);
if (columnNumber != 0) reindex(columnNumber, true);
setColumn(columnName,newCol);
}
// Additional Declarations
} // namespace CCfits