Commit 026012847831a07b9d0cf8cd216f8c3c31dc5b2a

Authored by knodlseder
1 parent c852d7b0

check input file and improve error message

src/ctselect/ctselect.cpp
@@ -32,6 +32,9 @@ @@ -32,6 +32,9 @@
32 #include "ctselect.hpp" 32 #include "ctselect.hpp"
33 #include "GTools.hpp" 33 #include "GTools.hpp"
34 34
  35 +/* __ Method name definitions ____________________________________________ */
  36 +#define G_RUN "ctselect::run()"
  37 +
35 /* __ Debug definitions __________________________________________________ */ 38 /* __ Debug definitions __________________________________________________ */
36 39
37 /* __ Coding definitions _________________________________________________ */ 40 /* __ Coding definitions _________________________________________________ */
@@ -277,6 +280,12 @@ void ctselect::run(void) @@ -277,6 +280,12 @@ void ctselect::run(void)
277 // Save observation in temporary file 280 // Save observation in temporary file
278 obs->save(filename, true); 281 obs->save(filename, true);
279 282
  283 + // Check temporary file
  284 + std::string message = check_infile(filename);
  285 + if (message.length() > 0) {
  286 + throw GException::app_error(G_RUN, message);
  287 + }
  288 +
280 // Load observation from temporary file, including event selection 289 // Load observation from temporary file, including event selection
281 select_events(obs, filename); 290 select_events(obs, filename);
282 291
@@ -379,10 +388,13 @@ void ctselect::get_parameters(void) @@ -379,10 +388,13 @@ void ctselect::get_parameters(void)
379 /***********************************************************************//** 388 /***********************************************************************//**
380 * @brief Select events 389 * @brief Select events
381 * 390 *
  391 + * @param[in] obs CTA observations.
  392 + * @param[in] filename File name.
  393 + *
382 * Select events from a FITS file by making use of the selection possibility 394 * Select events from a FITS file by making use of the selection possibility
383 * of the cfitsio library on loading a file. 395 * of the cfitsio library on loading a file.
384 * 396 *
385 - * @todo Implement a observation read() method to avoid saving the 397 + * @todo Implement an observation read() method to avoid saving the
386 * observation to a temporary file. 398 * observation to a temporary file.
387 ***************************************************************************/ 399 ***************************************************************************/
388 void ctselect::select_events(GCTAObservation* obs, const std::string& filename) 400 void ctselect::select_events(GCTAObservation* obs, const std::string& filename)
@@ -546,3 +558,84 @@ void ctselect::free_members(void) @@ -546,3 +558,84 @@ void ctselect::free_members(void)
546 // Return 558 // Return
547 return; 559 return;
548 } 560 }
  561 +
  562 +
  563 +/***********************************************************************//**
  564 + * @brief Check input filename
  565 + *
  566 + * @param[in] filename File name.
  567 + *
  568 + * This method checks if the input FITS file is correct.
  569 + ***************************************************************************/
  570 +std::string ctselect::check_infile(const std::string& filename) const
  571 +{
  572 + // Initialise message string
  573 + std::string message = "";
  574 +
  575 + // Open FITS file
  576 + GFits file(filename);
  577 +
  578 + // Check for EVENTS HDU
  579 + GFitsTable* table = NULL;
  580 + try {
  581 +
  582 + // Get pointer to FITS table
  583 + table = file.table("EVENTS");
  584 +
  585 + // Initialise list of missing columns
  586 + std::vector<std::string> missing;
  587 +
  588 + // Check for existence of TIME column
  589 + try {
  590 + GFitsTableCol* col = table->column("TIME");
  591 + }
  592 + catch (GException::fits_column_not_found& e) {
  593 + missing.push_back("TIME");
  594 + }
  595 +
  596 + // Check for existence of ENERGY column
  597 + try {
  598 + GFitsTableCol* col = table->column("ENERGY");
  599 + }
  600 + catch (GException::fits_column_not_found& e) {
  601 + missing.push_back("ENERGY");
  602 + }
  603 +
  604 + // Check for existence of RA column
  605 + try {
  606 + GFitsTableCol* col = table->column("RA");
  607 + }
  608 + catch (GException::fits_column_not_found& e) {
  609 + missing.push_back("RA");
  610 + }
  611 +
  612 + // Check for existence of DEC column
  613 + try {
  614 + GFitsTableCol* col = table->column("DEC");
  615 + }
  616 + catch (GException::fits_column_not_found& e) {
  617 + missing.push_back("DEC");
  618 + }
  619 +
  620 + // Set error message for missing columns
  621 + if (missing.size() > 0) {
  622 + message = "The following columns are missing in the"
  623 + " \"EVENTS\" extension of input file \""
  624 + + m_outfile + "\": ";
  625 + for (int i = 0; i < missing.size(); ++i) {
  626 + message += "\"" + missing[i] + "\"";
  627 + if (i < missing.size()-1)
  628 + message += ", ";
  629 + }
  630 + }
  631 +
  632 + }
  633 + catch (GException::fits_hdu_not_found& e) {
  634 + message = "No \"EVENTS\" extension found in input file \""
  635 + + m_outfile + "\".";
  636 + }
  637 +
  638 + // Return
  639 + return message;
  640 +}
  641 +
src/ctselect/ctselect.hpp
@@ -64,9 +64,10 @@ public: @@ -64,9 +64,10 @@ public:
64 64
65 protected: 65 protected:
66 // Protected methods 66 // Protected methods
67 - void init_members(void);  
68 - void copy_members(const ctselect& app);  
69 - void free_members(void); 67 + void init_members(void);
  68 + void copy_members(const ctselect& app);
  69 + void free_members(void);
  70 + std::string check_infile(const std::string& filename) const;
70 71
71 // User parameters 72 // User parameters
72 std::string m_infile; //!< Input event list 73 std::string m_infile; //!< Input event list
src/ctselect/main.cpp
@@ -62,12 +62,12 @@ int main (int argc, char *argv[]) @@ -62,12 +62,12 @@ int main (int argc, char *argv[])
62 " ctselect. Run aborted ..."; 62 " ctselect. Run aborted ...";
63 63
64 // Write error in logger 64 // Write error in logger
65 - application.log << signal << std::endl;  
66 application.log << message << std::endl; 65 application.log << message << std::endl;
  66 + application.log << signal << std::endl;
67 67
68 // Write error on standard output 68 // Write error on standard output
69 - std::cout << signal << std::endl;  
70 std::cout << message << std::endl; 69 std::cout << message << std::endl;
  70 + std::cout << signal << std::endl;
71 71
72 } // endcatch: catched any application error 72 } // endcatch: catched any application error
73 73