GFitsTable.cpp
21.4 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/***************************************************************************
* GFitsTable.cpp - FITS table base class *
* ----------------------------------------------------------------------- *
* copyright : (C) 2008 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* ----------------------------------------------------------------------- *
***************************************************************************/
/* __ Includes ___________________________________________________________ */
#include <iostream>
#include "GException.hpp"
#include "GTools.hpp"
#include "GFitsTable.hpp"
#include "GFitsTableStrCol.hpp"
#include "GFitsTableShtCol.hpp"
#include "GFitsTableLngCol.hpp"
#include "GFitsTableFltCol.hpp"
#include "GFitsTableDblCol.hpp"
/* __ Namespaces _________________________________________________________ */
/* __ Method name definitions ____________________________________________ */
#define G_OPEN "GFitsTable::open(fitsfile*)"
#define G_SAVE "GFitsTable::save()"
#define G_COLUMN1 "GFitsTable::column(const std::string&)"
#define G_COLUMN2 "GFitsTable::column(const int&)"
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/*==========================================================================
= =
= GFitsTable constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Constructor
***************************************************************************/
GFitsTable::GFitsTable() : GFitsData()
{
// Initialise class members for clean destruction
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Constructor
*
* @param[in] nrows Number of rows in table
* @param[in] ncols Number of columns in table
***************************************************************************/
GFitsTable::GFitsTable(int nrows, int ncols) : GFitsData()
{
// Initialise class members for clean destruction
init_members();
// Store table dimension
m_rows = nrows;
m_cols = ncols;
// If there are columns then allocate memory for column pointers
if (m_cols > 0) {
// Allocate pointer memory
m_columns = new GFitsTableCol*[m_cols];
// Initialise pointers
for (int i = 0; i < m_cols; ++i)
m_columns[i] = NULL;
} // endfor: looped over all columns
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] table Table which will be used to construct GFitsTable instance
***************************************************************************/
GFitsTable::GFitsTable(const GFitsTable& table) : GFitsData(table)
{
// Initialise class members for clean destruction
init_members();
// Copy members
copy_members(table);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GFitsTable::~GFitsTable()
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= GFitsTable operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] table Table which will be assigned
***************************************************************************/
GFitsTable& GFitsTable::operator= (const GFitsTable& table)
{
// Execute only if object is not identical
if (this != &table) {
// Copy base class members
this->GFitsData::operator=(table);
// Free members
free_members();
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(table);
} // endif: object was not identical
// Return this object
return *this;
}
/*==========================================================================
= =
= GFitsTable public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Open Table
*
* @param[in] fptr FITS file pointer
*
* Builds a description of the binary table in memory. Columns are not loaded
* but column descriptors are allocated. The column data will only be loaded
* once it needs to be accessed.
***************************************************************************/
void GFitsTable::open(__fitsfile* fptr)
{
// Move to HDU
int status = 0;
status = __ffmahd(fptr, (fptr->HDUposition)+1, NULL, &status);
if (status != 0)
throw GException::fits_error(G_OPEN, status);
// Save FITS file pointer
m_fitsfile = *fptr;
// Determine number of rows in table
long nrows = 0;
status = __ffgnrw(&m_fitsfile, &nrows, &status);
if (status != 0)
throw GException::fits_error(G_OPEN, status);
else
m_rows = (int)nrows;
// Determine number of columns in table
status = __ffgncl(&m_fitsfile, &m_cols, &status);
if (status != 0)
throw GException::fits_error(G_OPEN, status);
// Allocate memory for column pointers
if (m_columns != NULL) delete [] m_columns;
m_columns = new GFitsTableCol*[m_cols];
// Get table column information
int typecode = 0;
long repeat = 0;
long width = 0;
for (int i = 0; i < m_cols; ++i) {
// Get column name
char keyname[10];
char value[80];
sprintf(keyname, "TTYPE%d", i+1);
status = __ffgkey(&m_fitsfile, keyname, value, NULL, &status);
if (status != 0)
throw GException::fits_error(G_OPEN, status);
value[strlen(value)-1] = '\0';
// Get column definition
status = __ffgtcl(&m_fitsfile, i+1, &typecode, &repeat, &width, &status);
if (status != 0)
throw GException::fits_error(G_OPEN, status);
// Allocate column
switch (typecode) {
case __TSTRING:
m_columns[i] = new GFitsTableStrCol();
break;
case __TSHORT:
m_columns[i] = new GFitsTableShtCol();
break;
case __TLONG:
m_columns[i] = new GFitsTableLngCol();
break;
case __TFLOAT:
m_columns[i] = new GFitsTableFltCol();
break;
case __TDOUBLE:
m_columns[i] = new GFitsTableDblCol();
break;
default:
throw GException::fits_unknown_coltype(G_OPEN, typecode);
break;
}
// Store column definition
m_columns[i]->set_name(strip_whitespace(&(value[1])));
m_columns[i]->set_colnum(i+1);
m_columns[i]->set_type(typecode);
m_columns[i]->set_repeat(repeat);
m_columns[i]->set_width(width);
m_columns[i]->set_length(m_rows);
m_columns[i]->set_fitsfile(fptr);
} // endfor: looped over all columns
// Return
return;
}
/***********************************************************************//**
* @brief Save FITS table
***************************************************************************/
void GFitsTable::save(void)
{
//cout << "GFitsTable::save entry" << endl;
// Move to HDU
int status = 0;
status = __ffmahd(&m_fitsfile, (m_fitsfile.HDUposition)+1, NULL, &status);
// If HDU does not yet exist in file then create it now
if (status == 107) {
// Reset status
status = 0;
// Initialise number of fields
int tfields = 0;
// Setup cfitsio column definition arrays
char** ttype = NULL;
char** tform = NULL;
char** tunit = NULL;
if (m_cols > 0) {
ttype = new char*[m_cols];
tform = new char*[m_cols];
tunit = new char*[m_cols];
for (int i = 0; i < m_cols; ++i) {
ttype[i] = NULL;
tform[i] = NULL;
tunit[i] = NULL;
}
for (int i = 0; i < m_cols; ++i) {
ttype[tfields] = get_ttype(i);
tform[tfields] = get_tform(i);
tunit[tfields] = get_tunit(i);
if (ttype[tfields] != NULL && tform[tfields] != NULL &&
tunit[tfields] != NULL)
tfields++;
}
}
// Create FITS table
status = __ffcrtb(&m_fitsfile, m_type, m_rows, tfields, ttype, tform,
tunit, NULL, &status);
if (status != 0)
throw GException::fits_error(G_SAVE, status);
// De-allocate column definition arrays
if (m_cols > 0) {
for (int i = 0; i < m_cols; ++i) {
if (ttype[i] != NULL) delete [] ttype[i];
if (tform[i] != NULL) delete [] tform[i];
if (tunit[i] != NULL) delete [] tunit[i];
}
if (ttype != NULL) delete [] ttype;
if (ttype != NULL) delete [] tform;
if (ttype != NULL) delete [] tunit;
}
}
else if (status != 0)
throw GException::fits_error(G_SAVE, status);
// Return
return;
}
/***********************************************************************//**
* @brief Close table
***************************************************************************/
void GFitsTable::close(void)
{
// Free members
free_members();
// Initialise members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Return pointer to column with specified name
*
* @param[in] colname Name of column which should be accessed
*
* @exception GException::fits_no_data
* There are no column data in the table or there is not data
* for this column
* @exception GException::fits_column_not_found
* Requested column has not been found in table.
***************************************************************************/
GFitsTableCol* GFitsTable::column(const std::string& colname)
{
// If there is no data then throw an exception
if (m_columns == NULL)
throw GException::fits_no_data(G_COLUMN1, "No column data in table");
// Initialise pointer
GFitsTableCol* ptr = NULL;
// If there are columns then search for the specified name
if (m_columns != NULL) {
for (int i = 0; i < m_cols; ++i) {
if (m_columns[i]->name() == colname) {
ptr = m_columns[i];
if (ptr == NULL)
throw GException::fits_no_data(G_COLUMN1,
"No data for this column");
break;
}
}
}
// If column has not been found throw an exception
if (ptr == NULL)
throw GException::fits_column_not_found(G_COLUMN1, colname);
// Return column pointer
return ptr;
}
/***********************************************************************//**
* @brief Return pointer to column with number
*
* @param[in] colnum Number of requested column (starting from 0)
*
* @exception GException::fits_no_data
* There are no column data in the table or there is not data
* for this column
* @exception GException::out_of_range
* Requested column has not been found in table.
***************************************************************************/
GFitsTableCol* GFitsTable::column(const int& colnum)
{
// If there is no data then throw an exception
if (m_columns == NULL)
throw GException::fits_no_data(G_COLUMN2, "No column data in table");
// If column number is out of range then throw an exception
if (colnum < 0 || colnum >= m_cols)
throw GException::out_of_range(G_COLUMN2, colnum, 0, m_cols-1);
// Get column pointer
GFitsTableCol* ptr = ptr = m_columns[colnum];
if (ptr == NULL)
throw GException::fits_no_data(G_COLUMN2, "No data for this column");
// Return column pointer
return ptr;
}
/***********************************************************************//**
* @brief Return number of rows in table
***************************************************************************/
int GFitsTable::rows(void) const
{
// Return number of rows
return m_rows;
}
/***********************************************************************//**
* @brief Return number of columns in table
***************************************************************************/
int GFitsTable::cols(void) const
{
// Return number of columns
return m_cols;
}
/*==========================================================================
= =
= GFitsTable private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GFitsTable::init_members(void)
{
// Initialise members
m_type = -1;
m_rows = 0;
m_cols = 0;
m_columns = NULL;
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] table Table to copy
***************************************************************************/
void GFitsTable::copy_members(const GFitsTable& table)
{
// Copy attributes
m_type = table.m_type;
m_rows = table.m_rows;
m_cols = table.m_cols;
// Copy column definition
if (table.m_columns != NULL && m_cols > 0) {
m_columns = new GFitsTableCol*[m_cols];
for (int i = 0; i < m_cols; ++i) {
if (table.m_columns[i] != NULL)
m_columns[i] = table.m_columns[i]->clone();
else
m_columns[i] = NULL;
}
}
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GFitsTable::free_members(void)
{
// Free memory
for (int i = 0; i < m_cols; ++i) {
if (m_columns[i] != NULL) delete m_columns[i];
}
if (m_columns != NULL) delete [] m_columns;
// Mark memory as freed
m_columns = NULL;
// Return
return;
}
/***********************************************************************//**
* @brief Connect table to FITS file
*
* @param[in] fptr FITS file pointer to which the table should be connected
*
* The connection of the table is done by connecting all columns.
***************************************************************************/
void GFitsTable::connect(__fitsfile* fptr)
{
// First connect binary table
m_fitsfile = *fptr;
// Then connect all columns
if (m_columns != NULL) {
for (int i = 0; i < m_cols; ++i) {
if (m_columns[i] != NULL) m_columns[i]->connect(fptr);
}
}
// Return
return;
}
/***********************************************************************//**
* @brief Returns pointer to column type
*
* @param[in] colnum Column number for which type is to be returned
*
* This methods allocates memory for the character string that holds the
* column type. The client has to de-allocate this memory after usage.
* In case that the column does not exist a NULL pointer is returned.
***************************************************************************/
char* GFitsTable::get_ttype(const int& colnum) const
{
// Initialise result with NULL pointer
char* ptr = NULL;
// Get type only if column exists
if (m_columns != NULL && colnum >=0 && colnum < m_cols &&
m_columns[colnum] != NULL) {
int size = m_columns[colnum]->m_name.length() + 1;
ptr = new char[size];
strncpy(ptr, m_columns[colnum]->m_name.c_str(), size-1);
}
// Return result
return ptr;
}
/***********************************************************************//**
* @brief Returns pointer to column format
*
* @param[in] colnum Column number for which format is to be returned
*
* This methods allocates memory for the character string that holds the
* column format. The client has to de-allocate this memory after usage.
* In case that the column does not exist a NULL pointer is returned.
***************************************************************************/
char* GFitsTable::get_tform(const int& colnum) const
{
// Initialise result with NULL pointer
char* ptr = NULL;
// Get type only if column exists
if (m_columns != NULL && colnum >=0 && colnum < m_cols &&
m_columns[colnum] != NULL) {
int size = m_columns[colnum]->m_format.length() + 1;
ptr = new char[size];
strncpy(ptr, m_columns[colnum]->m_format.c_str(), size-1);
}
// Return result
return ptr;
}
/***********************************************************************//**
* @brief Returns pointer to column unit
*
* @param[in] colnum Column number for which unit is to be returned
*
* This methods allocates memory for the character string that holds the
* column unit. The client has to de-allocate this memory after usage.
* In case that the column does not exist a NULL pointer is returned.
***************************************************************************/
char* GFitsTable::get_tunit(const int& colnum) const
{
// Initialise result with NULL pointer
char* ptr = NULL;
// Get type only if column exists
if (m_columns != NULL && colnum >=0 && colnum < m_cols &&
m_columns[colnum] != NULL) {
int size = m_columns[colnum]->m_unit.length() + 1;
ptr = new char[size];
strncpy(ptr, m_columns[colnum]->m_unit.c_str(), size-1);
}
// Return result
return ptr;
}
/*==========================================================================
= =
= GFitsTable friends =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Output operator
*
* @param[in] os Output stream
* @param[in] table Table to put in output stream
***************************************************************************/
ostream& operator<< (ostream& os, const GFitsTable& table)
{
// Put header in stream
os << "=== GFitsTable ===" << endl;
os << " Table type ................: ";
switch (table.m_type) {
case 1:
os << "ASCII table" << endl;
break;
case 2:
os << "Binary table" << endl;
break;
default:
os << "Unknown" << endl;
break;
}
os << " Number of rows ............: " << table.m_rows << endl;
os << " Number of columns .........: " << table.m_cols << endl;
if (table.m_columns != NULL) {
for (int i = 0; i < table.m_cols; ++i) {
if (table.m_columns[i] != NULL)
os << " " << *(table.m_columns[i]);
else
os << " column " << i << " undefined" << endl;
}
}
else
os << " table columns undefined" << endl;
// Return output stream
return os;
}
/*==========================================================================
= =
= Other functions used by GFitsTable =
= =
==========================================================================*/