Commit 834a7cec018c37cf127ba69dbc0569d57c2178db

Authored by Jürgen Knödlseder
1 parent ba197c90

Refactor test source position and pointing direction setting in cssens (#3555).

A _set_source() method was introduced in cssens that deals with the setting of the test source direction independent of the code in the _set_obs() method, since that latter method is not necessarily called. The _set_obs() method now only deals with setting of the pointing direction of a single observation, in case that no observation definition XML file was specified on input.
Showing 1 changed file with 52 additions and 39 deletions
cscripts/cssens.py
... ... @@ -110,6 +110,7 @@ class cssens(ctools.csobservation):
110 110 # Return
111 111 return
112 112  
  113 +
113 114 # Private methods
114 115 def _get_parameters(self):
115 116 """
... ... @@ -126,8 +127,8 @@ class cssens(ctools.csobservation):
126 127 if self.obs().models().size() == 0:
127 128 self.obs().models(self['inmodel'].filename())
128 129  
129   - # Get source name
130   - self._srcname = self['srcname'].string()
  130 + # Set source name and position
  131 + self._set_source()
131 132  
132 133 # Read further parameters
133 134 emin = self['emin'].real()
... ... @@ -156,14 +157,40 @@ class cssens(ctools.csobservation):
156 157 # Write input parameters into logger
157 158 self._log_parameters(gammalib.TERSE)
158 159  
159   -
160 160 # Set number of processes for multiprocessing
161 161 self._nthreads = mputils.nthreads(self)
162 162  
163 163 # Return
164 164 return
165 165  
166   - def _set_obs(self, emin, emax, lpnt=None, bpnt=None):
  166 + def _set_source(self):
  167 + """
  168 + Set source name and position
  169 + """
  170 + # Set source name
  171 + self._srcname = self['srcname'].string()
  172 +
  173 + # Set source position. If the test source has no position then set the
  174 + # source position to (RA,Dec)=(0,0)
  175 + source = self.obs().models()[self._srcname]
  176 + if source.has_par('RA') and source.has_par('DEC'):
  177 + self._ra = source['RA'].value()
  178 + self._dec = source['DEC'].value()
  179 + elif source.has_par('GLON') and source.has_par('GLAT'):
  180 + glon = source['GLON'].value()
  181 + glat = source['GLAT'].value()
  182 + srcdir = gammalib.GSkyDir()
  183 + srcdir.lb_deg(glon, glat)
  184 + self._ra = srcdir.ra_deg()
  185 + self._dec = srcdir.dec_deg()
  186 + else:
  187 + self._ra = 0.0
  188 + self._dec = 0.0
  189 +
  190 + # Return
  191 + return
  192 +
  193 + def _set_obs(self, emin, emax):
167 194 """
168 195 Set an observation container
169 196  
... ... @@ -173,10 +200,6 @@ class cssens(ctools.csobservation):
173 200 Minimum energy (TeV)
174 201 emax : float
175 202 Maximum energy (TeV)
176   - lpnt : float, optional
177   - Galactic longitude of pointing (deg)
178   - bpnt : float, optional
179   - Galactic latitude of pointing (deg)
180 203  
181 204 Returns
182 205 -------
... ... @@ -187,47 +210,37 @@ class cssens(ctools.csobservation):
187 210 if self['inobs'].is_valid():
188 211 obs = self._get_observations()
189 212  
190   - # ... otherwise allocate a single observation
  213 + # ... otherwise allocate a single observation using the test source
  214 + # position as pointing direction, optionally offset by a certain
  215 + # amount
191 216 else:
192 217  
193   - # If no pointing is specified and if the test source has a position
194   - # then set the pointing to the position of the test source
195   - if lpnt is None and bpnt is None:
196   - models = gammalib.GModels(self['inmodel'].filename())
197   - source = models[self['srcname'].string()]
198   - if source.has_par('RA') and source.has_par('DEC'):
199   - pntdir = gammalib.GSkyDir()
200   - pntdir.radec_deg(source['RA'].value(), source['DEC'].value())
201   - lpnt = pntdir.l_deg()
202   - bpnt = pntdir.b_deg()
203   - elif source.has_par('GLON') and source.has_par('GLAT'):
204   - lpnt = source['GLON'].value()
205   - bpnt = source['GLAT'].value()
206   - else:
207   - lpnt = 0.0
208   - bpnt = 0.0
  218 + # Load models
  219 + models = gammalib.GModels(self['inmodel'].filename())
209 220  
210   - # Set source position
211   - srcdir = gammalib.GSkyDir()
212   - srcdir.lb_deg(lpnt, bpnt)
213   - self._ra = srcdir.ra_deg()
214   - self._dec = srcdir.dec_deg()
  221 + # Get test source
  222 + source = models[self['srcname'].string()]
215 223  
216   - # Set pointing direction offset in galactic latitude
217   - offset = self['offset'].real()
218   - bpnt += offset
219   - if bpnt > 90.0:
220   - bpnt = 180.0 - bpnt
221   - lpnt += 180.0
222   - pntdir = gammalib.GSkyDir()
223   - pntdir.lb_deg(lpnt, bpnt)
  224 + # Set pointing direction to test source position. If test source
  225 + # has no position then set the pointing to (RA,Dec)=(0,0)
  226 + pntdir = gammalib.GSkyDir()
  227 + if source.has_par('RA') and source.has_par('DEC'):
  228 + pntdir.radec_deg(source['RA'].value(), source['DEC'].value())
  229 + elif source.has_par('GLON') and source.has_par('GLAT'):
  230 + pntdir.lb_deg(source['GLON'].value(), source['GLAT'].value())
  231 + else:
  232 + pntdir.radec_deg(0.0, 0.0)
224 233  
225   - # Read relevant user parameters
  234 + # Read other relevant user parameters
226 235 caldb = self['caldb'].string()
227 236 irf = self['irf'].string()
228 237 deadc = self['deadc'].real()
229 238 duration = self['duration'].real()
230 239 rad = self['rad'].real()
  240 + offset = self['offset'].real()
  241 +
  242 + # Add offset to pointing direction
  243 + pntdir.rotate_deg(0.0, offset)
231 244  
232 245 # Allocate observation container
233 246 obs = gammalib.GObservations()
... ...