From b8a1e552bbd0801a8d7175978aea38f4678066f1 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 12 Jan 2018 16:14:03 +0100 Subject: [PATCH] Use GCTAObservation::off_regions() for CUSTOM method --- cscripts/csphagen.py | 168 +++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------- 1 file changed, 31 insertions(+), 137 deletions(-) diff --git a/cscripts/csphagen.py b/cscripts/csphagen.py index 195ce25..803da60 100755 --- a/cscripts/csphagen.py +++ b/cscripts/csphagen.py @@ -50,7 +50,7 @@ class csphagen(ctools.csobservation): self._has_exclusion = False self._srcshape = '' self._rad = 0.0 - self._bkg_reg_files = {} # dictionary: bkg region file per observation {obsid:filepath, ..} + #self._bkg_reg_files = {} # dictionary: bkg region file per observation {obsid:filepath, ..} # Return return @@ -103,15 +103,15 @@ class csphagen(ctools.csobservation): Raises ------ RuntimeError - Multiple On-Regions in on region file or missing Off-Regions files. + Only one On region is allowed """ # Set up source region - src_reg_file = self['srcregfile'].filename() - self._src_reg = gammalib.GSkyRegions(src_reg_file) + filename = self['srcregfile'].filename() + self._src_reg = gammalib.GSkyRegions(filename) # Raise an exception if there is more than one source region if len(self._src_reg) != 1: - raise RuntimeError('Only 1 On region is allowed.') + raise RuntimeError('Only one On region is allowed') # Set up source direction. Query parameters if neccessary. if isinstance(self._src_reg[0], gammalib.GSkyRegionCircle): @@ -120,51 +120,15 @@ class csphagen(ctools.csobservation): else: self._query_src_direction() - # Try opening file as XML file. If this files we have probably a FITS - # events file on input, hence we query later one background region - # file - try: - # Open observation definition XML file - obs_file = self['inobs'].filename() - xml = gammalib.GXml(obs_file) - - # Go through elements in - for elem in xml[0]: - - # Get observation id from header - obs_id = elem.attribute('id') - - # Get region file path from body - off_regions_file = '' - for entry in elem: - - # contains region file - if entry.attribute('name') == 'OffRegions': - off_regions_file = entry.attribute('file') - break - - # Store file paths - self._bkg_reg_files[obs_id] = gammalib.GFilename(off_regions_file) - - # Not existing background region files are set to None (for every obs) - if not self._bkg_reg_files[obs_id].exists(): - self._bkg_reg_files[obs_id] = None - - # XML file opening failed - except: - self._bkg_reg_files = {0: None} - - # Query missing background region file - if len([True for x in self._bkg_reg_files if self._bkg_reg_files[x] is None]) > 0: - if len(self._bkg_reg_files) > 1: - # Querying of multiple regions not supported - raise RuntimeError('There are missing bkg region files in the ' - 'observation definition XML file. Please ' - 'specify all background region files.') - else: - # Query bkg region file for single observation - self._bkg_reg_files[self._bkg_reg_files.keys()[0]] = \ - self['bkgregfile'].filename() + # Make sure that all CTA observations have an Off region by loading the + # Off region region the parameter 'bkgregfile' for all CTA observations + # without Off region + for obs in self.obs(): + if obs.instrument() == 'CTA': + if obs.off_regions().is_empty(): + filename = self['bkgregfile'].filename() + regions = gammalib.GSkyRegions(filename) + obs.off_regions(regions) # Return return @@ -202,9 +166,6 @@ class csphagen(ctools.csobservation): # Initialize empty src regions container self._src_reg = gammalib.GSkyRegions() - # Get background method parameters - self._get_parameters_bkgmethod() - # Exclusion map if self['inexclusion'].filename().is_fits(): self._excl_reg = gammalib.GSkyRegionMap(self['inexclusion'].filename()) @@ -225,6 +186,10 @@ class csphagen(ctools.csobservation): if self.obs().is_empty(): self.obs(self._get_observations(False)) + # Get background method parameters (have to come after setting up of + # observations) + self._get_parameters_bkgmethod() + # Write input parameters into logger self._log_parameters(gammalib.TERSE) @@ -295,77 +260,6 @@ class csphagen(ctools.csobservation): # Return reflected regions return regions - def _regions_from_file(self, obs, file_path): - """ - Read list of reflected regions for a single observation (pointing) - from file. - - Perform basic tests for circle shaped regions. - - Parameters - ---------- - obs : `~gammalib.GCTAObservation()` - CTA observation - file_path: `~gammalib.GFilename()` - File path to file containing off regions. - - Returns - ------- - regions : `~gammalib.GSkyRegions` - List of reflected regions - """ - # Load regions from ds9 file of FITS WCS map - regions = gammalib.GSkyRegions( file_path ) - - # Do region tests only for circle shaped on-region, simply return list - # for other shapes. - on_region = self._src_reg[0] - if isinstance(on_region, gammalib.GSkyRegionCircle): - - # Prepare parameters for comparison - pnt_dir = obs.pointing().dir() - offset_on = pnt_dir.dist_deg(self._src_dir) - radius_on = on_region.radius() - - # Do some tests - for region in regions: - - # Do region tests only for circle shaped off-regions - if isinstance(region, gammalib.GSkyRegionCircle): - - # Gather parameters - offset_off = pnt_dir.dist_deg(region.centre()) - radius_off = region.radius() - - # Warning: different radius - if radius_off != radius_on: - self._log_string(gammalib.EXPLICIT, 'Off region radius ' - 'differs from On region radius: %.2f ' - 'vs %.2f' % ((radius_off, radius_on))) - - # Warning: offset is too small or too large - if offset_off <= radius_on or offset_off >= self['maxoffset'].real(): - self._log_string(gammalib.EXPLICIT, 'Region offset ' - 'invalid for observation %s ' - '(%.3f deg) ' % ((obs.id(), offset_off))) - - # Warning: offset of off and on region differ - if offset_off != offset_on: - self._log_string(gammalib.EXPLICIT, 'Off region offset ' - 'differs %.3f deg from On region ' - 'offset' % (offset_off-offset_on)) - - # end: off-regions test for circle shaped regions - # end: for loop iterating over off regions - # end: do region tests only if on-region is circle shaped - - # Inform user how many regions were loaded - self._log_string(gammalib.NORMAL, 'Read %i regions from file %s' % \ - (regions.size(), file_path.url())) - - # return list of regions - return regions - def _set_models(self, obs): """ Set models in observation container @@ -456,19 +350,12 @@ class csphagen(ctools.csobservation): if self['bkgmethod'].string() == 'REFLECTED': bkg_reg = self._reflected_regions(obs) - # ... otherwise if custom background is requested the get the background - # regions from a region file + # ... otherwise if custom background is requested then get the + # background regions from the observation. We use a copy here since + # otherwise the background regions go out of scope once the observations + # are replaced by the On/Off observations. elif self['bkgmethod'].string() == 'CUSTOM': - - # Get file path of background region file from dictionary - if len(self._bkg_reg_files) > 1: - bkg_reg_file = self._bkg_reg_files[obs.id()] - else: - # for inobs==event list no obs id is set. - bkg_reg_file = self._bkg_reg_files[self._bkg_reg_files.keys()[0]] - - # Load background regions from region file for current observation - bkg_reg = self._regions_from_file(obs, bkg_reg_file) + bkg_reg = obs.off_regions().copy() # Return background regions return bkg_reg @@ -512,8 +399,15 @@ class csphagen(ctools.csobservation): # Loop through observations and generate pha, arf, rmf files for obs in self.obs(): + # Skip non CTA observations + if obs.instrument() != 'CTA': + self._log_string(gammalib.NORMAL, 'Skip %s observation "%s"' % \ + (obs.instrument(), obs.id())) + continue + # Log current observation - self._log_string(gammalib.NORMAL, 'Process Observation %s' % (obs.id())) + self._log_string(gammalib.NORMAL, 'Process observation "%s"' % \ + (obs.id())) # Set background regions for this observation bkg_reg = self._set_background_regions(obs) -- libgit2 0.21.4