I am having a tough time reading variable-length arrays from the entries of a FITS table, using the CFITSIO libraries (I have to use them due to another software I am developing).
Now, the FITS table I am trying to read looks like this:
As you can see, the last three columns, instead of having scalar values in their cells, contain variable-length arrays.
The CFITSIO
documentation is not very helpful with this particular case: most of the basic routine is thought to generate array by reading directly regular columns (with scalar in their cells, see section 2 of https://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/node46.html).
fits_read_col
will not work with this data structure.
Now it is recommended to use the fits_read_descript
routine when reading variable columns. The problem is that this function returns low-level information, in particular, the starting offset in the heap where the array is stored (see section 7 of https://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/node82.html).
So even if I get low-level information on the cell containing multiple arrays, then it is not clear how to use it to fetch the numerical values!
CFITSIO Iterators are marginally helpful, and there is no example with such complicated data structure.
Has anybody ever done this before? Is there anybody able to produce a snippet using the CFITSIO
to read variable-length array? It would be immensely helpful.
The FITS file of which I took the screenshot can be found here.
Here a tentative snippet opening the file and examining the columns and rows, applying the suggested fits_read_descript
function for variable-length columns. I do not know how to proceed further because I don't know how to exploit the parameters returned to fetch the actual numerical values in the table.
#include "fitsio.h"
#include <iostream>
int main(){
fitsfile *fp = 0; // pointer to fitsfile type provided in CFITSIO library
int status = 0; // variable passed down to different CFITSIO functions
// open the fits file, go to the Header Data Unit 1 containing the table
// with variable-length arrays
fits_open_file(&fp, "rmf_obs5029747.fits[1]", READONLY, &status);
// read HDU type
int hdutype;
fits_get_hdu_type(fp, &hdutype, &status);
std::cout << "found type " << hdutype << " HDU type." << "\n";
// read number of rows and columns
long nTableRows;
int nTableCols;
fits_get_num_rows(fp, &nTableRows, &status);
fits_get_num_cols(fp, &nTableCols, &status);
std::cout << "the table has " << nTableRows << " rows" << "\n";
std::cout << "the table has " << nTableCols << " columns" << "\n";
// loop through the columns and consider only those with a negative typecode
// indicating that they contain a variable-length array
// https://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/node29.html
int typecode;
long repeat;
long width;
long offset;
for (int colnum = 0; colnum < nTableCols; ++colnum) {
fits_get_coltype(fp, colnum+1, &typecode, &repeat, &width, &status);
if (typecode < 1) {
std::cout << "->column " << colnum << " contains a variable-length array" << "\n";
std::cout << "->examining its rows..." << "\n";
// loop through the rows
for (int rownum = 0; rownum < nTableRows; ++rownum)
fits_read_descript(fp, colnum, rownum, &repeat, &offset, &status);
}
}
}