Commit 1e00ea3def3c7c067f5237bfc76b903b838d5dbc

Authored by Jürgen Knödlseder
1 parent 60fca9e5

Add ctool::is_valid_filename() method (#1828)

pyext/ctool.i
... ... @@ -107,6 +107,7 @@ public:
107 107 %rename(_get_obs_header) get_obs_header;
108 108 %rename(_insert_energy_boundaries) insert_energy_boundaries;
109 109 %rename(_cube_layer_usage) cube_layer_usage;
  110 + %rename(_is_valid_filename) is_valid_filename;
110 111 %rename(_warn_too_few_energies) warn_too_few_energies;
111 112 %rename(_warn_xml_suffix) warn_xml_suffix;
112 113  
... ... @@ -154,6 +155,7 @@ public:
154 155 const GCTAObservation& obs);
155 156 std::vector<bool> cube_layer_usage(const GEbounds& cube_ebounds,
156 157 const GEbounds& list_ebounds) const;
  158 + bool is_valid_filename(const GFilename& filename) const;
157 159  
158 160 // Protected warning strings
159 161 std::string warn_too_few_energies(const GEnergies& energies) const;
... ...
src/ctbkgcube/ctbkgcube.cpp
... ... @@ -33,7 +33,6 @@
33 33 #include "GTools.hpp"
34 34  
35 35 /* __ Method name definitions ____________________________________________ */
36   -#define G_GET_PARAMETERS "ctbkgcube::get_parameters()"
37 36  
38 37 /* __ Debug definitions __________________________________________________ */
39 38  
... ... @@ -344,8 +343,7 @@ void ctbkgcube::save(void)
344 343 m_outmodel = (*this)["outmodel"].filename();
345 344  
346 345 // Determine whether a model definition file should be written
347   - bool write_moddef = ((!m_outmodel.is_empty()) &&
348   - (gammalib::tolower(m_outmodel.url()) != "none"));
  346 + bool write_moddef = is_valid_filename(m_outmodel);
