25 from cscripts
import modutils
33 Detects sources in a sky map
42 if len(argv) > 0
and isinstance(argv[0], gammalib.GSkyMap):
46 self.
_map = gammalib.GSkyMap()
49 self._init_cscript(self.__class__.__name__, ctools.__version__, argv)
64 Get parameters from parfile
67 if self._map.is_empty():
68 self[
'inmap'].filename()
71 self[
'srcmodel'].string()
72 self[
'bkgmodel'].string()
73 self[
'threshold'].real()
74 self[
'maxsrcs'].integer()
75 self[
'exclrad'].real()
76 self[
'fit_pos'].boolean()
77 self[
'fit_shape'].boolean()
81 self[
'corr_kern'].string()
82 if self[
'corr_kern'].string().upper() !=
'NONE':
83 self[
'corr_rad'].real()
86 if self._read_ahead():
87 self[
'outmodel'].filename()
88 self[
'outds9file'].filename()
91 self._log_parameters(gammalib.TERSE)
98 Detect sources in counts map
102 counts : `~gammalib.GSkyMap()`
106 for i
in range(self[
'maxsrcs'].integer()):
109 self._log_header3(gammalib.NORMAL,
'Iteration '+str(i+1))
112 mean, std = self.
_map_moments(counts, self[
'avgrad'].real())
115 sigmap = (counts - mean)/std
124 name =
'Src%3.3d' % (i+1)
127 self._log_value(gammalib.NORMAL,
'Map maximum', str(value))
128 self._log_value(gammalib.NORMAL, name+
' position', str(pos))
135 radius=self[
'exclrad'].real())
141 self._log_value(gammalib.NORMAL,
'Map maximum',
142 'None above threshold')
152 Find maximum in a sky map
156 skymap : `~gammalib.GSkyMap()`
161 value, pos : tuple of float and `~gammalib.GSkyDir()`
162 Maximum sky map value and corresponding sky direction
165 value = self[
'threshold'].real()
169 for i
in range(skymap.npix()):
170 if skymap[i] > value:
179 Remove maximum from sky map by replacing pixels with a given value
183 skymap : `~gammalib.GSkyMap()`
185 pos : `~gammalib.GSkyDir()`
186 Sky direction of maximum
187 value : float, optional
189 radius : float, optional
190 Radius within which pixel values are replaced
194 skymap : `~gammalib.GSkyMap()`
195 Sky map with maximum removed
198 skymap_copy = skymap.copy()
201 cos_radius = math.cos(math.radians(radius))
204 for i
in range(skymap_copy.npix()):
206 if skymap_dir.cos_dist(pos) > cos_radius:
207 skymap_copy[i] = value
214 Determine moments of sky map pixels
218 skymap : `~gammalib.GSkyMap()`
221 radius (deg) for pixel consideration
225 mean, std : tuple of GSkyMap
226 Mean and standard deviation of pixel values within a given radius
229 mean = gammalib.GSkyMap(skymap)
230 std = gammalib.GSkyMap(skymap)
234 mean.smooth(
'DISK', radius)
235 std.smooth(
'DISK', radius)
238 std = std - (mean*mean)
242 for index
in range(std.npix()):
243 if std[index] < std_offset:
244 std_offset = std[index]
249 for index
in range(std.npix()):
250 std[index] -= std_offset
260 Add model to model container
264 pos : `~gammalib.GSkyDir()`
265 Sky direction of model
276 self._models.append(model)
288 Model type ('IRF', 'AEFF', 'CUBE' or 'RACC')
292 model : `~gammalib.GModelData()`
296 epivot = gammalib.GEnergy(1.0,
'TeV')
299 if modeltype ==
'IRF':
300 spectral = gammalib.GModelSpectralPlaw(1.0, 0.0, epivot)
301 model = gammalib.GCTAModelIrfBackground(spectral)
302 elif modeltype ==
'AEFF':
303 spectral = gammalib.GModelSpectralPlaw(1.0e-13, -2.5, epivot)
304 model = gammalib.GCTAModelAeffBackground(spectral)
305 elif modeltype ==
'CUBE':
306 spectral = gammalib.GModelSpectralPlaw(1.0, 0.0, epivot)
307 model = gammalib.GCTAModelCubeBackground(spectral)
308 elif modeltype ==
'RACC':
309 radial = gammalib.GCTAModelRadialGauss(3.0)
310 spectral = gammalib.GModelSpectralPlaw(1.0e-4, -2.5, epivot)
311 model = gammalib.GCTAModelRadialAcceptance(radial, spectral)
316 if model
is not None:
317 model.name(
'Background')
324 Set point source model
328 pos : `~gammalib.GSkyDir()`
329 Sky direction of model
333 model : `~gammalib.GModelSky()`
337 spatial = gammalib.GModelSpatialPointSource(pos)
340 fit_pos = self[
'fit_pos'].boolean()
347 if par.name() ==
'RA' or par.name() ==
'DEC':
354 spectral = gammalib.GModelSpectralPlaw(5.7e-18, -2.48,
355 gammalib.GEnergy(0.3,
'TeV'))
358 model = gammalib.GModelSky(spatial, spectral)
366 Smooth the input sky map if a valid kernel was supplied
370 skymap : `~gammalib.GSkyMap()`
374 self._log_header3(gammalib.NORMAL,
'Smoothing Skymap')
377 if self[
'corr_kern'].string().upper() !=
'NONE':
378 self._log_value(gammalib.NORMAL,
'Kernel', self[
'corr_kern'].string())
379 self._log_value(gammalib.NORMAL,
'Parameter', self[
'corr_rad'].real())
380 skymap.smooth(self[
'corr_kern'].string(), self[
'corr_rad'].real())
382 self._log_string(gammalib.NORMAL,
383 'Smoothing kernel is "NONE", smoothing will be ignored.')
394 skymap : `~gammalib.GSkyMap()`
398 inmap = self[
'inmap'].filename()
401 fits = gammalib.GFits(inmap)
404 skymap = gammalib.GSkyMap(fits.image(0))
410 return skymap.extract(0)
415 Cache map pixel positions to save some computation time
418 if not skymap.is_empty():
419 self.
_map_dirs = [skymap.inx2dir(i)
for i
in range(skymap.npix())]
436 if self._map.is_empty():
445 self._log_header1(gammalib.NORMAL,
'Source detection')
451 self._log_models(gammalib.NORMAL, self.
_models,
'Detected source model')
454 self._log_header1(gammalib.NORMAL,
'Add background model')
457 bkgmodel = self.
_set_bkg(self[
'bkgmodel'].string())
458 if bkgmodel
is not None:
461 self._models.append(bkgmodel)
464 self._log_string(gammalib.NORMAL, str(bkgmodel))
468 self._log_value(gammalib.NORMAL,
'Background model',
'None')
475 Save sources into model definition XML file and ds9 file
478 self._log_header1(gammalib.TERSE,
'Save sources')
481 outmodel = self[
'outmodel'].filename()
482 outds9file = self[
'outds9file'].filename()
486 if outmodel.exists()
and not self._clobber:
487 msg = (
'Cannot save "'+outmodel.url()+
'": File already exists. '
488 'Use parameter clobber=yes to allow overwriting of files.')
489 raise RuntimeError(msg)
492 elif outds9file.exists()
and not self._clobber:
493 msg = (
'Cannot save "'+outds9file.url()+
'": File already exists. '
494 'Use parameter clobber=yes to allow overwriting of files.')
495 raise RuntimeError(msg)
500 self._log_value(gammalib.NORMAL,
'Model definition XML file',
504 self._models.save(outmodel)
507 self._log_value(gammalib.NORMAL,
'DS9 region file',
511 modutils.models2ds9file(self.
_models, outds9file.url())
518 Return model container
526 if __name__ ==
'__main__':