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,6 +45,13 @@ class cssrcdetect(ctools.cscript):
45 # Set protected members 45 # Set protected members
46 self._models = gammalib.GModels() 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 # Initialise application by calling the appropriate class constructor 55 # Initialise application by calling the appropriate class constructor
49 self._init_cscript(argv) 56 self._init_cscript(argv)
50 57
@@ -57,8 +64,9 @@ class cssrcdetect(ctools.cscript): @@ -57,8 +64,9 @@ class cssrcdetect(ctools.cscript):
57 """ 64 """
58 Get parameters from parfile 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 # Query further parameters 71 # Query further parameters
64 self['srcmodel'].string() 72 self['srcmodel'].string()
@@ -228,7 +236,7 @@ class cssrcdetect(ctools.cscript): @@ -228,7 +236,7 @@ class cssrcdetect(ctools.cscript):
228 # Compute mean and standard deviation 236 # Compute mean and standard deviation
229 mean /= float(map.npix()) 237 mean /= float(map.npix())
230 std /= float(map.npix()) 238 std /= float(map.npix())
231 - std -= mean 239 + std -= mean * mean
232 std = math.sqrt(std) 240 std = math.sqrt(std)
233 241
234 # Return mean and standard deviation 242 # Return mean and standard deviation
@@ -334,6 +342,31 @@ class cssrcdetect(ctools.cscript): @@ -334,6 +342,31 @@ class cssrcdetect(ctools.cscript):
334 return model 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 # Public methods 370 # Public methods
338 def run(self): 371 def run(self):
339 """ 372 """
@@ -352,20 +385,12 @@ class cssrcdetect(ctools.cscript): @@ -352,20 +385,12 @@ class cssrcdetect(ctools.cscript):
352 # Write header 385 # Write header
353 self._log_header1(gammalib.NORMAL, 'Source detection') 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 # Detect sources 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 # Write detected sources as models into logger 395 # Write detected sources as models into logger
371 self._log_models(gammalib.NORMAL, self._models, 'Detected source model') 396 self._log_models(gammalib.NORMAL, self._models, 'Detected source model')
@@ -433,6 +458,12 @@ class cssrcdetect(ctools.cscript): @@ -433,6 +458,12 @@ class cssrcdetect(ctools.cscript):
433 # Return 458 # Return
434 return 459 return
435 460
  461 + def models(self):
  462 + """
  463 + Return model container
  464 + """
  465 + return self._models
  466 +
436 def execute(self): 467 def execute(self):
437 """ 468 """
438 Execute the script 469 Execute the script