【发布时间】:2020-03-03 14:40:48
【问题描述】:
我很难使用 CFITSIO 库从 FITS 表的条目中读取可变长度数组(由于我正在开发另一个软件,我必须使用它们)。
现在,我尝试读取的 FITS 表如下所示:
如您所见,最后三列的单元格中没有标量值,而是包含可变长度数组。
CFITSIO 文档对于这种特殊情况不是很有帮助:大多数基本例程被认为是通过直接读取常规列来生成数组(在其单元格中带有标量,请参阅https://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/node46.html 的第 2 节)。
fits_read_col 不适用于此数据结构。
现在建议在读取变量列时使用fits_read_descript 例程。问题是该函数返回低级信息,特别是存储数组的堆中的起始偏移量(参见https://heasarc.gsfc.nasa.gov/docs/software/fitsio/c/c_user/node82.html 的第 7 节)。
因此,即使我获得了包含多个数组的单元格的低级信息,也不清楚如何使用它来获取数值!
CFITSIO Iterators有点用处,没有这么复杂数据结构的例子。
以前有人做过吗?有没有人能够使用CFITSIO 生成一个 sn-p 来读取可变长度数组?这将非常有帮助。
我截取的FITS文件可以在here找到。
这里试探性的 sn-p 打开文件并检查列和行,将建议的 fits_read_descript 函数应用于可变长度列。我不知道如何进一步进行,因为我不知道如何利用返回的参数来获取表中的实际数值。
#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);
}
}
}
【问题讨论】: