From 49470333fb941938df8805d6715bdca0faa847b9 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 23 Mar 2016 20:32:08 +0100 Subject: [PATCH] Add csobsdef script unit test. --- cscripts/Makefile.am | 4 +++- test/test_csobsdef.py | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_python_cscripts.py | 12 ++++++++---- 3 files changed, 201 insertions(+), 5 deletions(-) create mode 100755 test/test_csobsdef.py diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index d2dd60f..5e1bed8 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -27,10 +27,11 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ testscripts = tests/__init__.py \ ../test/test_python_cscripts.py \ ../test/test_cscaldb.py \ - ../test/test_csobs2caldb.py \ ../test/test_cslightcrv.py \ ../test/test_csmodelinfo.py \ ../test/test_csmodelmerge.py \ + ../test/test_csobs2caldb.py \ + ../test/test_csobsdef.py \ ../test/test_csfindobs.py \ ../test/test_csiactcopy.py \ ../test/test_csiactdata.py \ @@ -42,6 +43,7 @@ testdata = ../test/data/crab.xml \ ../test/data/crab_events.fits.gz \ ../test/data/crab_cntmap.fits.gz \ ../test/data/exclusion.reg \ + ../test/data/pntdef.dat \ ../test/data/models.txt \ ../test/data/model_cube_background.xml \ ../test/data/model_cube_background2.xml diff --git a/test/test_csobsdef.py b/test/test_csobsdef.py new file mode 100755 index 0000000..c199954 --- /dev/null +++ b/test/test_csobsdef.py @@ -0,0 +1,190 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for the csobsdef script. +# +# Copyright (C) 2016 Juergen Knoedlseder +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ========================================================================== +import os +import gammalib +import cscripts + + +# ============================== # +# Test class for csobsdef script # +# ============================== # +class Test(gammalib.GPythonTestSuite): + """ + Test class for csobsdef script. + + This test class makes unit tests for the csobsdef script by using it + from the command line and from Python. + """ + + # Constructor + def __init__(self): + """ + Constructor. + """ + # Call base class constructor + gammalib.GPythonTestSuite.__init__(self) + + # Return + return + + # Set test functions + def set(self): + """ + Set all test functions. + """ + # Set test name + self.name("csobsdef") + + # Append tests + self.append(self._test_cmd, "Test csobsdef on command line") + self.append(self._test_python, "Test csobsdef from Python") + + # Return + return + + # Test csobsdef on command line + def _test_cmd(self): + """ + Test csobsdef on the command line. + """ + # Kluge to set the command (installed version has no README file) + if os.path.isfile("README"): + csobsdef = "../cscripts/csobsdef.py" + else: + csobsdef = "csobsdef" + + # Setup csobsdef command + cmd = csobsdef+' inpnt="data/pntdef.dat"'+ \ + ' outobs="obsdef_cmd1.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' emin=0.1 emax=100.0 duration=1800.0 rad=5.0'+ \ + ' logfile="csobsdef_cmd1.log" chatter=1' + + # Execute csobsdef, make sure we catch any exception + try: + rc = os.system(cmd+" >/dev/null 2>&1") + except: + pass + + # Check if execution was successful + self.test_assert(rc == 0, + "Successful csobsdef execution on command line") + + # Check observation definition file + self._check_obsdef_file("obsdef_cmd1.xml") + + # Setup csobsdef command + cmd = csobsdef+' inpnt="obs_definition_that_does_not_exist.dat"'+ \ + ' outobs="obsdef_cmd1.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' emin=0.1 emax=100.0 duration=1800.0 rad=5.0'+ \ + ' logfile="csobsdef_cmd2.log"' + + # Execute csobsdef, make sure we catch any exception + try: + rc = os.system(cmd+" >/dev/null 2>&1") + except: + pass + + # Check if execution failed + self.test_assert(rc != 0, + "Failure of csobsdef execution on command line") + + # Return + return + + # Test csobsdef from Python + def _test_python(self): + """ + Test csobsdef from Python. + """ + # Set-up csobsdef + obsdef = cscripts.csobsdef() + obsdef["inpnt"] = "data/pntdef.dat" + obsdef["outobs"] = "obsdef_py1.xml" + obsdef["caldb"] = "prod2" + obsdef["irf"] = "South_0.5h" + obsdef["emin"] = 0.1 + obsdef["emax"] = 100.0 + obsdef["duration"] = 1800.0 + obsdef["rad"] = 5.0 + obsdef["logfile"] = "csobsdef_py1.log" + obsdef["chatter"] = 2 + + # Run csobsdef script and save XML file + obsdef.logFileOpen() # Make sure we get a log file + obsdef.run() + obsdef.save() + + # Check model file + self._check_obsdef_file("obsdef_py1.xml") + + # Set-up csobsdef + obsdef = cscripts.csobsdef() + obsdef["inpnt"] = "data/pntdef.dat" + obsdef["outobs"] = "obsdef_py2.xml" + obsdef["caldb"] = "prod2" + obsdef["irf"] = "South_0.5h" + obsdef["emin"] = 0.1 + obsdef["emax"] = 100.0 + obsdef["duration"] = 1800.0 + obsdef["rad"] = 5.0 + obsdef["logfile"] = "csobsdef_py2.log" + obsdef["chatter"] = 3 + + # Execute csobsdef script + obsdef.execute() + + # Check model file + self._check_obsdef_file("obsdef_py2.xml") + + # Return + return + + # Check observation definition file + def _check_obsdef_file(self, filename): + """ + Check observation definition file. + """ + # Load observation definition file + obs = gammalib.GObservations(filename) + + # Check number of observations + self.test_value(obs.size(), 3, + 'Check for 3 observations in observation definition file') + + # Loop over all observations + for o in obs: + self.test_assert(o.instrument() == "CTA", + 'Check for "CTA" instrument') + self.test_value(o.ontime(), 1800.0, 1.0e-6, + 'Check for ontime of 1800 sec') + self.test_value(o.deadc(), 0.95, 1.0e-6, + 'Check for deadtime correction of 0.95') + self.test_value(o.events().ebounds().emin().TeV(), 0.1, 1.0e-6, + 'Check for minimum energy of 0.1 TeV') + self.test_value(o.events().ebounds().emax().TeV(), 100.0, 1.0e-6, + 'Check for maximum energy of 100 TeV') + self.test_value(o.roi().radius(), 5.0, 1.0e-6, + 'Check for ROI radois of 5 deg') + + # Return + return diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py index e70b537..08ed09a 100755 --- a/test/test_python_cscripts.py +++ b/test/test_python_cscripts.py @@ -24,10 +24,11 @@ import gammalib import cscripts sys.path.append(os.path.dirname(os.path.abspath(__file__))) import test_cscaldb -import test_csobs2caldb import test_cslightcrv import test_csmodelinfo import test_csmodelmerge +import test_csobs2caldb +import test_csobsdef import test_csfindobs import test_csiactcopy import test_csiactdata @@ -81,24 +82,27 @@ def test(installed=False, debug=False): # Allocate test suites suite_cscaldb = test_cscaldb.Test() - suite_csobs2caldb = test_csobs2caldb.Test() suite_cslightcrv = test_cslightcrv.Test() suite_csmodelinfo = test_csmodelinfo.Test() suite_csmodelmerge = test_csmodelmerge.Test() + suite_csobs2caldb = test_csobs2caldb.Test() + suite_csobsdef = test_csobsdef.Test() # Setup unit tests suite_cscaldb.set() - suite_csobs2caldb.set() suite_cslightcrv.set() suite_csmodelinfo.set() suite_csmodelmerge.set() + suite_csobs2caldb.set() + suite_csobsdef.set() # Append tests to container suites.append(suite_cscaldb) - suites.append(suite_csobs2caldb) suites.append(suite_cslightcrv) suites.append(suite_csmodelinfo) suites.append(suite_csmodelmerge) + suites.append(suite_csobs2caldb) + suites.append(suite_csobsdef) # Append tests for Python 2.6+ (the IACT cscripts depend on the json # module which is only available since Python 2.6+ -- libgit2 0.21.4