32 #include <boost/bind.hpp> 33 #if BOOST_VERSION < 104400 34 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4)) 35 #undef BOOST_HAS_RVALUE_REFS 38 #include <boost/thread.hpp> 42 #include <CCfits/CCfits> 108 void writeValuesFromFits(vector<int>& offsets,ofstream& targetFile,
unsigned char* fitsBuffer);
136 str <<
"Output file is now " <<
fFileOut;
167 map<string, CCfits::Column*>::iterator it;
173 if (it->first == *jt)
181 int offset = offsets[it->second->index()-1];
182 const char* charSrc =
reinterpret_cast<char*
>(&fitsBuffer[offset]);
183 unsigned char copyBuffer[30];
184 for (
int width = 0; width<it->second->width(); width++)
186 switch (it->second->type())
189 targetFile << *charSrc;
190 charSrc +=
sizeof(char);
192 case CCfits::Tushort:
193 targetFile << *reinterpret_cast<const unsigned short*>(charSrc);
194 charSrc +=
sizeof(char);
197 targetFile << *reinterpret_cast<const short*>(charSrc);
198 charSrc +=
sizeof(char);
201 reverse_copy(charSrc, charSrc+
sizeof(
unsigned int), copyBuffer);
203 targetFile << *reinterpret_cast<unsigned int*>(copyBuffer);
204 charSrc +=
sizeof(int);
207 reverse_copy(charSrc, charSrc+
sizeof(
int), copyBuffer);
208 targetFile << *reinterpret_cast<int*>(copyBuffer);
209 charSrc +=
sizeof(int);
212 reverse_copy(charSrc, charSrc+
sizeof(
unsigned long), copyBuffer);
213 targetFile << *reinterpret_cast<unsigned long*>(copyBuffer);
214 charSrc +=
sizeof(int);
217 reverse_copy(charSrc, charSrc+
sizeof(
long), copyBuffer);
218 targetFile << *reinterpret_cast<long*>(copyBuffer);
219 charSrc +=
sizeof(int);
221 case CCfits::Tlonglong:
222 reverse_copy(charSrc, charSrc+
sizeof(
long long), copyBuffer);
223 targetFile << *reinterpret_cast<long long*>(copyBuffer);
224 charSrc +=
sizeof(
long long);
227 reverse_copy(charSrc, charSrc+
sizeof(
float), copyBuffer);
228 targetFile << *reinterpret_cast<float*>(copyBuffer);
229 charSrc +=
sizeof(float);
231 case CCfits::Tdouble:
232 reverse_copy(charSrc, charSrc+
sizeof(
double), copyBuffer);
233 targetFile << *reinterpret_cast<double*>(copyBuffer);
234 charSrc +=
sizeof(double);
238 case CCfits::Tlogical:
239 case CCfits::Tstring:
240 case CCfits::Tcomplex:
241 case CCfits::Tdblcomplex:
244 case CCfits::VTlogical:
245 case CCfits::VTushort:
246 case CCfits::VTshort:
249 case CCfits::VTulong:
251 case CCfits::VTlonglong:
252 case CCfits::VTfloat:
253 case CCfits::VTdouble:
254 case CCfits::VTcomplex:
255 case CCfits::VTdblcomplex:
256 Error(
"Data type not implemented yet.");
260 Error(
"THIS SHOULD NEVER BE REACHED");
280 for (map<string, CCfits::Column*>::iterator it=
fColMap.begin(); it !=
fColMap.end(); it++)
282 int width = it->second->width();
283 switch (it->second->type())
286 case CCfits::Tushort:
289 sizes[it->second->index()] =
sizeof(char)*width;
294 sizes[it->second->index()] =
sizeof(int)*width;
299 sizes[it->second->index()] =
sizeof(int)*width;
301 case CCfits::Tlonglong:
303 sizes[it->second->index()] =
sizeof(
long long)*width;
307 sizes[it->second->index()] =
sizeof(float)*width;
309 case CCfits::Tdouble:
311 sizes[it->second->index()] =
sizeof(double)*width;
315 case CCfits::Tlogical:
316 case CCfits::Tstring:
317 case CCfits::Tcomplex:
318 case CCfits::Tdblcomplex:
321 case CCfits::VTlogical:
322 case CCfits::VTushort:
323 case CCfits::VTshort:
326 case CCfits::VTulong:
328 case CCfits::VTlonglong:
329 case CCfits::VTfloat:
330 case CCfits::VTdouble:
331 case CCfits::VTcomplex:
332 case CCfits::VTdblcomplex:
333 Error(
"Data type not implemented yet.");
334 return vector<int>();
337 Error(
"THIS SHOULD NEVER BE REACHED");
338 return vector<int>();
343 for (map<int,int>::iterator it=sizes.begin(); it != sizes.end(); it++)
345 result.push_back(size);
347 if (it->first != checkIndex)
350 str <<
"Expected index " << checkIndex <<
" found " << it->first;
355 result.push_back(size);
372 (
"Loads the given Fits file");
375 (
"Unloads the given Fits file");
380 (
"List the columns that were loaded from that file");
383 (
"Add a given column to the dumping list");
386 (
"Clear the dumping list");
389 (
"Perform the dump of columns data, based on the to dump list");
392 (
"Gives the name of the Fits file to be loaded");
395 (
"Gives the name of the Table to be loaded");
398 (
"Set the precision of the ofstream, i.e. the number of significant digits being outputted");
401 (
"Set the name of the outputted file.");
428 catch (CCfits::FitsException e)
430 str <<
"Could not open FITS file " <<
fFileName <<
" reason: " << e.message();
435 const multimap< string, CCfits::ExtHDU * > extMap =
fFile->extension();
439 str <<
"Could not open table " <<
fTableName <<
". Tables in file are: ";
440 for (std::multimap<string, CCfits::ExtHDU*>::const_iterator it=extMap.begin(); it != extMap.end(); it++)
441 str << it->first <<
" ";
449 str <<
"Loaded table has " << numRows <<
" rows";
455 bool should_clear =
false;
461 Error(
"Config-given dump list contains invalid entry " + *it +
" clearing the list");
478 Error(
"Error: Fits file is NULL while it should not have been");
488 Message(
"Columns in the loaded table are:");
489 map<string, CCfits::Column*>::iterator it;
507 Error(
"Could not find column " + evtText +
" int table");
520 Message(
"Dump list is now empty");
529 fTable->makeThisCurrent();
531 int size = offsets[offsets.size()-1];
533 unsigned char* fitsBuffer =
new unsigned char[
size];
540 fits_read_tblbytes(
fFile->fitsPointer(),
i, 1,
size, fitsBuffer, &status);
544 str <<
"An error occurred while reading fits row #" <<
i <<
" error code: " << status;
547 for (
unsigned int j=0;j<offsets.size(); j++)
548 str << offsets[j] <<
" ";
588 if (conf.
Has(
"outfile"))
593 if (conf.
Has(
"fitsfile"))
598 if (conf.
Has(
"tablename"))
603 if (conf.
Has(
"dump"))
610 if (conf.
Has(
"precision"))
627 static T shell(conf.
GetName().c_str(), conf.
Get<
int>(
"console")!=1);
633 shell.SetReceiver(loader);
635 boost::thread
t(boost::bind(
RunThread, &loader));
647 cout <<
"This is a usage. to be completed" << endl;
651 cout <<
"This is the help. I know, not so helpfull at the moment..." << endl;
655 po::options_description configp(
"Programm options");
656 configp.add_options()
657 (
"console,c", var<int>(),
"Use console (0=shell, 1=simple buffered, X=simple unbuffered)");
659 po::options_description configs(
"Fits Loader options");
660 configs.add_options()
661 (
"outfile,o", var<string>(),
"Output file")
662 (
"fitsfile,f", var<string>(),
"Input Fits file")
663 (
"tablename,t", var<string>(),
"Input Table")
664 (
"dump,d", vars<string>(),
"List of columns to dump")
665 (
"precision,p", var<int>(),
"Precision of ofstream")
672 int main(
int argc,
const char** argv)
683 if (conf.
Get<
int>(
"console")==0)
684 return RunShell<LocalShell>(conf);
686 return RunShell<LocalConsole>(conf);
map< string, CCfits::Column * > fColMap
map between the column names and their CCfits objects
int LoadPlease()
Transition from ready to fileLoaded.
EventImp & AddEvent(const std::string &name, const std::string &states, const std::string &fmt)
virtual void Stop(int code=0)
Request to stop the mainloop.
static const char * fLoadFits
Define command names.
int SetOFStreamPrecisionPlease(const Event &)
Set the ofstream precision.
int main(int argc, const char **argv)
Mainloop running, state machine in operation.
int GetCurrentState() const
return the current state of the machine
const char * GetText() const
void writeValuesFromFits(vector< int > &offsets, ofstream &targetFile, unsigned char *fitsBuffer)
Write a single row of the selected data.
int UnloadPlease()
Transition from fileLoaded to ready.
int DoDumpPlease(const Event &)
Perform the dumping, based on the current dump list.
static const char * fListColumns
static const char * fConfigTableName
A C++ ostream to an ncurses window supporting attributes and colors.
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
static const char * fDumpColumns
static const char * fConfigFileName
void RunThread(FitsLoader *loader)
string fFileOut
Name of the output file.
static const char * fDoDump
enum FitsLoader::@19 localstates_t
int SetFileOutPlease(const Event &)
Set the name of the output file.
bool Has(const std::string &var)
void AddOptions(const po::options_description &opt, bool visible=true)
static const char * fUnloadFits
int ClearDumpListPlease(const Event &)
Clear the dump list.
CCfits::FITS * fFile
FITS pointer.
int Error(const std::string &str)
vector< string > fDumpList
List of the column names to be dumped.
int ListColumnsPlease(const Event &)
Lists the loaded column names.
Commandline parsing, resource file parsing and database access.
Load a given Fits file and table, and dump selected columns if requested.
int AddDumpColumnsPlease(const Event &)
Add a column name to the dump list.
int ConfigFileNamePlease(const Event &)
Set the name of the Fits file to be loaded.
void SetupConfiguration(Configuration &conf)
string fFileName
Name of the fits file to load.
Class for a state machine implementation within a DIM network.
Concerete implementation of an EventImp stroring name, format, data and time.
int fStreamPrecision
Precision of the ofstream. Used to output a given number of significant digits for floats or doubles...
CCfits::Table * fTable
Table pointer.
string fTableName
Name of the table to load from the file.
virtual int Run(bool dummy)
Start the mainloop.
int RunShell(Configuration &conf)
static const char * fConfigFileOutName
int Message(const std::string &str)
int ConfigTableNamePlease(const Event &)
Set the name of the table to be loaded.
void SetupConfig(Configuration &conf)
Configures the fitsLoader from the config file and/or command arguments.
static const char * fConfigPrecName
vector< int > CalculateBufferSize()
Calculate the buffer size required to read a row of the fits table, as well as the offsets to each co...
static const char * fClearDumpList
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
bool AddStateName(const int state, const std::string &name, const std::string &doc="")
const std::string & GetName() const