diff --git a/.gitignore b/.gitignore index 711bd04..50d115b 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ test/*.dat test/*.xml test/*.reg test/*.trs +test/*.txt test/test_cscripts.sh test/reports/*.xml test/butterfly.txt diff --git a/ChangeLog b/ChangeLog index f46401d..01cadce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,7 @@ * Version 1.1.0 released ======================== - Add cscript unit tests. + Add ctools and cscripts unit tests. Add "bgdoversample" and "bgdinfill" parameters to csroot2caldb Add csobsdef.pntdef() method (#1771) Add ctmapcube tool (#1760) diff --git a/test/test_cscaldb.py b/test/test_cscaldb.py index 6f73c08..83d8a43 100755 --- a/test/test_cscaldb.py +++ b/test/test_cscaldb.py @@ -51,11 +51,11 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("cscaldb") + self.name('cscaldb') # Append tests - self.append(self._test_cmd, "Test cscaldb on command line") - self.append(self._test_python, "Test cscaldb from Python") + self.append(self._test_cmd, 'Test cscaldb on command line') + self.append(self._test_python, 'Test cscaldb from Python') # Return return @@ -66,23 +66,23 @@ class Test(gammalib.GPythonTestSuite): Test cscaldb on the command line. """ # Kluge to set the command (installed version has no README file) - if os.path.isfile("README"): - cscaldb = "../cscripts/cscaldb.py" + if os.path.isfile('README'): + cscaldb = '../cscripts/cscaldb.py' else: - cscaldb = "cscaldb" + cscaldb = 'cscaldb' # Setup cscaldb command cmd = cscaldb+' logfile="cscaldb_cmd1.log" chatter=1' # Execute cscaldb, make sure we catch any exception try: - rc = os.system(cmd+" >/dev/null 2>&1") + rc = os.system(cmd+' >/dev/null 2>&1') except: pass # Check if execution was successful self.test_assert(rc == 0, - "Successful cscaldb execution on command line") + 'Successful cscaldb execution on command line') # Return return @@ -94,8 +94,8 @@ class Test(gammalib.GPythonTestSuite): """ # Set-up cscaldb caldb = cscripts.cscaldb() - caldb["logfile"] = "cscaldb_py1.log" - caldb["chatter"] = 2 + caldb['logfile'] = 'cscaldb_py1.log' + caldb['chatter'] = 2 # Run script caldb.logFileOpen() # Make sure we get a log file diff --git a/test/test_csfindobs.py b/test/test_csfindobs.py index 9e10eed..c9da2c5 100755 --- a/test/test_csfindobs.py +++ b/test/test_csfindobs.py @@ -51,11 +51,11 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("csfindobs") + self.name('csfindobs') # Append tests - self.append(self._test_cmd, "Test csfindobs on command line") - self.append(self._test_python, "Test csfindobs from Python") + self.append(self._test_cmd, 'Test csfindobs on command line') + self.append(self._test_python, 'Test csfindobs from Python') # Return return @@ -66,10 +66,10 @@ class Test(gammalib.GPythonTestSuite): Test csfindobs on the command line. """ # Kluge to set the command (installed version has no README file) - if os.path.isfile("README"): - csfindobs = "../cscripts/csfindobs.py" + if os.path.isfile('README'): + csfindobs = '../cscripts/csfindobs.py' else: - csfindobs = "csfindobs" + csfindobs = 'csfindobs' # Setup csfindobs command cmd = csfindobs+' datapath="iactdata/"'+ \ @@ -80,16 +80,16 @@ class Test(gammalib.GPythonTestSuite): # Execute csfindobs, make sure we catch any exception try: - rc = os.system(cmd+" >/dev/null 2>&1") + rc = os.system(cmd+' >/dev/null 2>&1') except: pass # Check if execution was successful self.test_assert(rc == 0, - "Successful csfindobs execution on command line") + 'Successful csfindobs execution on command line') # Check run list - self._check_runlist("runlist_cmd1.dat") + self._check_runlist('runlist_cmd1.dat') # Setup csfindobs command cmd = csfindobs+' datapath="path_that_does_not_exist"'+ \ @@ -100,13 +100,13 @@ class Test(gammalib.GPythonTestSuite): # Execute csfindobs, make sure we catch any exception try: - rc = os.system(cmd+" >/dev/null 2>&1") + rc = os.system(cmd+' >/dev/null 2>&1') except: pass # Check if execution failed self.test_assert(rc != 0, - "Failure of csfindobs execution on command line") + 'Failure of csfindobs execution on command line') # Return return @@ -118,14 +118,14 @@ class Test(gammalib.GPythonTestSuite): """ # Set-up csfindobs findobs = cscripts.csfindobs() - findobs["datapath"] = "iactdata/" - findobs["prodname"] = "unit-test" - findobs["outfile"] = "runlist_py1.dat" - findobs["ra"] = 83.63 - findobs["dec"] = 22.01 - findobs["rad"] = 1.0 - findobs["logfile"] = "csfindobs_py1.log" - findobs["chatter"] = 2 + findobs['datapath'] = 'iactdata/' + findobs['prodname'] = 'unit-test' + findobs['outfile'] = 'runlist_py1.dat' + findobs['ra'] = 83.63 + findobs['dec'] = 22.01 + findobs['rad'] = 1.0 + findobs['logfile'] = 'csfindobs_py1.log' + findobs['chatter'] = 2 # Run csfindobs script and save run list findobs.logFileOpen() # Make sure we get a log file @@ -133,7 +133,7 @@ class Test(gammalib.GPythonTestSuite): findobs.save() # Check run list - self._check_runlist("runlist_py1.dat") + self._check_runlist('runlist_py1.dat') # Return return diff --git a/test/test_ctbkgcube.py b/test/test_ctbkgcube.py index 5455980..3a73c23 100755 --- a/test/test_ctbkgcube.py +++ b/test/test_ctbkgcube.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,12 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.bkg_model = "data/crab.xml" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -52,61 +47,125 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctbkgcube") + self.name('ctbkgcube') # Append tests - self.append(self.test_functional, "Test ctbkgcube functionality") + self.append(self._test_cmd, 'Test ctbkgcube on command line') + self.append(self._test_python, 'Test ctbkgcube from Python') # Return return - # Test ctbkgcube functionnality - def test_functional(self): + # Test ctbkgcube on command line + def _test_cmd(self): """ - Test ctbkgcube functionnality. + Test ctbkgcube on the command line. """ - # Set-up ctbkgcube - bkgcube = ctools.ctbkgcube() - bkgcube["inobs"] = self.events_name - bkgcube["inmodel"] = self.bkg_model - bkgcube["incube"] = "NONE" - bkgcube["outcube"] = "bkgcube.fits" - bkgcube["outmodel"] = "bkgcube.xml" - bkgcube["caldb"] = self.caldb - bkgcube["irf"] = self.irf - bkgcube["ebinalg"] = "LOG" - bkgcube["emin"] = 0.1 - bkgcube["emax"] = 100 - bkgcube["enumbins"] = 20 - bkgcube["nxpix"] = 10 - bkgcube["nypix"] = 10 - bkgcube["binsz"] = 0.4 - bkgcube["coordsys"] = "CEL" - bkgcube["proj"] = "CAR" - bkgcube["xref"] = 83.63 - bkgcube["yref"] = 22.01 - - # Run tool - self.test_try("Run ctbkgcube") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctbkgcube = '../src/ctbkgcube/ctbkgcube' + else: + ctbkgcube = 'ctbkgcube' + + # Setup ctbkgcube command + cmd = ctbkgcube+' inobs="data/crab_events.fits"'+ \ + ' inmodel="data/crab.xml"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctbkgcube_cmd1.fits"'+ \ + ' outmodel="ctbkgcube_cmd1.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=10 nypix=10 binsz=0.4'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' logfile="ctbkgcube_cmd1.log" chatter=1' + + # Execute ctbkgcube, make sure we catch any exception try: - bkgcube.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctbkgcube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctbkgcube." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctbkgcube execution on command line') + + # Check result file + self._check_result_file('ctbkgcube_cmd1.fits') - # Save background cube - self.test_try("Save background cube") + # Setup ctbkgcube command + cmd = ctbkgcube+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' inmodel="data/crab.xml"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctbkgcube_cmd2.fits"'+ \ + ' outmodel="ctbkgcube_cmd2.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=10 nypix=10 binsz=0.4'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' logfile="ctbkgcube_cmd2.log" chatter=1' + + # Execute ctbkgcube, make sure we catch any exception try: - bkgcube.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving background cube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving background cube." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctbkgcube execution on command line') + + # Return + return + + # Test ctbkgcube from Python + def _test_python(self): + """ + Test ctbkgcube from Python. + """ + # Set-up ctbkgcube + bkgcube = ctools.ctbkgcube() + bkgcube['inobs'] = 'data/crab_events.fits' + bkgcube['inmodel'] = 'data/crab.xml' + bkgcube['incube'] = 'NONE' + bkgcube['outcube'] = 'ctbkgcube_py1.fits' + bkgcube['outmodel'] = 'ctbkgcube_py1.xml' + bkgcube['caldb'] = 'prod2' + bkgcube['irf'] = 'South_0.5h' + bkgcube['ebinalg'] = 'LOG' + bkgcube['emin'] = 0.1 + bkgcube['emax'] = 100 + bkgcube['enumbins'] = 20 + bkgcube['nxpix'] = 10 + bkgcube['nypix'] = 10 + bkgcube['binsz'] = 0.4 + bkgcube['coordsys'] = 'CEL' + bkgcube['proj'] = 'CAR' + bkgcube['xref'] = 83.63 + bkgcube['yref'] = 22.01 + bkgcube['logfile'] = 'ctbkgcube_py1.log' + bkgcube['chatter'] = 2 + + # Run ctbkgcube tool + bkgcube.logFileOpen() # Make sure we get a log file + bkgcube.run() + bkgcube.save() + + # Check result file + self._check_result_file('ctbkgcube_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GCTACubeBackground(filename) + + # Check dimensions + self.test_value(len(result.elogmeans()), 20, 'Check for 20 energy bins') # Return return diff --git a/test/test_ctbutterfly.py b/test/test_ctbutterfly.py index 4516d25..d0ef5ae 100755 --- a/test/test_ctbutterfly.py +++ b/test/test_ctbutterfly.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,12 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.model_name = "data/crab.xml" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -52,51 +47,108 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctbutterfly") + self.name('ctbutterfly') # Append tests - self.append(self.test_functional, "Test ctbutterfly functionality") + self.append(self._test_cmd, 'Test ctbutterfly on command line') + self.append(self._test_python, 'Test ctbutterfly from Python') # Return return - # Test ctbutterfly functionnality - def test_functional(self): + # Test ctbutterfly on command line + def _test_cmd(self): """ - Test ctbutterfly functionnality. + Test ctbutterfly on the command line. """ - # Set-up ctbutterfly - butterfly = ctools.ctbutterfly() - butterfly["inobs"] = self.events_name - butterfly["inmodel"] = self.model_name - butterfly["srcname"] = "Crab" - butterfly["caldb"] = self.caldb - butterfly["irf"] = self.irf - butterfly["emin"] = 0.1 - butterfly["emax"] = 100 - butterfly["outfile"] = "butterfly.txt" - - # Run tool - self.test_try("Run ctbutterfly") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctbutterfly = '../src/ctbutterfly/ctbutterfly' + else: + ctbutterfly = 'ctbutterfly' + + # Setup ctbutterfly command + cmd = ctbutterfly+' inobs="data/crab_events.fits"'+\ + ' outfile="ctbutterfly_cmd1.txt"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' emin=0.1 emax=100.0'+ \ + ' logfile="ctbutterfly_cmd1.log" chatter=1' + + # Execute ctbutterfly, make sure we catch any exception try: - butterfly.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctbutterfly: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctbutterfly." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctbutterfly execution on command line') + + # Check result file + self._check_result_file('ctbutterfly_cmd1.txt') - # Save results - self.test_try("Save results") + # Setup ctbutterfly command + cmd = ctbutterfly+' inobs="event_file_that_does_not_exist.fits"'+\ + ' outfile="ctbutterfly_cmd2.txt"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' emin=0.1 emax=100.0'+ \ + ' logfile="ctbutterfly_cmd2.log" chatter=1' + + # Execute ctbutterfly, make sure we catch any exception try: - butterfly.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving butterfly diagram: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving butterfly diagram." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctbutterfly execution on command line') + + # Return + return + + # Test ctbutterfly from Python + def _test_python(self): + """ + Test ctbutterfly from Python. + """ + # Set-up ctbutterfly + butterfly = ctools.ctbutterfly() + butterfly['inobs'] = 'data/crab_events.fits' + butterfly['inmodel'] = 'data/crab.xml' + butterfly['srcname'] = 'Crab' + butterfly['caldb'] = 'prod2' + butterfly['irf'] = 'South_0.5h' + butterfly['emin'] = 0.1 + butterfly['emax'] = 100.0 + butterfly['outfile'] = 'ctbutterfly_py1.txt' + butterfly['logfile'] = 'ctbutterfly_py1.log' + butterfly['chatter'] = 2 + + # Run ctbutterfly tool + butterfly.logFileOpen() # Make sure we get a log file + butterfly.run() + butterfly.save() + + # Check result file + self._check_result_file('ctbutterfly_py1.txt') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file as CSV file + results = gammalib.GCsv(filename) + + # Check dimensions + self.test_value(results.nrows(), 100, 'Check for 100 rows in butterfly file') + self.test_value(results.ncols(), 4, 'Check for 4 columns in butterfly file') # Return return diff --git a/test/test_ctcubemask.py b/test/test_ctcubemask.py index 447ab41..e9a6e7e 100755 --- a/test/test_ctcubemask.py +++ b/test/test_ctcubemask.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,10 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.cntmap_name = "data/crab_cntmap.fits" - self.regfile = "data/exclusion.reg" - # Return return @@ -50,51 +47,108 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctcubemask") + self.name('ctcubemask') # Append tests - self.append(self.test_functional, "Test ctcubemask functionality") + self.append(self._test_cmd, 'Test ctcubemask on command line') + self.append(self._test_python, 'Test ctcubemask from Python') # Return return - # Test ctcubemask functionnality - def test_functional(self): + # Test ctcubemask on command line + def _test_cmd(self): """ - Test ctcubemask functionnality. + Test ctcubemask on the command line. """ - # Set-up ctcubemask - mask = ctools.ctcubemask() - mask["inobs"] = self.cntmap_name - mask["regfile"] = self.regfile - mask["outcube"] = "filtered_cntmap.fits" - mask["ra"] = 83.63 - mask["dec"] = 22.01 - mask["rad"] = 2.0 - mask["emin"] = 0.1 - mask["emax"] = 100 - - # Run tool - self.test_try("Run ctcubemask") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctcubemask = '../src/ctcubemask/ctcubemask' + else: + ctcubemask = 'ctcubemask' + + # Setup ctcubemask command + cmd = ctcubemask+' inobs="data/crab_cntmap.fits"'+ \ + ' regfile="data/exclusion.reg"'+ \ + ' outcube="ctcubemask_cmd1.fits"'+ \ + ' ra=83.63 dec=22.01 rad=2.0'+ \ + ' emin=0.1 emax=100.0'+ \ + ' logfile="ctcubemask_cmd1.log" chatter=1' + + # Execute ctcubemask, make sure we catch any exception try: - mask.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctcubemask: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctcubemask." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctcubemask execution on command line') + + # Check result file + self._check_result_file('ctcubemask_cmd1.fits') - # Save counts cube - self.test_try("Save counts cube") + # Setup ctcubemask command + cmd = ctcubemask+' inobs="counts_cube_that_does_not_exist.fits"'+ \ + ' regfile="data/exclusion.reg"'+ \ + ' outcube="ctcubemask_cmd2.fits"'+ \ + ' ra=83.63 dec=22.01 rad=2.0'+ \ + ' emin=0.1 emax=100.0'+ \ + ' logfile="ctcubemask_cmd2.log" chatter=2' + + # Execute ctcubemask, make sure we catch any exception try: - mask.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving counts cube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving counts cube." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctcubemask execution on command line') + + # Return + return + + # Test ctcubemask from Python + def _test_python(self): + """ + Test ctcubemask from Python. + """ + # Set-up ctcubemask + mask = ctools.ctcubemask() + mask['inobs'] = 'data/crab_cntmap.fits' + mask['regfile'] = 'data/exclusion.reg' + mask['outcube'] = 'ctcubemask_py1.fits' + mask['ra'] = 83.63 + mask['dec'] = 22.01 + mask['rad'] = 2.0 + mask['emin'] = 0.1 + mask['emax'] = 100.0 + mask['logfile'] = 'ctcubemask_py1.log' + mask['chatter'] = 2 + + # Run ctcubemask tool + mask.logFileOpen() # Make sure we get a log file + mask.run() + mask.save() + + # Check result file + self._check_result_file('ctcubemask_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Load counts cube + cube = gammalib.GCTAEventCube(filename) + + # Check counts cube + self.test_value(cube.size(), 800000, 'Check for 800000 cube bins') + self.test_value(cube.number(), 4921, 'Check for 4921 events') # Return return diff --git a/test/test_ctedispcube.py b/test/test_ctedispcube.py index e91afea..8b0dc3c 100755 --- a/test/test_ctedispcube.py +++ b/test/test_ctedispcube.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,11 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -51,61 +47,124 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctedispcube") + self.name('ctedispcube') # Append tests - self.append(self.test_functional, "Test ctedispcube functionality") + self.append(self._test_cmd, 'Test ctedispcube on command line') + self.append(self._test_python, 'Test ctedispcube from Python') # Return return - # Test ctedispcube functionnality - def test_functional(self): + # Test ctedispcube on command line + def _test_cmd(self): """ - Test ctedispcube functionnality. + Test ctedispcube on the command line. """ - # Set-up ctedispcube - edispcube = ctools.ctedispcube() - edispcube["inobs"] = self.events_name - edispcube["incube"] = "NONE" - edispcube["outcube"] = "edispcube.fits" - edispcube["caldb"] = self.caldb - edispcube["irf"] = self.irf - edispcube["ebinalg"] = "LOG" - edispcube["emin"] = 0.1 - edispcube["emax"] = 100 - edispcube["enumbins"] = 20 - edispcube["nxpix"] = 10 - edispcube["nypix"] = 10 - edispcube["binsz"] = 0.4 - edispcube["coordsys"] = "CEL" - edispcube["proj"] = "CAR" - edispcube["xref"] = 83.63 - edispcube["yref"] = 22.01 - edispcube["migramax"] = 2.0 - edispcube["migrabins"] = 10 - - # Run tool - self.test_try("Run ctedispcube") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctedispcube = '../src/ctedispcube/ctedispcube' + else: + ctedispcube = 'ctedispcube' + + # Setup ctedispcube command + cmd = ctedispcube+' inobs="data/crab_events.fits"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctedispcube_cmd1.fits"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=10 nypix=10 binsz=0.4'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' migramax=2.0 migrabins=10'+ \ + ' logfile="ctedispcube_cmd1.log" chatter=1' + + # Execute ctedispcube, make sure we catch any exception try: - edispcube.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctedispcube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctedispcube." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctedispcube execution on command line') + + # Check result file + self._check_result_file('ctedispcube_cmd1.fits') - # Save EDISP cube - self.test_try("Save EDISP cube") + # Setup ctedispcube command + cmd = ctedispcube+' inobs="events_that_do_not_exist.fits"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctedispcube_cmd2.fits"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=10 nypix=10 binsz=0.4'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' migramax=2.0 migrabins=10'+ \ + ' logfile="ctedispcube_cmd2.log" chatter=2' + + # Execute ctedispcube, make sure we catch any exception try: - edispcube.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving EDISP cube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving EDISP cubes." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctedispcube execution on command line') + + # Return + return + + # Test ctedispcube from Python + def _test_python(self): + """ + Test ctedispcube from Python. + """ + # Set-up ctedispcube + edispcube = ctools.ctedispcube() + edispcube['inobs'] = 'data/crab_events.fits' + edispcube['incube'] = 'NONE' + edispcube['outcube'] = 'ctedispcube_py1.fits' + edispcube['caldb'] = 'prod2' + edispcube['irf'] = 'South_0.5h' + edispcube['ebinalg'] = 'LOG' + edispcube['emin'] = 0.1 + edispcube['emax'] = 100 + edispcube['enumbins'] = 20 + edispcube['nxpix'] = 10 + edispcube['nypix'] = 10 + edispcube['binsz'] = 0.4 + edispcube['coordsys'] = 'CEL' + edispcube['proj'] = 'CAR' + edispcube['xref'] = 83.63 + edispcube['yref'] = 22.01 + edispcube['migramax'] = 2.0 + edispcube['migrabins'] = 10 + edispcube['logfile'] = 'ctedispcube_py1.log' + edispcube['chatter'] = 2 + + # Run ctedispcube tool + edispcube.logFileOpen() # Make sure we get a log file + edispcube.run() + edispcube.save() + + # Check result file + self._check_result_file('ctedispcube_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GCTACubeEdisp(filename) + + # Check dimensions + self.test_value(len(result.energies()), 21, 'Check for 21 energy maps') + self.test_value(len(result.migras()), 10, 'Check for 10 migration bins') # Return return diff --git a/test/test_cterror.py b/test/test_cterror.py index d7128a2..7693807 100755 --- a/test/test_cterror.py +++ b/test/test_cterror.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -53,49 +54,122 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("cterror") + self.name('cterror') # Append tests - self.append(self.test_functional, "Test cterror functionality") + self.append(self._test_cmd, 'Test cterror on command line') + self.append(self._test_python, 'Test cterror from Python') # Return return - # Test cttsmap functionnality - def test_functional(self): + # Test cterror on command line + def _test_cmd(self): """ - Test cterror functionnality. + Test cterror on the command line. """ - # Set-up cterror - error = ctools.cterror() - error["inobs"] = self.events_name - error["inmodel"] = self.model_name - error["outmodel"] = self.result_name - error["srcname"] = "Crab" - error["caldb"] = self.caldb - error["irf"] = self.irf - - # Run tool - self.test_try("Run cterror") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + cterror = '../src/cterror/cterror' + else: + cterror = 'cterror' + + # Setup cterror command + cmd = cterror+' inobs="data/crab_events.fits"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' outmodel="cterror_cmd1.xml"'+ \ + ' logfile="cterror_cmd1.log" chatter=1' + + # Execute cterror, make sure we catch any exception try: - error.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in cterror: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in cterror." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful cterror execution on command line') + + # Check result file + self._check_result_file('cterror_cmd1.xml') + + # Setup cterror command + cmd = cterror+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' outmodel="cterror_cmd2.xml"'+ \ + ' logfile="cterror_cmd2.log" chatter=1' - # Save results - self.test_try("Save results") + # Execute cterror, make sure we catch any exception try: - error.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving results: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving results." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of cterror execution on command line') + + # Return + return + + # Test cterror from Python + def _test_python(self): + """ + Test cterror from Python. + """ + # Set-up cterror + error = ctools.cterror() + error['inobs'] = 'data/crab_events.fits' + error['inmodel'] = 'data/crab.xml' + error['outmodel'] = 'cterror_py1.xml' + error['srcname'] = 'Crab' + error['caldb'] = 'prod2' + error['irf'] = 'South_0.5h' + error['logfile'] = 'cterror_py1.log' + error['chatter'] = 2 + + # Run cterror tool + error.logFileOpen() # Make sure we get a log file + error.run() + error.save() + + # Check result file + self._check_result_file('cterror_py1.xml') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GModels(filename) + + # Check results + self.test_value(result['Crab']['Prefactor'].value(), 1.58907e-16, + 1.0e-3, 'Check fitted Crab Prefactor') + self.test_value(result['Crab']['Prefactor'].error(), 0.0529105e-16, + 1.0e-3, 'Check Crab Prefactor error') + self.test_value(result['Crab']['Index'].value(), -2.43549, + 1.0e-3, 'Check fitted Crab Index') + self.test_value(result['Crab']['Index'].error(), 0.027804, + 1.0e-3, 'Check Crab Index error') + self.test_value(result['Background']['Prefactor'].value(), 61.6919e-6, + 1.0e-3, 'Check fitted background Prefactor') + self.test_value(result['Background']['Prefactor'].error(), 1.49438e-6, + 1.0e-3, 'Check background Prefactor error') + self.test_value(result['Background']['Index'].value(), -2.20535, + 1.0e-3, 'Check fitted background Index') + self.test_value(result['Background']['Index'].error(), 0.0113269, + 1.0e-3, 'Check background Index error') + self.test_value(result['Background']['Sigma'].value(), 3.04252, + 1.0e-3, 'Check fitted background Sigma') + self.test_value(result['Background']['Sigma'].error(), 0.0307008, + 1.0e-3, 'Check background Sigma error') # Return return diff --git a/test/test_ctexpcube.py b/test/test_ctexpcube.py index 76bf41e..6940624 100755 --- a/test/test_ctexpcube.py +++ b/test/test_ctexpcube.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,11 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -51,59 +47,119 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctexpcube") + self.name('ctexpcube') # Append tests - self.append(self.test_functional, "Test ctexpcube functionality") + self.append(self._test_cmd, 'Test ctexpcube on command line') + self.append(self._test_python, 'Test ctexpcube from Python') # Return return - # Test ctexpcube functionnality - def test_functional(self): + # Test ctexpcube on command line + def _test_cmd(self): """ - Test ctexpcube functionnality. + Test ctexpcube on the command line. """ - # Set-up ctexpcube - expcube = ctools.ctexpcube() - expcube["inobs"] = self.events_name - expcube["incube"] = "NONE" - expcube["outcube"] = "expcube.fits" - expcube["caldb"] = self.caldb - expcube["irf"] = self.irf - expcube["ebinalg"] = "LOG" - expcube["emin"] = 0.1 - expcube["emax"] = 100 - expcube["enumbins"] = 20 - expcube["nxpix"] = 200 - expcube["nypix"] = 200 - expcube["binsz"] = 0.02 - expcube["coordsys"] = "CEL" - expcube["proj"] = "CAR" - expcube["xref"] = 83.63 - expcube["yref"] = 22.01 - - # Run tool - self.test_try("Run ctexpcube") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctexpcube = '../src/ctexpcube/ctexpcube' + else: + ctexpcube = 'ctexpcube' + + # Setup ctexpcube command + cmd = ctexpcube+' inobs="data/crab_events.fits"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctexpcube_cmd1.fits"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=200 nypix=200 binsz=0.02'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' logfile="ctexpcube_cmd1.log" chatter=1' + + # Execute ctexpcube, make sure we catch any exception try: - expcube.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctexpcube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctexpcube." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctexpcube execution on command line') + + # Check result file + self._check_result_file('ctexpcube_cmd1.fits') - # Save exposure cube - self.test_try("Save exposure cube") + # Setup ctexpcube command + cmd = ctexpcube+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctexpcube_cmd2.fits"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=200 nypix=200 binsz=0.02'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' logfile="ctexpcube_cmd2.log" chatter=1' + + # Execute ctexpcube, make sure we catch any exception try: - expcube.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving exposure cube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving exposure cube." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctexpcube execution on command line') + + # Return + return + + # Test ctexpcube from Python + def _test_python(self): + """ + Test ctexpcube from Python. + """ + # Set-up ctexpcube + expcube = ctools.ctexpcube() + expcube['inobs'] = 'data/crab_events.fits' + expcube['incube'] = 'NONE' + expcube['outcube'] = 'ctexpcube_py1.fits' + expcube['caldb'] = 'prod2' + expcube['irf'] = 'South_0.5h' + expcube['ebinalg'] = 'LOG' + expcube['emin'] = 0.1 + expcube['emax'] = 100 + expcube['enumbins'] = 20 + expcube['nxpix'] = 200 + expcube['nypix'] = 200 + expcube['binsz'] = 0.02 + expcube['coordsys'] = 'CEL' + expcube['proj'] = 'CAR' + expcube['xref'] = 83.63 + expcube['yref'] = 22.01 + expcube['logfile'] = 'ctexpcube_py1.log' + expcube['chatter'] = 2 + + # Run ctexpcube tool + expcube.logFileOpen() # Make sure we get a log file + expcube.run() + expcube.save() + + # Check result file + self._check_result_file('ctexpcube_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GCTACubeExposure(filename) + + # Check dimensions + self.test_value(len(result.elogmeans()), 20, 'Check for 20 energy bins') # Return return diff --git a/test/test_ctlike.py b/test/test_ctlike.py index 7d66653..df82b05 100755 --- a/test/test_ctlike.py +++ b/test/test_ctlike.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,12 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.model_name = "data/crab.xml" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -52,48 +47,121 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctlike") + self.name('ctlike') # Append tests - self.append(self.test_functional, "Test ctlike functionality") + self.append(self._test_cmd, 'Test ctlike on command line') + self.append(self._test_python, 'Test ctlike from Python') # Return return - # Test ctlike functionnality - def test_functional(self): + # Test ctlike on command line + def _test_cmd(self): """ - Test ctlike functionnality. + Test ctlike on the command line. """ - # Set-up ctlike - like = ctools.ctlike() - like["inobs"] = self.events_name - like["inmodel"] = self.model_name - like["caldb"] = self.caldb - like["irf"] = self.irf - like["outmodel"] = "result.xml" - - # Run tool - self.test_try("Run ctlike") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctlike = '../src/ctlike/ctlike' + else: + ctlike = 'ctlike' + + # Setup ctlike command + cmd = ctlike+' inobs="data/crab_events.fits"'+ \ + ' inmodel="data/crab.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' outmodel="ctlike_cmd1.xml"'+ \ + ' logfile="ctlike_cmd1.log" chatter=1' + + # Execute ctlike, make sure we catch any exception try: - like.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctlike: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctlike." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctlike execution on command line') + + # Check result file + self._check_result_file('ctlike_cmd1.xml') - # Save results - self.test_try("Save results") + # Setup ctlike command + cmd = ctlike+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' inmodel="data/crab.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' outmodel="ctlike_cmd2.xml"'+ \ + ' logfile="ctlike_cmd2.log" chatter=1' + + # Execute ctlike, make sure we catch any exception try: - like.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving results: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving results." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctlike execution on command line') + + # Return + return + + # Test ctlike from Python + def _test_python(self): + """ + Test ctlike from Python. + """ + # Set-up ctlike + like = ctools.ctlike() + like['inobs'] = 'data/crab_events.fits' + like['inmodel'] = 'data/crab.xml' + like['caldb'] = 'prod2' + like['irf'] = 'South_0.5h' + like['outmodel'] = 'ctlike_py1.xml' + like['logfile'] = 'ctlike_py1.log' + like['chatter'] = 2 + + # Run ctlike tool + like.logFileOpen() # Make sure we get a log file + like.run() + like.save() + + # Check result file + self._check_result_file('ctlike_py1.xml') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GModels(filename) + + # Check results + self.test_value(result['Crab']['Prefactor'].value(), 1.58907e-16, + 1.0e-3, 'Check fitted Crab Prefactor') + self.test_value(result['Crab']['Prefactor'].error(), 0.0526982e-16, + 1.0e-3, 'Check Crab Prefactor error') + self.test_value(result['Crab']['Index'].value(), -2.43549, + 1.0e-3, 'Check fitted Crab Index') + self.test_value(result['Crab']['Index'].error(), 0.0248116, + 1.0e-3, 'Check Crab Index error') + self.test_value(result['Background']['Prefactor'].value(), 61.6919e-6, + 1.0e-3, 'Check fitted background Prefactor') + self.test_value(result['Background']['Prefactor'].error(), 1.49438e-6, + 1.0e-3, 'Check background Prefactor error') + self.test_value(result['Background']['Index'].value(), -2.20535, + 1.0e-3, 'Check fitted background Index') + self.test_value(result['Background']['Index'].error(), 0.0113269, + 1.0e-3, 'Check background Index error') + self.test_value(result['Background']['Sigma'].value(), 3.04252, + 1.0e-3, 'Check fitted background Sigma') + self.test_value(result['Background']['Sigma'].error(), 0.0307008, + 1.0e-3, 'Check background Sigma error') # Return return diff --git a/test/test_ctmodel.py b/test/test_ctmodel.py index 7497183..3bacae3 100755 --- a/test/test_ctmodel.py +++ b/test/test_ctmodel.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,11 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.model_name = "data/crab.xml" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -51,67 +47,139 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctmodel") + self.name('ctmodel') # Append tests - self.append(self.test_functional, "Test ctmodel functionality") + self.append(self._test_cmd, 'Test ctmodel on command line') + self.append(self._test_python, 'Test ctmodel from Python') # Return return - # Test ctmodel functionnality - def test_functional(self): + # Test ctmodel on command line + def _test_cmd(self): """ - Test ctmodel functionnality. + Test ctmodel on the command line. """ - # Set-up ctmodel from scratch - model = ctools.ctmodel() - model["incube"] = "NONE" - model["outcube"] = "modmap.fits" - model["inmodel"] = self.model_name - model["inobs"] = "NONE" - model["expcube"] = "NONE" - model["psfcube"] = "NONE" - model["bkgcube"] = "NONE" - model["caldb"] = self.caldb - model["irf"] = self.irf - model["rad"] = 5 - model["ra"] = 83.63 - model["dec"] = 22.01 - model["tmin"] = 0 - model["tmax"] = 1800 - model["emin"] = 0.1 - model["emax"] = 100 - model["enumbins"] = 20 - model["nxpix"] = 200 - model["nypix"] = 200 - model["binsz"] = 0.02 - model["coordsys"] = "CEL" - model["proj"] = "CAR" - model["xref"] = 83.63 - model["yref"] = 22.01 - - # Run tool - self.test_try("Run ctmodel") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctmodel = '../src/ctmodel/ctmodel' + else: + ctmodel = 'ctmodel' + + # Setup ctmodel command + cmd = ctmodel+' incube="NONE" inobs="NONE" expcube="NONE"'+\ + ' psfcube="NONE" bkgcube="NONE"'+ \ + ' outcube="ctmodel_cmd1.fits"'+ \ + ' inmodel="data/crab.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' rad=5.0 ra=83.63 dec=22.01 tmin=0.0 tmax=1800'+ \ + ' emin=0.1 emax=100.0 enumbins=20 nxpix=200 nypix=200'+ \ + ' binsz=0.02 coordsys="CEL" proj="CAR"'+ \ + ' xref=83.63 yref=22.01'+ \ + ' logfile="ctmodel_cmd1.log" chatter=1' + + # Execute ctmodel, make sure we catch any exception try: - model.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctmodel: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctmodel." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctmodel execution on command line') + + # Check result file + self._check_result_file('ctmodel_cmd1.fits') - # Save counts cube - self.test_try("Save model cube") + # Setup ctmodel command + cmd = ctmodel+' incube="NONE" inobs="NONE" expcube="NONE"'+\ + ' psfcube="NONE" bkgcube="NONE"'+ \ + ' outcube="ctmodel_cmd2.fits"'+ \ + ' inmodel="model_that_does_not_exist.xml"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' rad=5.0 ra=83.63 dec=22.01 tmin=0.0 tmax=1800'+ \ + ' emin=0.1 emax=100.0 enumbins=20 nxpix=200 nypix=200'+ \ + ' binsz=0.02 coordsys="CEL" proj="CAR"'+ \ + ' xref=83.63 yref=22.01'+ \ + ' logfile="ctmodel_cmd2.log" chatter=1' + + # Execute ctmodel, make sure we catch any exception try: - model.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving model cube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving model cube." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctmodel execution on command line') + + # Return + return + + # Test ctmodel from Python + def _test_python(self): + """ + Test ctmodel from Python. + """ + # Set-up ctmodel from scratch + model = ctools.ctmodel() + model['incube'] = 'NONE' + model['outcube'] = 'ctmodel_py1.fits' + model['inmodel'] = 'data/crab.xml' + model['inobs'] = 'NONE' + model['expcube'] = 'NONE' + model['psfcube'] = 'NONE' + model['bkgcube'] = 'NONE' + model['caldb'] = 'prod2' + model['irf'] = 'South_0.5h' + model['rad'] = 5 + model['ra'] = 83.63 + model['dec'] = 22.01 + model['tmin'] = 0 + model['tmax'] = 1800 + model['emin'] = 0.1 + model['emax'] = 100 + model['enumbins'] = 20 + model['nxpix'] = 200 + model['nypix'] = 200 + model['binsz'] = 0.02 + model['coordsys'] = 'CEL' + model['proj'] = 'CAR' + model['xref'] = 83.63 + model['yref'] = 22.01 + model['logfile'] = 'ctmodel_py1.log' + model['chatter'] = 2 + + # Run ctmodel tool + model.logFileOpen() # Make sure we get a log file + model.run() + model.save() + + # Check result file + self._check_result_file('ctmodel_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + fits = gammalib.GFits(filename) + + # Get HDUs + cube = fits['Primary'] + ebounds = fits['EBOUNDS'] + gti = fits['GTI'] + + # Check dimensions + self.test_value(cube.naxis(), 3, 'Check for 3 cube dimensions') + self.test_value(cube.naxes(0), 200, 'Check for 200 pixels in X') + self.test_value(cube.naxes(1), 200, 'Check for 200 pixels in Y') + self.test_value(cube.naxes(2), 20, 'Check for 20 pixels in Z') # Return return diff --git a/test/test_ctpsfcube.py b/test/test_ctpsfcube.py index 80802bb..d6c3483 100755 --- a/test/test_ctpsfcube.py +++ b/test/test_ctpsfcube.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,11 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -51,61 +47,123 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctpsfcube") + self.name('ctpsfcube') # Append tests - self.append(self.test_functional, "Test ctpsfcube functionality") + self.append(self._test_cmd, 'Test ctpsfcube on command line') + self.append(self._test_python, 'Test ctpsfcube from Python') # Return return - # Test ctpsfcube functionnality - def test_functional(self): + # Test ctpsfcube on command line + def _test_cmd(self): """ - Test ctpsfcube functionnality. + Test ctpsfcube on the command line. """ - # Set-up ctpsfcube - psfcube = ctools.ctpsfcube() - psfcube["inobs"] = self.events_name - psfcube["incube"] = "NONE" - psfcube["outcube"] = "psfcube.fits" - psfcube["caldb"] = self.caldb - psfcube["irf"] = self.irf - psfcube["ebinalg"] = "LOG" - psfcube["emin"] = 0.1 - psfcube["emax"] = 100 - psfcube["enumbins"] = 20 - psfcube["nxpix"] = 10 - psfcube["nypix"] = 10 - psfcube["binsz"] = 0.4 - psfcube["coordsys"] = "CEL" - psfcube["proj"] = "CAR" - psfcube["xref"] = 83.63 - psfcube["yref"] = 22.01 - psfcube["amax"] = 0.3 - psfcube["anumbins"] = 10 - - # Run tool - self.test_try("Run ctpsfcube") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctpsfcube = '../src/ctpsfcube/ctpsfcube' + else: + ctpsfcube = 'ctpsfcube' + + # Setup ctpsfcube command + cmd = ctpsfcube+' inobs="data/crab_events.fits"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctpsfcube_cmd1.fits"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=200 nypix=200 binsz=0.02'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' amax=0.3 anumbins=10'+ \ + ' logfile="ctpsfcube_cmd1.log" chatter=1' + + # Execute ctpsfcube, make sure we catch any exception try: - psfcube.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctpsfcube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctpsfcube." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctpsfcube execution on command line') + + # Check result file + self._check_result_file('ctpsfcube_cmd1.fits') - # Save PSF cube - self.test_try("Save PSF cube") + # Setup ctpsfcube command + cmd = ctpsfcube+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' incube="NONE"'+ \ + ' outcube="ctpsfcube_cmd2.fits"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' ebinalg="LOG" emin=0.1 emax=100.0 enumbins=20'+ \ + ' nxpix=200 nypix=200 binsz=0.02'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' amax=0.3 anumbins=10'+ \ + ' logfile="ctpsfcube_cmd2.log" chatter=1' + + # Execute ctpsfcube, make sure we catch any exception try: - psfcube.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving PSF cube: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving PSF cubes." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctpsfcube execution on command line') + + # Return + return + + # Test ctpsfcube from Python + def _test_python(self): + """ + Test ctpsfcube from Python. + """ + # Set-up ctpsfcube + psfcube = ctools.ctpsfcube() + psfcube['inobs'] = 'data/crab_events.fits' + psfcube['incube'] = 'NONE' + psfcube['outcube'] = 'ctpsfcube_py1.fits' + psfcube['caldb'] = 'prod2' + psfcube['irf'] = 'South_0.5h' + psfcube['ebinalg'] = 'LOG' + psfcube['emin'] = 0.1 + psfcube['emax'] = 100 + psfcube['enumbins'] = 20 + psfcube['nxpix'] = 10 + psfcube['nypix'] = 10 + psfcube['binsz'] = 0.4 + psfcube['coordsys'] = 'CEL' + psfcube['proj'] = 'CAR' + psfcube['xref'] = 83.63 + psfcube['yref'] = 22.01 + psfcube['amax'] = 0.3 + psfcube['anumbins'] = 10 + psfcube['logfile'] = 'ctpsfcube_py1.log' + psfcube['chatter'] = 2 + + # Run ctpsfcube tool + psfcube.logFileOpen() # Make sure we get a log file + psfcube.run() + psfcube.save() + + # Check result file + self._check_result_file('ctpsfcube_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GCTACubePsf(filename) + + # Check dimensions + self.test_value(len(result.elogmeans()), 20, 'Check for 20 energy bins') # Return return diff --git a/test/test_ctselect.py b/test/test_ctselect.py index cb621b2..1ac97bd 100755 --- a/test/test_ctselect.py +++ b/test/test_ctselect.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,9 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - # Return return @@ -49,52 +47,114 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctselect") + self.name('ctselect') # Append tests - self.append(self.test_functional, "Test ctselect functionality") + self.append(self._test_cmd, 'Test ctselect on command line') + self.append(self._test_python, 'Test ctselect from Python') # Return return - # Test ctselect functionnality - def test_functional(self): + # Test ctselect on command line + def _test_cmd(self): """ - Test ctselect functionnality. + Test ctselect on the command line. """ - # Set-up ctselect - select = ctools.ctselect() - select["inobs"] = self.events_name - select["outobs"] = "selected_events.fits" - select["ra"] = 83.63 - select["dec"] = 22.01 - select["rad"] = 3 - select["tmin"] = 0 - select["tmax"] = 1800 - select["emin"] = 0.1 - select["emax"] = 100 - - # Run tool - self.test_try("Run ctselect") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctselect = '../src/ctselect/ctselect' + else: + ctselect = 'ctselect' + + # Setup ctselect command + cmd = ctselect+' inobs="data/crab_events.fits"'+ \ + ' outobs="ctselect_cmd1.fits"'+ \ + ' ra=83.63 dec=22.01 rad=3.0'+ \ + ' tmin=0.0 tmax=1800.0'+ \ + ' emin=0.1 emax=100.0'+ \ + ' logfile="ctselect_cmd1.log" chatter=1' + + # Execute ctselect, make sure we catch any exception try: - select.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctselect: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctselect." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctselect execution on command line') + + # Check result file + self._check_result_file('ctselect_cmd1.fits') - # Save events - self.test_try("Save events") + # Setup ctselect command + cmd = ctselect+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' outobs="ctselect_cmd2.fits"'+ \ + ' ra=83.63 dec=22.01 rad=3.0'+ \ + ' tmin=0.0 tmax=1800.0'+ \ + ' emin=0.1 emax=100.0'+ \ + ' logfile="ctselect_cmd2.log" chatter=1' + + # Execute ctselect, make sure we catch any exception try: - select.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving events: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving events." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctselect execution on command line') + + # Return + return + + # Test ctselect from Python + def _test_python(self): + """ + Test ctselect from Python. + """ + # Set-up ctselect + select = ctools.ctselect() + select['inobs'] = 'data/crab_events.fits' + select['outobs'] = 'ctselect_py1.fits' + select['ra'] = 83.63 + select['dec'] = 22.01 + select['rad'] = 3 + select['tmin'] = 0 + select['tmax'] = 1800 + select['emin'] = 0.1 + select['emax'] = 100 + select['logfile'] = 'ctselect_py1.log' + select['chatter'] = 2 + + # Run ctselect tool + select.logFileOpen() # Make sure we get a log file + select.run() + select.save() + + # Check result file + self._check_result_file('ctselect_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GCTAEventList(filename) + + # Check file + self.test_value(result.size(), 6127, 'Check for 6127 events') + self.test_value(result.roi().centre().dir().ra_deg(), 83.63, 1.0e-6, + 'Check for ROI Right Ascension') + self.test_value(result.roi().centre().dir().dec_deg(), 22.01, 1.0e-6, + 'Check for ROI Declination') + self.test_value(result.roi().radius(), 3.0, 1.0e-6, + 'Check for ROI Radius') # Return return diff --git a/test/test_ctskymap.py b/test/test_ctskymap.py index 1e8aec6..330bf44 100755 --- a/test/test_ctskymap.py +++ b/test/test_ctskymap.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,9 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - # Return return @@ -49,54 +47,112 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctskymap") + self.name('ctskymap') # Append tests - self.append(self.test_functional, "Test ctskymap functionality") + self.append(self._test_cmd, 'Test ctskymap on command line') + self.append(self._test_python, 'Test ctskymap from Python') # Return return - # Test ctskymap functionnality - def test_functional(self): + # Test ctskymap on command line + def _test_cmd(self): """ - Test ctskymap functionnality. + Test ctskymap on the command line. """ - # Set-up ctskymap - skymap = ctools.ctskymap() - skymap["inobs"] = self.events_name - skymap["outmap"] = "skymap.fits" - skymap["emin"] = 0.1 - skymap["emax"] = 100 - skymap["nxpix"] = 200 - skymap["nypix"] = 200 - skymap["binsz"] = 0.02 - skymap["coordsys"] = "CEL" - skymap["proj"] = "CAR" - skymap["xref"] = 83.63 - skymap["yref"] = 22.01 - - # Run tool - self.test_try("Run ctskymap") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctskymap = '../src/ctskymap/ctskymap' + else: + ctskymap = 'ctskymap' + + # Setup ctskymap command + cmd = ctskymap+' inobs="data/crab_events.fits"'+\ + ' outmap="ctskymap_cmd1.fits"'+ \ + ' emin=0.1 emax=100.0 nxpix=200 nypix=200'+ \ + ' binsz=0.02 coordsys="CEL" proj="CAR"'+ \ + ' xref=83.63 yref=22.01'+ \ + ' logfile="ctskymap_cmd1.log" chatter=1' + + # Execute ctskymap, make sure we catch any exception try: - skymap.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctskymap: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctskymap." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctskymap execution on command line') + + # Check result file + self._check_result_file('ctskymap_cmd1.fits') - # Save counts cube - self.test_try("Save sky map") + # Setup ctskymap command + cmd = ctskymap+' inobs="event_file_that_does_not_exist.fits"'+\ + ' outmap="ctskymap_cmd2.fits"'+ \ + ' emin=0.1 emax=100.0 nxpix=200 nypix=200'+ \ + ' binsz=0.02 coordsys="CEL" proj="CAR"'+ \ + ' xref=83.63 yref=22.01'+ \ + ' logfile="ctskymap_cmd2.log" chatter=2' + + # Execute ctskymap, make sure we catch any exception try: - skymap.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving sky map: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving sky map." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctskymap execution on command line') + + # Return + return + + # Test ctskymap from Python + def _test_python(self): + """ + Test ctskymap from Python. + """ + # Set-up ctskymap + skymap = ctools.ctskymap() + skymap['inobs'] = 'data/crab_events.fits' + skymap['outmap'] = 'ctskymap_py1.fits' + skymap['emin'] = 0.1 + skymap['emax'] = 100 + skymap['nxpix'] = 200 + skymap['nypix'] = 200 + skymap['binsz'] = 0.02 + skymap['coordsys'] = 'CEL' + skymap['proj'] = 'CAR' + skymap['xref'] = 83.63 + skymap['yref'] = 22.01 + skymap['logfile'] = 'ctskymap_py1.log' + skymap['chatter'] = 2 + + # Run ctskymap tool + skymap.logFileOpen() # Make sure we get a log file + skymap.run() + skymap.save() + + # Check result file + self._check_result_file('ctskymap_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + result = gammalib.GSkyMap(filename) + + # Check dimensions + self.test_value(result.nmaps(), 1, 'Check for one map') + self.test_value(result.nx(), 200, 'Check for 200 pixels in X') + self.test_value(result.ny(), 200, 'Check for 200 pixels in Y') # Return return diff --git a/test/test_cttsmap.py b/test/test_cttsmap.py index e0034a3..ef215d7 100755 --- a/test/test_cttsmap.py +++ b/test/test_cttsmap.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,12 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.model_name = "data/crab.xml" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -52,56 +47,127 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("cttsmap") + self.name('cttsmap') # Append tests - self.append(self.test_functional, "Test cttsmap functionality") + self.append(self._test_cmd, 'Test cttsmap on command line') + self.append(self._test_python, 'Test cttsmap from Python') # Return return - # Test cttsmap functionnality - def test_functional(self): + # Test cttsmap on command line + def _test_cmd(self): """ - Test cttsmap functionality. + Test cttsmap on the command line. """ - # Set-up cttsmap - tsmap = ctools.cttsmap() - tsmap["inobs"] = self.events_name - tsmap["inmodel"] = self.model_name - tsmap["srcname"] = "Crab" - tsmap["caldb"] = self.caldb - tsmap["irf"] = self.irf - tsmap["outmap"] = "tsmap.fits" - tsmap["nxpix"] = 5 - tsmap["nypix"] = 5 - tsmap["binsz"] = 0.02 - tsmap["coordsys"] = "CEL" - tsmap["proj"] = "CAR" - tsmap["xref"] = 83.63 - tsmap["yref"] = 22.01 - - # Run tool - self.test_try("Run cttsmap") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + cttsmap = '../src/cttsmap/cttsmap' + else: + cttsmap = 'cttsmap' + + # Setup cttsmap command + cmd = cttsmap+' inobs="data/crab_events.fits"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' outmap="cttsmap_cmd1.fits"'+ \ + ' nxpix=5 nypix=5 binsz=0.02'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' logfile="cttsmap_cmd1.log" chatter=1' + + # Execute cttsmap, make sure we catch any exception try: - tsmap.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in cttsmap: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in cttsmap." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful cttsmap execution on command line') + + # Check result file + self._check_result_file('cttsmap_cmd1.fits') - # Save results - self.test_try("Save results") + # Setup cttsmap command + cmd = cttsmap+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' outmap="cttsmap_cmd2.fits"'+ \ + ' nxpix=5 nypix=5 binsz=0.02'+ \ + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ + ' logfile="cttsmap_cmd2.log" chatter=2' + + # Execute cttsmap, make sure we catch any exception try: - tsmap.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving results: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving results." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of cttsmap execution on command line') + + # Return + return + + # Test cttsmap from Python + def _test_python(self): + """ + Test cttsmap from Python. + """ + # Set-up cttsmap + tsmap = ctools.cttsmap() + tsmap['inobs'] = 'data/crab_events.fits' + tsmap['inmodel'] = 'data/crab.xml' + tsmap['srcname'] = 'Crab' + tsmap['caldb'] = 'prod2' + tsmap['irf'] = 'South_0.5h' + tsmap['outmap'] = 'cttsmap_py1.fits' + tsmap['nxpix'] = 5 + tsmap['nypix'] = 5 + tsmap['binsz'] = 0.02 + tsmap['coordsys'] = 'CEL' + tsmap['proj'] = 'CAR' + tsmap['xref'] = 83.63 + tsmap['yref'] = 22.01 + tsmap['logfile'] = 'cttsmap_py1.log' + tsmap['chatter'] = 2 + + # Run cttsmap tool + tsmap.logFileOpen() # Make sure we get a log file + tsmap.run() + tsmap.save() + + # Check result file + self._check_result_file('cttsmap_py1.fits') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ + # Open result file + fits = gammalib.GFits(filename) + + # Get HDUs + ts = fits['Primary'] + prefactor = fits['Prefactor'] + index = fits['Index'] + + # Check dimensions + self.test_value(ts.naxis(), 2, 'Check for 2 dimensions') + self.test_value(ts.naxes(0), 5, 'Check for 5 pixels in X') + self.test_value(ts.naxes(1), 5, 'Check for 5 pixels in Y') + self.test_value(prefactor.naxis(), 2, 'Check for 2 dimensions') + self.test_value(prefactor.naxes(0), 5, 'Check for 5 pixels in X') + self.test_value(prefactor.naxes(1), 5, 'Check for 5 pixels in Y') + self.test_value(index.naxis(), 2, 'Check for 2 dimensions') + self.test_value(index.naxes(0), 5, 'Check for 5 pixels in X') + self.test_value(index.naxes(1), 5, 'Check for 5 pixels in Y') # Return return diff --git a/test/test_ctulimit.py b/test/test_ctulimit.py index 6f1391c..9cd8d31 100755 --- a/test/test_ctulimit.py +++ b/test/test_ctulimit.py @@ -18,6 +18,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib import ctools @@ -37,12 +38,6 @@ class Test(gammalib.GPythonTestSuite): # Call base class constructor gammalib.GPythonTestSuite.__init__(self) - # Set members - self.events_name = "data/crab_events.fits" - self.model_name = "data/crab.xml" - self.caldb = "irf" - self.irf = "cta_dummy_irf" - # Return return @@ -52,48 +47,94 @@ class Test(gammalib.GPythonTestSuite): Set all test functions. """ # Set test name - self.name("ctulimit") + self.name('ctulimit') # Append tests - self.append(self.test_functional, "Test ctulimit functionality") + self.append(self._test_cmd, 'Test ctulimit on command line') + self.append(self._test_python, 'Test ctulimit from Python') # Return return - # Test cttsmap functionnality - def test_functional(self): + # Test ctulimit on command line + def _test_cmd(self): """ - Test ctulimit functionnality. + Test ctulimit on the command line. """ - # Set-up cttsmap - ulimit = ctools.ctulimit() - ulimit["inobs"] = self.events_name - ulimit["inmodel"] = self.model_name - ulimit["srcname"] = "Crab" - ulimit["caldb"] = self.caldb - ulimit["irf"] = self.irf - - # Run tool - self.test_try("Run ctulimit") + # Kluge to set the command (installed version has no README file) + if os.path.isfile('README'): + ctulimit = '../src/ctulimit/ctulimit' + else: + ctulimit = 'ctulimit' + + # Setup ctulimit command + cmd = ctulimit+' inobs="data/crab_events.fits"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' logfile="ctulimit_cmd1.log" chatter=1' + + # Execute ctulimit, make sure we catch any exception try: - ulimit.run() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in ctulimit: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in ctulimit." - self.test_try_failure(msg) + pass + + # Check if execution was successful + self.test_assert(rc == 0, + 'Successful ctulimit execution on command line') + + # Check result file + self._check_result_file('ctulimit_cmd1.log') - # Save results - self.test_try("Save results") + # Setup ctulimit command + cmd = ctulimit+' inobs="event_file_that_does_not_exist.fits"'+ \ + ' inmodel="data/crab.xml" srcname="Crab"'+ \ + ' caldb="prod2" irf="South_0.5h"'+ \ + ' logfile="ctulimit_cmd2.log" chatter=1' + + # Execute ctulimit, make sure we catch any exception try: - ulimit.save() - self.test_try_success() - #except Exception as e: - # msg = "Exception occured in saving results: %s." % (e,) + rc = os.system(cmd+' >/dev/null 2>&1') except: - msg = "Exception occured in saving results." - self.test_try_failure(msg) + pass + + # Check if execution failed + self.test_assert(rc != 0, + 'Failure of ctulimit execution on command line') + + # Return + return + # Test ctulimit from Python + def _test_python(self): + """ + Test ctulimit from Python. + """ + # Set-up ctulimit + ulimit = ctools.ctulimit() + ulimit['inobs'] = 'data/crab_events.fits' + ulimit['inmodel'] = 'data/crab.xml' + ulimit['srcname'] = 'Crab' + ulimit['caldb'] = 'prod2' + ulimit['irf'] = 'South_0.5h' + ulimit['logfile'] = 'ctulimit_py1.log' + ulimit['chatter'] = 2 + + # Run ctulimit tool + ulimit.logFileOpen() # Make sure we get a log file + ulimit.run() + ulimit.save() + + # Check result file + self._check_result_file('ctulimit_py1.log') + + # Return + return + + # Check result file + def _check_result_file(self, filename): + """ + Check result file. + """ # Return return diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py index f5817d6..af6dbc3 100755 --- a/test/test_python_cscripts.py +++ b/test/test_python_cscripts.py @@ -50,6 +50,13 @@ import test_csiactobs def test(installed=False, debug=False): """ Perform cscripts unit testing. + + Parameters + ---------- + installed : bool, optional + Flag indicating whether the script has been installed or not + debug : bool, optional + Enables debugging """ # If we have an installed version then create a temporary directory and # copy over all information that is needed diff --git a/test/test_python_ctools.py b/test/test_python_ctools.py index 7b3a178..67cf118 100755 --- a/test/test_python_ctools.py +++ b/test/test_python_ctools.py @@ -32,6 +32,7 @@ import test_ctmodel import test_ctskymap import test_ctexpcube import test_ctpsfcube +import test_ctedispcube import test_ctbkgcube import test_ctmapcube import test_ctcubemask @@ -47,9 +48,14 @@ import test_pipelines def test(installed=False): """ Perform unit testing for ctools. + + Parameters + ---------- + installed : bool, optional + Flag indicating whether the script has been installed or not """ # Allocate test suite container - suites = gammalib.GTestSuites("ctools unit testing") + suites = gammalib.GTestSuites('ctools unit testing') # Allocate test suites and append them to the container suite_ctobssim = test_ctobssim.Test() @@ -61,6 +67,7 @@ def test(installed=False): suite_ctskymap = test_ctskymap.Test() suite_ctexpcube = test_ctexpcube.Test() suite_ctpsfcube = test_ctpsfcube.Test() + suite_ctedispcube = test_ctedispcube.Test() suite_ctbkgcube = test_ctbkgcube.Test() suite_ctmapcube = test_ctmapcube.Test() suite_ctcubemask = test_ctcubemask.Test() @@ -79,6 +86,7 @@ def test(installed=False): suite_ctskymap.set() suite_ctexpcube.set() suite_ctpsfcube.set() + suite_ctedispcube.set() suite_ctbkgcube.set() suite_ctmapcube.set() suite_ctcubemask.set() @@ -97,6 +105,7 @@ def test(installed=False): suites.append(suite_ctskymap) suites.append(suite_ctexpcube) suites.append(suite_ctpsfcube) + suites.append(suite_ctedispcube) suites.append(suite_ctbkgcube) suites.append(suite_ctmapcube) suites.append(suite_ctcubemask) @@ -120,28 +129,28 @@ def test(installed=False): dirname = os.path.dirname(testdir) # Copy over test data and irf - os.system("cp -r %s %s" % (dirname+"/data", "data")) - os.system("cp -r %s %s" % (dirname+"/irf", "irf")) + os.system('cp -r %s %s' % (dirname+'/data', 'data')) + os.system('cp -r %s %s' % (dirname+'/irf', 'irf')) # Set PFILES environment variable try: - os.mkdir("pfiles") + os.mkdir('pfiles') except: pass - os.environ['PFILES'] = "pfiles" + os.environ['PFILES'] = 'pfiles' # Copy over pfiles if not installed: - os.system("cp -r ../src/*/*.par pfiles/") + os.system('cp -r ../src/*/*.par pfiles/') else: - os.system("cp -r %s/syspfiles/*.par pfiles/" % (os.environ['CTOOLS'])) + os.system('cp -r %s/syspfiles/*.par pfiles/' % (os.environ['CTOOLS'])) # Run test suite success = suites.run() # If we have a non-installed version then save test results if not installed: - suites.save("reports/ctools.xml") + suites.save('reports/ctools.xml') # Set return code if success: