Commit 653cef59ae1556adb056ebba69f5b94e76fb694b

Authored by knodlseder
1 parent e60b9633

first functional version

pyext/ctselect.i
@@ -38,6 +38,8 @@ public: @@ -38,6 +38,8 @@ public:
38 void run(void); 38 void run(void);
39 void get_parameters(void); 39 void get_parameters(void);
40 void select(void); 40 void select(void);
  41 + void append_gti(void);
  42 + void copy(void);
41 }; 43 };
42 44
43 45
src/ctselect/ctselect.cpp
@@ -22,11 +22,17 @@ @@ -22,11 +22,17 @@
22 #endif 22 #endif
23 #include <stdio.h> 23 #include <stdio.h>
24 #include "ctselect.hpp" 24 #include "ctselect.hpp"
  25 +#include "GTools.hpp"
25 26
26 /* __ Debug definitions __________________________________________________ */ 27 /* __ Debug definitions __________________________________________________ */
27 #define BIT_COLUMN_TEST 0 28 #define BIT_COLUMN_TEST 0
28 #define STRING_COLUMN_TEST 1 29 #define STRING_COLUMN_TEST 1
29 30
  31 +/* __ Coding definitions _________________________________________________ */
  32 +#define SELECT_TIME 0
  33 +#define SELECT_ENERGY 1
  34 +#define SELECT_ROI 1
  35 +
