CCfits  2.6
Selecting Table Data

This function demonstrates the operation of filtering a table by selecting rows that satisfy a condition and writing them to a new file, or overwriting a table with the filtered data. A third mode, where a filtered dataset is appended to the file containing the source data, will be available shortly, but is currently not supported by cfitsio.

The expression syntax for the conditions that may be applied to table data are described in the cfitsio manual. In the example below, we illustrate filtering with a boolean expression involving one of the columns.

The two flags at the end of the call to FITS::filter are an `overwrite' flag - which only has meaning if the inFile and outFile are the same, and a 'read' flag. overwrite defaults to true. The second flag is a 'read' flag which defaults to false. When set true the user has immediate access to the filtered data.

(Also see the section "Reading with Extended File Name Syntax")

int selectRows()
{
const string inFile("atestfil.fit");
const string outFile("btestfil.fit");
const string newFile("ctestfil.fit");
remove(newFile.c_str());
// test 1: write to a new file
std::auto_ptr<FITS> pInfile(new FITS(inFile,Write,string("PLANETS_ASCII")));
FITS* infile(pInfile.get());
std::auto_ptr<FITS> pNewfile(new FITS(newFile,Write));
ExtHDU& source = infile->extension("PLANETS_ASCII");
const string expression("DENSITY > 3.0");
Table& sink1 = pNewfile->filter(expression,source,false,true);
std::cout << sink1 << std::endl;
// test 2: write a new HDU to the current file, overwrite false, read true.
// AS OF 7/2/01 does not work because of a bug in cfitsio, but does not
// crash, simply writes a new header to the file without also writing the
// selected data.
Table& sink2 = infile->filter(expression,source,false,true);
std::cout << sink2 << std::endl;
// reset the source file back to the extension in question.
source = infile->extension("PLANETS_ASCII");
// test 3: overwrite the current HDU with filtered data.
Table& sink3 = infile->filter(expression,source,true,true);
std::cout << sink3 << std::endl;
return 0;
}