Commit 8d250ac361c7ab3ff51c64e1cd3b601fca9bba21

Authored by Jürgen Knödlseder
1 parent 72aad6b0

Remove code duplication is cslightcrv and csphasecrv

cscripts/cslightcrv.py
@@ -22,6 +22,7 @@ import sys @@ -22,6 +22,7 @@ import sys
22 import gammalib 22 import gammalib
23 import ctools 23 import ctools
24 from cscripts import obsutils 24 from cscripts import obsutils
  25 +from cscripts import ioutils
25 26
26 27
27 # ================ # 28 # ================ #
@@ -281,52 +282,24 @@ class cslightcrv(ctools.cscript): @@ -281,52 +282,24 @@ class cslightcrv(ctools.cscript):
281 table = gammalib.GFitsBinTable(nrows) 282 table = gammalib.GFitsBinTable(nrows)
282 table.extname('LIGHTCURVE') 283 table.extname('LIGHTCURVE')
283 284
284 - # Append time columns "MJD" and "e_MJD"  
285 - mjd = gammalib.GFitsTableDoubleCol('MJD', nrows)  
286 - e_mjd = gammalib.GFitsTableDoubleCol('e_MJD', nrows)  
287 - mjd.unit('days')  
288 - e_mjd.unit('days')  
289 - for i, result in enumerate(results):  
290 - mjd[i] = result['mjd']  
291 - e_mjd[i] = result['e_mjd']  
292 - table.append(mjd)  
293 - table.append(e_mjd)  
294 -  
295 - # Create parameter columns  
296 - for par in self._obs.models()[self._srcname]:  
297 - if par.is_free():  
298 - name = par.name()  
299 - e_name = 'e_'+par.name()  
300 - col = gammalib.GFitsTableDoubleCol(name, nrows)  
301 - e_col = gammalib.GFitsTableDoubleCol(e_name, nrows)  
302 - col.unit(par.unit())  
303 - e_col.unit(par.unit())  
304 - for i, result in enumerate(results):  
305 - col[i] = result['values'][name]  
306 - e_col[i] = result['values'][e_name]  
307 - table.append(col)  
308 - table.append(e_col) 285 + # Append time columns
  286 + ioutils.append_result_column(table, results, 'MJD', 'days', 'mjd')
  287 + ioutils.append_result_column(table, results, 'e_MJD', 'days', 'e_mjd')
  288 +
  289 + # Append parameter columns
  290 + ioutils.append_model_par_column(table, self._obs.models()[self._srcname],
  291 + results)
309 292
310 # Append Test Statistic column "TS" 293 # Append Test Statistic column "TS"
311 - ts = gammalib.GFitsTableDoubleCol('TS', nrows)  
312 - for i, result in enumerate(results):  
313 - ts[i] = result['ts']  
314 - table.append(ts)  
315 -  
316 - # Append upper limit column "UpperLimit"  
317 - ul_diff = gammalib.GFitsTableDoubleCol('DiffUpperLimit', nrows)  
318 - ul_flux = gammalib.GFitsTableDoubleCol('FluxUpperLimit', nrows)  
319 - ul_eflux = gammalib.GFitsTableDoubleCol('EFluxUpperLimit', nrows)  
320 - ul_diff.unit('ph/cm2/s/MeV')  
321 - ul_flux.unit('ph/cm2/s')  
322 - ul_eflux.unit('erg/cm2/s')  
323 - for i, result in enumerate(results):  
324 - ul_diff[i] = result['ul_diff']  
325 - ul_flux[i] = result['ul_flux']  
326 - ul_eflux[i] = result['ul_eflux']  
327 - table.append(ul_diff)  
328 - table.append(ul_flux)  
329 - table.append(ul_eflux) 294 + ioutils.append_result_column(table, results, 'TS', '', 'ts')
  295 +
  296 + # Append upper limit columns
  297 + ioutils.append_result_column(table, results, 'DiffUpperLimit',
  298 + 'ph/cm2/s/MeV', 'ul_diff')
  299 + ioutils.append_result_column(table, results, 'FluxUpperLimit',
  300 + 'ph/cm2/s', 'ul_flux')
  301 + ioutils.append_result_column(table, results, 'EFluxUpperLimit',
  302 + 'erg/cm2/s', 'ul_eflux')
