Commit 6476d854894c296fd7c09d974629e15b6cc29d6f

Authored by Jürgen Knödlseder
1 parent 4f9b688d

Implement _set_input_obs() and _init_cscript() methods at base class level to av…

…oid code duplication in cscripts.
cscripts/cscaldb.py
... ... @@ -37,7 +37,7 @@ class cscaldb(ctools.cscript):
37 37 into the "cscaldb.log" log file.
38 38 """
39 39  
40   - # Constructors and destructors
  40 + # Constructor
41 41 def __init__(self, *argv):
42 42 """
43 43 Constructor.
... ... @@ -46,25 +46,10 @@ class cscaldb(ctools.cscript):
46 46 self._name = "cscaldb"
47 47 self._version = "1.1.0"
48 48  
49   - # Initialise application
50   - if len(argv) == 0:
51   - ctools.cscript.__init__(self, self._name, self._version)
52   - elif len(argv) ==1:
53   - ctools.cscript.__init__(self, self._name, self._version, *argv)
54   - else:
55   - raise TypeError("Invalid number of arguments given.")
  49 + # Initialise application by calling the appropriate class
  50 + # constructor.
  51 + self._init_cscript(argv)
56 52  
57   - # Set logger properties
58   - self._log_header()
59   - self._log.date(True)
60   -
61   - # Return
62   - return
63   -
64   - def __del__(self):
65   - """
66   - Destructor.
67   - """
68 53 # Return
69 54 return
70 55  
... ...
cscripts/csfindobs.py
... ... @@ -33,7 +33,7 @@ class csfindobs(ctools.cscript):
33 33 Find observations from an IACT data store.
34 34 """
35 35  
36   - # Constructors and destructors
  36 + # Constructor
37 37 def __init__(self, *argv):
38 38 """
39 39 Constructor.
... ... @@ -44,32 +44,13 @@ class csfindobs(ctools.cscript):
44 44 self._verbose = False
45 45 self._datapath = os.getenv("VHEFITS","")
46 46  
47   - # Initialise application
48   - if len(argv) == 0:
49   - ctools.cscript.__init__(self, self._name, self._version)
50   - elif len(argv) ==1:
51   - ctools.cscript.__init__(self, self._name, self._version, *argv)
52   - else:
53   - raise TypeError("Invalid number of arguments given.")
54   -
55   - # Set logger properties
56   - self._log_header()
57   - self._log.date(True)
  47 + # Initialise application by calling the appropriate class
  48 + # constructor.
  49 + self._init_cscript(argv)
58 50  
59 51 # Return
60 52 return
61 53  
62   - def __del__(self):
63   - """
64   - Destructor.
65   - """
66   - # Write separator into logger
67   - if self._logTerse():
68   - self._log("\n")
69   -
70   - # Return
71   - return
72   -
73 54  
74 55 # Private methods
75 56 def _get_parameters(self):
... ...
cscripts/csiactcopy.py
... ... @@ -35,7 +35,7 @@ class csiactcopy(ctools.cscript):
35 35 observations. Index files get merged and updated accordingly.
36 36 """
37 37  
38   - # Constructors and destructors
  38 + # Constructor
39 39 def __init__(self, *argv):
40 40 """
41 41 Constructor.
... ... @@ -45,26 +45,10 @@ class csiactcopy(ctools.cscript):
45 45 self._version = "1.1.0"
46 46 self._datapath = os.getenv("VHEFITS","")
47 47  
48   - # Initialise application
49   - if len(argv) == 0:
50   - ctools.cscript.__init__(self, self._name, self._version)
51   - elif len(argv) == 1:
52   - ctools.cscript.__init__(self, self._name, self._version, *argv)
53   - else:
54   - raise TypeError("Invalid number of arguments given.")
55   -
56   - # Set logger properties
57   - self._log_header()
58   - self._log.date(True)
59   -
60   - # Return
61   - return
  48 + # Initialise application by calling the appropriate class
  49 + # constructor.
  50 + self._init_cscript(argv)
