Commit ab0848b3f103428af8dea58a6265c8e1afa66a8d

Authored by Luigi Tibaldo
1 parent 25495305

implemented stacking

cscripts/csphagen.py
@@ -184,6 +184,7 @@ class csphagen(ctools.cscript): @@ -184,6 +184,7 @@ class csphagen(ctools.cscript):
184 self._log_header1(gammalib.NORMAL, 184 self._log_header1(gammalib.NORMAL,
185 'Generation of source and background spectra') 185 'Generation of source and background spectra')
186 # Loop through observations and generate pha, arf, rmf files 186 # Loop through observations and generate pha, arf, rmf files
  187 + # Save ON/OFF regions in ds9 reg files
187 outobs = gammalib.GObservations() 188 outobs = gammalib.GObservations()
188 etrue = self._ebounds 189 etrue = self._ebounds
189 ereco = self._ebounds 190 ereco = self._ebounds
@@ -195,9 +196,14 @@ class csphagen(ctools.cscript): @@ -195,9 +196,14 @@ class csphagen(ctools.cscript):
195 bkg_reg.append(region) 196 bkg_reg.append(region)
196 if bkg_reg.size() >= 1 or ( 197 if bkg_reg.size() >= 1 or (
197 self._bkgmethod == "REFLECTED" and bkg_reg.size() >= self._bkgregmin): 198 self._bkgmethod == "REFLECTED" and bkg_reg.size() >= self._bkgregmin):
198 - onoff = gammalib.GCTAOnOffObservation(obs, self._src_dir, etrue, ereco,self._src_reg, bkg_reg) 199 + onoff = gammalib.GCTAOnOffObservation(obs, self._src_dir, etrue,
  200 + ereco, self._src_reg,
  201 + bkg_reg)
199 onoff.id(obs.id()) 202 onoff.id(obs.id())
200 outobs.append(onoff) 203 outobs.append(onoff)
  204 + self._src_reg.save(
  205 + self._outroot + '_{}_on.reg'.format(obs.id()))
  206 + bkg_reg.save(self._outroot + '_{}_off.reg'.format(obs.id()))
201 else: 207 else:
202 if self._logNormal(): 208 if self._logNormal():
203 msg = 'Observation {} not included in spectra generation\n'.format( 209 msg = 'Observation {} not included in spectra generation\n'.format(
@@ -205,8 +211,8 @@ class csphagen(ctools.cscript): @@ -205,8 +211,8 @@ class csphagen(ctools.cscript):
205 self._log(msg) 211 self._log(msg)
206 212
207 # Save PHA, ARF and RMFs 213 # Save PHA, ARF and RMFs
208 - for obs in outobs:  
209 - if outobs.size() < 2 or self._stack == False: 214 + if outobs.size() < 2 or self._stack == False:
  215 + for obs in outobs:
210 obs.on_spec().save( 216 obs.on_spec().save(
211 self._outroot + '_{}_pha_on.fits'.format(obs.id()), 217 self._outroot + '_{}_pha_on.fits'.format(obs.id()),
212 True) 218 True)
@@ -217,26 +223,24 @@ class csphagen(ctools.cscript): @@ -217,26 +223,24 @@ class csphagen(ctools.cscript):
217 True) 223 True)
218 obs.rmf().save(self._outroot + '_{}_rmf.fits'.format(obs.id()), 224 obs.rmf().save(self._outroot + '_{}_rmf.fits'.format(obs.id()),
219 True) 225 True)
220 - obs.on_regions().save(self._outroot + '_{}_on.reg'.format(obs.id()))  
221 - obs.off_regions().save(  
222 - self._outroot + '_{}_off.reg'.format(obs.id()))  
223 -  
224 - if outobs.size() > 1 and self._stack == True: 226 + else:
225 if self._logVerbose(): 227 if self._logVerbose():
226 msg = 'Stacking {} observations\n'.format( 228 msg = 'Stacking {} observations\n'.format(
227 outobs.size()) 229 outobs.size())
228 self._log(msg) 230 self._log(msg)
229 - outobs = gammalib.GCTAOnOffObservation(outobs)  
230 - outobs.on_spec().save( 231 + stackobs = gammalib.GCTAOnOffObservation(outobs)
  232 + stackobs.on_spec().save(
231 self._outroot + '_stacked_pha_on.fits', 233 self._outroot + '_stacked_pha_on.fits',
232 True) 234 True)
233 - outobs.off_spec().save( 235 + stackobs.off_spec().save(
234 self._outroot + '_stacked_pha_off.fits', 236 self._outroot + '_stacked_pha_off.fits',
235 True) 237 True)
236 - outobs.arf().save(self._outroot + '_stacked_arf.fits',  
237 - True)  
238 - outobs.rmf().save(self._outroot + '_stacked_rmf.fits',  
239 - True) 238 + stackobs.arf().save(self._outroot + '_stacked_arf.fits',
  239 + True)
  240 + stackobs.rmf().save(self._outroot + '_stacked_rmf.fits',
  241 + True)
  242 + outobs = gammalib.GObservations()
  243 + outobs.append(stackobs)