30 36
31 /*========================================================================== 37 /*==========================================================================
32 = = 38 = =
@@ -97,6 +103,9 @@ ctselect::~ctselect(void) @@ -97,6 +103,9 @@ ctselect::~ctselect(void)
97 103
98 /***********************************************************************//** 104 /***********************************************************************//**
99 * @brief Select event data 105 * @brief Select event data
  106 + *
  107 + * A GTI is appended to the FITS file as in the actual data format no GTI
  108 + * exists.
100 ***************************************************************************/ 109 ***************************************************************************/
101 void ctselect::run(void) 110 void ctselect::run(void)
102 { 111 {
@@ -112,6 +121,13 @@ void ctselect::run(void) @@ -112,6 +121,13 @@ void ctselect::run(void)
112 // Select events 121 // Select events
113 select(); 122 select();
114 123
  124 + // Append GTI to FITS file
  125 + append_gti();
  126 +
  127 + // Write separator into logger
  128 + if (logTerse())
  129 + log << std::endl;
  130 +
115 // Return 131 // Return
116 return; 132 return;
117 } 133 }
@@ -144,30 +160,105 @@ void ctselect::get_parameters(void) @@ -144,30 +160,105 @@ void ctselect::get_parameters(void)
144 /***********************************************************************//** 160 /***********************************************************************//**
145 * @brief Select events 161 * @brief Select events
146 * 162 *
147 - * @todo An interesting alternative is the fits_select_rows routine which copies  
148 - * the selected rows from one FITS table to another. That's in fact the thing  
149 - * we want to achieve here. The only way to use this routine would be to  
150 - * open 2 FITS files, one for the input data and a fresh one for the output  
151 - * data. The events table would then be copied over using the select routine,  
152 - * any other tables would be copied over directly. The code would look like:  
153 - *  
154 - * GFits infile(m_infile);  
155 - * GFits outfile(m_outfile);  
156 - * for (int extno = 0; extno < infile.size(); ++extno) {  
157 - * GFitsHDU* inhdu = infile.hdu(i);  
158 - * if (inhdu->extname() == "EVENTS") {  
159 - * GFitsHDU* outhdu = inhdu->select("expression");  
160 - * outfile.append_hdu(*outhdu);  
161 - * }  
162 - * else  
163 - * outfile.append_hdu(*inhdu);  
164 - * }  
165 - * outfile.save();  
166 - * outfile.close(); 163 + * Select events from a FITS file by making use of the selection possibility
  164 + * of the cfitsio library on loading a file into memory. Event selection is
  165 + * kept optional (on compile time) as the data format may still evolve (for
  166 + * the time being the TIME information is not valid).
167 * 167 *
  168 + * @todo Set data selection keywords.
168 ***************************************************************************/ 169 ***************************************************************************/
169 void ctselect::select(void) 170 void ctselect::select(void)
170 { 171 {
  172 + // Write Header for event selection
  173 + if (logTerse()) {
  174 + log << std::endl;
  175 + log.header1("Event selection");
  176 + }
  177 +
  178 + // Build selection string
  179 + std::string selection;
  180 + #if SELECT_TIME
  181 + selection = "TIME >= "+str(m_tmin)+" && TIME <= "+str(m_tmax);
  182 + if (logTerse())
  183 + log << " Time range ................: " << m_tmin << "-" << m_tmax
  184 + << std::endl;
  185 + #endif
  186 + #if SELECT_ENERGY
  187 + if (selection.length() > 0)
  188 + selection += " && ";
  189 + selection += "ENERGY >= "+str(m_emin)+" && ENERGY <= "+str(m_emax);
  190 + if (logTerse())
  191 + log << " Energy range ..............: " << m_emin << "-" << m_emax
  192 + << " TeV" << std::endl;
  193 + #endif
  194 + #if SELECT_ROI
  195 + if (selection.length() > 0)
  196 + selection += " && ";
  197 + selection += "ANGSEP("+str(m_ra)+","+str(m_dec)+",RA,DEC) <= "+str(m_rad);
  198 + if (logTerse()) {
  199 + log << " Acceptance cone centre ....: RA=" << m_ra << ", DEC=" << m_dec
  200 + << " deg" << std::endl;
  201 + log << " Acceptance cone radius ....: " << m_rad << " deg" << std::endl;
  202 + }
  203 + #endif
  204 + if (logTerse())
  205 + log << " cfitsio selection .........: " << selection << std::endl;
  206 +
  207 + // Build input filename including selection expression
  208 + std::string expression = m_infile;
  209 + if (selection.length() > 0)
  210 + expression += "[EVENTS]["+selection+"]";
  211 + if (logTerse())
  212 + log << " FITS filename .............: " << expression << std::endl;
  213 +
  214 + // Open FITS file
  215 + GFits file(expression);
  216 +
  217 + // Save output FITS file
  218 + file.saveto(m_outfile, clobber());
  219 +
  220 + // Close FITS file
  221 + file.close();
  222 +
  223 + // Return
  224 + return;
  225 +}
  226 +
  227 +
  228 +/***********************************************************************//**
  229 + * @brief Append GTI to FITS file
  230 + ***************************************************************************/
  231 +void ctselect::append_gti(void)
  232 +{
  233 + // Allocate Gti
  234 + GGti gti;
  235 +
  236 + // Setup single GTI covering the selected time range
  237 + GTime tstart;
  238 + GTime tstop;
  239 + tstart.met(m_tmin);
  240 + tstop.met(m_tmax);
  241 + gti.add(tstart, tstop);
  242 +
  243 + // Save GTI
  244 + GFits file(m_outfile);
  245 + gti.write(&file);
  246 + file.save();
  247 + file.close();
  248 +
  249 + // Return
  250 + return;
  251 +}
  252 +
  253 +
  254 +/***********************************************************************//**
  255 + * @brief Copy events
  256 + *
  257 + * This is some old code that is not used but that is kept for the moment
  258 + * for debugging purposes.
  259 + ***************************************************************************/
  260 +void ctselect::copy(void)
  261 +{
171 // Open FITS file 262 // Open FITS file
172 GFits file(m_infile); 263 GFits file(m_infile);
173 //std::cout << file << std::endl; 264 //std::cout << file << std::endl;
src/ctselect/ctselect.hpp
@@ -43,6 +43,8 @@ public: @@ -43,6 +43,8 @@ public:
43 void run(void); 43 void run(void);
44 void get_parameters(void); 44 void get_parameters(void);
45 void select(void); 45 void select(void);
  46 + void append_gti(void);
  47 + void copy(void);
46 48
47 protected: 49 protected:
48 // Protected methods 50 // Protected methods