Commit 69ab04e73e4cd2e0ec87af59e71557e6c0aae2d7

Authored by Josh Cardenzana
1 parent ed0d4b4b

Added csmodelsois tool (#2180)

csmodelsois allows generating a DiffuseMapCube source from a model XML file. Allows passing a CSV list of source names (via 'soilist') to be excluded from the cube. Also allows outputing an updated model XML file (if 'outmodel' param is specified) in which all models filled into the cube are replaced by the generated diffuse map cube.
cscripts/Makefile.am
... ... @@ -28,6 +28,7 @@ pythonscripts = $(top_builddir)/cscripts/__init__.py \
28 28 $(srcdir)/csmodelinfo.py \
29 29 $(srcdir)/csmodelmerge.py \
30 30 $(srcdir)/csmodelselect.py \
  31 + $(srcdir)/csmodelsois.py \
31 32 $(srcdir)/csobs2caldb.py \
32 33 $(srcdir)/csobsdef.py \
33 34 $(srcdir)/csobsinfo.py \
... ... @@ -60,6 +61,7 @@ parfiles = $(srcdir)/cscaldb.par \
60 61 $(srcdir)/csmodelinfo.par \
61 62 $(srcdir)/csmodelmerge.par \
62 63 $(srcdir)/csmodelselect.par \
  64 + $(srcdir)/csmodelsois.par \
63 65 $(srcdir)/csobs2caldb.par \
64 66 $(srcdir)/csobsdef.par \
65 67 $(srcdir)/csobsinfo.par \
... ... @@ -89,6 +91,7 @@ linkscripts = cscaldb \
89 91 csmodelinfo \
90 92 csmodelmerge \
91 93 csmodelselect \
  94 + csmodelsois \
92 95 csobs2caldb \
93 96 csobsdef \
94 97 csobsinfo \
... ... @@ -123,6 +126,7 @@ testscripts = $(srcdir)/tests/__init__.py \
123 126 $(top_srcdir)/test/test_csmodelinfo.py \
124 127 $(top_srcdir)/test/test_csmodelmerge.py \
125 128 $(top_srcdir)/test/test_csmodelselect.py \
  129 + $(top_srcdir)/test/test_csmodelsois.py \
126 130 $(top_srcdir)/test/test_csobs2caldb.py \
127 131 $(top_srcdir)/test/test_csobsdef.py \
128 132 $(top_srcdir)/test/test_csobsinfo.py \
... ...
cscripts/__init__.py.in
... ... @@ -31,6 +31,7 @@ __all__ = [
31 31 "csmodelinfo",
32 32 "csmodelmerge",
33 33 "csmodelselect",
  34 + "csmodelsois",
34 35 "csobs2caldb",
35 36 "csobsdef",
36 37 "csobsinfo",
... ... @@ -66,6 +67,7 @@ from cscripts.cslightcrv import cslightcrv
66 67 from cscripts.csmodelinfo import csmodelinfo
67 68 from cscripts.csmodelmerge import csmodelmerge
68 69 from cscripts.csmodelselect import csmodelselect
  70 +from cscripts.csmodelsois import csmodelsois
69 71 from cscripts.csobs2caldb import csobs2caldb
70 72 from cscripts.csobsdef import csobsdef
71 73 from cscripts.csobsinfo import csobsinfo
... ...
cscripts/csmodelsois.par 0 → 100644
  1 +#############################################################################
  2 +# #
  3 +# CTA Science Analysis Tools #
  4 +# #
  5 +# ------------------------------------------------------------------------- #
  6 +# #
  7 +# File: csmodelsois.par #
  8 +# #
  9 +# Author: Josh Cardenzana #
  10 +# josh.cardenzana@irap.omp.eu #
  11 +# IRAP #
  12 +# #
  13 +# Purpose: Parameter file for the csmodelsois application #
  14 +# #
  15 +#############################################################################
  16 +#
  17 +# General parameters
  18 +#===================
  19 +inmodel, f, a, "$CTOOLS/share/models/crab.xml",,, "Input model definition XML file"
  20 +outcube, f, a, "mapcube.fits",,, "Output map cube file"
  21 +ptsrcsig, r, h, 1.0,0,60, "Point source Gaussian sigma in arcmin"
  22 +soilist, s, a, "",,, "CSV list of sources to exclude from cube model"
  23 +outmodel, f, a, "NONE",,, "Output model definition XML file"
  24 +
  25 +#
  26 +# Energy binning parameters
  27 +#==========================
  28 +ebinalg, s, a, LOG,FILE|LIN|LOG,, "Algorithm for defining energy bins"
  29 +emin, r, a, 0.1,,, "Start value for first energy bin in TeV"
  30 +emax, r, a, 100.0,,, "Stop value for last energy bin in TeV"
  31 +enumbins, i, a, 20,1,200, "Number of energy bins"
  32 +ebinfile, f, a, NONE,,, "Name of the file containing the energy bin definition"
  33 +
  34 +#
  35 +# Spatial binning parameters
  36 +#===========================
  37 +nxpix, i, a, 200,,, "Size of the X axis in pixels"
  38 +nypix, i, a, 200,,, "Size of the Y axis in pixels"
  39 +binsz, r, a, 0.02,,, "Image scale (in degrees/pixel)"
  40 +coordsys, s, a, CEL,CEL|GAL,,"Coordinate system (CEL - celestial, GAL - galactic)"
  41 +proj, s, a, CAR,AIT|AZP|CAR|GLS|MER|MOL|SFL|SIN|STG|TAN,, "Projection method"
  42 +ra, r, a, 83.63,0,360, "Right ascension of image center (in degrees)"
  43 +dec, r, a, 22.01,-90,90, "Declination of image center (in degrees)"
  44 +glon, r, a, 184.55,0,360, "Galactic longitude of image center (in degrees)"
  45 +glat, r, a, -5.80,-90,90, "Galactic latitude of image center (in degrees)"
  46 +
  47 +#
  48 +# Standard parameters
  49 +#====================
  50 +publish, b, h, no,,, "Publish map cube on VO Hub?"
  51 +chatter, i, h, 2,0,4, "Chattiness of output"
  52 +clobber, b, h, yes,,, "Overwrite existing output files with new output files?"
  53 +debug, b, h, no,,, "Debugging mode activated"
  54 +mode, s, h, "ql",,, "Mode of automatic parameters"
  55 +logfile, f, h, "ctmapcube.log",,, "Log filename"
... ...
cscripts/csmodelsois.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# Allows generation of DiffuseMapCube model from subset of sources in a
  4 +# model definition XML file
  5 +#
  6 +# Copyright (C) 2017 Josh Cardenzana
  7 +#
  8 +# This program is free software: you can redistribute it and/or modify
  9 +# it under the terms of the GNU General Public License as published by
  10 +# the Free Software Foundation, either version 3 of the License, or
  11 +# (at your option) any later version.
  12 +#
  13 +# This program is distributed in the hope that it will be useful,
  14 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 +# GNU General Public License for more details.
  17 +#
  18 +# You should have received a copy of the GNU General Public License
  19 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  20 +#
  21 +# ==========================================================================
  22 +import sys
  23 +import gammalib
  24 +import ctools
  25 +
  26 +#=============#
  27 +# csmodelsois #
  28 +#=============#
  29 +class csmodelsois(ctools.cscript):
  30 + """
  31 + Generates a DiffuseMapCube model from a subset of sources in a model
  32 + definition XML file. All background models will be ignored!
  33 + """
  34 +
  35 + # Constructor
  36 + def __init__(self, *argv):
  37 + """
  38 + Constructor.
  39 +
  40 + Parameters
  41 + ----------
  42 + argv : list of parameters
  43 +
  44 + Raises
  45 + ------
  46 + TypeError
  47 + An invalid number of command line arguments was provided.
  48 + """
  49 + # Set name and version
  50 + self._name = 'csmodelsois'
  51 + self._version = ctools.__version__
  52 +
  53 + # Initialize parameters
  54 + self._init_members()
  55 +
  56 + # Initialise application by calling the appropriate class constructor
  57 + self._init_cscript(argv)
  58 +
  59 + # Return
  60 + return
  61 +
  62 +
  63 + def _init_members(self):
  64 + """
  65 + Initialize the parameters for this object
  66 + """
  67 + self.cubegen = ctools.ctmapcube() # Implements ctmapcube for generate the cube
  68 + self.m_models = gammalib.GModels() # Stores the full list of input models
  69 + self.cubemodels = gammalib.GModels() # Stores the list of models to be written to file
  70 + return
  71 +
  72 +
  73 + def _set_cube_parameters(self):
  74 + """
  75 + Fill in the parameters associated with this object
  76 + """
  77 +
  78 + # Get the cube centre coordinates
  79 + self.cubegen['coordsys'].string( self['coordsys'].string() )
  80 + if self['coordsys'].string() == 'CEL':
  81 + # Center in celestial coordinates
  82 + self.cubegen['xref'].real( self['ra'].real() )
  83 + self.cubegen['yref'].real( self['dec'].real() )
  84 + else:
  85 + # Otherwise assume center in galactic coordinates
  86 + self.cubegen['xref'].real( self['glon'].real() )
  87 + self.cubegen['yref'].real( self['glat'].real() )
  88 +
  89 + # Get spatial binning parameters
  90 + self.cubegen['binsz'].real( self['binsz'].real() )
  91 + self.cubegen['nxpix'].integer( self['nxpix'].integer() )
  92 + self.cubegen['nypix'].integer( self['nypix'].integer() )
  93 + self.cubegen['proj'].string( self['proj'].string() )
  94 +
  95 + # Get the energy binning parameters
  96 + self.cubegen['ebinalg'].string( self['ebinalg'].string() )
  97 + if self['ebinalg'].string() == "FILE":
  98 + # Read energy binning from a file
  99 + self.cubegen['ebinfile'].filename( self['ebinfile'].filename() )
  100 + else:
  101 + # Create the energy binning information
  102 + self.cubegen['emin'].real( self['emin'].real() )
  103 + self.cubegen['emax'].real( self['emax'].real() )
  104 + self.cubegen['enumbins'].integer( self['enumbins'].integer() )
  105 +
  106 + # Get remaining hidden parameters
  107 + self.cubegen['ptsrcsig'].real( self['ptsrcsig'].real() )
  108 +
  109 + # Read optionally output cube filenames
  110 + if self._read_ahead():
  111 + self.cubegen['outcube'].filename( self['outcube'].filename() )
  112 + self['soilist'].string()
  113 + self['outmodel'].filename()
  114 +
  115 + # Write parameters into logger
  116 + self._log_parameters(gammalib.TERSE);
  117 +
  118 + return
  119 +
  120 +
  121 + def _gen_cubemodel(self):
  122 + """
  123 + Generates a binned model from the model file using ctmapcube
  124 + returns: updated model filename
  125 + """
  126 + # Get a list of sources we DONT want to put into the cube
  127 + sources = self['soilist'].string().split(',')
  128 +
  129 + # Load the model into a model container
  130 + if (self.m_models.size() == 0):
  131 + self.m_models = gammalib.GModels(self['inmodel'].filename())
  132 +
  133 + self.cubemodels = self.m_models.copy()
  134 +
  135 + # Loop through models and pull out all models not in the list
  136 + models2remove = []
  137 + for model in self.m_models:
  138 + # Convert the model to a GModelSky
  139 + mod = model.classname()
  140 +
  141 + # Check the model
  142 + if mod != "GModelSky":
  143 + # Model is most likely a background model, so continue
  144 + self.cubemodels.remove(model.name())
  145 + elif model.name() in sources:
  146 + # Model is a source of interest, so remove it from list of models
  147 + # to be used in generating the cube
  148 + self.cubemodels.remove(model.name())
  149 +
  150 + # Log the number of models to be put into the cube
  151 + self._log_value(self['chatter'].integer(), 'Cube models', self.cubemodels.size())
  152 +
  153 + return
  154 +
  155 +
  156 + def mapcube(self):
  157 + """
  158 + Return the mapcube generated by the underlying 'ctmapcube' object
  159 + """
  160 + return self.cubegen.mapcube()
  161 +
  162 +
  163 + def cubemodelname(self):
  164 + """
  165 + Return the name to be given to the cube model in the output XML file
  166 + """
  167 + return "FixedSourcesCube"
  168 +
  169 +
  170 + def models(self, models:gammalib.GModels):
  171 + """
  172 + Set the models for analysis
  173 + """
  174 + self.m_models = models
  175 + return
  176 +
  177 +
  178 + def run(self):
  179 + """
  180 + Implements the actual bulk of the script's tasks
  181 + """
  182 + # Switch screen logging on in debug mode
  183 + if self._logDebug():
  184 + self._log.cout(True)
  185 +
  186 + # Get parameters
  187 + self._set_cube_parameters()
  188 +
  189 + # Set the models to put into the cube
  190 + self._gen_cubemodel()
  191 +
  192 + # Only run if there still models to be filled into the cube
  193 + if not self.cubemodels.is_empty():
  194 + self._log_string(gammalib.NORMAL, 'Generting model cube')
  195 +
  196 + # Fill the cube models into the ctmapcube object
  197 + self.cubegen.models( self.cubemodels )
  198 +
  199 + # Run the map generating method
  200 + self.cubegen.run()
  201 +
  202 + else:
  203 + self._log_string(gammalib.NORMAL, 'List of models is empty, nothing will be done')
  204 +
  205 + return
  206 +
  207 +
  208 + def save(self):
  209 + """
  210 + Save the cube and optionally the updated XML model file
  211 + """
  212 + #cube = gammalib.GModelSpatialDiffuseCube(self.cubegen.mapcube())
  213 +
  214 + # Save the generated cube if the cube and filename are not empty
  215 + if ((not self.cubegen.mapcube().cube().is_empty()) and
  216 + self._is_valid_filename(self['outcube'].filename())):
  217 +
  218 + self.cubegen.mapcube().save( self['outcube'].filename(), self['clobber'].boolean() )
  219 +
  220 + # If requested, save the updated list of models
  221 + if self._is_valid_filename( self['outmodel'].filename() ):
  222 + # Generate a list of models that will be output to file
  223 + outmodels = gammalib.GModels(self.m_models)
  224 +
  225 + # Remove all models used in the generated cube
  226 + for model in self.cubemodels:
  227 + outmodels.remove(model.name())
  228 +
  229 + # Generate the actual cube model
  230 + spat = gammalib.GModelSpatialDiffuseCube( self['outcube'].filename() )
  231 + spec = gammalib.GModelSpectralConst()
  232 + newmodel = gammalib.GModelSky(spat, spec)
  233 +
  234 + # Give the model a default name
  235 + newmodel.name( self.cubemodelname() )
  236 +
  237 + # Now append the model to the list of output models
  238 + outmodels.append(newmodel)
  239 +
  240 + # Save the model list
  241 + outmodels.save( self['outmodel'].filename() )
  242 +
  243 + return
  244 +
  245 +
  246 + def execute(self):
  247 + """
  248 + Execute the script
  249 + """
  250 + # Open logfile
  251 + self.logFileOpen()
  252 +
  253 + # Read ahead output parameters
  254 + self._read_ahead(True)
  255 +
  256 + # Run the script
  257 + self.run()
  258 + self.save()
  259 +
  260 + # Return
  261 + return
  262 +
  263 +
  264 +# ======================== #
  265 +# Main routine entry point #
  266 +# ======================== #
  267 +if __name__ == '__main__':
  268 +
  269 + # Create instance of application
  270 + app = csmodelsois(sys.argv)
  271 +
  272 + # Execute application
  273 + app.execute()
... ...
doc/source/users/reference_manual/csmodelsois.rst 0 → 100644
  1 +.. _csmodelsois:
  2 +
  3 +csmodelsois
  4 +===========
  5 +
  6 +Generate a map cube from a subset of models in an input model.
  7 +
  8 +
  9 +Synopsis
  10 +----------
  11 +
  12 +This tool generates a map cube from a subset of sky model components contained
  13 +in an input model. A map cube is a series of sky maps, each spanned by Right
  14 +Ascension or Galactic longitude and Declination or Galactic latitude. Each
  15 +map corresponds to a specific photon energy. The energy binning of the map
  16 +cube may be either linear, logarithmic, or custom defined using an input
  17 +file.
  18 +
  19 +csmodelsois generates a map cube FITS file comprising two extensions. The
  20 +primary extension contains a 3-dimensional image containing the map cube
  21 +values. A second extension named ``ENERGIES`` contains a binary table that
  22 +defines the energy values of each map in the cube.
  23 +
  24 +Optionally, csmodelsois can also output an updated model XML file in which all
  25 +sources from the input model XML file used to generate the map cube are
  26 +replaced with an updated model that uses the generated map cube. This is done
  27 +by specifying the 'outmodel' parameter. The 'soilist' parameter specifies a
  28 +list of source names (separated by ',') to be excluded from the generated
  29 +cube.
  30 +
  31 +General parameters
  32 +------------------
  33 +
  34 +``inmodel [file]``
  35 + Input model XML file.
  36 +
  37 +``outmodel [file]``
  38 + Output model XML file (not generated if set to 'NONE').
  39 +
  40 +``outcube [file]``
  41 + Output map cube file.
  42 +
  43 +``soilist [string]``
  44 + CSV list of sources to be excluded from the generated map cube.
  45 +
  46 +``(ptsrcsig = 1.0) [real]``
  47 + Sigma of Gaussian to be used for point sources (in arcmin).
  48 +
  49 +``ebinalg <FILE|LIN|LOG> [string]``
  50 + Algorithm for defining energy bins.
  51 +
  52 +``emin [real]``
  53 + Lower energy value for first energy bin (in TeV).
  54 +
  55 +``emax [real]``
  56 + Upper energy value for last energy bin (in TeV).
  57 +
  58 +``enumbins [integer]``
  59 + Number of energy bins.
  60 +
  61 +``ebinfile [file]``
  62 + Name of the file containing the energy bin definition.
  63 +
  64 +``nxpix [integer]``
  65 + Number of cube bins in Right Ascension or Galactic longitude.
  66 +
  67 +``nypix [integer]``
  68 + Number of cube bins in Declination or Galactic latitude.
  69 +
  70 +``binsz [real]``
  71 + Image scale (in degrees/pixel).
  72 +
  73 +``coordsys <CEL|GAL> [string]``
  74 + Coordinate system (CEL - celestial, GAL - galactic).
  75 +
  76 +``proj <AIT|AZP|CAR|GLS|MER|MOL|SFL|SIN|STG|TAN> [string]``
  77 + Projection method.
  78 +
  79 +``ra [real]``
  80 + Right Ascension of cube centre (J2000, in degrees).
  81 +
  82 +``dec [real]``
  83 + Declination of cube centre (J2000, in degrees).
  84 +
  85 +``glon [real]``
  86 + Galactic longitude of cube centre (in degrees).
  87 +
  88 +``glat [real]``
  89 + Galactic latitude of cube centre (in degrees).
  90 +
  91 +Standard parameters
  92 +-------------------
  93 +
  94 +``(publish = no) [boolean]``
  95 + Specifies whether the map cube should be published on VO Hub.
  96 +
  97 +``(chatter = 2) [integer]``
  98 + Verbosity of the executable:
  99 + ``chatter = 0``: no information will be logged
  100 +
  101 + ``chatter = 1``: only errors will be logged
  102 +
  103 + ``chatter = 2``: errors and actions will be logged
  104 +
  105 + ``chatter = 3``: report about the task execution
  106 +
  107 + ``chatter = 4``: detailed report about the task execution
  108 +
  109 +``(clobber = yes) [boolean]``
  110 + Specifies whether an existing output counts cube file should be overwritten.
  111 +
  112 +``(debug = no) [boolean]``
  113 + Enables debug mode. In debug mode the executable will dump any log file output to the console.
  114 +
  115 +``(mode = ql) [string]``
  116 + Mode of automatic parameters (default is "ql", i.e. "query and learn").
  117 +
  118 +``(logfile = csmodelsois.log) [string]``
  119 + Name of log file.
  120 +
  121 +
  122 +Related tools or scripts
  123 +------------------------
  124 +
  125 +None
0 126 \ No newline at end of file
... ...
test/test_csmodelsois.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csmodelsois tool.
  4 +#
  5 +# Copyright (C) 2016-2017 Juergen Knoedlseder
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +import os
  22 +import gammalib
  23 +import cscripts
  24 +from testing import test
  25 +
  26 +
  27 +# =============================== #
  28 +# Test class for csmodelsois tool #
  29 +# =============================== #
  30 +class Test(test):
  31 + """
  32 + Test class for csmodelsois tool
  33 +
  34 + This test class makes unit tests for the csmodelsois tool by using it from
  35 + the command line and from Python.
  36 + """
  37 +
  38 + # Constructor
  39 + def __init__(self):
  40 + """
  41 + Constructor
  42 + """
  43 + # Call base class constructor
  44 + test.__init__(self)
  45 +
  46 + # Return
  47 + return
  48 +
  49 + # Set test functions
  50 + def set(self):
  51 + """
  52 + Set all test functions
  53 + """
  54 + # Set test name
  55 + self.name('csmodelsois')
  56 +
  57 + # Append tests
  58 + self.append(self._test_cmd, 'Test csmodelsois on command line')
  59 + self.append(self._test_python, 'Test csmodelsois from Python')
  60 +
  61 + # Return
  62 + return
  63 +
  64 + # Test csmodelsois on command line
  65 + def _test_cmd(self):
  66 + """
  67 + Test csmodelsois on the command line
  68 + """
  69 + # Set tool name
  70 + csmodelsois = self._script('csmodelsois')
  71 +
  72 + # Setup csmodelsois command
  73 + cmd = csmodelsois+' inmodel="'+self._model+'"'+ \
  74 + ' outcube="csmodelsois_cmd1.fits"'+\
  75 + ' emin=0.1 emax=100.0 enumbins=10 ebinalg=LOG'+ \
  76 + ' nxpix=100 nypix=100 binsz=0.04 coordsys=CEL'+ \
  77 + ' soilist="" outmodel=NONE'+ \
  78 + ' ra=83.63 dec=22.01 proj=CAR'+ \
  79 + ' logfile="csmodelsois_cmd1.log" chatter=1'
  80 +
  81 + # Check if execution of wrong command fails
  82 + self.test_assert(self._execute('command_that_does_not_exist') != 0,
  83 + 'Self test of test script')
  84 +
  85 + # Check if execution was successful
  86 + self.test_assert(self._execute(cmd) == 0,
  87 + 'Check successful execution from command line')
  88 +
  89 + # Check map cube
  90 + self._check_result_file('csmodelsois_cmd1.fits')
  91 +
  92 + # Setup csmodelsois command
  93 + cmd = csmodelsois+' inmodel="events_that_do_not_exist.fits"'+ \
  94 + ' outcube="ccsmodelsois_cmd2.fits"'+\
  95 + ' emin=0.1 emax=100.0 enumbins=10 ebinalg=LOG'+ \
  96 + ' nxpix=100 nypix=100 binsz=0.04 coordsys=CEL'+ \
  97 + ' soilist="" outmodel=NONE'+ \
  98 + ' ra=83.63 dec=22.01 proj=CAR'+ \
  99 + ' logfile="csmodelsois_cmd2.log"'
  100 +
  101 + # Check if execution failed
  102 + self.test_assert(self._execute(cmd) != 0,
  103 + 'Check invalid input file when executed from command line')
  104 +
  105 + # Check csmodelsois --help
  106 + self._check_help(csmodelsois)
  107 +
  108 + # Return
  109 + return
  110 +
  111 + # Test csmodelsois from Python
  112 + def _test_python(self):
  113 + """
  114 + Test csmodelsois from Python
  115 + """
  116 + # Allocate csmodelsois
  117 + modelsois = cscripts.csmodelsois()
  118 +
  119 + # Check that empty csmodelsois tool holds an map cube that has no
  120 + # energy bins
  121 + self._check_cube(modelsois.mapcube(), nmaps=0, npixels=0)
  122 +
  123 + # Check that saving does nothing
  124 + modelsois['outcube'] = 'csmodelsois_py0.fits'
  125 + modelsois['outmodel'] = 'NONE'
  126 + modelsois['logfile'] = 'csmodelsois_py0.log'
  127 + modelsois.logFileOpen()
  128 + modelsois.save()
  129 + self.test_assert(not os.path.isfile('csmodelsois_py0.fits'),
  130 + 'Check that no map cube has been created')
  131 +
  132 + # Check that clearing does not lead to an exception or segfault
  133 + modelsois.clear()
  134 +
  135 + # Now set csmodelsois parameters
  136 + modelsois['inmodel'] = self._model
  137 + modelsois['ebinalg'] = 'LOG'
  138 + modelsois['emin'] = 0.1
  139 + modelsois['emax'] = 100.0
  140 + modelsois['enumbins'] = 10
  141 + modelsois['nxpix'] = 100
  142 + modelsois['nypix'] = 100
  143 + modelsois['binsz'] = 0.04
  144 + modelsois['coordsys'] = 'CEL'
  145 + modelsois['proj'] = 'CAR'
  146 + modelsois['ra'] = 83.63
  147 + modelsois['dec'] = 22.01
  148 + modelsois['ptsrcsig'] = 1.0
  149 + modelsois['outcube'] = 'csmodelsois_py1.fits'
  150 + modelsois['logfile'] = 'csmodelsois_py1.log'
  151 + modelsois['chatter'] = 2
  152 + modelsois['soilist'] = ''
  153 + modelsois['outmodel'] = 'NONE'
  154 +
  155 + # Run csmodelsois tool
  156 + modelsois.logFileOpen() # Make sure we get a log file
  157 + modelsois.run()
  158 +
  159 + # Save map cube
  160 + modelsois.save()
  161 +
  162 + # Check map cube
  163 + self._check_result_file('csmodelsois_py1.fits')
  164 +
  165 + # Allocate csmodelsois scripts, set models and do now a linear
  166 + # binning; also do not use Gaussian point sources
  167 + modelsois = cscripts.csmodelsois()
  168 + modelsois['inmodel'] = self._model
  169 + modelsois['ebinalg'] = 'LIN'
  170 + modelsois['emin'] = 0.1
  171 + modelsois['emax'] = 100.0
  172 + modelsois['enumbins'] = 10
  173 + modelsois['nxpix'] = 100
  174 + modelsois['nypix'] = 100
  175 + modelsois['binsz'] = 0.04
  176 + modelsois['coordsys'] = 'CEL'
  177 + modelsois['proj'] = 'CAR'
  178 + modelsois['ra'] = 83.63
  179 + modelsois['dec'] = 22.01
  180 + modelsois['ptsrcsig'] = 0.0
  181 + modelsois['outcube'] = 'csmodelsois_py2.fits'
  182 + modelsois['logfile'] = 'csmodelsois_py2.log'
  183 + modelsois['chatter'] = 4
  184 + modelsois['soilist'] = ''
  185 + modelsois['outmodel'] = 'NONE'
  186 +
  187 + # Execute mapcube
  188 + modelsois.logFileOpen() # Needed to get a new log file
  189 + modelsois.execute()
  190 +
  191 + # Check result file
  192 + self._check_result_file('csmodelsois_py2.fits')
  193 +
  194 + # Update output filenames and output a new model file
  195 + modelsois['outcube'] = 'csmodelsois_py3.fits'
  196 + modelsois['logfile'] = 'csmodelsois_py3.log'
  197 + modelsois['outmodel'] = 'csmodelsois_py3.xml'
  198 +
  199 + # Execute the file
  200 + modelsois.logFileOpen()
  201 + modelsois.execute()
  202 +
  203 + # Now check that the output model file doesnt contain a Crab Sources
  204 + models3 = gammalib.GModels('csmodelsois_py3.xml')
  205 + self.test_assert(not models3.contains("Crab"), 'Check Crab model is not present')
  206 + self.test_assert(models3.contains(modelsois.cubemodelname()),
  207 + 'Check cube model is present')
  208 + self._check_result_file('csmodelsois_py3.fits')
  209 +
  210 + # Return
  211 + return
  212 +
  213 + # Check result file
  214 + def _check_result_file(self, filename):
  215 + """
  216 + Check content of map cube
  217 +
  218 + Parameters
  219 + ----------
  220 + filename : str
  221 + Map cube file name
  222 + """
  223 + # Load map cube
  224 + cube = gammalib.GModelSpatialDiffuseCube(filename)
  225 +
  226 + # Check map cube
  227 + self._check_cube(cube)
  228 +
  229 + # Return
  230 + return
  231 +
  232 + # Check map cube
  233 + def _check_cube(self, cube, nmaps=11, npixels=10000):
  234 + """
  235 + Check map cube
  236 +
  237 + Parameters
  238 + ----------
  239 + cube : `~gammalib.GModelSpatialDiffuseCube`
  240 + Map cube
  241 + nmaps : int, optional
  242 + Number of maps
  243 + npixels : int, optional
  244 + Number of pixels
  245 + """
  246 + # Get energies (this forces loading in case the map cube is not
  247 + # loaded)
  248 + energies = cube.energies()
  249 + self.test_value(len(cube.energies()), nmaps, 'Check number of energies')
  250 +
  251 + # Check dimensions
  252 + self.test_value(cube.maps(), nmaps, 'Check number of maps')
  253 + self.test_value(cube.pixels(), npixels, 'Check number of pixels')
  254 +
  255 + # Return
  256 + return
... ...
test/test_python_cscripts.py
... ... @@ -32,6 +32,7 @@ import test_cslightcrv
32 32 import test_csmodelinfo
33 33 import test_csmodelmerge
34 34 import test_csmodelselect
  35 +import test_csmodelsois
35 36 import test_csobs2caldb
36 37 import test_csobsdef
37 38 import test_csobsinfo
... ... @@ -121,6 +122,7 @@ def test(installed=False, debug=False):
121 122 test_csmodelinfo.Test(),
122 123 test_csmodelmerge.Test(),
123 124 test_csmodelselect.Test(),
  125 + test_csmodelsois.Test(),
124 126 test_csobs2caldb.Test(),
125 127 test_csobsdef.Test(),
126 128 test_csobsinfo.Test(),
... ...