349 347  
350 348 // Save background cube if filename and the background cube are not empty
351 349 if (!m_outcube.is_empty() && !m_background.cube().is_empty()) {
... ... @@ -465,58 +463,29 @@ void ctbkgcube::free_members(void)
465 463 /***********************************************************************//**
466 464 * @brief Get application parameters
467 465 *
468   - * @exception GException::invalid_value
469   - * No background model definition XML file specified.
470   - *
471 466 * Get all task parameters from parameter file or (if required) by querying
472 467 * the user. The parameters are read in the correct order.
473 468 ***************************************************************************/
474 469 void ctbkgcube::get_parameters(void)
475 470 {
476   - // Setup observations from "inobs" parameter. Do not accept counts cubes.
  471 + // Setup observations from "inobs" parameter. Require response and accept
  472 + // event lists, but do not accept counts cubes.
477 473 setup_observations(m_obs, true, true, false);
478 474  
479 475 // Get the incube filename
480 476 std::string incube = (*this)["incube"].filename();
481 477  
482   - // Check for filename validity
483   - if ((gammalib::toupper(incube) == "NONE") ||
484   - (gammalib::strip_whitespace(incube) == "")) {
485   -
486   - // Create an event cube based on task parameters
487   - GCTAEventCube cube = create_cube(m_obs);
488   -
489   - // Define background cube
490   - m_background = GCTACubeBackground(cube);
491   -
492   - } // endif: filename was not valid
493   -
494   - // ... otherwise setup the background cube from the counts map
495   - else {
496   -
497   - // Load event cube from filename
498   - GCTAEventCube cube(incube);
499   -
500   - // Define background cube from file
501   - m_background = GCTACubeBackground(cube);
  478 + // If the "incube" file name is valid then setup the background cube from
  479 + // the counts cube. Otherwise create a counts cube from the user
  480 + // parameters
  481 + GCTAEventCube cube = is_valid_filename(incube) ? GCTAEventCube(incube)
  482 + : create_cube(m_obs);
502 483  
503   - } // endelse: cube was loaded from file
504   -
505   - // If there are no models associated with the observations then load now
506   - // the model definition from the XML file. Throw an exception if the
507   - // input model is not set.
508   - if (m_obs.models().size() == 0) {
509   - std::string inmodel = (*this)["inmodel"].filename();
510   - if ((gammalib::toupper(inmodel) == "NONE") ||
511   - (gammalib::strip_whitespace(inmodel) == "")) {
512   - std::string msg = "No model definition XML file specified. "
513   - "Please set the \"inmodel\" parameter to the "
514   - "XML file that contains the background model "
515   - "definition.";
516   - throw GException::invalid_value(G_GET_PARAMETERS, msg);
517   - }
518   - m_obs.models(GModels(inmodel));
519   - }
  484 + // Define background cube
  485 + m_background = GCTACubeBackground(cube);
  486 +
  487 + // Setup models from "inmodel" parameter
  488 + setup_models(m_obs);
520 489  
521 490 // Get remaining parameters
522 491 m_addbounds = (*this)["addbounds"].boolean();
... ...
src/ctedispcube/ctedispcube.cpp
... ... @@ -356,27 +356,14 @@ void ctedispcube::get_parameters(void)
356 356 double migramax = (*this)["migramax"].real();
357 357 int migrabins = (*this)["migrabins"].integer();
358 358  
359   - // Check for filename validity
360   - if ((incube == "NONE") || (gammalib::strip_whitespace(incube) == "")) {
361   -
362   - // Create an event cube based on task parameters
363   - GCTAEventCube cube = create_cube(m_obs);
364   -
365   - // Define edisp cube
366   - m_edispcube = GCTACubeEdisp(cube, migramax, migrabins);
367   -
368   - }
369   -
370   - // ... otherwise setup the energy disperison cube from the counts map
371   - else {
372   -
373   - // Load event cube from filename
374   - GCTAEventCube cube(incube);
375   -
376   - // Define Edisp cube
377   - m_edispcube = GCTACubeEdisp(cube, migramax, migrabins);
378   -
379   - } // endelse: cube loaded from file
  359 + // If the "incube" file name is valid then setup the energy dispersion
  360 + // cube from the counts cube. Otherwise create a counts cube from the
  361 + // user parameters
  362 + GCTAEventCube cube = is_valid_filename(incube) ? GCTAEventCube(incube)
  363 + : create_cube(m_obs);
  364 +
  365 + // Define edisp cube
  366 + m_edispcube = GCTACubeEdisp(cube, migramax, migrabins);
380 367  
381 368 // Get remaining parameters
382 369 m_addbounds = (*this)["addbounds"].boolean();
... ...
src/cterror/cterror.cpp
... ... @@ -354,9 +354,8 @@ void cterror::save(void)
354 354 // Get output filename
355 355 m_outmodel = (*this)["outmodel"].filename();
356 356  
357   - // Save only if filename is not empty and not "NONE"
358   - if (!m_outmodel.is_empty() &&
359   - gammalib::toupper(m_outmodel.url()) != "NONE") {
  357 + // Save only if filename is valid
  358 + if (is_valid_filename(m_outmodel)) {
360 359  
361 360 // Log filename
362 361 log_value(NORMAL, "Model definition file", m_outmodel.url());
... ... @@ -366,7 +365,7 @@ void cterror::save(void)
366 365  
367 366 }
368 367  
369   - // ... otherwise signal that file was not save
  368 + // ... otherwise signal that file was not saved
370 369 else {
371 370 log_value(NORMAL, "Model definition file", "NONE");
372 371 }
... ...
src/ctexpcube/ctexpcube.cpp
... ... @@ -376,27 +376,14 @@ void ctexpcube::get_parameters(void)
376 376 // Get the incube filename
377 377 std::string incube = (*this)["incube"].filename();
378 378  
379   - // Check for filename validity
380   - if ((incube == "NONE") || (gammalib::strip_whitespace(incube) == "")) {
381   -
382   - // Create an event cube based on task parameters
383   - GCTAEventCube cube = create_cube(m_obs);
384   -
385   - // Define exposure cube
386   - m_expcube = GCTACubeExposure(cube);
387   -
388   - } // endif: filename was not valid
389   -
390   - // ... otherwise setup the exposure cube from the counts map
391   - else {
392   -
393   - // Load event cube from filename
394   - GCTAEventCube cube(incube);
395   -
396   - // Define exposure cube
397   - m_expcube = GCTACubeExposure(cube);
398   -
399   - } // endelse: cube loaded from file
  379 + // If the "incube" file name is valid then setup the exposure cube from
  380 + // the counts cube. Otherwise create a counts cube from the user
  381 + // parameters
  382 + GCTAEventCube cube = is_valid_filename(incube) ? GCTAEventCube(incube)
  383 + : create_cube(m_obs);
  384 +
  385 + // Define exposure cube
  386 + m_expcube = GCTACubeExposure(cube);
400 387  
401 388 // Get remaining parameters
402 389 m_addbounds = (*this)["addbounds"].boolean();
... ...
src/ctlike/ctlike.cpp
... ... @@ -319,9 +319,8 @@ void ctlike::save(void)
319 319 // Get output filename
320 320 m_outmodel = (*this)["outmodel"].filename();
321 321  
322   - // Save only if filename is non-empty
323   - if (!m_outmodel.is_empty() &&
324   - gammalib::toupper(m_outmodel.url()) != "NONE") {
  322 + // Save only if filename is valid
  323 + if (is_valid_filename(m_outmodel)) {
325 324  
326 325 // Log filename
327 326 log_value(NORMAL, "Model definition file", m_outmodel.url());
... ... @@ -331,7 +330,7 @@ void ctlike::save(void)
331 330  
332 331 }
333 332  
334   - // ... otherwise signal that file was not save
  333 + // ... otherwise signal that file was not saved
335 334 else {
336 335 log_value(NORMAL, "Model definition file", "NONE");
337 336 }
... ...
src/ctmodel/ctmodel.cpp
... ... @@ -517,19 +517,9 @@ void ctmodel::get_parameters(void)
517 517 // Read cube definition file
518 518 std::string incube = (*this)["incube"].filename();
519 519  
520   - // If no cube file has been specified then create a cube from
521   - // the task parameters ...
522   - if ((incube == "NONE") ||
523   - (gammalib::strip_whitespace(incube) == "")) {
524   -
525   - // Create cube from scratch
526   - m_cube = create_cube(m_obs);
527   -
528   - }
529   -
530   - // ... otherwise load the cube from file and reset all bins
531   - // to zero
532   - else {
  520 + // If the cube filename is valid the load the cube and set all cube
  521 + // bins to zero
  522 + if (is_valid_filename(incube)) {
533 523  
534 524 // Load cube from given file
535 525 m_cube.load(incube);
... ... @@ -538,6 +528,12 @@ void ctmodel::get_parameters(void)
538 528 for (int i = 0; i < m_cube.size(); ++i) {
539 529 m_cube[i]->counts(0.0);
540 530 }
  531 +
  532 + } // endif: cube filename was valid
  533 +
  534 + // ... otherwise create a cube from the user parameters
  535 + else {
  536 + m_cube = create_cube(m_obs);
541 537 }
542 538  
543 539 // Signal that cube has been set
... ... @@ -601,6 +597,8 @@ void ctmodel::get_parameters(void)
601 597 * a stacked observation is requested (as detected by the presence of the
602 598 * "expcube", "psfcube", and "bkgcube" parameters). In that case, it appended
603 599 * a dummy event cube to the observation.
  600 + *
  601 + * @TODO Support stacked energy dispersion
604 602 ***************************************************************************/
605 603 void ctmodel::get_obs(void)
606 604 {
... ... @@ -609,7 +607,7 @@ void ctmodel::get_obs(void)
609 607  
610 608 // If no observation definition file has been specified then read all
611 609 // parameters that are necessary to create an observation from scratch
612   - if ((filename == "NONE") || (gammalib::strip_whitespace(filename) == "")) {
  610 + if (!is_valid_filename(filename)) {
613 611  
614 612 // Get response cube filenames
615 613 std::string expcube = (*this)["expcube"].filename();
... ... @@ -618,10 +616,8 @@ void ctmodel::get_obs(void)
618 616  
619 617 // If the filenames are valid then build an observation from cube
620 618 // response information
621   - if ((expcube != "NONE") && (psfcube != "NONE") && (bkgcube != "NONE") &&
622   - (gammalib::strip_whitespace(expcube) != "") &&
623   - (gammalib::strip_whitespace(psfcube) != "") &&
624   - (gammalib::strip_whitespace(bkgcube) != "")) {
  619 + if (is_valid_filename(expcube) && is_valid_filename(psfcube) &&
  620 + is_valid_filename(bkgcube)) {
625 621  
626 622 // Get exposure, PSF and background cubes
627 623 GCTACubeExposure exposure(expcube);
... ... @@ -667,7 +663,7 @@ void ctmodel::get_obs(void)
667 663  
668 664 }
669 665  
670   - } // endif: filename was "NONE" or ""
  666 + } // endif: filename was not valid
671 667  
672 668 // ... otherwise we have a file name
673 669 else {
... ...
src/ctpsfcube/ctpsfcube.cpp
... ... @@ -340,27 +340,13 @@ void ctpsfcube::get_parameters(void)
340 340 double amax = (*this)["amax"].real();
341 341 int anumbins = (*this)["anumbins"].integer();
342 342  
343   - // Check for filename validity
344   - if ((incube == "NONE") || (gammalib::strip_whitespace(incube) == "")) {
  343 + // If the "incube" file name is valid then setup the PSF cube from the
  344 + // counts cube. Otherwise create a counts cube from the user parameters
  345 + GCTAEventCube cube = is_valid_filename(incube) ? GCTAEventCube(incube)
  346 + : create_cube(m_obs);
345 347  
346   - // Create an event cube based on task parameters
347   - GCTAEventCube cube = create_cube(m_obs);
348   -
349   - // Define psf cube
350   - m_psfcube = GCTACubePsf(cube, amax, anumbins);
351   -
352   - }
353   -
354   - // ... otherwise setup the PSF cube from the counts map
355   - else {
356   -
357   - // Load event cube from filename
358   - GCTAEventCube cube(incube);
359   -
360   - // Define psf cube
361   - m_psfcube = GCTACubePsf(cube, amax, anumbins);
362   -
363   - } // endelse: cube loaded from file
  348 + // Define PSF cube
  349 + m_psfcube = GCTACubePsf(cube, amax, anumbins);
364 350  
365 351 // Get remaining parameters
366 352 m_addbounds = (*this)["addbounds"].boolean();
... ...
src/support/ctool.cpp
... ... @@ -50,7 +50,7 @@
50 50  
51 51 /* __ Method name definitions ____________________________________________ */
52 52 #define G_SETUP_OBSERVATION "ctool::setup_observations(GObservations&)"
53   -#define SETUP_MODELS "ctool::setup_models(GObservations&, std::string&)"
  53 +#define G_SETUP_MODELS "ctool::setup_models(GObservations&, std::string&)"
54 54 #define G_GET_MEAN_POINTING "ctool::get_mean_pointing(GObservations&)"
55 55 #define G_CREATE_EBOUNDS "ctool::create_ebounds()"
56 56 #define G_RESTORE_EDISP "ctool::restore_edisp(GObservations&, "\
... ... @@ -319,9 +319,9 @@ void ctool::free_members(void)
319 319 * information from user parameters.
320 320 *
321 321 * If the observation container @p obs is empty, the method will extract the
322   - * filename from the "inobs" parameter, and if the filename is neither empty
323   - * nor "NONE", it will load the observation container from the specified file.
324   - * Otherwise, the method will throw an exception.
  322 + * filename from the "inobs" parameter and load the observation container
  323 + * from the specified file. If the filename is either empty or "NONE" the
  324 + * method will throw an exception.
325 325 *
326 326 * If the "inobs" file is a FITS file, and depending on the information that
327 327 * is found in the FITS file, the method will append a single CTA observation
... ... @@ -362,8 +362,7 @@ void ctool::setup_observations(GObservations&amp; obs,
362 362  
363 363 // Throw an exception if the "inobs" parameter is not a valid
364 364 // filename
365   - if ((gammalib::toupper(filename.url()) == "NONE") ||
366   - filename.is_empty()) {
  365 + if (!is_valid_filename(filename)) {
367 366 std::string msg = "The \"inobs\" parameter \""+filename.url()+
368 367 "\" is not a valid filename. Please specify "
369 368 "a valid event list, counts cube or "
... ... @@ -450,7 +449,8 @@ void ctool::setup_observations(GObservations&amp; obs,
450 449 * @param[in] name Mandatory model name
451 450 *
452 451 * @exception GException::invalid_value
453   - * Mandatory model name not found in observation container.
  452 + * Model definiton XML filename not valid or mandatory model name
  453 + * not found in model container.
454 454 *
455 455 * Setup a model container by loading the models from the "inmodel"
456 456 * parameter.
... ... @@ -458,10 +458,12 @@ void ctool::setup_observations(GObservations&amp; obs,
458 458 * If the model container in the observation container @p obs is empty, the
459 459 * method will extract the model container filename from the "inmodel"
460 460 * parameter, load the model container and assign it to the observations
461   - * container.
  461 + * container. If the filename is either empty or "NONE" the method will
  462 + * throw an exception.
462 463 *
463 464 * If a mandatory model name is specified, the method will further check
464   - * whether a model with the name exists in the model container.
  465 + * whether a model with the name exists in the model container. If it does
  466 + * not exist, an exception is thrown.
465 467 ***************************************************************************/
466 468 void ctool::setup_models(GObservations& obs,
467 469 const std::string& name)
... ... @@ -474,6 +476,15 @@ void ctool::setup_models(GObservations&amp; obs,
474 476 // Get models XML filename
475 477 GFilename filename = (*this)["inmodel"].filename();
476 478  
  479 + // Throw an exception if the "inmodel" parameter is not a valid
  480 + // filename
  481 + if (!is_valid_filename(filename)) {
  482 + std::string msg = "The \"inmodel\" parameter \""+filename.url()+
  483 + "\" is not a valid filename. Please specify "
  484 + "a valid model definition XML file.";
  485 + throw GException::invalid_value(G_SETUP_MODELS, msg);
  486 + }
  487 +
477 488 // Load model container and assign it to observations container
478 489 obs.models(GModels(filename.url()));
479 490  
... ... @@ -489,7 +500,7 @@ void ctool::setup_models(GObservations&amp; obs,
489 500 std::string msg = "Model \""+name+"\" not found in model "
490 501 "container. Please add a model with that name "
491 502 "or check for possible typos.";
492   - throw GException::invalid_value(SETUP_MODELS, msg);
  503 + throw GException::invalid_value(G_SETUP_MODELS, msg);
493 504 }
494 505  
495 506 } // endif: mandatory model name existed
... ... @@ -790,18 +801,18 @@ GCTAObservation ctool::create_cta_obs(void)
790 801 *
791 802 * @param[in] method Method name.
792 803 *
793   - * Throw an exception if the inobs parameter is either NONE or an empty
  804 + * Throw an exception if the "inobs" parameter is either "NONE" or an empty
794 805 * string.
795 806 ***************************************************************************/
796 807 void ctool::require_inobs(const std::string& method)
797 808 {
798 809 // Get inobs filename
799   - std::string filename = (*this)["inobs"].filename();
  810 + GFilename filename = (*this)["inobs"].filename();
800 811  
801 812 // Throw exception if no infile is given
802   - if (filename == "NONE" || gammalib::strip_whitespace(filename) == "") {
  813 + if (!is_valid_filename(filename)) {
803 814 std::string msg = "A valid file needs to be specified for the "
804   - "\"inobs\" parameter, yet \""+filename+
  815 + "\"inobs\" parameter, yet \""+filename.url()+
805 816 "\" was given."
806 817 " Specify a valid observation definition or "
807 818 "FITS file to proceed.";
... ... @@ -1165,12 +1176,8 @@ void ctool::set_obs_response(GCTAObservation* obs)
1165 1176 GFilename bkgcube((*this)["bkgcube"].filename());
1166 1177  
1167 1178 // Extract stacked response information if available
1168   - if ((gammalib::toupper(expcube.url()) != "NONE") &&
1169   - (gammalib::toupper(psfcube.url()) != "NONE") &&
1170   - (gammalib::toupper(bkgcube.url()) != "NONE") &&
1171   - !expcube.is_empty() &&
1172   - !psfcube.is_empty() &&
1173   - !bkgcube.is_empty()) {
  1179 + if (is_valid_filename(expcube) && is_valid_filename(psfcube) &&
  1180 + is_valid_filename(bkgcube)) {
1174 1181  
1175 1182 // Load exposure, PSF and background cubes
1176 1183 GCTACubeExposure exposure(expcube);
... ... @@ -1182,8 +1189,7 @@ void ctool::set_obs_response(GCTAObservation* obs)
1182 1189 if (query_edisp) {
1183 1190  
1184 1191 // If filename is valid then use energy dispersion ...
1185   - if ((gammalib::toupper(edispcube.url()) != "NONE") &&
1186   - !edispcube.is_empty()) {
  1192 + if (is_valid_filename(edispcube)) {
1187 1193  
1188 1194 // Load energy dispersion cube
1189 1195 GCTACubeEdisp edisp(edispcube);
... ... @@ -1355,7 +1361,7 @@ GObservations ctool::get_observations(const bool&amp; get_response)
1355 1361  
1356 1362 // If no observation definition file has been specified then read all
1357 1363 // parameters that are necessary to create an observation from scratch
1358   - if ((filename == "NONE") || (gammalib::strip_whitespace(filename) == "")) {
  1364 + if (!is_valid_filename(filename)) {
1359 1365  
1360 1366 // Setup a new CTA observation
1361 1367 GCTAObservation cta_obs = create_cta_obs();
... ... @@ -1371,7 +1377,7 @@ GObservations ctool::get_observations(const bool&amp; get_response)
1371 1377 // Append observation to container
1372 1378 obs.append(cta_obs);
1373 1379  
1374   - } // endif: filename was "NONE" or ""
  1380 + } // endif: filename was valid
1375 1381  
1376 1382 // ... otherwise we have a file name
1377 1383 else {
... ...
src/support/ctool.hpp
... ... @@ -112,6 +112,7 @@ protected:
112 112 const GCTAObservation& obs);
113 113 std::vector<bool> cube_layer_usage(const GEbounds& cube_ebounds,
114 114 const GEbounds& list_ebounds) const;
  115 + bool is_valid_filename(const GFilename& filename) const;
115 116  
116 117 // Protected warning strings
117 118 std::string warn_too_few_energies(const GEnergies& energies) const;
... ... @@ -131,9 +132,14 @@ protected:
131 132  
132 133  
133 134 /***********************************************************************//**
134   - * @brief Return observation container
  135 + * @brief Signal whether parameters should be read ahead
135 136 *
136   - * @return Reference to observation container
  137 + * @return True if parameters should be read ahead
  138 + *
  139 + * Ahead reading of parameter is useful in case that some parameters are only
  140 + * required in a late stage of the processing. By reading them ahead it is
  141 + * assured that these parameters will be queried at the beginning of the
  142 + * tool execution.
137 143 ***************************************************************************/
138 144 inline
139 145 const bool& ctool::read_ahead(void) const
... ... @@ -153,4 +159,19 @@ const GTimeReference&amp; ctool::time_reference(void) const
153 159 return (m_cta_ref);
154 160 }
155 161  
  162 +
  163 +/***********************************************************************//**
  164 + * @brief Check of a filename is valid
  165 + *
  166 + * @return True if filename is valid
  167 + *
  168 + * Checks whether a filename is not empty and not "NONE".
  169 + ***************************************************************************/
  170 +inline
  171 +bool ctool::is_valid_filename(const GFilename& filename) const
  172 +{
  173 + return (!filename.is_empty() &&
  174 + (gammalib::tolower(filename.url()) != "none"));
  175 +}
  176 +
156 177 #endif /* CTOOL_HPP */
... ...