62 51  
63   - def __del__(self):
64   - """
65   - Destructor.
66   - """
67   -
68 52 # Return
69 53 return
70 54  
... ...
cscripts/csiactdata.py
... ... @@ -36,7 +36,7 @@ class csiactdata(ctools.cscript):
36 36 for 'csiactobs'.
37 37 """
38 38  
39   - # Constructors and destructors
  39 + # Constructor
40 40 def __init__(self, *argv):
41 41 """
42 42 Constructor.
... ... @@ -47,25 +47,10 @@ class csiactdata(ctools.cscript):
47 47 self._datapath = os.getenv("VHEFITS","")
48 48 self._prodnames = []
49 49  
50   - # Initialise application
51   - if len(argv) == 0:
52   - ctools.cscript.__init__(self, self._name, self._version)
53   - elif len(argv) == 1:
54   - ctools.cscript.__init__(self, self._name, self._version, *argv)
55   - else:
56   - raise TypeError("Invalid number of arguments given.")
  50 + # Initialise application by calling the appropriate class
  51 + # constructor.
  52 + self._init_cscript(argv)
57 53  
58   - # Set logger properties
59   - self._log_header()
60   - self._log.date(True)
61   -
62   - # Return
63   - return
64   -
65   - def __del__(self):
66   - """
67   - Destructor.
68   - """
69 54 # Return
70 55 return
71 56  
... ...
cscripts/csiactobs.py
... ... @@ -38,7 +38,7 @@ class csiactobs(ctools.cscript):
38 38 http://gamma-astro-data-formats.readthedocs.org/en/latest/
39 39 """
40 40  
41   - # Constructors and destructors
  41 + # Constructor
42 42 def __init__(self, *argv):
43 43 """
44 44 Constructor.
... ... @@ -55,28 +55,13 @@ class csiactobs(ctools.cscript):
55 55 self._models = gammalib.GModels()
56 56 self._runlist = []
57 57  
58   - # Initialise application
59   - if len(argv) == 0:
60   - ctools.cscript.__init__(self, self._name, self._version)
61   - elif len(argv) == 1:
62   - ctools.cscript.__init__(self, self._name, self._version, *argv)
63   - else:
64   - raise TypeError("Invalid number of arguments given.")
65   -
66   - # Set logger properties
67   - self._log_header()
68   - self._log.date(True)
  58 + # Initialise application by calling the appropriate class
  59 + # constructor.
  60 + self._init_cscript(argv)
69 61  
70 62 # Return
71 63 return
72 64  
73   - def __del__(self):
74   - """
75   - Destructor.
76   - """
77   - # Return
78   - return
79   -
80 65  
81 66 # Private methods
82 67 def _get_parameters(self):
... ...
cscripts/cslightcrv.py
... ... @@ -61,7 +61,7 @@ class cslightcrv(ctools.cscript):
61 61 observation container and saves results in a FITS file.
62 62 """
63 63  
64   - # Constructors and destructors
  64 + # Constructor
65 65 def __init__(self, *argv):
66 66 """
67 67 Constructor.
... ... @@ -75,34 +75,13 @@ class cslightcrv(ctools.cscript):
75 75 self._tbins = gammalib.GGti()
76 76 self._stacked = False
77 77  
78   - # Initialise observation container. In case that an observation
79   - # container is provided on the command line, this container will
80   - # be used to initialise the class.
81   - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
82   - self._obs = argv[0]
83   - argv = argv[1:]
84   - else:
85   - self._obs = gammalib.GObservations()
86   -
87   - # Initialise application
88   - if len(argv) == 0:
89   - ctools.cscript.__init__(self, self._name, self._version)
90   - elif len(argv) == 1:
91   - ctools.cscript.__init__(self, self._name, self._version, *argv)
92   - else:
93   - raise TypeError("Invalid number of arguments given.")
94   -
95   - # Set logger properties
96   - self._log_header()
97   - self._log.date(True)
  78 + # Initialise observation container from constructor arguments.
  79 + self._obs = self._set_input_obs(argv)
