Commit a410969178d8e05ffd7dab686e391bf50811f628
1 parent
29069d9d
Parameter srcdir is now always queried because from map regions srcdir is not ex…
…ctractable. Additional minor text changes.
Showing
1 changed file
with
52 additions
and
51 deletions
cscripts/csphagen.py
| @@ -67,22 +67,23 @@ class csphagen(ctools.csobservation): | @@ -67,22 +67,23 @@ class csphagen(ctools.csobservation): | ||
| 67 | # Set energy bounds | 67 | # Set energy bounds |
| 68 | self._ebounds = self._create_ebounds() | 68 | self._ebounds = self._create_ebounds() |
| 69 | 69 | ||
| 70 | + # Initialise source position/region querying relevant parameters | ||
| 71 | + self._src_dir = gammalib.GSkyDir() | ||
| 72 | + self._src_reg = gammalib.GSkyRegions() | ||
| 73 | + coordsys = self['coordsys'].string() | ||
| 74 | + if coordsys == 'CEL': | ||
| 75 | + ra = self['ra'].real() | ||
| 76 | + dec = self['dec'].real() | ||
| 77 | + self._src_dir.radec_deg(ra, dec) | ||
| 78 | + elif coordsys == 'GAL': | ||
| 79 | + glon = self['glon'].real() | ||
| 80 | + glat = self['glat'].real() | ||
| 81 | + self._src_dir.lb_deg(glon, glat) | ||
| 82 | + | ||
| 70 | # Query background estimation method and parameters | 83 | # Query background estimation method and parameters |
| 71 | bkgmethod = self['bkgmethod'].string() | 84 | bkgmethod = self['bkgmethod'].string() |
| 72 | if bkgmethod == 'REFLECTED': | 85 | if bkgmethod == 'REFLECTED': |
| 73 | 86 | ||
| 74 | - # Initialise source position/region querying relevant parameters | ||
| 75 | - self._src_dir = gammalib.GSkyDir() | ||
| 76 | - self._src_reg = gammalib.GSkyRegions() | ||
| 77 | - coordsys = self['coordsys'].string() | ||
| 78 | - if coordsys == 'CEL': | ||
| 79 | - ra = self['ra'].real() | ||
| 80 | - dec = self['dec'].real() | ||
| 81 | - self._src_dir.radec_deg(ra, dec) | ||
| 82 | - elif coordsys == 'GAL': | ||
| 83 | - glon = self['glon'].real() | ||
| 84 | - glat = self['glat'].real() | ||
| 85 | - self._src_dir.lb_deg(glon, glat) | ||
| 86 | self._srcshape = self['srcshape'].string() | 87 | self._srcshape = self['srcshape'].string() |
| 87 | if self._srcshape == 'CIRCLE': | 88 | if self._srcshape == 'CIRCLE': |
| 88 | self._rad = self['rad'].real() | 89 | self._rad = self['rad'].real() |
| @@ -116,7 +117,7 @@ class csphagen(ctools.csobservation): | @@ -116,7 +117,7 @@ class csphagen(ctools.csobservation): | ||
| 116 | if entry.attribute('name') == 'OffRegions': | 117 | if entry.attribute('name') == 'OffRegions': |
| 117 | offregionsfile = entry.attribute('file') | 118 | offregionsfile = entry.attribute('file') |
| 118 | 119 | ||
| 119 | - # <parameter name="OnRegion" file=".."/> contains region file | 120 | + # <parameter name="OnRegion" file=".."/> contains region file. |
| 120 | if entry.attribute('name') == 'OnRegion': | 121 | if entry.attribute('name') == 'OnRegion': |
| 121 | onregionfile = entry.attribute('file') | 122 | onregionfile = entry.attribute('file') |
| 122 | 123 | ||
| @@ -139,16 +140,17 @@ class csphagen(ctools.csobservation): | @@ -139,16 +140,17 @@ class csphagen(ctools.csobservation): | ||
| 139 | self._bkgregfiles = {0:None} | 140 | self._bkgregfiles = {0:None} |
| 140 | srcregfile = None | 141 | srcregfile = None |
| 141 | 142 | ||
| 142 | - # Query bkg region file | 143 | + # Query missing bkg region file |
| 143 | if len([True for x in self._bkgregfiles if self._bkgregfiles[x] is None]) > 0: | 144 | if len([True for x in self._bkgregfiles if self._bkgregfiles[x] is None]) > 0: |
| 144 | if len(self._bkgregfiles) > 1: | 145 | if len(self._bkgregfiles) > 1: |
| 145 | # Querying of multiple regions not supported | 146 | # Querying of multiple regions not supported |
| 146 | - raise RuntimeError('Please specify all bkg region files in obs definition XML.') | 147 | + raise RuntimeError('There are missing bkg region files in the obs definition' |
| 148 | + +' XML. Please specify all bkg region files!') | ||
| 147 | else: | 149 | else: |
| 148 | # Query bkg region file for single observation | 150 | # Query bkg region file for single observation |
| 149 | self._bkgregfiles[self._bkgregfiles.keys()[0]] = self['bkgreg'].filename() | 151 | self._bkgregfiles[self._bkgregfiles.keys()[0]] = self['bkgreg'].filename() |
| 150 | 152 | ||
| 151 | - # Query src region file | 153 | + # Query missing src region file |
| 152 | if srcregfile is None: | 154 | if srcregfile is None: |
| 153 | srcregfile = self['srcreg'].filename() | 155 | srcregfile = self['srcreg'].filename() |
| 154 | 156 | ||
| @@ -156,20 +158,7 @@ class csphagen(ctools.csobservation): | @@ -156,20 +158,7 @@ class csphagen(ctools.csobservation): | ||
| 156 | self._src_reg = gammalib.GSkyRegions(srcregfile) | 158 | self._src_reg = gammalib.GSkyRegions(srcregfile) |
| 157 | 159 | ||
| 158 | if len(self._src_reg) > 1: | 160 | if len(self._src_reg) > 1: |
| 159 | - raise RuntimeError('Only 1 On-region is allowed.') | ||
| 160 | - | ||
| 161 | - onRegion = self._src_reg[0] | ||
| 162 | - if isinstance(onRegion, gammalib.GSkyRegionCircle): | ||
| 163 | - self._srcshape = 'CIRCLE' | ||
| 164 | - self._src_dir = onRegion.centre() | ||
| 165 | - self._rad = onRegion.radius() | ||
| 166 | - elif isinstance(onRegion, gammalib.GSkyRegionMap): | ||
| 167 | - self._srcshape = 'MAP' | ||
| 168 | - raise RuntimeError('On region is MAP. Src direction can not be determined. Use CIRCLE!') | ||
| 169 | - else: | ||
| 170 | - raise RuntimeError('On region is unknown. Src direction can not be determined. Use CIRCLE!') | ||
| 171 | - | ||
| 172 | - # END if bkgmethod=='CUSTOM' | 161 | + raise RuntimeError('Only 1 ON region is allowed.') |
| 173 | 162 | ||
| 174 | self['maxoffset'].real() | 163 | self['maxoffset'].real() |
| 175 | 164 | ||
| @@ -281,33 +270,45 @@ class csphagen(ctools.csobservation): | @@ -281,33 +270,45 @@ class csphagen(ctools.csobservation): | ||
| 281 | """ | 270 | """ |
| 282 | 271 | ||
| 283 | # Load regions from ds9 file of FITS WCS map | 272 | # Load regions from ds9 file of FITS WCS map |
| 284 | - regions = gammalib.GSkyRegions( filePath ) | 273 | + regions = gammalib.GSkyRegions( filePath ) |
| 274 | + | ||
| 275 | + # Do region tests only for circle shaped on-region. Else simply return list. | ||
| 276 | + onRegion = self._src_reg[0] | ||
| 277 | + if isinstance(onRegion, gammalib.GSkyRegionCircle): | ||
| 278 | + | ||
| 279 | + # Prepare parameters for comparison | ||
| 280 | + pnt_dir = obs.pointing().dir() | ||
| 281 | + offsetOn = pnt_dir.dist_deg(self._src_dir) | ||
| 282 | + radiusOn = onRegion.radius() | ||
| 283 | + | ||
| 284 | + # Do some tests | ||
| 285 | + for region in regions: | ||
| 285 | 286 | ||
| 286 | - # Prepare parameters for comparison | ||
| 287 | - pnt_dir = obs.pointing().dir() | ||
| 288 | - offsetOn = pnt_dir.dist_deg(self._src_dir) | ||
| 289 | - radiusOn = self._src_reg[0].radius() | 287 | + # Do region tests only for circle shaped off-regions |
| 288 | + if isinstance(region, gammalib.GSkyRegionCircle): | ||
| 290 | 289 | ||
| 291 | - # Do some tests | ||
| 292 | - for region in regions: | 290 | + # Gather parameters |
| 291 | + offsetOff = pnt_dir.dist_deg(region.centre()) | ||
| 292 | + radiusOff = region.radius() | ||
| 293 | 293 | ||
| 294 | - offsetOff = pnt_dir.dist_deg(region.centre()) | ||
| 295 | - radiusOff = region.radius() | 294 | + # Warn: different radius |
| 295 | + if radiusOff != radiusOn: | ||
| 296 | + self._log_string(gammalib.EXPLICIT, 'Off region radius differs from on' | ||
| 297 | + 'region radius: %.2f vs %.2f' % ((radiusOff, radiusOn))) | ||
| 296 | 298 | ||
| 297 | - # Warn: different radius | ||
| 298 | - if radiusOff != radiusOn: | ||
| 299 | - self._log_string(gammalib.EXPLICIT, 'Off region radius differs from on' | ||
| 300 | - 'region radius: %.2f vs %.2f' % ((radiusOff, radiusOn))) | 299 | + # Warn: offset is too small or too large |
| 300 | + if offsetOff <= radiusOn or offsetOff >= self['maxoffset'].real(): | ||
| 301 | + self._log_string(gammalib.EXPLICIT, 'Region offset invalid for observation ' | ||
| 302 | + '%s (%.3f deg) ' % ((obs.id(), offsetOff))) | ||
| 301 | 303 | ||
| 302 | - # Warn: offset is too small or too large | ||
| 303 | - if offsetOff <= self._rad or offsetOff >= self['maxoffset'].real(): | ||
| 304 | - self._log_string(gammalib.EXPLICIT, 'Region offset invalid for observation ' | ||
| 305 | - '%s (%.3f deg) ' % ((obs.id(), offsetOff))) | 304 | + # Warn: offset of off and on region differ |
| 305 | + if offsetOff != offsetOn: | ||
| 306 | + self._log_string(gammalib.EXPLICIT, 'Off region offset differs ' | ||
| 307 | + '%.3f deg from on region offset' % (offsetOff-offsetOn)) | ||
| 306 | 308 | ||
| 307 | - # Warn: offset of off and on region differ | ||
| 308 | - if offsetOff != offsetOn: | ||
| 309 | - self._log_string(gammalib.EXPLICIT, 'Off region offset differs ' | ||
| 310 | - '%.3f deg from on region offset' % (offsetOff-offsetOn)) | 309 | + # end: off-regions test for circle shaped regions |
| 310 | + # end: for loop iterating over off regions | ||
| 311 | + # end: do region tests only if on-region is circle shaped | ||
| 311 | 312 | ||
| 312 | # Inform user how many regions were loaded | 313 | # Inform user how many regions were loaded |
| 313 | self._log_string(gammalib.NORMAL, 'Read %i regions from file %s.' % (regions.size(), filePath.url())) | 314 | self._log_string(gammalib.NORMAL, 'Read %i regions from file %s.' % (regions.size(), filePath.url())) |