Commit fa1d8f0cfbfda98bac7db5306431b44b8b409883
1 parent
c001eecb
Test existence of root directory.
Showing
2 changed files
with
67 additions
and
41 deletions
include/GCaldb.hpp
| ... | ... | @@ -41,9 +41,10 @@ |
| 41 | 41 | * |
| 42 | 42 | * This class holds the definition of a calibration database. If the void |
| 43 | 43 | * constructor is invoked it tries setting the calibration database access |
| 44 | - * path using the CALDB environment variable. Alternatively, the calibration | |
| 45 | - * database path can be passed to the constructor to override the environment | |
| 46 | - * variable, or set using the dir() method. | |
| 44 | + * path using the GAMMALIB_CALDB or CALDB environment variables (the former | |
| 45 | + * takes precedence over the latter). Alternatively, the calibration database | |
| 46 | + * path can be passed to the constructor to override the environment variable | |
| 47 | + * or set using the dir() method. | |
| 47 | 48 | * |
| 48 | 49 | * It is assumed that the calibration data are found under |
| 49 | 50 | * |
| ... | ... | @@ -95,6 +96,7 @@ protected: |
| 95 | 96 | void init_members(void); |
| 96 | 97 | void copy_members(const GCaldb& caldb); |
| 97 | 98 | void free_members(void); |
| 99 | + std::string rootdir(void) const; | |
| 98 | 100 | void set_database(const std::string& pathname); |
| 99 | 101 | std::string path(const std::string& mission, const std::string& instrument = ""); |
| 100 | 102 | std::string cifname(const std::string& mission, const std::string& instrument = ""); | ... | ... |
src/obs/GCaldb.cpp
| ... | ... | @@ -37,6 +37,7 @@ |
| 37 | 37 | /* __ Method name definitions ____________________________________________ */ |
| 38 | 38 | #define G_CALDB1 "GCaldb()" |
| 39 | 39 | #define G_CALDB2 "GCaldb(std::string)" |
| 40 | +#define G_ROOTDIR "GCaldb::rootdir()" | |
| 40 | 41 | #define G_SET_DATABASE "GCaldb::set_database(std::string)" |
| 41 | 42 | #define G_PATH "GCaldb::path(std::string, std::string)" |
| 42 | 43 | #define G_CIF "GCaldb::cif(std::string, std::string)" |
| ... | ... | @@ -58,30 +59,16 @@ |
| 58 | 59 | /***********************************************************************//** |
| 59 | 60 | * @brief Constructor |
| 60 | 61 | * |
| 61 | - * @exception GException::env_not_found | |
| 62 | - * CALDB environment variable not found | |
| 63 | - * | |
| 64 | 62 | * The void constructor determines the calibration database root directory |
| 65 | - * from the CALDB environment variable. If this environment variable is not | |
| 66 | - * set the constructor will throw an exception. | |
| 63 | + * from the GAMMALIB_CALDB or CALDB environment variables. | |
| 67 | 64 | ***************************************************************************/ |
| 68 | 65 | GCaldb::GCaldb(void) |
| 69 | 66 | { |
| 70 | 67 | // Initialise class members |
| 71 | 68 | init_members(); |
| 72 | 69 | |
| 73 | - // Get root directory from CALDB environment variable | |
| 74 | - char* ptr = std::getenv("CALDB"); | |
| 75 | - | |
| 76 | - // Throw an exception if the environment variable is not found | |
| 77 | - if (ptr == NULL) { | |
| 78 | - throw GException::env_not_found(G_CALDB1, "CALDB", | |
| 79 | - "Please set the CALDB environment variable to a valid" | |
| 80 | - " calibration database root directory."); | |
| 81 | - } | |
| 82 | - | |
| 83 | - // ... otherwise set calibration database | |
| 84 | - set_database(std::string(ptr)); | |
| 70 | + // Set calibration database | |
| 71 | + set_database(rootdir()); | |
| 85 | 72 | |
| 86 | 73 | // Return |
| 87 | 74 | return; |
| ... | ... | @@ -137,28 +124,9 @@ GCaldb::GCaldb(const std::string& pathname) |
| 137 | 124 | } |
| 138 | 125 | |
| 139 | 126 | // ... otherwise try to determine root pathname from GAMMALIB_CALDB |
| 140 | - // environment variable. | |
| 127 | + // or CALDB environment variables. | |
| 141 | 128 | else { |
| 142 | - | |
| 143 | - // Get root directory from GAMMALIB_CALDB environment variable | |
| 144 | - char* ptr = std::getenv("GAMMALIB_CALDB"); | |
| 145 | - | |
| 146 | - // If this failed, get root directory from CALDB environment | |
| 147 | - // variable | |
| 148 | - if (ptr == NULL) { | |
| 149 | - ptr = std::getenv("CALDB"); | |
| 150 | - } | |
| 151 | - | |
| 152 | - // Throw an exception if the environment variable is not found | |
| 153 | - if (ptr == NULL) { | |
| 154 | - throw GException::env_not_found(G_CALDB2, "CALDB", | |
| 155 | - "Please set the GAMMALIB_CALDB or the CALDB environment" | |
| 156 | - " variable to a valid calibration database root directory."); | |
| 157 | - } | |
| 158 | - | |
| 159 | - // ... otherwise set calibration database | |
| 160 | - set_database(std::string(ptr)); | |
| 161 | - | |
| 129 | + set_database(rootdir()); | |
| 162 | 130 | } |
| 163 | 131 | |
| 164 | 132 | // Return |
| ... | ... | @@ -438,6 +406,62 @@ void GCaldb::free_members(void) |
| 438 | 406 | |
| 439 | 407 | |
| 440 | 408 | /***********************************************************************//** |
| 409 | + * @brief Return path to CALDB root directory | |
| 410 | + * | |
| 411 | + * @exception GException::directory_not_found | |
| 412 | + * Calibration directory not found. | |
| 413 | + * @exception GException::directory_not_accessible | |
| 414 | + * No read permission granted to calibration directory. | |
| 415 | + * | |
| 416 | + * The calibration directory path is given by one of the following | |
| 417 | + * | |
| 418 | + * $CALDB/data/<mission> | |
| 419 | + * $CALDB/data/<mission>/<instrument> | |
| 420 | + * $GAMMALIB_CALDB/data/<mission> | |
| 421 | + * $GAMMALIB_CALDB/data/<mission>/<instrument> | |
| 422 | + * | |
| 423 | + * where \<mission\> is the name of the mission and \<instrument\> is the | |
| 424 | + * optional instrument name (all lower case). The arguments provided to the | |
| 425 | + * method are transformed to lower case. | |
| 426 | + ***************************************************************************/ | |
| 427 | +std::string GCaldb::rootdir(void) const | |
| 428 | +{ | |
| 429 | + // Get root directory from GAMMALIB_CALDB and CALDB environment variables | |
| 430 | + char* ptr1 = std::getenv("GAMMALIB_CALDB"); | |
| 431 | + char* ptr2 = std::getenv("CALDB"); | |
| 432 | + | |
| 433 | + // Throw an exception if non of the environment variables was set | |
| 434 | + if (ptr1 == NULL && ptr2 == NULL) { | |
| 435 | + throw GException::env_not_found(G_ROOTDIR, | |
| 436 | + "GAMMALIB_CALDB or CALDB", | |
| 437 | + "Please set the GAMMALIB_CALDB or the CALDB environment" | |
| 438 | + " variable to a valid calibration database root directory."); | |
| 439 | + } | |
| 440 | + | |
| 441 | + // Initialise root directory string | |
| 442 | + std::string rootdir; | |
| 443 | + | |
| 444 | + // If GAMMALIB_CALDB was set then check whether the directory exists | |
| 445 | + if (ptr1 != NULL) { | |
| 446 | + if (access(ptr1, F_OK) == 0) { | |
| 447 | + rootdir = std::string(ptr1); | |
| 448 | + } | |
| 449 | + } | |
| 450 | + | |
| 451 | + // ... otherwise, if CALDB was set then check whether the directory | |
| 452 | + // exists | |
| 453 | + if (ptr2 != NULL) { | |
| 454 | + if (access(ptr2, F_OK) == 0) { | |
| 455 | + rootdir = std::string(ptr2); | |
| 456 | + } | |
| 457 | + } | |
| 458 | + | |
| 459 | + // Return root directory | |
| 460 | + return rootdir; | |
| 461 | +} | |
| 462 | + | |
| 463 | + | |
| 464 | +/***********************************************************************//** | |
| 441 | 465 | * @brief Set calibration database |
| 442 | 466 | * |
| 443 | 467 | * @param[in] pathname Calibration database root directory. | ... | ... |