98 80  
99   - # Return
100   - return
  81 + # Initialise application by calling the appropriate class
  82 + # constructor.
  83 + self._init_cscript(argv)
101 84  
102   - def __del__(self):
103   - """
104   - Destructor.
105   - """
106 85 # Return
107 86 return
108 87  
... ...
cscripts/csmodelinfo.py
... ... @@ -36,7 +36,7 @@ class csmodelinfo(ctools.cscript):
36 36 with DS9.
37 37 """
38 38  
39   - # Constructors and destructors
  39 + # Constructor
40 40 def __init__(self, *argv):
41 41 """
42 42 Constructor.
... ... @@ -60,25 +60,10 @@ class csmodelinfo(ctools.cscript):
60 60 self._free_color = "green"
61 61 self._fixed_color = "magenta"
62 62  
63   - # Initialise application
64   - if len(argv) == 0:
65   - ctools.cscript.__init__(self, self._name, self._version)
66   - elif len(argv) == 1:
67   - ctools.cscript.__init__(self, self._name, self._version, *argv)
68   - else:
69   - raise TypeError("Invalid number of arguments given.")
  63 + # Initialise application by calling the appropriate class
  64 + # constructor.
  65 + self._init_cscript(argv)
70 66  
71   - # Set logger properties
72   - self._log_header()
73   - self._log.date(True)
74   -
75   - # Return
76   - return
77   -
78   - def __del__(self):
79   - """
80   - Destructor.
81   - """
82 67 # Return
83 68 return
84 69  
... ...
cscripts/csmodelmerge.py
... ... @@ -35,7 +35,7 @@ class csmodelmerge(ctools.cscript):
35 35 a single model definition XML file.
36 36 """
37 37  
38   - # Constructors and destructors
  38 + # Constructor
39 39 def __init__(self, *argv):
40 40 """
41 41 Constructor.
... ... @@ -48,28 +48,13 @@ class csmodelmerge(ctools.cscript):
48 48 self._files = None
49 49 self._models = gammalib.GModels()
50 50  
51   - # Initialise application
52   - if len(argv) == 0:
53   - ctools.cscript.__init__(self, self._name, self._version)
54   - elif len(argv) == 1:
55   - ctools.cscript.__init__(self, self._name, self._version, *argv)
56   - else:
57   - raise TypeError("Invalid number of arguments given.")
58   -
59   - # Set logger properties
60   - self._log_header()
61   - self._log.date(True)
  51 + # Initialise application by calling the appropriate class
  52 + # constructor.
  53 + self._init_cscript(argv)
62 54  
63 55 # Return
64 56 return
65 57  
66   - def __del__(self):
67   - """
68   - Destructor.
69   - """
70   - # Return
71   - return
72   -
73 58  
74 59 # Private methods
75 60 def _get_parameters(self):
... ...
cscripts/csobs2caldb.py
... ... @@ -40,7 +40,7 @@ class csobs2caldb(ctools.cscript):
40 40 index of any observation using the hidden "index" parameter.
41 41 """
42 42  
43   - # Constructors and destructors
  43 + # Constructor
44 44 def __init__(self, *argv):
45 45 """
46 46 Constructor.
... ... @@ -59,28 +59,13 @@ class csobs2caldb(ctools.cscript):
59 59 self._base_dir = ""
60 60 self._cal_dir = ""
61 61  
62   - # Initialise application
63   - if len(argv) == 0:
64   - ctools.cscript.__init__(self, self._name, self._version)
65   - elif len(argv) == 1:
66   - ctools.cscript.__init__(self, self._name, self._version, *argv)
67   - else:
68   - raise TypeError("Invalid number of arguments given.")
69   -
70   - # Set logger properties
71   - self._log_header()
72   - self._log.date(True)
  62 + # Initialise application by calling the appropriate class
  63 + # constructor.
  64 + self._init_cscript(argv)
