Commit 49470333fb941938df8805d6715bdca0faa847b9

Authored by Jürgen Knödlseder
1 parent 87f0705c

Add csobsdef script unit test.

cscripts/Makefile.am
... ... @@ -27,10 +27,11 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \
27 27 testscripts = tests/__init__.py \
28 28 ../test/test_python_cscripts.py \
29 29 ../test/test_cscaldb.py \
30   - ../test/test_csobs2caldb.py \
31 30 ../test/test_cslightcrv.py \
32 31 ../test/test_csmodelinfo.py \
33 32 ../test/test_csmodelmerge.py \
  33 + ../test/test_csobs2caldb.py \
  34 + ../test/test_csobsdef.py \
34 35 ../test/test_csfindobs.py \
35 36 ../test/test_csiactcopy.py \
36 37 ../test/test_csiactdata.py \
... ... @@ -42,6 +43,7 @@ testdata = ../test/data/crab.xml \
42 43 ../test/data/crab_events.fits.gz \
43 44 ../test/data/crab_cntmap.fits.gz \
44 45 ../test/data/exclusion.reg \
  46 + ../test/data/pntdef.dat \
45 47 ../test/data/models.txt \
46 48 ../test/data/model_cube_background.xml \
47 49 ../test/data/model_cube_background2.xml
... ...
test/test_csobsdef.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csobsdef script.
  4 +#
  5 +# Copyright (C) 2016 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 +
  25 +
  26 +# ============================== #
  27 +# Test class for csobsdef script #
  28 +# ============================== #
  29 +class Test(gammalib.GPythonTestSuite):
  30 + """
  31 + Test class for csobsdef script.
  32 +
  33 + This test class makes unit tests for the csobsdef script by using it
  34 + from the command line and from Python.
  35 + """
  36 +
  37 + # Constructor
  38 + def __init__(self):
  39 + """
  40 + Constructor.
  41 + """
  42 + # Call base class constructor
  43 + gammalib.GPythonTestSuite.__init__(self)
  44 +
  45 + # Return
  46 + return
  47 +
  48 + # Set test functions
  49 + def set(self):
  50 + """
  51 + Set all test functions.
  52 + """
  53 + # Set test name
  54 + self.name("csobsdef")
  55 +
  56 + # Append tests
  57 + self.append(self._test_cmd, "Test csobsdef on command line")
  58 + self.append(self._test_python, "Test csobsdef from Python")
  59 +
  60 + # Return
  61 + return
  62 +
  63 + # Test csobsdef on command line
  64 + def _test_cmd(self):
  65 + """
  66 + Test csobsdef on the command line.
  67 + """
  68 + # Kluge to set the command (installed version has no README file)
  69 + if os.path.isfile("README"):
  70 + csobsdef = "../cscripts/csobsdef.py"
  71 + else:
  72 + csobsdef = "csobsdef"
  73 +
  74 + # Setup csobsdef command
  75 + cmd = csobsdef+' inpnt="data/pntdef.dat"'+ \
  76 + ' outobs="obsdef_cmd1.xml"'+ \
  77 + ' caldb="prod2" irf="South_0.5h"'+ \
  78 + ' emin=0.1 emax=100.0 duration=1800.0 rad=5.0'+ \
  79 + ' logfile="csobsdef_cmd1.log" chatter=1'
  80 +
  81 + # Execute csobsdef, make sure we catch any exception
  82 + try:
  83 + rc = os.system(cmd+" >/dev/null 2>&1")
  84 + except:
  85 + pass
  86 +
  87 + # Check if execution was successful
  88 + self.test_assert(rc == 0,
  89 + "Successful csobsdef execution on command line")
  90 +
  91 + # Check observation definition file
  92 + self._check_obsdef_file("obsdef_cmd1.xml")
  93 +
  94 + # Setup csobsdef command
  95 + cmd = csobsdef+' inpnt="obs_definition_that_does_not_exist.dat"'+ \
  96 + ' outobs="obsdef_cmd1.xml"'+ \
  97 + ' caldb="prod2" irf="South_0.5h"'+ \
  98 + ' emin=0.1 emax=100.0 duration=1800.0 rad=5.0'+ \
  99 + ' logfile="csobsdef_cmd2.log"'
  100 +
  101 + # Execute csobsdef, make sure we catch any exception
  102 + try:
  103 + rc = os.system(cmd+" >/dev/null 2>&1")
  104 + except:
  105 + pass
  106 +
  107 + # Check if execution failed
  108 + self.test_assert(rc != 0,
  109 + "Failure of csobsdef execution on command line")
  110 +
  111 + # Return
  112 + return
  113 +
  114 + # Test csobsdef from Python
  115 + def _test_python(self):
  116 + """
  117 + Test csobsdef from Python.
  118 + """
  119 + # Set-up csobsdef
  120 + obsdef = cscripts.csobsdef()
  121 + obsdef["inpnt"] = "data/pntdef.dat"
  122 + obsdef["outobs"] = "obsdef_py1.xml"
  123 + obsdef["caldb"] = "prod2"
  124 + obsdef["irf"] = "South_0.5h"
  125 + obsdef["emin"] = 0.1
  126 + obsdef["emax"] = 100.0
  127 + obsdef["duration"] = 1800.0
  128 + obsdef["rad"] = 5.0
  129 + obsdef["logfile"] = "csobsdef_py1.log"
  130 + obsdef["chatter"] = 2
  131 +
  132 + # Run csobsdef script and save XML file
  133 + obsdef.logFileOpen() # Make sure we get a log file
  134 + obsdef.run()
  135 + obsdef.save()
  136 +
  137 + # Check model file
  138 + self._check_obsdef_file("obsdef_py1.xml")
  139 +
  140 + # Set-up csobsdef
  141 + obsdef = cscripts.csobsdef()
  142 + obsdef["inpnt"] = "data/pntdef.dat"
  143 + obsdef["outobs"] = "obsdef_py2.xml"
  144 + obsdef["caldb"] = "prod2"
  145 + obsdef["irf"] = "South_0.5h"
  146 + obsdef["emin"] = 0.1
  147 + obsdef["emax"] = 100.0
  148 + obsdef["duration"] = 1800.0
  149 + obsdef["rad"] = 5.0
  150 + obsdef["logfile"] = "csobsdef_py2.log"
  151 + obsdef["chatter"] = 3
  152 +
  153 + # Execute csobsdef script
  154 + obsdef.execute()
  155 +
  156 + # Check model file
  157 + self._check_obsdef_file("obsdef_py2.xml")
  158 +
  159 + # Return
  160 + return
  161 +
  162 + # Check observation definition file
  163 + def _check_obsdef_file(self, filename):
  164 + """
  165 + Check observation definition file.
  166 + """
  167 + # Load observation definition file
  168 + obs = gammalib.GObservations(filename)
  169 +
  170 + # Check number of observations
  171 + self.test_value(obs.size(), 3,
  172 + 'Check for 3 observations in observation definition file')
  173 +
  174 + # Loop over all observations
  175 + for o in obs:
  176 + self.test_assert(o.instrument() == "CTA",
  177 + 'Check for "CTA" instrument')
  178 + self.test_value(o.ontime(), 1800.0, 1.0e-6,
  179 + 'Check for ontime of 1800 sec')
  180 + self.test_value(o.deadc(), 0.95, 1.0e-6,
  181 + 'Check for deadtime correction of 0.95')
  182 + self.test_value(o.events().ebounds().emin().TeV(), 0.1, 1.0e-6,
  183 + 'Check for minimum energy of 0.1 TeV')
  184 + self.test_value(o.events().ebounds().emax().TeV(), 100.0, 1.0e-6,
  185 + 'Check for maximum energy of 100 TeV')
  186 + self.test_value(o.roi().radius(), 5.0, 1.0e-6,
  187 + 'Check for ROI radois of 5 deg')
  188 +
  189 + # Return
  190 + return