240 244
241 # Save On/Off observations 245 # Save On/Off observations
242 outname = self._outroot + '.xml' 246 outname = self._outroot + '.xml'
test/test_csphagen.py
@@ -198,7 +198,7 @@ class Test(test): @@ -198,7 +198,7 @@ class Test(test):
198 # Run script 198 # Run script
199 phagen.execute() 199 phagen.execute()
200 200
201 - # Check outout 201 + # Check output
202 for s in range(2): 202 for s in range(2):
203 self._check_output('phagen_py3_' + str(s + 1), nbins, 203 self._check_output('phagen_py3_' + str(s + 1), nbins,
204 self._nreg_mul[s]) 204 self._nreg_mul[s])
@@ -225,10 +225,10 @@ class Test(test): @@ -225,10 +225,10 @@ class Test(test):
225 # Run script 225 # Run script
226 phagen.execute() 226 phagen.execute()
227 227
228 - # Check outout 228 + # Check output
229 for s in range(2): 229 for s in range(2):
230 self._check_output('phagen_py4_stacked', nbins, 230 self._check_output('phagen_py4_stacked', nbins,
231 - 0,check_regions=False) 231 + 0, check_regions=False)
232 self._check_outobs('phagen_py4', 1) 232 self._check_outobs('phagen_py4', 1)
233 233
234 return 234 return
@@ -240,10 +240,10 @@ class Test(test): @@ -240,10 +240,10 @@ class Test(test):
240 cols = ['E_MIN', 'E_MAX'] 240 cols = ['E_MIN', 'E_MAX']
241 241
242 self.test_value(table.ncols(), len(cols), 242 self.test_value(table.ncols(), len(cols),
243 - 'Check for %d columns in light curve FITS table' % len( 243 + 'Check for %d columns in energy bounds table' % len(
244 cols)) 244 cols))
245 self.test_value(table.nrows(), bins, 245 self.test_value(table.nrows(), bins,
246 - 'Check for %d rows in light curve FITS table' % bins) 246 + 'Check for %d rows in energy bounds table' % bins)
247 for col in cols: 247 for col in cols:
248 self.test_assert(table.contains(col), 248 self.test_assert(table.contains(col),
249 'FITS file contains "' + col + '" column') 249 'FITS file contains "' + col + '" column')
@@ -263,7 +263,7 @@ class Test(test): @@ -263,7 +263,7 @@ class Test(test):
263 263
264 # Check FITS file structure 264 # Check FITS file structure
265 self.test_value(fits.size(), 3, 265 self.test_value(fits.size(), 3,
266 - 'Check for 3 extensions in light curve FITS file') 266 + 'Check for 3 extensions in PHA file')
267 self.test_assert(fits.contains('SPECTRUM'), 267 self.test_assert(fits.contains('SPECTRUM'),
268 'FITS file contains "SPECTRUM" extension') 268 'FITS file contains "SPECTRUM" extension')
269 self.test_assert(fits.contains('EBOUNDS'), 269 self.test_assert(fits.contains('EBOUNDS'),
@@ -274,10 +274,10 @@ class Test(test): @@ -274,10 +274,10 @@ class Test(test):
274 274
275 # Check FITS table structure 275 # Check FITS table structure
276 self.test_value(table.ncols(), len(cols), 276 self.test_value(table.ncols(), len(cols),
277 - 'Check for %d columns in light curve FITS table' % len( 277 + 'Check for %d columns in PHA table' % len(
278 cols)) 278 cols))
279 self.test_value(table.nrows(), bins, 279 self.test_value(table.nrows(), bins,
280 - 'Check for %d rows in light curve FITS table' % bins) 280 + 'Check for %d rows in PHA table' % bins)
281 for col in cols: 281 for col in cols:
282 self.test_assert(table.contains(col), 282 self.test_assert(table.contains(col),
283 'FITS file contains "' + col + '" column') 283 'FITS file contains "' + col + '" column')
@@ -297,14 +297,14 @@ class Test(test): @@ -297,14 +297,14 @@ class Test(test):
297 Check ARF file 297 Check ARF file
298 """ 298 """
299 # Expected column names 299 # Expected column names
300 - cols = ['ENERG_LO', 'ENERG_HI', 'SPECRESP', 'BACKGROUND', 'ALPHA'] 300 + cols = ['ENERG_LO', 'ENERG_HI', 'SPECRESP', 'BACKRESP']
301 301
302 # Open FITS file 302 # Open FITS file
303 fits = gammalib.GFits(filename) 303 fits = gammalib.GFits(filename)
304 304
305 # Check FITS file structure 305 # Check FITS file structure
306 self.test_value(fits.size(), 2, 306 self.test_value(fits.size(), 2,
307 - 'Check for 2 extensions in light curve FITS file') 307 + 'Check for 2 extensions in ARF file')
308 self.test_assert(fits.contains('SPECRESP'), 308 self.test_assert(fits.contains('SPECRESP'),
309 'FITS file contains "SPECRESP" extension') 309 'FITS file contains "SPECRESP" extension')
310 310
@@ -313,10 +313,10 @@ class Test(test): @@ -313,10 +313,10 @@ class Test(test):
313 313
314 # Check FITS table structure 314 # Check FITS table structure
315 self.test_value(table.ncols(), len(cols), 315 self.test_value(table.ncols(), len(cols),
316 - 'Check for %d columns in light curve FITS table' % len( 316 + 'Check for %d columns in ARF table' % len(
317 cols)) 317 cols))
318 self.test_value(table.nrows(), bins, 318 self.test_value(table.nrows(), bins,
319 - 'Check for %d rows in light curve FITS table' % bins) 319 + 'Check for %d rows in ARF table' % bins)
320 for col in cols: 320 for col in cols:
321 self.test_assert(table.contains(col), 321 self.test_assert(table.contains(col),
322 'FITS file contains "' + col + '" column') 322 'FITS file contains "' + col + '" column')
@@ -339,7 +339,7 @@ class Test(test): @@ -339,7 +339,7 @@ class Test(test):
339 339
340 # Check FITS file structure 340 # Check FITS file structure
341 self.test_value(fits.size(), 3, 341 self.test_value(fits.size(), 3,
342 - 'Check for 3 extensions in light curve FITS file') 342 + 'Check for 3 extensions in RMF file')
343 self.test_assert(fits.contains('MATRIX'), 343 self.test_assert(fits.contains('MATRIX'),
344 'FITS file contains "MATRIX" extension') 344 'FITS file contains "MATRIX" extension')
345 self.test_assert(fits.contains('EBOUNDS'), 345 self.test_assert(fits.contains('EBOUNDS'),
@@ -350,10 +350,10 @@ class Test(test): @@ -350,10 +350,10 @@ class Test(test):
350 350
351 # Check FITS table structure 351 # Check FITS table structure
352 self.test_value(table.ncols(), len(cols), 352 self.test_value(table.ncols(), len(cols),
353 - 'Check for %d columns in light curve FITS table' % len( 353 + 'Check for %d columns in RMF table' % len(
354 cols)) 354 cols))
355 self.test_value(table.nrows(), bins, 355 self.test_value(table.nrows(), bins,
356 - 'Check for %d rows in light curve FITS table' % bins) 356 + 'Check for %d rows in RMF table' % bins)
357 for col in cols: 357 for col in cols:
358 self.test_assert(table.contains(col), 358 self.test_assert(table.contains(col),
359 'FITS file contains "' + col + '" column') 359 'FITS file contains "' + col + '" column')
@@ -368,7 +368,7 @@ class Test(test): @@ -368,7 +368,7 @@ class Test(test):
368 # Return 368 # Return
369 return 369 return
370 370
371 - def _check_output(self, filenameroot, bins, nreg, check_regions = True): 371 + def _check_output(self, filenameroot, bins, nreg, check_regions=True):
372 """ 372 """
373 Check the output from a csphagen run 373 Check the output from a csphagen run
374 """ 374 """