73 65  
74 66 # Return
75 67 return
76 68  
77   - def __del__(self):
78   - """
79   - Destructor.
80   - """
81   - # Return
82   - return
83   -
84 69  
85 70 # Private methods
86 71 def _get_parameters(self):
... ...
cscripts/csobsdef.py
... ... @@ -72,7 +72,7 @@ class csobsdef(ctools.cscript):
72 72 IRF in the "dummy" calibration database.
73 73 """
74 74  
75   - # Constructors and destructors
  75 + # Constructor
76 76 def __init__(self, *argv):
77 77 """
78 78 Constructor.
... ... @@ -89,25 +89,10 @@ class csobsdef(ctools.cscript):
89 89 self._clobber = True
90 90 self._debug = False
91 91  
92   - # Initialise application
93   - if len(argv) == 0:
94   - ctools.cscript.__init__(self, self._name, self._version)
95   - elif len(argv) ==1:
96   - ctools.cscript.__init__(self, self._name, self._version, *argv)
97   - else:
98   - raise TypeError("Invalid number of arguments given.")
  92 + # Initialise application by calling the appropriate class
  93 + # constructor.
  94 + self._init_cscript(argv)
99 95  
100   - # Set logger properties
101   - self._log_header()
102   - self._log.date(True)
103   -
104   - # Return
105   - return
106   -
107   - def __del__(self):
108   - """
109   - Destructor.
110   - """
111 96 # Return
112 97 return
113 98  
... ...
cscripts/csobsinfo.py
... ... @@ -31,7 +31,7 @@ class csobsinfo(ctools.cscript):
31 31 Shows the content of an observation container.
32 32 """
33 33  
34   - # Constructors and destructors
  34 + # Constructor
35 35 def __init__(self, *argv):
36 36 """
37 37 Constructor.
... ... @@ -51,33 +51,13 @@ class csobsinfo(ctools.cscript):
51 51 self._ebounds = gammalib.GEbounds()
52 52 self._gti = gammalib.GGti()
53 53  
54   - # Initialise observation
55   - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
56   - self._obs = argv[0]
57   - argv = argv[1:]
58   - else:
59   - self._obs = gammalib.GObservations()
60   - self._obs.clear()
  54 + # Initialise observation container from constructor arguments.
  55 + self._obs = self._set_input_obs(argv)
61 56  
62   - # Initialise application
63   - if len(argv) == 0:
64   - ctools.cscript.__init__(self, self._name, self._version)
65   - elif len(argv) == 1:
66   - ctools.cscript.__init__(self, self._name, self._version, *argv)
67   - else:
68   - raise TypeError("Invalid number of arguments given.")
  57 + # Initialise application by calling the appropriate class
  58 + # constructor.
  59 + self._init_cscript(argv)
69 60  
70   - # Set logger properties
71   - self._log_header()
72   - self._log.date(True)
73   -
74   - # Return
75   - return
76   -
77   - def __del__(self):
78   - """
79   - Destructor.
80   - """
81 61 # Return
82 62 return
83 63  
... ...
cscripts/cspull.py
... ... @@ -38,7 +38,7 @@ class cspull(ctools.cscript):
38 38 script behaves just as a regular ctool.
39 39 """
40 40  
41   - # Constructors and destructors
  41 + # Constructor
42 42 def __init__(self, *argv):
43 43 """
44 44 Constructor.
... ... @@ -62,28 +62,13 @@ class cspull(ctools.cscript):
62 62 self._proj = "TAN"
63 63 self._log_clients = False
64 64  
65   - # Initialise application
66   - if len(argv) == 0:
67   - ctools.cscript.__init__(self, self._name, self._version)
68   - elif len(argv) ==1:
69   - ctools.cscript.__init__(self, self._name, self._version, *argv)
70   - else:
71   - raise TypeError("Invalid number of arguments given.")
72   -
73   - # Set logger properties
74   - self._log_header()
75   - self._log.date(True)
  65 + # Initialise application by calling the appropriate class
  66 + # constructor.
  67 + self._init_cscript(argv)
