diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index 5ccff4d..18e7ed9 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -31,7 +31,8 @@ testscripts = tests/__init__.py \ ../test/test_cslightcrv.py \ ../test/test_csfindobs.py \ ../test/test_csiactcopy.py \ - ../test/test_csiactdata.py + ../test/test_csiactdata.py \ + ../test/test_csiactobs.py # Test data to be distributed in $(pyexecdir)/cscripts/tests/data testdata = ../test/data/crab.xml \ diff --git a/cscripts/csiactobs.py b/cscripts/csiactobs.py index b5fbd90..c63e5ff 100755 --- a/cscripts/csiactobs.py +++ b/cscripts/csiactobs.py @@ -201,6 +201,11 @@ class csiactobs(ctools.cscript): self._subdir = os.path.dirname(self._hdu_index) self._debug = False # Debugging in client tools + # Write input parameters into logger + if self._logTerse(): + self._log_parameters() + self._log("\n") + # Return return @@ -345,11 +350,6 @@ class csiactobs(ctools.cscript): # Get parameters self._get_parameters() - # Write input parameters into logger - if self._logTerse(): - self._log_parameters() - self._log("\n") - # Clear models self._models.clear() @@ -618,7 +618,7 @@ class csiactobs(ctools.cscript): self._xml.save(self._outobs) # Save model XML file - self._models.save(self._outmodel, self._clobber()) + self._models.save(self._outmodel) # Return return diff --git a/test/test_csiactobs.py b/test/test_csiactobs.py new file mode 100755 index 0000000..06dfd4a --- /dev/null +++ b/test/test_csiactobs.py @@ -0,0 +1,210 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for the csiactobs 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 csiactobs script # +# =============================== # +class Test(gammalib.GPythonTestSuite): + """ + Test class for csiactobs script. + + This test class makes unit tests for the csiactobs 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("csiactobs") + + # Append tests + self.append(self._test_cmd, "Test csiactobs on command line") + self.append(self._test_python, "Test csiactobs from Python") + + # Return + return + + # Test csiactobs on command line + def _test_cmd(self): + """ + Test csiactobs on the command line. + """ + # Kluge to set the command (installed version has no README file) + if os.path.isfile("README"): + csiactobs = "../cscripts/csiactobs.py" + else: + csiactobs = "csiactobs" + + # Setup csiactobs command + cmd = csiactobs+' datapath="iactdata"'+ \ + ' prodname="unit-test"'+ \ + ' infile="runlist_cmd1.dat"'+ \ + ' bkgpars=1'+\ + ' outobs="obs_iactobs_cmd1.xml"'+ \ + ' outmodel="bgd_iactobs_cmd1.xml"'+ \ + ' logfile="csiactobs_cmd1.log" chatter=1' + + # Execute csiactobs, 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 csiactobs execution on command line") + + # Check observation definition XML file + self._check_obsdef("obs_iactobs_cmd1.xml") + + # Check model definition XML file + self._check_moddef("bgd_iactobs_cmd1.xml") + + # Setup csiactobs command + cmd = csiactobs+' datapath="data_path_that_does_not_exist"'+ \ + ' prodname="unit-test"'+ \ + ' infile="runlist_cmd1.dat"'+ \ + ' bkgpars=1'+\ + ' outobs="obs_iactobs_cmd2.xml"'+ \ + ' outmodel="bgd_iactobs_cmd2.xml"'+ \ + ' logfile="csiactobs_cmd2.log" chatter=1' + + # Execute csiactobs, 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 csiactobs execution on command line") + + # Return + return + + # Test csiactobs from Python + def _test_python(self): + """ + Test csiactobs from Python. + """ + # Set-up csiactobs + iactobs = cscripts.csiactobs() + iactobs["datapath"] = "iactdata" + iactobs["prodname"] = "unit-test" + iactobs["infile"] = "runlist_py1.dat" + iactobs["bkgpars"] = 1 + iactobs["outobs"] = "obs_iactobs_py1.xml" + iactobs["outmodel"] = "bgd_iactobs_py1.xml" + iactobs["logfile"] = "csiactobs_py1.log" + iactobs["chatter"] = 2 + + # Run csiactobs script and save run list + iactobs.logFileOpen() # Make sure we get a log file + iactobs.run() + iactobs.save() + + # Check observation definition XML file + self._check_obsdef("obs_iactobs_py1.xml") + + # Check model definition XML file + self._check_moddef("bgd_iactobs_py1.xml") + + # Return + return + + # Check observation definition XML file + def _check_obsdef(self, filename): + """ + Check observation definition XML file. + """ + # Load observation definition XML file + obs = gammalib.GObservations(filename) + + # Get response + rsp = obs[0].response() + + # Check number of observations + self.test_value(obs.size(), 1, + 'Check for single observation in XML file') + self.test_assert(obs[0].eventfile().file() == 'crab_events.fits.gz', + 'Check event file name') + self.test_assert(obs[0].eventfile().extname() == 'EVENTS', + 'Check event extension name') + self.test_assert(rsp.aeff().filename().file() == + 'irf_file.fits.gz', + 'Check effective area name') + self.test_assert(rsp.aeff().filename().extname() == + 'EFFECTIVE AREA', + 'Check effective area extension name') + self.test_assert(rsp.psf().filename().file() == + 'irf_file.fits.gz', + 'Check point spread function name') + self.test_assert(rsp.psf().filename().extname() == + 'POINT SPREAD FUNCTION', + 'Check point spread function extension name') + self.test_assert(rsp.edisp().filename().file() == + 'irf_file.fits.gz', + 'Check energy dispersion name') + self.test_assert(rsp.edisp().filename().extname() == + 'ENERGY DISPERSION', + 'Check energy dispersion extension name') + self.test_assert(rsp.background().filename().file() == + 'irf_file.fits.gz', + 'Check background name') + self.test_assert(rsp.background().filename().extname() == + 'BACKGROUND', + 'Check background extension name') + + # Return + return + + # Check model XML file + def _check_moddef(self, filename): + """ + Check model definition XML file. + """ + # Load model definition XML file + models = gammalib.GModels(filename) + + # Check number of models + self.test_value(models.size(), 1, + 'Check for single model in XML file') + + # Return + return diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py index 3b64691..e928cd9 100755 --- a/test/test_python_cscripts.py +++ b/test/test_python_cscripts.py @@ -29,6 +29,7 @@ import test_cslightcrv import test_csfindobs import test_csiactcopy import test_csiactdata +import test_csiactobs # ================== # @@ -100,16 +101,19 @@ def test(installed=False, debug=False): suite_csfindobs = test_csfindobs.Test() suite_csiactcopy = test_csiactcopy.Test() suite_csiactdata = test_csiactdata.Test() + suite_csiactobs = test_csiactobs.Test() # Setup unit tests suite_csfindobs.set() suite_csiactcopy.set() suite_csiactdata.set() + suite_csiactobs.set() # Append tests to container suites.append(suite_csfindobs) suites.append(suite_csiactcopy) suites.append(suite_csiactdata) + suites.append(suite_csiactobs) # Run test suite success = suites.run()