In this section of the program we create a new Table extension of type AsciiTbl, and write three columns with 6 rows. Then we add another copy of the data two rows down (starting from row 3) thus overwriting values and creating new rows. We test the use of null values, and writing a date string. Implicit data conversion, as illustrated for images above, is supported. However, writing numeric data as character data, supported by cfitsio, is not supported by CCfits.
The FITS::addTable function takes as one of its last arguments a HDU Type parameter, which needs to be AsciiTbl or BinTbl. The default is to create a BinTable (see next function).
addTable returns a pointer to Table, which is the abstract immediate superclass of the concrete classes AsciiTable and BinTable, whereas addImage returns a pointer to ExtHDU, which is the abstract base class of all FITS extensions. These base classes implement the public interface necessary to avoid the user of the library needing to downcast to a concrete type.
int writeAscii ()
{
std::auto_ptr<FITS> pFits(0);
try
{
const std::string fileName("atestfil.fit");
pFits.reset( new FITS(fileName,Write) );
}
{
return -1;
}
unsigned long rows(6);
string hduName("PLANETS_ASCII");
std::vector<string> colName(3,"");
std::vector<string> colForm(3,"");
std::vector<string> colUnit(3,"");
colName[0] = "Planet";
colName[1] = "Diameter";
colName[2] = "Density";
colForm[0] = "a8";
colForm[1] = "i6";
colForm[2] = "f4.2";
colUnit[0] = "";
colUnit[1] = "km";
colUnit[2] = "g/cm^-3";
std::vector<string> planets(rows);
const char *planet[] = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn"};
const char *mnemoy[] = {"Many","Volcanoes","Erupt","Mulberry","Jam","Sandwiches",
"Under","Normal","Pressure"};
long diameter[] = { 4880, 12112, 12742, 6800, 143000, 121000};
float density[] = { 5.1f, 5.3f, 5.52f, 3.94f, 1.33f, 0.69f};
Table* newTable = pFits->addTable(hduName,rows,colName,colForm,colUnit,AsciiTbl);
size_t j = 0;
for ( ; j < rows; ++j) planets[j] = string(planet[j]);
try
{
newTable->column(colName[0]).write(planets,1);
newTable->column(colName[1]).write(diameter,rows,1);
newTable->column(colName[2]).write(density,rows,1);
}
catch (FitsException&)
{
std::cerr << " Error in writing to columns - check e.g. that columns of specified name "
<< " exist in the extension \n";
}
FITSUtil::auto_array_ptr<long> pDiameter(new long[rows]);
FITSUtil::auto_array_ptr<float> pDensity(new float[rows]);
long* Cdiameter = pDiameter.get();
float* Cdensity = pDensity.get();
Cdiameter[0] = 4880; Cdiameter[1] = 12112; Cdiameter[2] = 12742; Cdiameter[3] = 6800;
Cdiameter[4] = 143000; Cdiameter[5] = 121000;
Cdensity[0] = 5.1f; Cdensity[1] = 5.3f; Cdensity[2] = 5.52f;
Cdensity[3] = 3.94f; Cdensity[4] = 1.33f; Cdensity[5] = 0.69;
std::cout << *newTable << std::endl;
pFits->pHDU().addKey("NEWVALUE",42," Test of adding keyword to different extension");
pFits->pHDU().addKey("STRING",std::string(" Rope "),"trailing blank test 1 ");
pFits->pHDU().addKey("STRING2",std::string("Cord"),"trailing blank test 2 ");
long ignoreVal(12112);
long nullNumber(-999);
try
{
newTable->column(colName[1]).addNullValue(nullNumber);
newTable->column(colName[2]).write(density,rows,rows-3);
newTable->column(colName[1]).write(diameter,rows,rows-3,&ignoreVal);
}
catch (FitsException&)
{
}
std::cout << pFits->pHDU() << std::endl;
std::vector<string> mnemon(9);
for ( j = 0; j < 9; ++j) mnemon[j] = string(mnemoy[j]);
newTable->addColumn(Tstring,"Mnemonic",10," words ");
newTable->column("Mnemonic").write(mnemon,1);
newTable->writeDate();
std::cout << *newTable << std::endl;
return 0;
}