Commit b6f44a5bd7852f3d5cfa8bfbab51acbed10fc21f
1 parent
e009c0e3
Modified variable names and some comment lines to follow coding principles.
Showing
1 changed file
with
52 additions
and
46 deletions
cscripts/csphagen.py
| ... | ... | @@ -50,7 +50,7 @@ class csphagen(ctools.csobservation): |
| 50 | 50 | self._has_exclusion = False |
| 51 | 51 | self._srcshape = '' |
| 52 | 52 | self._rad = 0.0 |
| 53 | - self._bkgregfiles = {} | |
| 53 | + self._bkg_reg_files = {} # dictionary: bkg region file per observation {obsid:filepath, ..} | |
| 54 | 54 | |
| 55 | 55 | # Return |
| 56 | 56 | return |
| ... | ... | @@ -59,6 +59,11 @@ class csphagen(ctools.csobservation): |
| 59 | 59 | def _get_parameters(self): |
| 60 | 60 | """ |
| 61 | 61 | Get parameters from parfile and setup observations |
| 62 | + | |
| 63 | + Raises | |
| 64 | + ------ | |
| 65 | + RuntimeError | |
| 66 | + Multiple On-Regions in on region file or missing Off-Regions files. | |
| 62 | 67 | """ |
| 63 | 68 | # Setup observations (require response and allow event list, don't |
| 64 | 69 | # allow counts cube) |
| ... | ... | @@ -93,69 +98,69 @@ class csphagen(ctools.csobservation): |
| 93 | 98 | |
| 94 | 99 | elif bkgmethod == 'CUSTOM': |
| 95 | 100 | |
| 96 | - obsfile = self['inobs'].filename() | |
| 97 | - srcregfile = gammalib.GFilename() | |
| 101 | + obs_file = self['inobs'].filename() | |
| 102 | + src_reg_file = gammalib.GFilename() | |
| 98 | 103 | |
| 99 | 104 | # obs def XML: look for region files |
| 100 | - if obsfile.file()[-3:] == 'xml': | |
| 105 | + if obs_file.file()[-3:] == 'xml': | |
| 101 | 106 | # open and load XML obs definition file |
| 102 | 107 | xml = gammalib.GXml() |
| 103 | - xml.load(obsfile) | |
| 108 | + xml.load(obs_file) | |
| 104 | 109 | |
| 105 | 110 | # Go through elements in <observationlist> |
| 106 | 111 | for elem in xml[0]: |
| 107 | 112 | |
| 108 | 113 | # Get observation id from <observation> header |
| 109 | - obsid = elem.attribute('id') | |
| 114 | + obs_id = elem.attribute('id') | |
| 110 | 115 | |
| 111 | 116 | # Get region file path from <observation> body |
| 112 | - offregionsfile = '' | |
| 113 | - onregionfile = '' | |
| 117 | + off_regions_file = '' | |
| 118 | + on_region_file = '' | |
| 114 | 119 | for entry in elem: |
| 115 | 120 | |
| 116 | 121 | # <parameter name="OffRegions" file=".."/> contains region file |
| 117 | 122 | if entry.attribute('name') == 'OffRegions': |
| 118 | - offregionsfile = entry.attribute('file') | |
| 123 | + off_regions_file = entry.attribute('file') | |
| 119 | 124 | |
| 120 | 125 | # <parameter name="OnRegion" file=".."/> contains region file. |
| 121 | 126 | if entry.attribute('name') == 'OnRegion': |
| 122 | - onregionfile = entry.attribute('file') | |
| 127 | + on_region_file = entry.attribute('file') | |
| 123 | 128 | |
| 124 | 129 | # Store file paths |
| 125 | - self._bkgregfiles[obsid] = gammalib.GFilename(offregionsfile) | |
| 130 | + self._bkg_reg_files[obs_id] = gammalib.GFilename(off_regions_file) | |
| 126 | 131 | |
| 127 | - if onregionfile != '': | |
| 128 | - srcregfile = gammalib.GFilename(onregionfile) | |
| 132 | + if on_region_file != '': | |
| 133 | + src_reg_file = gammalib.GFilename(on_region_file) | |
| 129 | 134 | |
| 130 | 135 | # Not existing bkg region files are set to None (for every obs) |
| 131 | - if not self._bkgregfiles[obsid].exists(): | |
| 132 | - self._bkgregfiles[obsid] = None | |
| 136 | + if not self._bkg_reg_files[obs_id].exists(): | |
| 137 | + self._bkg_reg_files[obs_id] = None | |
| 133 | 138 | |
| 134 | 139 | # Not existing src region files are set to None (one time) |
| 135 | - if not srcregfile.exists(): | |
| 136 | - srcregfile = None | |
| 140 | + if not src_reg_file.exists(): | |
| 141 | + src_reg_file = None | |
| 137 | 142 | |
| 138 | 143 | else: |
| 139 | 144 | # event FITS files: query later 1 bkg & 1 src region file |
| 140 | - self._bkgregfiles = {0:None} | |
| 141 | - srcregfile = None | |
| 145 | + self._bkg_reg_files = {0:None} | |
| 146 | + src_reg_file = None | |
| 142 | 147 | |
| 143 | 148 | # Query missing bkg region file |
| 144 | - if len([True for x in self._bkgregfiles if self._bkgregfiles[x] is None]) > 0: | |
| 145 | - if len(self._bkgregfiles) > 1: | |
| 149 | + if len([True for x in self._bkg_reg_files if self._bkg_reg_files[x] is None]) > 0: | |
| 150 | + if len(self._bkg_reg_files) > 1: | |
| 146 | 151 | # Querying of multiple regions not supported |
| 147 | 152 | raise RuntimeError('There are missing bkg region files in the obs definition' |
| 148 | 153 | +' XML. Please specify all bkg region files!') |
| 149 | 154 | else: |
| 150 | 155 | # Query bkg region file for single observation |
| 151 | - self._bkgregfiles[self._bkgregfiles.keys()[0]] = self['bkgreg'].filename() | |
| 156 | + self._bkg_reg_files[self._bkg_reg_files.keys()[0]] = self['bkgreg'].filename() | |
| 152 | 157 | |
| 153 | 158 | # Query missing src region file |
| 154 | - if srcregfile is None: | |
| 155 | - srcregfile = self['srcreg'].filename() | |
| 159 | + if src_reg_file is None: | |
| 160 | + src_reg_file = self['srcreg'].filename() | |
| 156 | 161 | |
| 157 | 162 | # Set up src (=On) region. Only 1 region is allowed |
| 158 | - self._src_reg = gammalib.GSkyRegions(srcregfile) | |
| 163 | + self._src_reg = gammalib.GSkyRegions(src_reg_file) | |
| 159 | 164 | |
| 160 | 165 | if len(self._src_reg) > 1: |
| 161 | 166 | raise RuntimeError('Only 1 ON region is allowed.') |
| ... | ... | @@ -252,15 +257,16 @@ class csphagen(ctools.csobservation): |
| 252 | 257 | # Return reflected regions |
| 253 | 258 | return regions |
| 254 | 259 | |
| 255 | - def _regions_from_file(self, obs, filePath): | |
| 260 | + def _regions_from_file(self, obs, file_path): | |
| 256 | 261 | """ |
| 257 | 262 | Read list of reflected regions for a single observation (pointing) from file. |
| 263 | + Perform basic tests for circle shaped regions. | |
| 258 | 264 | |
| 259 | 265 | Parameters |
| 260 | 266 | ---------- |
| 261 | 267 | obs : `~gammalib.GCTAObservation()` |
| 262 | 268 | CTA observation |
| 263 | - filePath: `~gammalib.GFilename()` | |
| 269 | + file_path: `~gammalib.GFilename()` | |
| 264 | 270 | File path to file containing off regions. |
| 265 | 271 | |
| 266 | 272 | Returns |
| ... | ... | @@ -270,16 +276,16 @@ class csphagen(ctools.csobservation): |
| 270 | 276 | """ |
| 271 | 277 | |
| 272 | 278 | # Load regions from ds9 file of FITS WCS map |
| 273 | - regions = gammalib.GSkyRegions( filePath ) | |
| 279 | + regions = gammalib.GSkyRegions( file_path ) | |
| 274 | 280 | |
| 275 | 281 | # 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): | |
| 282 | + on_region = self._src_reg[0] | |
| 283 | + if isinstance(on_region, gammalib.GSkyRegionCircle): | |
| 278 | 284 | |
| 279 | 285 | # Prepare parameters for comparison |
| 280 | 286 | pnt_dir = obs.pointing().dir() |
| 281 | - offsetOn = pnt_dir.dist_deg(self._src_dir) | |
| 282 | - radiusOn = onRegion.radius() | |
| 287 | + offset_on = pnt_dir.dist_deg(self._src_dir) | |
| 288 | + radius_on = on_region.radius() | |
| 283 | 289 | |
| 284 | 290 | # Do some tests |
| 285 | 291 | for region in regions: |
| ... | ... | @@ -288,30 +294,30 @@ class csphagen(ctools.csobservation): |
| 288 | 294 | if isinstance(region, gammalib.GSkyRegionCircle): |
| 289 | 295 | |
| 290 | 296 | # Gather parameters |
| 291 | - offsetOff = pnt_dir.dist_deg(region.centre()) | |
| 292 | - radiusOff = region.radius() | |
| 297 | + offset_off = pnt_dir.dist_deg(region.centre()) | |
| 298 | + radius_off = region.radius() | |
| 293 | 299 | |
| 294 | 300 | # Warn: different radius |
| 295 | - if radiusOff != radiusOn: | |
| 301 | + if radius_off != radius_on: | |
| 296 | 302 | self._log_string(gammalib.EXPLICIT, 'Off region radius differs from on' |
| 297 | - 'region radius: %.2f vs %.2f' % ((radiusOff, radiusOn))) | |
| 303 | + 'region radius: %.2f vs %.2f' % ((radius_off, radius_on))) | |
| 298 | 304 | |
| 299 | 305 | # Warn: offset is too small or too large |
| 300 | - if offsetOff <= radiusOn or offsetOff >= self['maxoffset'].real(): | |
| 306 | + if offset_off <= radius_on or offset_off >= self['maxoffset'].real(): | |
| 301 | 307 | self._log_string(gammalib.EXPLICIT, 'Region offset invalid for observation ' |
| 302 | - '%s (%.3f deg) ' % ((obs.id(), offsetOff))) | |
| 308 | + '%s (%.3f deg) ' % ((obs.id(), offset_off))) | |
| 303 | 309 | |
| 304 | 310 | # Warn: offset of off and on region differ |
| 305 | - if offsetOff != offsetOn: | |
| 311 | + if offset_off != offset_on: | |
| 306 | 312 | self._log_string(gammalib.EXPLICIT, 'Off region offset differs ' |
| 307 | - '%.3f deg from on region offset' % (offsetOff-offsetOn)) | |
| 313 | + '%.3f deg from on region offset' % (offset_off-offset_on)) | |
| 308 | 314 | |
| 309 | 315 | # end: off-regions test for circle shaped regions |
| 310 | 316 | # end: for loop iterating over off regions |
| 311 | 317 | # end: do region tests only if on-region is circle shaped |
| 312 | 318 | |
| 313 | 319 | # Inform user how many regions were loaded |
| 314 | - self._log_string(gammalib.NORMAL, 'Read %i regions from file %s.' % (regions.size(), filePath.url())) | |
| 320 | + self._log_string(gammalib.NORMAL, 'Read %i regions from file %s.' % (regions.size(), file_path.url())) | |
| 315 | 321 | |
| 316 | 322 | # return list of regions |
| 317 | 323 | return regions |
| ... | ... | @@ -435,14 +441,14 @@ class csphagen(ctools.csobservation): |
| 435 | 441 | bkg_reg = self._reflected_regions(obs) |
| 436 | 442 | elif self['bkgmethod'].string() == 'CUSTOM': |
| 437 | 443 | # Get file path of bkg region file from dictionary |
| 438 | - if len(self._bkgregfiles) > 1: | |
| 439 | - bkgregfile = self._bkgregfiles[obs.id()] | |
| 444 | + if len(self._bkg_reg_files) > 1: | |
| 445 | + bkg_reg_file = self._bkg_reg_files[obs.id()] | |
| 440 | 446 | else: |
| 441 | 447 | # for inobs==event list no obs id is set. |
| 442 | - bkgregfile = self._bkgregfiles[self._bkgregfiles.keys()[0]] | |
| 448 | + bkg_reg_file = self._bkg_reg_files[self._bkg_reg_files.keys()[0]] | |
| 443 | 449 | |
| 444 | - # If file is valid load. Else warn and use reflected method. | |
| 445 | - bkg_reg = self._regions_from_file(obs, bkgregfile) | |
| 450 | + # Load off regions from region file for current observation | |
| 451 | + bkg_reg = self._regions_from_file(obs, bkg_reg_file) | |
| 446 | 452 | |
| 447 | 453 | |
| 448 | 454 | # If there are reflected regions then create On/Off observation | ... | ... |