29 WriteKey(
"TELESCOP",
"FACT",
"Telescope that acquired this data");
32 WriteKey(
"CREATOR", prgname,
"Program that wrote this file");
33 WriteKey(
"EXTREL", version,
"Release Number");
34 WriteKey(
"COMPILED", __DATE__
" " __TIME__,
"Compile time");
35 WriteKey(
"REVISION",
REVISION,
"SVN revision");
36 WriteKey(
"ORIGIN",
"FACT",
"Institution that wrote the file");
37 WriteKey(
"DATE", now.
Iso(),
"File creation date");
38 WriteKey(
"NIGHT", now.
NightAsInt(),
"Night as int");
39 WriteKey(
"TIMESYS",
"UTC",
"Time system");
40 WriteKey(
"TIMEUNIT",
"d",
"Time given in days w.r.t. to MJDREF");
41 WriteKey(
"MJDREF", 40587,
"Store times in UNIX time (for convenience, seconds since 1970/1/1)");
46 catch (
const CCfits::FitsException &e)
48 Error(
"CCfits::Table::addKey failed for '"+fTable->name()+
"' in '"+fFile->name()+
"': "+e.message());
66 fColNames.push_back(name);
67 fColUnits.push_back(unit);
73 switch (toupper(
type))
75 case 'B': str <<
'L';
break;
76 case 'C': str <<
'B';
break;
77 case 'S': str <<
'I';
break;
78 case 'I': str <<
'J';
break;
79 case 'X': str <<
'K';
break;
80 case 'F': str <<
'E';
break;
81 case 'D': str <<
'D';
break;
84 fColTypes.push_back(str.str());
89 fColNames.push_back(name);
90 fColUnits.push_back(unit);
91 fColTypes.push_back(format);
98 Error(
"FitsFile::OpenFile - File already open.");
102 if (!allow_open && access(filename.c_str(), F_OK)==0)
104 Error(
"File '"+filename+
"' already existing.");
110 fFile =
new CCfits::FITS(filename, CCfits::RWmode::Write);
112 catch (CCfits::FitsException e)
114 Error(
"CCfits::FITS failed for '"+filename+
"': "+e.message());
143 Error(
"Fits::SetFile failed: NULL argument.");
149 Error(
"Fits::SetFile failed: File already set.");
163 Error(
"FitsFile::OpenTable - No file open.");
168 Error(
"FitsFile::OpenTable - Table already open.");
173 CCfits::Table *table = 0;
176 table = fFile->addTable(tablename, 0, fColNames, fColTypes, fColUnits);
178 catch (
const CCfits::FitsException &e)
180 Error(
"CCfits::Table::addTable failed for '"+tablename+
"' in '"+fFile->name()+
"': "+e.message());
184 if (table->rows() != 0)
186 Error(
"FITS table '"+tablename+
"' created in '"+fFile->name()+
"' on the fly looks non-empty.");
210 Error(
"FitsFile::OpenNewTable - No file open.");
216 Error(
"FitsFile::OpenNewTable - Table already open.");
221 fFile->read(vector<string>(1, tableName));
224 const multimap<string, CCfits::ExtHDU *> &extMap = fFile->extension();
226 for (
int i=0;
i<maxtry;
i++)
236 const string tname = str.str();
238 const multimap<string,CCfits::ExtHDU*>::const_iterator it = extMap.find(tname);
241 if (it == extMap.end())
248 return OpenTable(tname);
251 CCfits::Table *table =
dynamic_cast<CCfits::Table*
>(it->second);
256 Error(
"HDU '"+tname+
"' found in file, but it is not a proper CCfits::Table.");
262 table->makeThisCurrent();
273 fNumRows = table->rows();
284 CCfits::BinTable* bTable =
dynamic_cast<CCfits::BinTable*
>(table);
287 Error(
"Table '"+tableName+
"' found in '"+fFile->name()+
"' is not a binary table.");
292 vector<string> colName;
293 bTable->readData(
true, colName);
297 const map<string,CCfits::Column*> &cMap = table->column();
300 for (map<string, CCfits::Column*>::const_iterator mapIt = cMap.begin(); mapIt != cMap.end(); mapIt++)
303 for (
unsigned int ii=0;ii<fColNames.size();ii++)
305 if (mapIt->first == fColNames[ii])
308 if (mapIt->second->format() != fColTypes[ii])
310 Error(
"Column "+fColNames[ii]+
" has wrong format ("+fColTypes[ii]+
" vs "+mapIt->second->format()+
" in file)");
317 Error(
"Column "+mapIt->first+
" only exist in written file");
322 for (
unsigned int ii=0;ii<fColNames.size();ii++)
325 for (map<string, CCfits::Column*>::const_iterator mapIt = cMap.begin(); mapIt != cMap.end(); mapIt++)
327 if (fColNames[ii] == mapIt->first)
330 if (fColTypes[ii] != mapIt->second->format())
332 Error(
"Column "+fColNames[ii]+
" has wrong format ("+fColTypes[ii]+
" vs "+mapIt->second->format()+
" in file)");
339 Error(
"Column "+fColNames[ii]+
" only exist in requested description");
344 for (map<string,CCfits::Column*>::const_iterator cMapIt = cMap.begin();
345 cMapIt != cMap.end(); cMapIt++)
347 if (!cMapIt->second->isRead())
349 Error(
"Reading column '"+cMapIt->first+
"' back from '"+fFile->name()+
"' failed.");
362 str <<
"FitsFile::OpenNewTable failed - more than " << maxtry <<
" tables tried." << endl;
370 if (!fFile || !fTable)
372 Error(
"FitsFile::AddRow - No table open.");
378 fits_insert_rows(fFile->fitsPointer(), fNumRows, 1, &status);
385 fits_get_errstatus(status, text);
388 str <<
"Inserting row " << fNumRows <<
" failed in '"+fFile->name()+
"': " << text <<
" (fits_insert_rows,rc=" << status <<
")";
402 if (!fFile || !fTable)
404 Error(
"FitsFile::AddRow - No table open.");
409 fits_write_tblbytes(fFile->fitsPointer(), fNumRows,
start,
size,
410 (
unsigned char*)ptr, &status);
417 fits_get_errstatus(status, text);
420 str <<
"Writing row " << fNumRows <<
" failed in '"+fFile->name()+
"': " << text <<
" (file_write_tblbytes,rc=" << status <<
")";
435 const string name = fFile->name();
451 fits_flush_file(fFile->fitsPointer(), &status);
456 fits_get_errstatus(status, text);
459 str <<
"Flushing file " << fFile->name() <<
" failed: " << text <<
" (fits_flush_file, rc=" << status <<
")";
467 for (vector<string>::const_iterator it=fColTypes.begin();
468 it!=fColTypes.end(); it++)
473 try { n = stoi(*it, &
id); }
474 catch (
const exception&) { }
482 case 'A': size += n*1;
break;
483 case 'B': size += n*1;
break;
484 case 'I': size += n*2;
break;
485 case 'J': size += n*4;
break;
486 case 'K': size += n*8;
break;
487 case 'E': size += n*4;
break;
488 case 'D': size += n*8;
break;
490 throw runtime_error(
"FitsFile::GetDataSize - id not known.");
size_t GetDataSize() const
Adds some functionality to boost::posix_time::ptime for our needs.
uint32_t NightAsInt() const
bool SetFile(CCfits::FITS *file=0)
bool WriteDefaultKeys(const string &prgname, float version=1.0)
bool OpenFile(const string &filename, bool allow_open=false)
bool OpenTable(const string &tablename)
void AddColumn(char type, const string &name, int numElems=1, const string &unit="")
bool WriteData(size_t &start, const void *ptr, size_t size)
bool OpenNewTable(const string &tableName, int maxtry=1)