330 303
331 # Return table 304 # Return table
332 return table 305 return table
cscripts/csphasecrv.py
@@ -22,6 +22,7 @@ import sys @@ -22,6 +22,7 @@ import sys
22 import gammalib 22 import gammalib
23 import ctools 23 import ctools
24 from cscripts import obsutils 24 from cscripts import obsutils
  25 +from cscripts import ioutils
25 26
26 27
27 # ================ # 28 # ================ #
@@ -183,7 +184,7 @@ class csphasecrv(ctools.cscript): @@ -183,7 +184,7 @@ class csphasecrv(ctools.cscript):
183 184
184 def _create_fits_table(self, results): 185 def _create_fits_table(self, results):
185 """ 186 """
186 - Creates FITS binary table containing light curve results 187 + Creates FITS binary table containing phase curve results
187 188
188 Parameters 189 Parameters
189 ---------- 190 ----------
@@ -193,7 +194,7 @@ class csphasecrv(ctools.cscript): @@ -193,7 +194,7 @@ class csphasecrv(ctools.cscript):
193 Returns 194 Returns
194 ------- 195 -------
195 table : `~gammalib.GFitsBinTable` 196 table : `~gammalib.GFitsBinTable`
196 - FITS binary table containing light curve 197 + FITS binary table containing phase curve
197 """ 198 """
198 # Determine number of rows in FITS table 199 # Determine number of rows in FITS table
199 nrows = len(results) 200 nrows = len(results)
@@ -202,29 +203,13 @@ class csphasecrv(ctools.cscript): @@ -202,29 +203,13 @@ class csphasecrv(ctools.cscript):
202 table = gammalib.GFitsBinTable(nrows) 203 table = gammalib.GFitsBinTable(nrows)
203 table.extname('PHASECURVE') 204 table.extname('PHASECURVE')
204 205
205 - # Append phase columns "PHASE_MIN" and "PHASE_MAX"  
206 - phmin = gammalib.GFitsTableDoubleCol('PHASE_MIN', nrows)  
207 - phmax = gammalib.GFitsTableDoubleCol('PHASE_MAX', nrows)  
208 - for i, result in enumerate(results):  
209 - phmin[i] = result['phmin']  
210 - phmax[i] = result['phmax']  
211 - table.append(phmin)  
212 - table.append(phmax) 206 + # Append phase columns
  207 + ioutils.append_result_column(table, results, 'PHASE_MIN', '', 'phmin')
  208 + ioutils.append_result_column(table, results, 'PHASE_MAX', '', 'phmax')
213 209
214 - # Create parameter columns  
215 - for par in self._obs.models()[self._srcname]:  
216 - if par.is_free():  
217 - name = par.name()  
218 - e_name = 'e_'+par.name()  
219 - col = gammalib.GFitsTableDoubleCol(name, nrows)  
220 - e_col = gammalib.GFitsTableDoubleCol(e_name, nrows)  
221 - col.unit(par.unit())  
222 - e_col.unit(par.unit())  
223 - for i, result in enumerate(results):  
224 - col[i] = result['values'][name]  
225 - e_col[i] = result['values'][e_name]  
226 - table.append(col)  
227 - table.append(e_col) 210 + # Append parameter columns
  211 + ioutils.append_model_par_column(table, self._obs.models()[self._srcname],
  212 + results)
