diff --git a/cscripts/cscaldb.py b/cscripts/cscaldb.py index 8c52016..5f3f589 100755 --- a/cscripts/cscaldb.py +++ b/cscripts/cscaldb.py @@ -37,7 +37,7 @@ class cscaldb(ctools.cscript): into the "cscaldb.log" log file. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -46,25 +46,10 @@ class cscaldb(ctools.cscript): self._name = "cscaldb" self._version = "1.1.0" - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - # Set logger properties - self._log_header() - self._log.date(True) - - # Return - return - - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/csfindobs.py b/cscripts/csfindobs.py index d807230..086774d 100755 --- a/cscripts/csfindobs.py +++ b/cscripts/csfindobs.py @@ -33,7 +33,7 @@ class csfindobs(ctools.cscript): Find observations from an IACT data store. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -44,32 +44,13 @@ class csfindobs(ctools.cscript): self._verbose = False self._datapath = os.getenv("VHEFITS","") - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Write separator into logger - if self._logTerse(): - self._log("\n") - - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/csiactcopy.py b/cscripts/csiactcopy.py index 2cd6562..327f5e0 100755 --- a/cscripts/csiactcopy.py +++ b/cscripts/csiactcopy.py @@ -35,7 +35,7 @@ class csiactcopy(ctools.cscript): observations. Index files get merged and updated accordingly. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -45,26 +45,10 @@ class csiactcopy(ctools.cscript): self._version = "1.1.0" self._datapath = os.getenv("VHEFITS","") - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) - - # Return - return + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - def __del__(self): - """ - Destructor. - """ - # Return return diff --git a/cscripts/csiactdata.py b/cscripts/csiactdata.py index 2fd5eb7..3ebeb18 100755 --- a/cscripts/csiactdata.py +++ b/cscripts/csiactdata.py @@ -36,7 +36,7 @@ class csiactdata(ctools.cscript): for 'csiactobs'. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -47,25 +47,10 @@ class csiactdata(ctools.cscript): self._datapath = os.getenv("VHEFITS","") self._prodnames = [] - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - # Set logger properties - self._log_header() - self._log.date(True) - - # Return - return - - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/csiactobs.py b/cscripts/csiactobs.py index 1c2db5a..3c74c5c 100755 --- a/cscripts/csiactobs.py +++ b/cscripts/csiactobs.py @@ -38,7 +38,7 @@ class csiactobs(ctools.cscript): http://gamma-astro-data-formats.readthedocs.org/en/latest/ """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -55,28 +55,13 @@ class csiactobs(ctools.cscript): self._models = gammalib.GModels() self._runlist = [] - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/cslightcrv.py b/cscripts/cslightcrv.py index 61b22be..313bc55 100755 --- a/cscripts/cslightcrv.py +++ b/cscripts/cslightcrv.py @@ -61,7 +61,7 @@ class cslightcrv(ctools.cscript): observation container and saves results in a FITS file. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -75,34 +75,13 @@ class cslightcrv(ctools.cscript): self._tbins = gammalib.GGti() self._stacked = False - # Initialise observation container. In case that an observation - # container is provided on the command line, this container will - # be used to initialise the class. - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations): - self._obs = argv[0] - argv = argv[1:] - else: - self._obs = gammalib.GObservations() - - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise observation container from constructor arguments. + self._obs = self._set_input_obs(argv) - # Return - return + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/csmodelinfo.py b/cscripts/csmodelinfo.py index 3f22562..475d079 100755 --- a/cscripts/csmodelinfo.py +++ b/cscripts/csmodelinfo.py @@ -36,7 +36,7 @@ class csmodelinfo(ctools.cscript): with DS9. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -60,25 +60,10 @@ class csmodelinfo(ctools.cscript): self._free_color = "green" self._fixed_color = "magenta" - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - # Set logger properties - self._log_header() - self._log.date(True) - - # Return - return - - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/csmodelmerge.py b/cscripts/csmodelmerge.py index 811c056..3d998d1 100755 --- a/cscripts/csmodelmerge.py +++ b/cscripts/csmodelmerge.py @@ -35,7 +35,7 @@ class csmodelmerge(ctools.cscript): a single model definition XML file. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -48,28 +48,13 @@ class csmodelmerge(ctools.cscript): self._files = None self._models = gammalib.GModels() - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/csobs2caldb.py b/cscripts/csobs2caldb.py index 786a433..664bac7 100755 --- a/cscripts/csobs2caldb.py +++ b/cscripts/csobs2caldb.py @@ -40,7 +40,7 @@ class csobs2caldb(ctools.cscript): index of any observation using the hidden "index" parameter. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -59,28 +59,13 @@ class csobs2caldb(ctools.cscript): self._base_dir = "" self._cal_dir = "" - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/csobsdef.py b/cscripts/csobsdef.py index 047ad0f..ebe2592 100755 --- a/cscripts/csobsdef.py +++ b/cscripts/csobsdef.py @@ -72,7 +72,7 @@ class csobsdef(ctools.cscript): IRF in the "dummy" calibration database. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -89,25 +89,10 @@ class csobsdef(ctools.cscript): self._clobber = True self._debug = False - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - # Set logger properties - self._log_header() - self._log.date(True) - - # Return - return - - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/csobsinfo.py b/cscripts/csobsinfo.py index 9b6049c..10ef2cc 100755 --- a/cscripts/csobsinfo.py +++ b/cscripts/csobsinfo.py @@ -31,7 +31,7 @@ class csobsinfo(ctools.cscript): Shows the content of an observation container. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -51,33 +51,13 @@ class csobsinfo(ctools.cscript): self._ebounds = gammalib.GEbounds() self._gti = gammalib.GGti() - # Initialise observation - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations): - self._obs = argv[0] - argv = argv[1:] - else: - self._obs = gammalib.GObservations() - self._obs.clear() + # Initialise observation container from constructor arguments. + self._obs = self._set_input_obs(argv) - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - # Set logger properties - self._log_header() - self._log.date(True) - - # Return - return - - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/cspull.py b/cscripts/cspull.py index 11cf622..c995e97 100755 --- a/cscripts/cspull.py +++ b/cscripts/cspull.py @@ -38,7 +38,7 @@ class cspull(ctools.cscript): script behaves just as a regular ctool. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -62,28 +62,13 @@ class cspull(ctools.cscript): self._proj = "TAN" self._log_clients = False - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/csresmap.py b/cscripts/csresmap.py index af5caa1..e91fb6e 100755 --- a/cscripts/csresmap.py +++ b/cscripts/csresmap.py @@ -36,7 +36,7 @@ class csresmap(ctools.cscript): behaves just as a regular ctool. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -74,32 +74,13 @@ class csresmap(ctools.cscript): self._debug = False self._log_clients = False - # Initialise observation - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations): - self._obs = argv[0] - argv = argv[1:] - else: - self._obs = gammalib.GObservations() - - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise observation container from constructor arguments. + self._obs = self._set_input_obs(argv) - # Return - return + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) - def __del__(self): - """ - Destructor. - """ # Return return diff --git a/cscripts/cssens.py b/cscripts/cssens.py index 670748a..e526b67 100755 --- a/cscripts/cssens.py +++ b/cscripts/cssens.py @@ -41,7 +41,7 @@ class cssens(ctools.cscript): the likelihood difference between fitting with and without the test source. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -71,28 +71,13 @@ class cssens(ctools.cscript): self._log_clients = False self._debug = False - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/csspec.py b/cscripts/csspec.py index 8fa5361..764c3e7 100755 --- a/cscripts/csspec.py +++ b/cscripts/csspec.py @@ -60,24 +60,12 @@ class csspec(ctools.cscript): self._clobber = True self._debug = False - # Initialise observation - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations): - self._obs = argv[0] - argv = argv[1:] - else: - self._obs = gammalib.GObservations() - - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") + # Initialise observation container from constructor arguments. + self._obs = self._set_input_obs(argv) - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return diff --git a/cscripts/cstsdist.py b/cscripts/cstsdist.py index 29f2044..137671b 100755 --- a/cscripts/cstsdist.py +++ b/cscripts/cstsdist.py @@ -39,7 +39,7 @@ class cstsdist(ctools.cscript): script behaves just as a regular ctool. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -61,28 +61,13 @@ class cstsdist(ctools.cscript): self._debug = False self._log_clients = False - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) ==1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/cscripts/cstsmapmerge.py b/cscripts/cstsmapmerge.py index 7503b89..4eccec7 100755 --- a/cscripts/cstsmapmerge.py +++ b/cscripts/cstsmapmerge.py @@ -33,7 +33,7 @@ class cstsmapmerge(ctools.cscript): Merge Test Statistic maps. """ - # Constructors and destructors + # Constructor def __init__(self, *argv): """ Constructor. @@ -53,28 +53,13 @@ class cstsmapmerge(ctools.cscript): self._overwrite = True self._delete = False - # Initialise application - if len(argv) == 0: - ctools.cscript.__init__(self, self._name, self._version) - elif len(argv) == 1: - ctools.cscript.__init__(self, self._name, self._version, *argv) - else: - raise TypeError("Invalid number of arguments given.") - - # Set logger properties - self._log_header() - self._log.date(True) + # Initialise application by calling the appropriate class + # constructor. + self._init_cscript(argv) # Return return - def __del__(self): - """ - Destructor. - """ - # Return - return - # Private methods def _get_parameters(self): diff --git a/pyext/ctool.i b/pyext/ctool.i index 00eaff8..3f6c940 100644 --- a/pyext/ctool.i +++ b/pyext/ctool.i @@ -152,7 +152,7 @@ public: /***********************************************************************//** - * @brief ctool base class Python extension + * @brief ctool base class Python extensions ***************************************************************************/ %extend ctool { void _read_ahead(const bool& flag) { @@ -160,3 +160,35 @@ public: return; } } + +/***********************************************************************//** + * @brief cscript base class Python extensions + ***************************************************************************/ +%pythoncode %{ + +# Initialise observation container from constructor arguments. In case that +# an observation container is provided as argument, this container will be +# used to initialise the class. +def _set_input_obs(self, argv): + if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations): + obs = argv[0] + argv = argv[1:] + else: + obs = gammalib.GObservations() + return obs +cscript._set_input_obs = _set_input_obs + +# Initialise application by calling the appropriate class constructor. +def _init_cscript(self, argv): + if len(argv) == 0: + cscript.__init__(self, self._name, self._version) + elif len(argv) == 1: + cscript.__init__(self, self._name, self._version, *argv) + else: + raise TypeError("Invalid number of arguments given.") + # Set logger properties + self._log_header() + self._log.date(True) +cscript._init_cscript = _init_cscript + +%}