Commit b0f2c752434fea48ddc3e1824024a6182b130908
1 parent
e9867c1d
script skeleton
Showing
2 changed files
with
137 additions
and
7 deletions
cscripts/csphagen.par
| ... | ... | @@ -16,16 +16,21 @@ |
| 16 | 16 | # |
| 17 | 17 | # Input and output parameters |
| 18 | 18 | #============================ |
| 19 | -inobs, f, a, obs.xml,,, "Input event list or observation definition XML file" | |
| 19 | +inobs, f, a, obs.xml,,, "Input event list or observation definition XML file" | |
| 20 | 20 | exclusion, f, h, NONE,,, "FITS WCS map of exclusion regions" |
| 21 | -outroot, f, a, spectrum.pha,,, "Root of the name for output PHA and XML files" | |
| 22 | -outreg, f, h, NONE,,, "Output ds9 reg file showing source/background regions" | |
| 21 | +outroot, f, a, spectrum,,, "Root of the name for output PHA and XML files" | |
| 22 | +outreg, f, h, NONE,,, "Output ds9 reg file showing source/background regions" | |
| 23 | 23 | |
| 24 | 24 | # |
| 25 | 25 | # Script parameters |
| 26 | 26 | #================== |
| 27 | +emin, r, a, 0.1,,, "Lower energy limit (TeV)" | |
| 28 | +emax, r, a, 100.0,,, "Upper energy limit (TeV)" | |
| 29 | +enumbins, i, a, 120,,, "Number of energy bins" | |
| 30 | +ebinalg, s, a, LOG,LIN|LOG|FILE,, "Binning algorithm" | |
| 31 | +ebinfile, f, a, NONE,,, "Name of the file containing the energy bin definition" | |
| 27 | 32 | bkgmethod, s, a, REFLECTED,REFLECTED,, "Method for background estimation" |
| 28 | -srcshape, s, a, CIRCLE,CIRCLE,, "Shape of the source region" | |
| 33 | +srcshape, s, a, CIRCLE,CIRCLE,, "Shape of the source region" | |
| 29 | 34 | coordsys, s, a, CEL,CEL|GAL,, "Coordinate system (CEL - celestial, GAL - galactic)" |
| 30 | 35 | ra, r, a, 83.63,0,360, "Right Ascension of source region centre (deg)" |
| 31 | 36 | dec, r, a, 22.01,-90,90, "Declination of source region centre (deg)" |
| ... | ... | @@ -33,7 +38,7 @@ glon, r, a, 184.56,0,360, "Galactic longitude of source region centre (deg) |
| 33 | 38 | glat, r, a, -5.79,-90,90, "Galactic latitude of source region centre (deg)" |
| 34 | 39 | rad, r, a, 0.2,0,180, "Radius of source region circle (deg)" |
| 35 | 40 | bkgregmin, i, h, 2,1,, "Minimum number of background region for REFLECTED method" |
| 36 | -stacking, b, a, yes,,, "Stack all runs into single pha or produce run-by-run files" | |
| 41 | +stack, b, a, yes,,, "Stack all runs into single pha (if no produce run-by-run files)" | |
| 37 | 42 | |
| 38 | 43 | # |
| 39 | 44 | # Standard parameters | ... | ... |
cscripts/csphagen.py
| ... | ... | @@ -19,5 +19,130 @@ |
| 19 | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | 20 | # |
| 21 | 21 | # ========================================================================== |
| 22 | -import sys | |
| 23 | -import gammalib | |
| 24 | 22 | \ No newline at end of file |
| 23 | +import gammalib | |
| 24 | +import ctools | |
| 25 | + | |
| 26 | + | |
| 27 | +# =============== # | |
| 28 | +# csfindobs class # | |
| 29 | +# =============== # | |
| 30 | +class csphagen(ctools.cscript): | |
| 31 | + """ | |
| 32 | + Generate pha files for classical IACT spectral analysis | |
| 33 | + """ | |
| 34 | + | |
| 35 | + # Constructor | |
| 36 | + def __init__(self, *argv): | |
| 37 | + """ | |
| 38 | + Constructor | |
| 39 | + """ | |
| 40 | + # Set name, version | |
| 41 | + self._name = 'csfindobs' | |
| 42 | + self._version = ctools.__version__ | |
| 43 | + | |
| 44 | + # Initialise observation container from constructor arguments | |
| 45 | + self._obs, argv = self._set_input_obs(argv) | |
| 46 | + | |
| 47 | + # Initialise application by calling the appropriate class constructor | |
| 48 | + self._init_cscript(argv) | |
| 49 | + | |
| 50 | + # Return | |
| 51 | + return | |
| 52 | + | |
| 53 | + # Private methods | |
| 54 | + def _get_parameters(self): | |
| 55 | + """ | |
| 56 | + Get parameters from parfile and setup observations | |
| 57 | + """ | |
| 58 | + # If there are no observations in container then query the inobs | |
| 59 | + # parameter | |
| 60 | + if self._obs.size() == 0: | |
| 61 | + self['inobs'].filename() | |
| 62 | + | |
| 63 | + # Query energy binning parameters | |
| 64 | + self['ebinalg'].string() | |
| 65 | + if self['ebinalg'].string() == 'FILE': | |
| 66 | + self['ebinfile'].filename() | |
| 67 | + else: | |
| 68 | + self['emin'].real() | |
| 69 | + self['emax'].real() | |
| 70 | + self['enumbins'].integer() | |
| 71 | + | |
| 72 | + # Initialise background estimation method | |
| 73 | + self._bkgmethod = self["bkgmethod"].string() | |
| 74 | + | |
| 75 | + # Initialise source position/region querying relevant parameters | |
| 76 | + self._src_dir = gammalib.GSkyDir() | |
| 77 | + self._src_reg = gammalib.GSkyRegions() | |
| 78 | + coordsys = self['coordsys'].string() | |
| 79 | + if coordsys == "CEL": | |
| 80 | + ra = self['ra'].real() | |
| 81 | + dec = self['dec'].real() | |
| 82 | + self._src_dir.radec_deg(ra, dec) | |
| 83 | + elif coordsys == "GAL": | |
| 84 | + glon = self['glon'].real() | |
| 85 | + glat = self['glat'].real() | |
| 86 | + self._src_dir.lb_deg(glon, glat) | |
| 87 | + self._srcshape = self['srcshape'].string() | |
| 88 | + if self._srcshape == "CIRCLE": | |
| 89 | + self._rad = self['rad'].real() | |
| 90 | + self._src_reg.append( | |
| 91 | + gammalib.GSkyRegionCircle(self._src_dir, self._rad)) | |
| 92 | + | |
| 93 | + # Stacking | |
| 94 | + self._stack = self['stack'].bool() | |
| 95 | + | |
| 96 | + # Query ahead output parameters | |
| 97 | + if (self._read_ahead()): | |
| 98 | + self['outroot'].string() | |
| 99 | + | |
| 100 | + # Write input parameters into logger | |
| 101 | + self._log_parameters(gammalib.TERSE) | |
| 102 | + | |
| 103 | + # Return | |
| 104 | + return | |
| 105 | + | |
| 106 | + def _reflected_regions(self, obs): | |
| 107 | + """ | |
| 108 | + Calculate list of reflected regions for a single observation | |
| 109 | + :param obs: observation | |
| 110 | + :return: list of reflected regions | |
| 111 | + """ | |
| 112 | + pnt_dir = obs.pointing().dir() | |
| 113 | + offset = pnt_dir.dist_deg(self._src_dir) | |
| 114 | + outregions = [] | |
| 115 | + | |
| 116 | + return outregions | |
| 117 | + | |
| 118 | + # Public methods | |
| 119 | + def run(self): | |
| 120 | + # Switch screen logging on in debug mode | |
| 121 | + if self._logDebug(): | |
| 122 | + self._log.cout(True) | |
| 123 | + | |
| 124 | + # Write observation into logger | |
| 125 | + self._log_observations(gammalib.NORMAL, self._obs, 'Observation') | |
| 126 | + | |
| 127 | + # Set energy binning | |
| 128 | + if self['ebinalg'] == "FILE": | |
| 129 | + pass | |
| 130 | + ## need to load binning from file | |
| 131 | + else: | |
| 132 | + etrue = gammalib.GEbounds(self["enumbins"], | |
| 133 | + gammalib.GEnergy(self["emin"], 'TeV'), | |
| 134 | + gammalib.GEnergy(self["emax"], 'TeV')) | |
| 135 | + ereco = gammalib.GEbounds(self["enumbins"], | |
| 136 | + gammalib.GEnergy(self["emin"], 'TeV'), | |
| 137 | + gammalib.GEnergy(self["emax"], 'TeV')) | |
| 138 | + | |
| 139 | + # Loop through observations and generate pha, arf, rmf files | |
| 140 | + outobs = gammalib.GObservations() | |
| 141 | + for obs in self._obs: | |
| 142 | + bkg_reg = gammalib.GSkyRegions() | |
| 143 | + if self._bkgmethod == "REFLECTED": | |
| 144 | + regions = self.reflected_regions(obs) | |
| 145 | + for region in regions: | |
| 146 | + bkg_reg.append(region) | |
| 147 | + onoff = gammalib.GCTAOnOffObservation(obs, etrue, ereco, | |
| 148 | + self._src_region, bkg_reg) | |
| 149 | + outobs.append(onoff) | ... | ... |