228 213
229 # Return table 214 # Return table
230 return table 215 return table
@@ -401,7 +386,7 @@ class csphasecrv(ctools.cscript): @@ -401,7 +386,7 @@ class csphasecrv(ctools.cscript):
401 # Create FITS file 386 # Create FITS file
402 self._create_fits() 387 self._create_fits()
403 388
404 - # Optionally publish light curve 389 + # Optionally publish phase curve
405 if self['publish'].boolean(): 390 if self['publish'].boolean():
406 self.publish() 391 self.publish()
407 392
@@ -433,7 +418,7 @@ class csphasecrv(ctools.cscript): @@ -433,7 +418,7 @@ class csphasecrv(ctools.cscript):
433 # Write header 418 # Write header
434 self._log_header1(gammalib.TERSE, 'Publish phase curve') 419 self._log_header1(gammalib.TERSE, 'Publish phase curve')
435 420
436 - # Continue only if light curve is valid 421 + # Continue only if phase curve is valid
437 if self._fits.contains('PHASECURVE'): 422 if self._fits.contains('PHASECURVE'):
438 423
439 # Set default name is user name is empty 424 # Set default name is user name is empty
cscripts/ioutils.py
1 # ========================================================================== 1 # ==========================================================================
2 # Utility functions for input and output 2 # Utility functions for input and output
3 # 3 #
4 -# Copyright (C) 2016 Juergen Knoedlseder 4 +# Copyright (C) 2016-2017 Juergen Knoedlseder
5 # 5 #
6 # This program is free software: you can redistribute it and/or modify 6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by 7 # it under the terms of the GNU General Public License as published by
@@ -193,3 +193,89 @@ def get_args_options(options, usage): @@ -193,3 +193,89 @@ def get_args_options(options, usage):
193 193
194 # Return arguments and options 194 # Return arguments and options
195 return args, options 195 return args, options
  196 +
  197 +
  198 +# =========================================== #
  199 +# Append model parameter column to FITS table #
  200 +# =========================================== #
  201 +def append_result_column(table, results, name, unit, key):
  202 + """
  203 + Append result column to FITS table
  204 +
  205 + Parameters
  206 + ----------
  207 + table : `~gammalib.GFitsTable`
  208 + FITS table
  209 + results : list of dict
  210 + Parameter values
  211 + name : str
  212 + Column name
  213 + unit : str
  214 + Column unit
  215 + key : str
  216 + Result key
  217 + """
  218 + # Determine number of rows in FITS table
  219 + nrows = len(results)
  220 +
  221 + # Allocate double precision column and set units
  222 + col = gammalib.GFitsTableDoubleCol(name, nrows)
  223 + col.unit(unit)
  224 +
  225 + # Fill results in columns
  226 + for i, result in enumerate(results):
  227 + col[i] = result[key]
  228 +
  229 + # Append column to table
  230 + table.append(col)
  231 +
  232 + # Return
  233 + return
  234 +
  235 +
  236 +# =========================================== #
  237 +# Append model parameter column to FITS table #
  238 +# =========================================== #
  239 +def append_model_par_column(table, model, results):
  240 + """
  241 + Append model parameter column to FITS table
  242 +
  243 + Parameters
  244 + ----------
  245 + table : `~gammalib.GFitsTable`
  246 + FITS table
  247 + model : `~gammalib.GModel`
  248 + Model
  249 + results : list of dict
  250 + Parameter values
  251 + """
  252 + # Determine number of rows in FITS table
  253 + nrows = len(results)
  254 +
  255 + # Loop over all parameters in model
  256 + for par in model:
  257 +
  258 + # Append parameter column only if model parameter is free
  259 + if par.is_free():
  260 +
  261 + # Set column names
  262 + name = par.name()
  263 + e_name = 'e_'+par.name()
  264 +
  265 + # Allocate double precision columns and set units
  266 + col = gammalib.GFitsTableDoubleCol(name, nrows)
  267 + e_col = gammalib.GFitsTableDoubleCol(e_name, nrows)
  268 + col.unit(par.unit())
  269 + e_col.unit(par.unit())
  270 +
  271 + # Fill results in columns
  272 + for i, result in enumerate(results):
  273 + col[i] = result['values'][name]
  274 + e_col[i] = result['values'][e_name]
  275 +
  276 + # Append columns to table
  277 + table.append(col)
  278 + table.append(e_col)
  279 +
  280 + # Return
  281 + return
sonar-project.properties
@@ -21,7 +21,7 @@ sonar.cxx.missingIncludeWarnings=False @@ -21,7 +21,7 @@ sonar.cxx.missingIncludeWarnings=False
21 21
22 # Exclusions 22 # Exclusions
23 sonar.exclusions=cscripts/tests/**,pyext/build/**,pyext/ctools/tools.py,pyext/ctools/*_wrap.cpp,pyext/ctools/tests/**,dev/**,doc/**,test/dev/** 23 sonar.exclusions=cscripts/tests/**,pyext/build/**,pyext/ctools/tools.py,pyext/ctools/*_wrap.cpp,pyext/ctools/tests/**,dev/**,doc/**,test/dev/**
24 -sonar.coverage.exclusions=test/science_verification.py,test/test_examples.py 24 +sonar.coverage.exclusions=test/science_verification.py,test/test_examples.py,pyext/setup.py
25 sonar.cpd.exclusions=examples/**,test/**,pyext/ctools/tools.py 25 sonar.cpd.exclusions=examples/**,test/**,pyext/ctools/tools.py
26 26
27 # C++ configuration 27 # C++ configuration