From ce48d5053a80a387ebc3d22de21598b8c1d89105 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 10 Aug 2016 22:53:30 +0200 Subject: [PATCH] Use GSkyMap::is_empty() method --- src/ctbkgcube/ctbkgcube.cpp | 22 +++++++++------------- src/ctbkgcube/ctbkgcube.hpp | 1 - src/ctedispcube/ctedispcube.cpp | 9 ++------- src/ctexpcube/ctexpcube.cpp | 9 ++------- src/ctmapcube/ctmapcube.cpp | 9 ++------- src/ctpsfcube/ctpsfcube.cpp | 9 ++------- src/ctskymap/ctskymap.cpp | 7 ++----- src/cttsmap/cttsmap.cpp | 7 ++----- test/test_ctbkgcube.py | 48 +++++++++++++++++++++++++++++++----------------- 9 files changed, 52 insertions(+), 69 deletions(-) diff --git a/src/ctbkgcube/ctbkgcube.cpp b/src/ctbkgcube/ctbkgcube.cpp index 5d3b4bd..6e1137b 100644 --- a/src/ctbkgcube/ctbkgcube.cpp +++ b/src/ctbkgcube/ctbkgcube.cpp @@ -343,17 +343,12 @@ void ctbkgcube::save(void) m_outcube = (*this)["outcube"].filename(); m_outmodel = (*this)["outmodel"].filename(); - // Determine whether the cube is empty - bool cube_is_empty = ((m_background.cube().nx() == 0) || - (m_background.cube().ny() == 0) || - (m_background.cube().nmaps() == 0)); - // Determine whether a model definition file should be written bool write_moddef = ((!m_outmodel.is_empty()) && (gammalib::tolower(m_outmodel.url()) != "none")); // Save background cube if filename and the background cube are not empty - if (!m_outcube.is_empty() && !cube_is_empty) { + if (!m_outcube.is_empty() && !m_background.cube().is_empty()) { m_background.save(m_outcube, clobber()); } @@ -365,7 +360,7 @@ void ctbkgcube::save(void) // Write into logger what has been done std::string fname = (m_outcube.is_empty()) ? "NONE" : m_outcube.url(); std::string mname = (write_moddef) ? m_outmodel.url() : "NONE"; - if (cube_is_empty) { + if (m_background.cube().is_empty()) { fname.append(" (cube is empty, no file created)"); } log_value(NORMAL, "Background cube file", fname); @@ -497,7 +492,7 @@ void ctbkgcube::get_parameters(void) std::string incube = (*this)["incube"].filename(); // Check for filename validity - if ((gammalib::toupper(incube) == "NONE") || + if ((gammalib::toupper(incube) == "NONE") || (gammalib::strip_whitespace(incube) == "")) { // Create an event cube based on task parameters @@ -520,18 +515,19 @@ void ctbkgcube::get_parameters(void) } // endelse: cube was loaded from file // If there are no models associated with the observations then load now - // the model definition from the XML file + // the model definition from the XML file. Throw an exception if the + // input model is not set. if (m_obs.models().size() == 0) { - if ((*this)["inmodel"].is_undefined()) { + std::string inmodel = (*this)["inmodel"].filename(); + if ((gammalib::toupper(inmodel) == "NONE") || + (gammalib::strip_whitespace(inmodel) == "")) { std::string msg = "No model definition XML file specified. " "Please set the \"inmodel\" parameter to the " "XML file that contains the background model " "definition."; throw GException::invalid_value(G_GET_PARAMETERS, msg); } - std::string inmodel = (*this)["inmodel"].filename(); - GModels models(inmodel); - m_obs.models(models); + m_obs.models(GModels(inmodel)); } // Get remaining parameters diff --git a/src/ctbkgcube/ctbkgcube.hpp b/src/ctbkgcube/ctbkgcube.hpp index 8e6c095..374f5ba 100644 --- a/src/ctbkgcube/ctbkgcube.hpp +++ b/src/ctbkgcube/ctbkgcube.hpp @@ -70,7 +70,6 @@ protected: void free_members(void); void get_parameters(void); void init_cube(void); - //void fill_cube(GCTAObservation* obs); // User parameters GFilename m_outcube; //!< Filename of output cube diff --git a/src/ctedispcube/ctedispcube.cpp b/src/ctedispcube/ctedispcube.cpp index 95435f1..de61c9c 100644 --- a/src/ctedispcube/ctedispcube.cpp +++ b/src/ctedispcube/ctedispcube.cpp @@ -264,20 +264,15 @@ void ctedispcube::save(void) // Get output filename m_outcube = (*this)["outcube"].filename(); - // Determine whether the cube is empty - bool cube_is_empty = ((m_edispcube.cube().nx() == 0) || - (m_edispcube.cube().ny() == 0) || - (m_edispcube.cube().nmaps() == 0)); - // Save energy dispersion cube if filename and the energy dispersion cube // are not empty - if (!m_outcube.is_empty() && !cube_is_empty) { + if (!m_outcube.is_empty() && !m_edispcube.cube().is_empty()) { m_edispcube.save(m_outcube, clobber()); } // Write into logger what has been done std::string fname = (m_outcube.is_empty()) ? "NONE" : m_outcube.url(); - if (cube_is_empty) { + if (m_edispcube.cube().is_empty()) { fname.append(" (cube is empty, no file created)"); } log_value(NORMAL, "Energy dispersion cube file", fname); diff --git a/src/ctexpcube/ctexpcube.cpp b/src/ctexpcube/ctexpcube.cpp index 3f2f9a7..df67659 100644 --- a/src/ctexpcube/ctexpcube.cpp +++ b/src/ctexpcube/ctexpcube.cpp @@ -261,19 +261,14 @@ void ctexpcube::save(void) // Get exposure cube filename m_outcube = (*this)["outcube"].filename(); - // Determine whether the cube is empty - bool cube_is_empty = ((m_expcube.cube().nx() == 0) || - (m_expcube.cube().ny() == 0) || - (m_expcube.cube().nmaps() == 0)); - // Save exposure cube if filename and the exposure cube are not empty - if (!m_outcube.is_empty() && !cube_is_empty) { + if (!m_outcube.is_empty() && !m_expcube.cube().is_empty()) { m_expcube.save(m_outcube, clobber()); } // Write into logger what has been done std::string fname = (m_outcube.is_empty()) ? "NONE" : m_outcube.url(); - if (cube_is_empty) { + if (m_expcube.cube().is_empty()) { fname.append(" (cube is empty, no file created)"); } log_value(NORMAL, "Exposure cube file", fname); diff --git a/src/ctmapcube/ctmapcube.cpp b/src/ctmapcube/ctmapcube.cpp index e1c304f..b8a13b3 100644 --- a/src/ctmapcube/ctmapcube.cpp +++ b/src/ctmapcube/ctmapcube.cpp @@ -225,19 +225,14 @@ void ctmapcube::save(void) // Get map cube filename m_outcube = (*this)["outcube"].filename(); - // Determine whether the cube is empty - bool cube_is_empty = ((m_cube.cube().nx() == 0) || - (m_cube.cube().ny() == 0) || - (m_cube.cube().nmaps() == 0)); - // Save map cube if filename and the map cube are not empty - if (!m_outcube.is_empty() && !cube_is_empty) { + if (!m_outcube.is_empty() && !m_cube.cube().is_empty()) { m_cube.save(m_outcube, clobber()); } // Write into logger what has been done std::string fname = (m_outcube.is_empty()) ? "NONE" : m_outcube.url(); - if (cube_is_empty) { + if (m_cube.cube().is_empty()) { fname.append(" (cube is empty, no file created)"); } log_value(NORMAL, "Map cube file", fname); diff --git a/src/ctpsfcube/ctpsfcube.cpp b/src/ctpsfcube/ctpsfcube.cpp index 3ffb0dd..f27d88b 100644 --- a/src/ctpsfcube/ctpsfcube.cpp +++ b/src/ctpsfcube/ctpsfcube.cpp @@ -250,19 +250,14 @@ void ctpsfcube::save(void) // Get output filename m_outcube = (*this)["outcube"].filename(); - // Determine whether the cube is empty - bool cube_is_empty = ((m_psfcube.cube().nx() == 0) || - (m_psfcube.cube().ny() == 0) || - (m_psfcube.cube().nmaps() == 0)); - // Save PSF cube if filename and the PSF cube are not empty - if (!m_outcube.is_empty() && !cube_is_empty) { + if (!m_outcube.is_empty() && !m_psfcube.cube().is_empty()) { m_psfcube.save(m_outcube, clobber()); } // Write into logger what has been done std::string fname = (m_outcube.is_empty()) ? "NONE" : m_outcube.url(); - if (cube_is_empty) { + if (m_psfcube.cube().is_empty()) { fname.append(" (cube is empty, no file created)"); } log_value(NORMAL, "PSF cube file", fname); diff --git a/src/ctskymap/ctskymap.cpp b/src/ctskymap/ctskymap.cpp index 397a3ef..cfa1dc2 100644 --- a/src/ctskymap/ctskymap.cpp +++ b/src/ctskymap/ctskymap.cpp @@ -284,11 +284,8 @@ void ctskymap::save(void) // Get sky map filename m_outmap = (*this)["outmap"].filename(); - // Determine whether the map is empty - bool map_is_empty = ((m_skymap.nx() == 0) || (m_skymap.ny() == 0)); - // Save sky map if filename and the map are not empty - if (!m_outmap.is_empty() && !map_is_empty) { + if (!m_outmap.is_empty() && !m_skymap.is_empty()) { // Create empty FITS file GFits fits; @@ -303,7 +300,7 @@ void ctskymap::save(void) // Write into logger what has been done std::string fname = (m_outmap.is_empty()) ? "NONE" : m_outmap.url(); - if (map_is_empty) { + if (m_skymap.is_empty()) { fname.append(" (map is empty, no file created)"); } log_value(NORMAL, "Sky map file", fname); diff --git a/src/cttsmap/cttsmap.cpp b/src/cttsmap/cttsmap.cpp index 058b2a8..448912d 100644 --- a/src/cttsmap/cttsmap.cpp +++ b/src/cttsmap/cttsmap.cpp @@ -355,11 +355,8 @@ void cttsmap::save(void) // Get TS map filename m_outmap = (*this)["outmap"].filename(); - // Determine whether the TS map is empty - bool map_is_empty = ((m_tsmap.nx() == 0) || (m_tsmap.ny() == 0)); - // Save only if filename is non-empty - if (!m_outmap.is_empty() && !map_is_empty) { + if (!m_outmap.is_empty() && !m_tsmap.is_empty()) { // Create fits file GFits fits; @@ -388,7 +385,7 @@ void cttsmap::save(void) // Write into logger what has been done std::string fname = (m_outmap.is_empty()) ? "NONE" : m_outmap.url(); - if (map_is_empty) { + if (m_tsmap.is_empty()) { fname.append(" (TS map is empty, no file created)"); } log_value(NORMAL, "TS map file", fname); diff --git a/test/test_ctbkgcube.py b/test/test_ctbkgcube.py index 573f9af..05d71b1 100755 --- a/test/test_ctbkgcube.py +++ b/test/test_ctbkgcube.py @@ -131,6 +131,24 @@ class Test(test): # Check model container self._check_models(gammalib.GModels('ctbkgcube_py0.xml'), nmodels=0) + # Check saving with empty file names + bkgcube['outcube'] = '' + bkgcube['outmodel'] = '' + bkgcube['logfile'] = 'ctbkgcube_py1.log' + bkgcube.logFileOpen() + bkgcube.save() + + # Check saving with "none" model definiton name + bkgcube['outcube'] = '' + bkgcube['outmodel'] = 'NONE' + bkgcube['logfile'] = 'ctbkgcube_py2.log' + bkgcube.logFileOpen() + bkgcube.save() + + # Check that publish method with user name does not lead to an + # exception or segfault + bkgcube.publish('My background cube') + # Check that clearing does not lead to an exception or segfault bkgcube.clear() @@ -151,9 +169,9 @@ class Test(test): bkgcube['proj'] = 'CAR' bkgcube['xref'] = 83.63 bkgcube['yref'] = 22.01 - bkgcube['outcube'] = 'ctbkgcube_py1.fits' - bkgcube['outmodel'] = 'ctbkgcube_py1.xml' - bkgcube['logfile'] = 'ctbkgcube_py1.log' + bkgcube['outcube'] = 'ctbkgcube_py3.fits' + bkgcube['outmodel'] = 'ctbkgcube_py3.xml' + bkgcube['logfile'] = 'ctbkgcube_py3.log' bkgcube['chatter'] = 2 # Run ctbkgcube tool @@ -162,7 +180,7 @@ class Test(test): bkgcube.save() # Check result files - self._check_result_files('ctbkgcube_py1') + self._check_result_files('ctbkgcube_py3') # Copy ctbkgcube tool cpy_bkgcube = bkgcube.copy() @@ -174,14 +192,10 @@ class Test(test): # Execute copy of ctbkgcube tool again, now with a higher chatter # level than before. In addition, use counts cube to define the # background cube - #cpy_bkgcube['inobs'] = self._events - #cpy_bkgcube['inmodel'] = self._model cpy_bkgcube['incube'] = self._cntcube - #cpy_bkgcube['caldb'] = self._caldb - #cpy_bkgcube['irf'] = self._irf - cpy_bkgcube['outcube'] = 'ctbkgcube_py2.fits' - cpy_bkgcube['outmodel'] = 'ctbkgcube_py2.xml' - cpy_bkgcube['logfile'] = 'ctbkgcube_py2.log' + cpy_bkgcube['outcube'] = 'ctbkgcube_py4.fits' + cpy_bkgcube['outmodel'] = 'ctbkgcube_py4.xml' + cpy_bkgcube['logfile'] = 'ctbkgcube_py4.log' cpy_bkgcube['chatter'] = 3 cpy_bkgcube['publish'] = True cpy_bkgcube['addbounds'] = True @@ -189,7 +203,7 @@ class Test(test): cpy_bkgcube.execute() # Check result files - self._check_result_files('ctbkgcube_py2') + self._check_result_files('ctbkgcube_py4') # Now clear copy of ctbkgcube tool cpy_bkgcube.clear() @@ -215,7 +229,7 @@ class Test(test): # Set-up ctbkgcube from observation container bkgcube = ctools.ctbkgcube(obs) - bkgcube['incube'] = 'NONE' + bkgcube['incube'] = '' bkgcube['caldb'] = self._caldb bkgcube['irf'] = self._irf bkgcube['ebinalg'] = 'LOG' @@ -229,9 +243,9 @@ class Test(test): bkgcube['proj'] = 'CAR' bkgcube['xref'] = 83.63 bkgcube['yref'] = 22.01 - bkgcube['outcube'] = 'ctbkgcube_py3.fits' - bkgcube['outmodel'] = 'ctbkgcube_py3.xml' - bkgcube['logfile'] = 'ctbkgcube_py3.log' + bkgcube['outcube'] = 'ctbkgcube_py5.fits' + bkgcube['outmodel'] = 'ctbkgcube_py5.xml' + bkgcube['logfile'] = 'ctbkgcube_py5.log' bkgcube['addbounds'] = True bkgcube['chatter'] = 4 @@ -240,7 +254,7 @@ class Test(test): bkgcube.execute() # Check result files - self._check_result_files('ctbkgcube_py3', nenergies=23) + self._check_result_files('ctbkgcube_py5', nenergies=23) # Return return -- libgit2 0.21.4