76 68  
77 69 # Return
78 70 return
79 71  
80   - def __del__(self):
81   - """
82   - Destructor.
83   - """
84   - # Return
85   - return
86   -
87 72  
88 73 # Private methods
89 74 def _get_parameters(self):
... ...
cscripts/csresmap.py
... ... @@ -36,7 +36,7 @@ class csresmap(ctools.cscript):
36 36 behaves just as a regular ctool.
37 37 """
38 38  
39   - # Constructors and destructors
  39 + # Constructor
40 40 def __init__(self, *argv):
41 41 """
42 42 Constructor.
... ... @@ -74,32 +74,13 @@ class csresmap(ctools.cscript):
74 74 self._debug = False
75 75 self._log_clients = False
76 76  
77   - # Initialise observation
78   - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
79   - self._obs = argv[0]
80   - argv = argv[1:]
81   - else:
82   - self._obs = gammalib.GObservations()
83   -
84   - # Initialise application
85   - if len(argv) == 0:
86   - ctools.cscript.__init__(self, self._name, self._version)
87   - elif len(argv) ==1:
88   - ctools.cscript.__init__(self, self._name, self._version, *argv)
89   - else:
90   - raise TypeError("Invalid number of arguments given.")
91   -
92   - # Set logger properties
93   - self._log_header()
94   - self._log.date(True)
  77 + # Initialise observation container from constructor arguments.
  78 + self._obs = self._set_input_obs(argv)
95 79  
96   - # Return
97   - return
  80 + # Initialise application by calling the appropriate class
  81 + # constructor.
  82 + self._init_cscript(argv)
98 83  
99   - def __del__(self):
100   - """
101   - Destructor.
102   - """
103 84 # Return
104 85 return
105 86  
... ...
cscripts/cssens.py
... ... @@ -41,7 +41,7 @@ class cssens(ctools.cscript):
41 41 the likelihood difference between fitting with and without the test source.
42 42 """
43 43  
44   - # Constructors and destructors
  44 + # Constructor
45 45 def __init__(self, *argv):
46 46 """
47 47 Constructor.
... ... @@ -71,28 +71,13 @@ class cssens(ctools.cscript):
71 71 self._log_clients = False
72 72 self._debug = False
73 73  
74   - # Initialise application
75   - if len(argv) == 0:
76   - ctools.cscript.__init__(self, self._name, self._version)
77   - elif len(argv) ==1:
78   - ctools.cscript.__init__(self, self._name, self._version, *argv)
79   - else:
80   - raise TypeError("Invalid number of arguments given.")
81   -
82   - # Set logger properties
83   - self._log_header()
84   - self._log.date(True)
  74 + # Initialise application by calling the appropriate class
  75 + # constructor.
  76 + self._init_cscript(argv)
85 77  
86 78 # Return
87 79 return
88 80  
89   - def __del__(self):
90   - """
91   - Destructor.
92   - """
93   - # Return
94   - return
95   -
96 81  
97 82 # Private methods
98 83 def _get_parameters(self):
... ...
cscripts/csspec.py
... ... @@ -60,24 +60,12 @@ class csspec(ctools.cscript):
60 60 self._clobber = True
61 61 self._debug = False
62 62  
63   - # Initialise observation
64   - if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
65   - self._obs = argv[0]
66   - argv = argv[1:]
67   - else:
68   - self._obs = gammalib.GObservations()
69   -
70   - # Initialise application
71   - if len(argv) == 0:
72   - ctools.cscript.__init__(self, self._name, self._version)
73   - elif len(argv) ==1:
74   - ctools.cscript.__init__(self, self._name, self._version, *argv)
75   - else:
76   - raise TypeError("Invalid number of arguments given.")
  63 + # Initialise observation container from constructor arguments.
  64 + self._obs = self._set_input_obs(argv)
77 65  
78   - # Set logger properties
79   - self._log_header()
80   - self._log.date(True)
  66 + # Initialise application by calling the appropriate class
  67 + # constructor.
  68 + self._init_cscript(argv)
81 69  
82 70 # Return
83 71 return
... ...
cscripts/cstsdist.py
... ... @@ -39,7 +39,7 @@ class cstsdist(ctools.cscript):
39 39 script behaves just as a regular ctool.
40 40 """
41 41  
42   - # Constructors and destructors
  42 + # Constructor
43 43 def __init__(self, *argv):
44 44 """
45 45 Constructor.
... ... @@ -61,28 +61,13 @@ class cstsdist(ctools.cscript):
61 61 self._debug = False
62 62 self._log_clients = False
63 63  
64   - # Initialise application
65   - if len(argv) == 0:
66   - ctools.cscript.__init__(self, self._name, self._version)
67   - elif len(argv) ==1:
68   - ctools.cscript.__init__(self, self._name, self._version, *argv)
69   - else:
70   - raise TypeError("Invalid number of arguments given.")
71   -
72   - # Set logger properties
73   - self._log_header()
74   - self._log.date(True)
  64 + # Initialise application by calling the appropriate class
  65 + # constructor.
  66 + self._init_cscript(argv)
75 67  
76 68 # Return
77 69 return
78 70  
79   - def __del__(self):
80   - """
81   - Destructor.
82   - """
83   - # Return
84   - return
85   -
86 71  
87 72 # Private methods
88 73 def _get_parameters(self):
... ...
cscripts/cstsmapmerge.py
... ... @@ -33,7 +33,7 @@ class cstsmapmerge(ctools.cscript):
33 33 Merge Test Statistic maps.
34 34 """
35 35  
36   - # Constructors and destructors
  36 + # Constructor
37 37 def __init__(self, *argv):
38 38 """
39 39 Constructor.
... ... @@ -53,28 +53,13 @@ class cstsmapmerge(ctools.cscript):
53 53 self._overwrite = True
54 54 self._delete = False
55 55  
56   - # Initialise application
57   - if len(argv) == 0:
58   - ctools.cscript.__init__(self, self._name, self._version)
59   - elif len(argv) == 1:
60   - ctools.cscript.__init__(self, self._name, self._version, *argv)
61   - else:
62   - raise TypeError("Invalid number of arguments given.")
63   -
64   - # Set logger properties
65   - self._log_header()
66   - self._log.date(True)
  56 + # Initialise application by calling the appropriate class
  57 + # constructor.
  58 + self._init_cscript(argv)
67 59  
68 60 # Return
69 61 return
70 62  
71   - def __del__(self):
72   - """
73   - Destructor.
74   - """
75   - # Return
76   - return
77   -
78 63  
79 64 # Private methods
80 65 def _get_parameters(self):
... ...
pyext/ctool.i
... ... @@ -152,7 +152,7 @@ public:
152 152  
153 153  
154 154 /***********************************************************************//**
155   - * @brief ctool base class Python extension
  155 + * @brief ctool base class Python extensions
156 156 ***************************************************************************/
157 157 %extend ctool {
158 158 void _read_ahead(const bool& flag) {
... ... @@ -160,3 +160,35 @@ public:
160 160 return;
161 161 }
162 162 }
  163 +
  164 +/***********************************************************************//**
  165 + * @brief cscript base class Python extensions
  166 + ***************************************************************************/
  167 +%pythoncode %{
  168 +
  169 +# Initialise observation container from constructor arguments. In case that
  170 +# an observation container is provided as argument, this container will be
  171 +# used to initialise the class.
  172 +def _set_input_obs(self, argv):
  173 + if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
  174 + obs = argv[0]
  175 + argv = argv[1:]
  176 + else:
  177 + obs = gammalib.GObservations()
  178 + return obs
  179 +cscript._set_input_obs = _set_input_obs
  180 +
  181 +# Initialise application by calling the appropriate class constructor.
  182 +def _init_cscript(self, argv):
  183 + if len(argv) == 0:
  184 + cscript.__init__(self, self._name, self._version)
  185 + elif len(argv) == 1:
  186 + cscript.__init__(self, self._name, self._version, *argv)
  187 + else:
  188 + raise TypeError("Invalid number of arguments given.")
  189 + # Set logger properties
  190 + self._log_header()
  191 + self._log.date(True)
  192 +cscript._init_cscript = _init_cscript
  193 +
  194 +%}
... ...