Commit ab0848b3f103428af8dea58a6265c8e1afa66a8d

Authored by Luigi Tibaldo
1 parent 25495305

implemented stacking

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