... ...
test/test_python_cscripts.py
... ... @@ -24,10 +24,11 @@ import gammalib
24 24 import cscripts
25 25 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
26 26 import test_cscaldb
27   -import test_csobs2caldb
28 27 import test_cslightcrv
29 28 import test_csmodelinfo
30 29 import test_csmodelmerge
  30 +import test_csobs2caldb
  31 +import test_csobsdef
31 32 import test_csfindobs
32 33 import test_csiactcopy
33 34 import test_csiactdata
... ... @@ -81,24 +82,27 @@ def test(installed=False, debug=False):
81 82  
82 83 # Allocate test suites
83 84 suite_cscaldb = test_cscaldb.Test()
84   - suite_csobs2caldb = test_csobs2caldb.Test()
85 85 suite_cslightcrv = test_cslightcrv.Test()
86 86 suite_csmodelinfo = test_csmodelinfo.Test()
87 87 suite_csmodelmerge = test_csmodelmerge.Test()
  88 + suite_csobs2caldb = test_csobs2caldb.Test()
  89 + suite_csobsdef = test_csobsdef.Test()
88 90  
89 91 # Setup unit tests
90 92 suite_cscaldb.set()
91   - suite_csobs2caldb.set()
92 93 suite_cslightcrv.set()
93 94 suite_csmodelinfo.set()
94 95 suite_csmodelmerge.set()
  96 + suite_csobs2caldb.set()
  97 + suite_csobsdef.set()
95 98  
96 99 # Append tests to container
97 100 suites.append(suite_cscaldb)
98   - suites.append(suite_csobs2caldb)
99 101 suites.append(suite_cslightcrv)
100 102 suites.append(suite_csmodelinfo)
101 103 suites.append(suite_csmodelmerge)
  104 + suites.append(suite_csobs2caldb)
  105 + suites.append(suite_csobsdef)
102 106  
103 107 # Append tests for Python 2.6+ (the IACT cscripts depend on the json
104 108 # module which is only available since Python 2.6+
... ...