Commit b8a1e552bbd0801a8d7175978aea38f4678066f1
1 parent
0c6e2b1a
Use GCTAObservation::off_regions() for CUSTOM method
Showing
1 changed file
with
31 additions
and
137 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._bkg_reg_files = {} # dictionary: bkg region file per observation {obsid:filepath, ..} | |
| 53 | + #self._bkg_reg_files = {} # dictionary: bkg region file per observation {obsid:filepath, ..} | |
| 54 | 54 | |
| 55 | 55 | # Return |
| 56 | 56 | return |
| ... | ... | @@ -103,15 +103,15 @@ class csphagen(ctools.csobservation): |
| 103 | 103 | Raises |
| 104 | 104 | ------ |
| 105 | 105 | RuntimeError |
| 106 | - Multiple On-Regions in on region file or missing Off-Regions files. | |
| 106 | + Only one On region is allowed | |
| 107 | 107 | """ |
| 108 | 108 | # Set up source region |
| 109 | - src_reg_file = self['srcregfile'].filename() | |
| 110 | - self._src_reg = gammalib.GSkyRegions(src_reg_file) | |
| 109 | + filename = self['srcregfile'].filename() | |
| 110 | + self._src_reg = gammalib.GSkyRegions(filename) | |
| 111 | 111 | |
| 112 | 112 | # Raise an exception if there is more than one source region |
| 113 | 113 | if len(self._src_reg) != 1: |
| 114 | - raise RuntimeError('Only 1 On region is allowed.') | |
| 114 | + raise RuntimeError('Only one On region is allowed') | |
| 115 | 115 | |
| 116 | 116 | # Set up source direction. Query parameters if neccessary. |
| 117 | 117 | if isinstance(self._src_reg[0], gammalib.GSkyRegionCircle): |
| ... | ... | @@ -120,51 +120,15 @@ class csphagen(ctools.csobservation): |
| 120 | 120 | else: |
| 121 | 121 | self._query_src_direction() |
| 122 | 122 | |
| 123 | - # Try opening file as XML file. If this files we have probably a FITS | |
| 124 | - # events file on input, hence we query later one background region | |
| 125 | - # file | |
| 126 | - try: | |
| 127 | - # Open observation definition XML file | |
| 128 | - obs_file = self['inobs'].filename() | |
| 129 | - xml = gammalib.GXml(obs_file) | |
| 130 | - | |
| 131 | - # Go through elements in <observationlist> | |
| 132 | - for elem in xml[0]: | |
| 133 | - | |
| 134 | - # Get observation id from <observation> header | |
| 135 | - obs_id = elem.attribute('id') | |
| 136 | - | |
| 137 | - # Get region file path from <observation> body | |
| 138 | - off_regions_file = '' | |
| 139 | - for entry in elem: | |
| 140 | - | |
| 141 | - # <parameter name="OffRegions" file=".."/> contains region file | |
| 142 | - if entry.attribute('name') == 'OffRegions': | |
| 143 | - off_regions_file = entry.attribute('file') | |
| 144 | - break | |
| 145 | - | |
| 146 | - # Store file paths | |
| 147 | - self._bkg_reg_files[obs_id] = gammalib.GFilename(off_regions_file) | |
| 148 | - | |
| 149 | - # Not existing background region files are set to None (for every obs) | |
| 150 | - if not self._bkg_reg_files[obs_id].exists(): | |
| 151 | - self._bkg_reg_files[obs_id] = None | |
| 152 | - | |
| 153 | - # XML file opening failed | |
| 154 | - except: | |
| 155 | - self._bkg_reg_files = {0: None} | |
| 156 | - | |
| 157 | - # Query missing background region file | |
| 158 | - if len([True for x in self._bkg_reg_files if self._bkg_reg_files[x] is None]) > 0: | |
| 159 | - if len(self._bkg_reg_files) > 1: | |
| 160 | - # Querying of multiple regions not supported | |
| 161 | - raise RuntimeError('There are missing bkg region files in the ' | |
| 162 | - 'observation definition XML file. Please ' | |
| 163 | - 'specify all background region files.') | |
| 164 | - else: | |
| 165 | - # Query bkg region file for single observation | |
| 166 | - self._bkg_reg_files[self._bkg_reg_files.keys()[0]] = \ | |
| 167 | - self['bkgregfile'].filename() | |
| 123 | + # Make sure that all CTA observations have an Off region by loading the | |
| 124 | + # Off region region the parameter 'bkgregfile' for all CTA observations | |
| 125 | + # without Off region | |
| 126 | + for obs in self.obs(): | |
| 127 | + if obs.instrument() == 'CTA': | |
| 128 | + if obs.off_regions().is_empty(): | |
| 129 | + filename = self['bkgregfile'].filename() | |
| 130 | + regions = gammalib.GSkyRegions(filename) | |
| 131 | + obs.off_regions(regions) | |
| 168 | 132 | |
| 169 | 133 | # Return |
| 170 | 134 | return |
| ... | ... | @@ -202,9 +166,6 @@ class csphagen(ctools.csobservation): |
| 202 | 166 | # Initialize empty src regions container |
| 203 | 167 | self._src_reg = gammalib.GSkyRegions() |
| 204 | 168 | |
| 205 | - # Get background method parameters | |
| 206 | - self._get_parameters_bkgmethod() | |
| 207 | - | |
| 208 | 169 | # Exclusion map |
| 209 | 170 | if self['inexclusion'].filename().is_fits(): |
| 210 | 171 | self._excl_reg = gammalib.GSkyRegionMap(self['inexclusion'].filename()) |
| ... | ... | @@ -225,6 +186,10 @@ class csphagen(ctools.csobservation): |
| 225 | 186 | if self.obs().is_empty(): |
| 226 | 187 | self.obs(self._get_observations(False)) |
| 227 | 188 | |
| 189 | + # Get background method parameters (have to come after setting up of | |
| 190 | + # observations) | |
| 191 | + self._get_parameters_bkgmethod() | |
| 192 | + | |
| 228 | 193 | # Write input parameters into logger |
| 229 | 194 | self._log_parameters(gammalib.TERSE) |
| 230 | 195 | |
| ... | ... | @@ -295,77 +260,6 @@ class csphagen(ctools.csobservation): |
| 295 | 260 | # Return reflected regions |
| 296 | 261 | return regions |
| 297 | 262 | |
| 298 | - def _regions_from_file(self, obs, file_path): | |
| 299 | - """ | |
| 300 | - Read list of reflected regions for a single observation (pointing) | |
| 301 | - from file. | |
| 302 | - | |
| 303 | - Perform basic tests for circle shaped regions. | |
| 304 | - | |
| 305 | - Parameters | |
| 306 | - ---------- | |
| 307 | - obs : `~gammalib.GCTAObservation()` | |
| 308 | - CTA observation | |
| 309 | - file_path: `~gammalib.GFilename()` | |
| 310 | - File path to file containing off regions. | |
| 311 | - | |
| 312 | - Returns | |
| 313 | - ------- | |
| 314 | - regions : `~gammalib.GSkyRegions` | |
| 315 | - List of reflected regions | |
| 316 | - """ | |
| 317 | - # Load regions from ds9 file of FITS WCS map | |
| 318 | - regions = gammalib.GSkyRegions( file_path ) | |
| 319 | - | |
| 320 | - # Do region tests only for circle shaped on-region, simply return list | |
| 321 | - # for other shapes. | |
| 322 | - on_region = self._src_reg[0] | |
| 323 | - if isinstance(on_region, gammalib.GSkyRegionCircle): | |
| 324 | - | |
| 325 | - # Prepare parameters for comparison | |
| 326 | - pnt_dir = obs.pointing().dir() | |
| 327 | - offset_on = pnt_dir.dist_deg(self._src_dir) | |
| 328 | - radius_on = on_region.radius() | |
| 329 | - | |
| 330 | - # Do some tests | |
| 331 | - for region in regions: | |
| 332 | - | |
| 333 | - # Do region tests only for circle shaped off-regions | |
| 334 | - if isinstance(region, gammalib.GSkyRegionCircle): | |
| 335 | - | |
| 336 | - # Gather parameters | |
| 337 | - offset_off = pnt_dir.dist_deg(region.centre()) | |
| 338 | - radius_off = region.radius() | |
| 339 | - | |
| 340 | - # Warning: different radius | |
| 341 | - if radius_off != radius_on: | |
| 342 | - self._log_string(gammalib.EXPLICIT, 'Off region radius ' | |
| 343 | - 'differs from On region radius: %.2f ' | |
| 344 | - 'vs %.2f' % ((radius_off, radius_on))) | |
| 345 | - | |
| 346 | - # Warning: offset is too small or too large | |
| 347 | - if offset_off <= radius_on or offset_off >= self['maxoffset'].real(): | |
| 348 | - self._log_string(gammalib.EXPLICIT, 'Region offset ' | |
| 349 | - 'invalid for observation %s ' | |
| 350 | - '(%.3f deg) ' % ((obs.id(), offset_off))) | |
| 351 | - | |
| 352 | - # Warning: offset of off and on region differ | |
| 353 | - if offset_off != offset_on: | |
| 354 | - self._log_string(gammalib.EXPLICIT, 'Off region offset ' | |
| 355 | - 'differs %.3f deg from On region ' | |
| 356 | - 'offset' % (offset_off-offset_on)) | |
| 357 | - | |
| 358 | - # end: off-regions test for circle shaped regions | |
| 359 | - # end: for loop iterating over off regions | |
| 360 | - # end: do region tests only if on-region is circle shaped | |
| 361 | - | |
| 362 | - # Inform user how many regions were loaded | |
| 363 | - self._log_string(gammalib.NORMAL, 'Read %i regions from file %s' % \ | |
| 364 | - (regions.size(), file_path.url())) | |
| 365 | - | |
| 366 | - # return list of regions | |
| 367 | - return regions | |
| 368 | - | |
| 369 | 263 | def _set_models(self, obs): |
| 370 | 264 | """ |
| 371 | 265 | Set models in observation container |
| ... | ... | @@ -456,19 +350,12 @@ class csphagen(ctools.csobservation): |
| 456 | 350 | if self['bkgmethod'].string() == 'REFLECTED': |
| 457 | 351 | bkg_reg = self._reflected_regions(obs) |
| 458 | 352 | |
| 459 | - # ... otherwise if custom background is requested the get the background | |
| 460 | - # regions from a region file | |
| 353 | + # ... otherwise if custom background is requested then get the | |
| 354 | + # background regions from the observation. We use a copy here since | |
| 355 | + # otherwise the background regions go out of scope once the observations | |
| 356 | + # are replaced by the On/Off observations. | |
| 461 | 357 | elif self['bkgmethod'].string() == 'CUSTOM': |
| 462 | - | |
| 463 | - # Get file path of background region file from dictionary | |
| 464 | - if len(self._bkg_reg_files) > 1: | |
| 465 | - bkg_reg_file = self._bkg_reg_files[obs.id()] | |
| 466 | - else: | |
| 467 | - # for inobs==event list no obs id is set. | |
| 468 | - bkg_reg_file = self._bkg_reg_files[self._bkg_reg_files.keys()[0]] | |
| 469 | - | |
| 470 | - # Load background regions from region file for current observation | |
| 471 | - bkg_reg = self._regions_from_file(obs, bkg_reg_file) | |
| 358 | + bkg_reg = obs.off_regions().copy() | |
| 472 | 359 | |
| 473 | 360 | # Return background regions |
| 474 | 361 | return bkg_reg |
| ... | ... | @@ -512,8 +399,15 @@ class csphagen(ctools.csobservation): |
| 512 | 399 | # Loop through observations and generate pha, arf, rmf files |
| 513 | 400 | for obs in self.obs(): |
| 514 | 401 | |
| 402 | + # Skip non CTA observations | |
| 403 | + if obs.instrument() != 'CTA': | |
| 404 | + self._log_string(gammalib.NORMAL, 'Skip %s observation "%s"' % \ | |
| 405 | + (obs.instrument(), obs.id())) | |
| 406 | + continue | |
| 407 | + | |
| 515 | 408 | # Log current observation |
| 516 | - self._log_string(gammalib.NORMAL, 'Process Observation %s' % (obs.id())) | |
| 409 | + self._log_string(gammalib.NORMAL, 'Process observation "%s"' % \ | |
| 410 | + (obs.id())) | |
| 517 | 411 | |
| 518 | 412 | # Set background regions for this observation |
| 519 | 413 | bkg_reg = self._set_background_regions(obs) | ... | ... |