Commit 2bb02a07b75400fea7bcbf89d9b192a6a2c70ada

Authored by Jürgen Knödlseder
1 parent f7c5fe8b

Correct standard deviation computation in cssrcdetect and add constructor from s…

…kymap and method that returns model container
Showing 1 changed file with 46 additions and 15 deletions
cscripts/cssrcdetect.py
... ... @@ -45,6 +45,13 @@ class cssrcdetect(ctools.cscript):
45 45 # Set protected members
46 46 self._models = gammalib.GModels()
47 47  
  48 + # Initialise sky map from constructor arguments
  49 + if len(argv) > 0 and isinstance(argv[0], gammalib.GSkyMap):
  50 + self._map = argv[0]
  51 + argv = argv[1:]
  52 + else:
  53 + self._map = gammalib.GSkyMap()
  54 +
48 55 # Initialise application by calling the appropriate class constructor
49 56 self._init_cscript(argv)
50 57  
... ... @@ -57,8 +64,9 @@ class cssrcdetect(ctools.cscript):
57 64 """
58 65 Get parameters from parfile
59 66 """
60   - # Query input parameters
61   - self['inmap'].filename()
  67 + # Query input parameters if sky map is empty
  68 + if self._map.is_empty():
  69 + self['inmap'].filename()
62 70  
63 71 # Query further parameters
64 72 self['srcmodel'].string()
... ... @@ -228,7 +236,7 @@ class cssrcdetect(ctools.cscript):
228 236 # Compute mean and standard deviation
229 237 mean /= float(map.npix())
230 238 std /= float(map.npix())
231   - std -= mean
  239 + std -= mean * mean
232 240 std = math.sqrt(std)
233 241  
234 242 # Return mean and standard deviation
... ... @@ -334,6 +342,31 @@ class cssrcdetect(ctools.cscript):
334 342 return model
335 343  
336 344  
  345 + def _load_skymap(self):
  346 + """
  347 + Load sky map
  348 +
  349 + Returns
  350 + -------
  351 + map : `~gammalib.GSkyMap()`
  352 + Sky map
  353 + """
  354 + # Get skymap filename
  355 + inmap = self['inmap'].filename()
  356 +
  357 + # Open sky map file
  358 + fits = gammalib.GFits(inmap)
  359 +
  360 + # Extract primary extension as sky map
  361 + map = gammalib.GSkyMap(fits.image(0))
  362 +
  363 + # Close sky map file
  364 + fits.close()
  365 +
  366 + # Return
  367 + return map
  368 +
  369 +
337 370 # Public methods
338 371 def run(self):
339 372 """
... ... @@ -352,20 +385,12 @@ class cssrcdetect(ctools.cscript):
352 385 # Write header
353 386 self._log_header1(gammalib.NORMAL, 'Source detection')
354 387  
355   - # Get skymap filename
356   - inmap = self['inmap'].filename()
357   -
358   - # Open sky map file
359   - fits = gammalib.GFits(inmap)
360   -
361   - # Extract primary extension as counts map
362   - counts = gammalib.GSkyMap(fits.image(0))
  388 + # If sky map is empty then load it from input parameter
  389 + if self._map.is_empty():
  390 + self._map = self._load_skymap()
363 391  
364 392 # Detect sources
365   - self._detect_sources(counts)
366   -
367   - # Close sky map file
368   - fits.close()
  393 + self._detect_sources(self._map)
369 394  
370 395 # Write detected sources as models into logger
371 396 self._log_models(gammalib.NORMAL, self._models, 'Detected source model')
... ... @@ -433,6 +458,12 @@ class cssrcdetect(ctools.cscript):
433 458 # Return
434 459 return
435 460  
  461 + def models(self):
  462 + """
  463 + Return model container
  464 + """
  465 + return self._models
  466 +
436 467 def execute(self):
437 468 """
438 469 Execute the script
... ...