Commit 17ba1c076166dba3e8fd104c927ce62f5e77e259
1 parent
fae992c1
Add GFilename methods exists(), is_fits() and remove() and remove the equivalent…
… methods from GTools.
Showing
14 changed files
with
207 additions
and
190 deletions
ChangeLog
| 1 | -2016-02-13 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> | |
| 1 | +2016-02-16 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> | |
| 2 | 2 | |
| 3 | 3 | * Version 1.1.0 released |
| 4 | 4 | ========================= |
| 5 | 5 | |
| 6 | + Add GFilename methods and remove equivalents from GTools | |
| 6 | 7 | Use column names for CTA response table access (#1656) |
| 7 | 8 | Add non-const GObservation::events() method (#1664) |
| 8 | 9 | Remove event fetching from GCTAObservation class (#1648) | ... | ... |
NEWS
| ... | ... | @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.1.0 |
| 2 | 2 | |
| 3 | 3 | Author(s) : Juergen Knoedlseder |
| 4 | 4 | |
| 5 | -13 February 2016 | |
| 5 | +16 February 2016 | |
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | 1. Introduction |
| ... | ... | @@ -15,7 +15,10 @@ made since the last release of GammaLib. |
| 15 | 15 | --------------------------------- |
| 16 | 16 | The following methods have been added to the public interface: |
| 17 | 17 | - GObservation::events() (non-const variant) |
| 18 | -- GFilename::operator() | |
| 18 | +- GFilename::operator std::string() | |
| 19 | +- GFilename::exists() | |
| 20 | +- GFilename::is_fits() | |
| 21 | +- GFilename::remove() | |
| 19 | 22 | - GCTAResponseTable::axis(std::string) |
| 20 | 23 | - GCTAResponseTable::table(std::string) |
| 21 | 24 | - GCTAResponseTable::has_axis(std::string) |
| ... | ... | @@ -23,6 +26,9 @@ The following methods have been added to the public interface: |
| 23 | 26 | |
| 24 | 27 | The following methods have been removed: |
| 25 | 28 | - GFilename::size() |
| 29 | +- gammalib::file_exists() | |
| 30 | +- gammalib::file_exists_gzip() | |
| 31 | +- gammalib::is_fits() | |
| 26 | 32 | |
| 27 | 33 | The following methods have been renamed: |
| 28 | 34 | - GCTAResponseTable::tables(), was GCTAResponseTable::size() |
| ... | ... | @@ -127,8 +133,12 @@ None |
| 127 | 133 | |
| 128 | 134 | 13. Support module |
| 129 | 135 | ------------------ |
| 130 | -Add GFilename::fullname() method that returns the full filename including | |
| 131 | -any FITS extension. | |
| 136 | +The GFilename class has been extended and some methods have been renamed. | |
| 137 | +The full filename including and FITS extension is now returned by an | |
| 138 | +implicit type conversion operator std::string(). The filename() method has | |
| 139 | +been renamed to url(), and the methods exists(), is_fits() and remove() | |
| 140 | +have been added to replace functions of the GTools module that were used | |
| 141 | +for this purpose in the past. | |
| 132 | 142 | |
| 133 | 143 | |
| 134 | 144 | 14. XML module | ... | ... |
include/GFilename.hpp
| ... | ... | @@ -89,9 +89,9 @@ public: |
| 89 | 89 | //std::string protocol(void) const; |
| 90 | 90 | //std::string& path(void) const; |
| 91 | 91 | //std::string& file(void) const; |
| 92 | - //bool exists(void) const; | |
| 93 | - //bool is_fits(void) const; | |
| 94 | - //void remove(void) const; | |
| 92 | + bool exists(void) const; | |
| 93 | + bool is_fits(void) const; | |
| 94 | + void remove(void) const; | |
| 95 | 95 | std::string extname(const std::string& defaultname = "") const; |
| 96 | 96 | const std::string& expression(void) const; |
| 97 | 97 | int extno(const int& defaultno = -1) const; |
| ... | ... | @@ -110,10 +110,9 @@ protected: |
| 110 | 110 | void set_filename(const std::string& filename); |
| 111 | 111 | |
| 112 | 112 | // Protected members |
| 113 | - std::string m_fullname; //!< Full file name | |
| 114 | - std::string m_filename; //!< File name (with stripped extension info) | |
| 113 | + std::string m_filename; //!< Full file name | |
| 115 | 114 | //std::string m_protocol; //!< Access protocol |
| 116 | - //std::string m_url; //!< File name (with stripped extension info) | |
| 115 | + std::string m_url; //!< File name (with stripped extension info) | |
| 117 | 116 | //std::string m_path; //!< Path |
| 118 | 117 | std::string m_extname; //!< Extension name ("": not set) |
| 119 | 118 | int m_extno; //!< Extension number (-1: not set) |
| ... | ... | @@ -132,7 +131,7 @@ protected: |
| 132 | 131 | inline |
| 133 | 132 | GFilename::operator std::string(void) const |
| 134 | 133 | { |
| 135 | - return (gammalib::expand_env(m_fullname)); | |
| 134 | + return (gammalib::expand_env(m_filename)); | |
| 136 | 135 | } |
| 137 | 136 | |
| 138 | 137 | |
| ... | ... | @@ -156,7 +155,7 @@ std::string GFilename::classname(void) const |
| 156 | 155 | inline |
| 157 | 156 | bool GFilename::is_empty(void) const |
| 158 | 157 | { |
| 159 | - return (m_fullname.empty()); | |
| 158 | + return (m_filename.empty()); | |
| 160 | 159 | } |
| 161 | 160 | |
| 162 | 161 | |
| ... | ... | @@ -164,11 +163,13 @@ bool GFilename::is_empty(void) const |
| 164 | 163 | * @brief Return length of filename |
| 165 | 164 | * |
| 166 | 165 | * @return Length of filename. |
| 166 | + * | |
| 167 | + * Returns the length of the filename, excluding any FITS extension. | |
| 167 | 168 | ***************************************************************************/ |
| 168 | 169 | inline |
| 169 | 170 | int GFilename::length(void) const |
| 170 | 171 | { |
| 171 | - return (m_filename.length()); | |
| 172 | + return (m_url.length()); | |
| 172 | 173 | } |
| 173 | 174 | |
| 174 | 175 | |
| ... | ... | @@ -183,7 +184,7 @@ int GFilename::length(void) const |
| 183 | 184 | inline |
| 184 | 185 | std::string GFilename::url(void) const |
| 185 | 186 | { |
| 186 | - return (gammalib::expand_env(m_filename)); | |
| 187 | + return (gammalib::expand_env(m_url)); | |
| 187 | 188 | } |
| 188 | 189 | |
| 189 | 190 | ... | ... |
include/GTools.hpp
| ... | ... | @@ -95,12 +95,9 @@ namespace gammalib { |
| 95 | 95 | const double& epivot, |
| 96 | 96 | const double& gamma); |
| 97 | 97 | GEnergy elogmean(const GEnergy& a, const GEnergy& b); |
| 98 | - bool file_exists(const std::string& filename); | |
| 99 | - bool file_exists_gzip(const std::string& filename); | |
| 100 | 98 | bool dir_exists(const std::string& dirname); |
| 101 | 99 | bool is_infinite(const double& x); |
| 102 | 100 | bool is_notanumber(const double& x); |
| 103 | - bool is_fits(const std::string& filename); | |
| 104 | 101 | bool contains(const std::string& str, |
| 105 | 102 | const std::string& substring); |
| 106 | 103 | void warning(const std::string& origin, | ... | ... |
inst/com/src/GCOMResponse.cpp
| ... | ... | @@ -465,23 +465,17 @@ void GCOMResponse::load(const std::string& rspname) |
| 465 | 465 | m_rspname = rspname; |
| 466 | 466 | |
| 467 | 467 | // First attempt reading the response using the GCaldb interface |
| 468 | - std::string filename = m_caldb.filename("","","IAQ","","",rspname); | |
| 468 | + GFilename filename = m_caldb.filename("","","IAQ","","",rspname); | |
| 469 | 469 | |
| 470 | 470 | // If filename is empty then build filename from CALDB root path and |
| 471 | 471 | // response name |
| 472 | - if (filename.empty()) { | |
| 472 | + if (filename.is_empty()) { | |
| 473 | 473 | filename = gammalib::filepath(m_caldb.rootdir(), rspname); |
| 474 | - if (!gammalib::file_exists(filename)) { | |
| 475 | - std::string testname = filename + ".fits"; | |
| 476 | - if (gammalib::file_exists(testname)) { | |
| 474 | + if (!filename.exists()) { | |
| 475 | + GFilename testname = filename + ".fits"; | |
| 476 | + if (testname.exists()) { | |
| 477 | 477 | filename = testname; |
| 478 | 478 | } |
| 479 | - else { | |
| 480 | - std::string testname = filename + ".fits.gz"; | |
| 481 | - if (gammalib::file_exists(testname)) { | |
| 482 | - filename = testname; | |
| 483 | - } | |
| 484 | - } | |
| 485 | 479 | } |
| 486 | 480 | } |
| 487 | 481 | ... | ... |
inst/cta/src/GCTAEventList.cpp
| ... | ... | @@ -642,10 +642,10 @@ void GCTAEventList::fetch(void) const |
| 642 | 642 | if (!m_filename.is_empty()) { |
| 643 | 643 | |
| 644 | 644 | // Throw an exception if the file does not exist |
| 645 | - if (!gammalib::file_exists_gzip(m_filename)) { | |
| 646 | - std::string msg = "File \""+m_filename+"\" not " | |
| 647 | - "found. Cannot fetch events. Maybe the " | |
| 648 | - "file has been deleted in the meantime."; | |
| 645 | + if (!m_filename.exists()) { | |
| 646 | + std::string msg = "File \""+m_filename+"\" not found. " | |
| 647 | + "Cannot fetch events. Maybe the file has " | |
| 648 | + "been deleted in the meantime."; | |
| 649 | 649 | GException::file_error(G_FETCH, msg); |
| 650 | 650 | } |
| 651 | 651 | ... | ... |
inst/cta/src/GCTAResponseIrf.cpp
| ... | ... | @@ -1095,14 +1095,14 @@ void GCTAResponseIrf::load_aeff(const GFilename& filename) |
| 1095 | 1095 | m_aeff = NULL; |
| 1096 | 1096 | |
| 1097 | 1097 | // Check for existence of file |
| 1098 | - if (!gammalib::file_exists_gzip(filename)) { | |
| 1098 | + if (!filename.exists()) { | |
| 1099 | 1099 | std::string msg = "File \""+filename+"\" not found. Please specify " |
| 1100 | 1100 | "a valid effective area response file."; |
| 1101 | 1101 | throw GException::file_error(G_LOAD_AEFF, msg); |
| 1102 | 1102 | } |
| 1103 | 1103 | |
| 1104 | 1104 | // If file is a FITS file ... |
| 1105 | - if (gammalib::is_fits(filename)) { | |
| 1105 | + if (filename.is_fits()) { | |
| 1106 | 1106 | |
| 1107 | 1107 | // Open FITS file |
| 1108 | 1108 | GFits fits(filename); |
| ... | ... | @@ -1213,14 +1213,14 @@ void GCTAResponseIrf::load_psf(const GFilename& filename) |
| 1213 | 1213 | m_psf = NULL; |
| 1214 | 1214 | |
| 1215 | 1215 | // Check for existence of file |
| 1216 | - if (!gammalib::file_exists_gzip(filename)) { | |
| 1216 | + if (!filename.exists()) { | |
| 1217 | 1217 | std::string msg = "File \""+filename+"\" not found. Please specify " |
| 1218 | 1218 | "a valid point spread function response file."; |
| 1219 | 1219 | throw GException::file_error(G_LOAD_PSF, msg); |
| 1220 | 1220 | } |
| 1221 | 1221 | |
| 1222 | 1222 | // If file is a FITS file ... |
| 1223 | - if (gammalib::is_fits(filename)) { | |
| 1223 | + if (filename.is_fits()) { | |
| 1224 | 1224 | |
| 1225 | 1225 | // Open FITS file |
| 1226 | 1226 | GFits fits(filename); |
| ... | ... | @@ -1336,14 +1336,14 @@ void GCTAResponseIrf::load_edisp(const GFilename& filename) |
| 1336 | 1336 | m_edisp = NULL; |
| 1337 | 1337 | |
| 1338 | 1338 | // Check for existence of file |
| 1339 | - if (!gammalib::file_exists_gzip(filename)) { | |
| 1339 | + if (!filename.exists()) { | |
| 1340 | 1340 | std::string msg = "File \""+filename+"\" not found. Please specify " |
| 1341 | 1341 | "a valid energy dispersion response file."; |
| 1342 | 1342 | throw GException::file_error(G_LOAD_EDISP, msg); |
| 1343 | 1343 | } |
| 1344 | 1344 | |
| 1345 | 1345 | // If file is a FITS file ... |
| 1346 | - if (gammalib::is_fits(filename)) { | |
| 1346 | + if (filename.is_fits()) { | |
| 1347 | 1347 | |
| 1348 | 1348 | // Open FITS file |
| 1349 | 1349 | GFits fits(filename); |
| ... | ... | @@ -1441,14 +1441,14 @@ void GCTAResponseIrf::load_background(const GFilename& filename) |
| 1441 | 1441 | m_background = NULL; |
| 1442 | 1442 | |
| 1443 | 1443 | // Check for existence of file |
| 1444 | - if (!gammalib::file_exists_gzip(filename)) { | |
| 1444 | + if (!filename.exists()) { | |
| 1445 | 1445 | std::string msg = "File \""+filename+"\" not found. Please specify " |
| 1446 | 1446 | "a valid background response file."; |
| 1447 | 1447 | throw GException::file_error(G_LOAD_BACKGROUND, msg); |
| 1448 | 1448 | } |
| 1449 | 1449 | |
| 1450 | 1450 | // If file is a FITS file than load background as 3D background |
| 1451 | - if (gammalib::is_fits(filename)) { | |
| 1451 | + if (filename.is_fits()) { | |
| 1452 | 1452 | m_background = new GCTABackground3D(filename); |
| 1453 | 1453 | } |
| 1454 | 1454 | |
| ... | ... | @@ -2159,18 +2159,18 @@ void GCTAResponseIrf::free_members(void) |
| 2159 | 2159 | std::string GCTAResponseIrf::irf_filename(const std::string& filename) const |
| 2160 | 2160 | { |
| 2161 | 2161 | // Set input filename as result filename |
| 2162 | - std::string result = filename; | |
| 2162 | + GFilename result = filename; | |
| 2163 | 2163 | |
| 2164 | 2164 | // If file does not exist then try a variant with extension .dat |
| 2165 | - if (!gammalib::file_exists(result)) { | |
| 2166 | - std::string testname = result + ".dat"; | |
| 2167 | - if (gammalib::file_exists(testname)) { | |
| 2165 | + if (!result.exists()) { | |
| 2166 | + GFilename testname = filename + ".dat"; | |
| 2167 | + if (testname.exists()) { | |
| 2168 | 2168 | result = testname; |
| 2169 | 2169 | } |
| 2170 | 2170 | } |
| 2171 | 2171 | |
| 2172 | 2172 | // Return result |
| 2173 | - return result; | |
| 2173 | + return (result.url()); | |
| 2174 | 2174 | } |
| 2175 | 2175 | |
| 2176 | 2176 | ... | ... |
pyext/GFilename.i
| ... | ... | @@ -54,6 +54,9 @@ public: |
| 54 | 54 | //std::string protocol(void) const; |
| 55 | 55 | //std::string path(void) const; |
| 56 | 56 | //std::string file(void) const; |
| 57 | + bool exists(void) const; | |
| 58 | + bool is_fits(void) const; | |
| 59 | + void remove(void) const; | |
| 57 | 60 | std::string extname(const std::string& defaultname = "") const; |
| 58 | 61 | const std::string& expression(void) const; |
| 59 | 62 | int extno(const int& defaultno = -1) const; | ... | ... |
pyext/GTools.i
| ... | ... | @@ -62,12 +62,9 @@ namespace gammalib { |
| 62 | 62 | const double& epivot, |
| 63 | 63 | const double& gamma); |
| 64 | 64 | GEnergy elogmean(const GEnergy& a, const GEnergy& b); |
| 65 | - bool file_exists(const std::string& filename); | |
| 66 | - bool file_exists_gzip(const std::string& filename); | |
| 67 | 65 | bool dir_exists(const std::string& dirname); |
| 68 | 66 | bool is_infinite(const double& x); |
| 69 | 67 | bool is_notanumber(const double& x); |
| 70 | - bool is_fits(const std::string& filename); | |
| 71 | 68 | bool contains(const std::string& str, |
| 72 | 69 | const std::string& substring); |
| 73 | 70 | void warning(const std::string& origin, | ... | ... |
src/fits/GFits.cpp
| ... | ... | @@ -1213,7 +1213,7 @@ void GFits::save(const bool& clobber) |
| 1213 | 1213 | * @brief Saves to specified FITS file |
| 1214 | 1214 | * |
| 1215 | 1215 | * @param[in] filename Filename. |
| 1216 | - * @param[in] clobber Overwrite existing FITS file (true/false). | |
| 1216 | + * @param[in] clobber Overwrite existing FITS file? (default: false) | |
| 1217 | 1217 | * |
| 1218 | 1218 | * @exception GException::fits_file_exist |
| 1219 | 1219 | * Specified file exists already. Overwriting requires |
| ... | ... | @@ -1233,23 +1233,12 @@ void GFits::saveto(const GFilename& filename, const bool& clobber) |
| 1233 | 1233 | << " (size=" << size() << ") -->" << std::endl; |
| 1234 | 1234 | #endif |
| 1235 | 1235 | |
| 1236 | - // If overwriting has been specified then remove any existing file. We | |
| 1237 | - // consider here also the possibility that the file is gzipped, but | |
| 1238 | - // the file name does not contain the .gz extension. | |
| 1236 | + // If overwriting has been specified then remove the file. Otherwise, | |
| 1237 | + // throw an exception if the file exists. | |
| 1239 | 1238 | if (clobber) { |
| 1240 | - if (gammalib::file_exists(filename)) { | |
| 1241 | - std::remove(filename.url().c_str()); | |
| 1242 | - } | |
| 1243 | - else { | |
| 1244 | - if (gammalib::file_exists(gzfname)) { | |
| 1245 | - std::remove(gzfname.c_str()); | |
| 1246 | - } | |
| 1247 | - } | |
| 1239 | + filename.remove(); | |
| 1248 | 1240 | } |
| 1249 | - | |
| 1250 | - // Otherwise, if file exists then throw an exception | |
| 1251 | - else if (gammalib::file_exists(filename) || | |
| 1252 | - gammalib::file_exists(gzfname)) { | |
| 1241 | + else if (filename.exists()) { | |
| 1253 | 1242 | throw GException::fits_file_exist(G_SAVETO, filename); |
| 1254 | 1243 | } |
| 1255 | 1244 | ... | ... |
src/support/GCsv.cpp
| ... | ... | @@ -454,29 +454,25 @@ void GCsv::load(const GFilename& filename, const std::string& sep) |
| 454 | 454 | * @exception GException::file_error |
| 455 | 455 | * Unable to create file. |
| 456 | 456 | * |
| 457 | - * Save CSV table into ASCII file. Any environment variable present in the | |
| 458 | - * filename will be expanded. | |
| 457 | + * Save CSV table into ASCII file. | |
| 459 | 458 | **************************************************************************/ |
| 460 | 459 | void GCsv::save(const GFilename& filename, |
| 461 | 460 | const std::string& sep, |
| 462 | 461 | const bool& clobber) const |
| 463 | 462 | { |
| 464 | 463 | // Throw exception if file exists but clobber flag is false |
| 465 | - if (!clobber && gammalib::file_exists(filename.url())) { | |
| 466 | - std::string msg = "File \""+filename.url()+"\" exists already but " | |
| 464 | + if (!clobber && filename.exists()) { | |
| 465 | + std::string msg = "File \""+filename+"\" exists already but " | |
| 467 | 466 | "the clobber flag is set to \"false\". Set the " |
| 468 | 467 | "clobber flag to true to overwrite the existing " |
| 469 | 468 | "file or specify another file name."; |
| 470 | 469 | throw GException::invalid_value(G_SAVE, msg); |
| 471 | 470 | } |
| 472 | 471 | |
| 473 | - // Expand environment variables | |
| 474 | - std::string fname = gammalib::expand_env(filename.url()); | |
| 475 | - | |
| 476 | 472 | // Open CSV table (write-only) |
| 477 | - FILE* fptr = std::fopen(fname.c_str(), "w"); | |
| 473 | + FILE* fptr = std::fopen(filename.url().c_str(), "w"); | |
| 478 | 474 | if (fptr == NULL) { |
| 479 | - std::string msg = "Unable to create file \""+filename.url()+"\"."; | |
| 475 | + std::string msg = "Unable to create file \""+filename+"\"."; | |
| 480 | 476 | throw GException::file_error(G_SAVE, msg); |
| 481 | 477 | } |
| 482 | 478 | ... | ... |
src/support/GFilename.cpp
| ... | ... | @@ -28,6 +28,16 @@ |
| 28 | 28 | #ifdef HAVE_CONFIG_H |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | +#if defined(HAVE_LIBCFITSIO) | |
| 32 | +#if defined(HAVE_CFITSIO_FITSIO_H) | |
| 33 | +#include <cfitsio/fitsio.h> | |
| 34 | +#elif defined(HAVE_LIBCFITSIO0_FITSIO_H) | |
| 35 | +#include <libcfitsio0/fitsio.h> | |
| 36 | +#else | |
| 37 | +#include <fitsio.h> | |
| 38 | +#endif | |
| 39 | +#endif | |
| 40 | +#include <sys/stat.h> // for stat structure and S_ISREG | |
| 31 | 41 | #include "GFilename.hpp" |
| 32 | 42 | #include "GTools.hpp" |
| 33 | 43 | #include "GException.hpp" |
| ... | ... | @@ -200,6 +210,108 @@ GFilename* GFilename::clone(void) const |
| 200 | 210 | |
| 201 | 211 | |
| 202 | 212 | /***********************************************************************//** |
| 213 | + * @brief Checks whether file exists | |
| 214 | + * | |
| 215 | + * @return True if file exists. | |
| 216 | + * | |
| 217 | + * Checks whether a file exists on disk. In case that the file is a FITS | |
| 218 | + * file, the method also checks whether a compressed version of the file | |
| 219 | + * (with a .gz, .Z, .z, or .zip extension) exists on disk (see is_fits() | |
| 220 | + * method). | |
| 221 | + ***************************************************************************/ | |
| 222 | +bool GFilename::exists(void) const | |
| 223 | +{ | |
| 224 | + // First check if file is a FITS file | |
| 225 | + bool exists = is_fits(); | |
| 226 | + | |
| 227 | + // If the file is not a FITS file, then check whether a file with the | |
| 228 | + // given name exists | |
| 229 | + if (!exists) { | |
| 230 | + | |
| 231 | + // Allocate file information structure | |
| 232 | + struct stat info; | |
| 233 | + | |
| 234 | + // Get file information structure | |
| 235 | + int ret = stat(url().c_str(), &info); | |
| 236 | + | |
| 237 | + // If the file is a regular file then signal that it exists | |
| 238 | + if (ret == 0 && S_ISREG(info.st_mode)) { | |
| 239 | + exists = true; | |
| 240 | + } | |
| 241 | + | |
| 242 | + } // endif: file was not a FITS file | |
| 243 | + | |
| 244 | + // Return result | |
| 245 | + return (exists); | |
| 246 | +} | |
| 247 | + | |
| 248 | + | |
| 249 | +/***********************************************************************//** | |
| 250 | + * @brief Checks whether file is a FITS file | |
| 251 | + * | |
| 252 | + * @return True if file is a FITS file. | |
| 253 | + * | |
| 254 | + * Test if the file or a compressed version of the file (with a .gz, .Z, .z, | |
| 255 | + * or .zip extension) is a FITS file. | |
| 256 | + ***************************************************************************/ | |
| 257 | +bool GFilename::is_fits(void) const | |
| 258 | +{ | |
| 259 | + // Initialise result | |
| 260 | + bool is_fits = false; | |
| 261 | + | |
| 262 | + // Check now for a FITS file. This works only if cfitsio is available. | |
| 263 | + #if defined(HAVE_LIBCFITSIO) | |
| 264 | + int status = 0; | |
| 265 | + fitsfile* fptr = NULL; | |
| 266 | + status = ffopen(&fptr, url().c_str(), 0, &status); | |
| 267 | + if (status == 0) { | |
| 268 | + is_fits = true; | |
| 269 | + } | |
| 270 | + ffclos(fptr, &status); | |
| 271 | + #endif | |
| 272 | + | |
| 273 | + // Return result | |
| 274 | + return (is_fits); | |
| 275 | +} | |
| 276 | + | |
| 277 | + | |
| 278 | +/***********************************************************************//** | |
| 279 | + * @brief Remove file from disk | |
| 280 | + * | |
| 281 | + * Removes file or a compressed version of the file (with a .gz, .Z, .z, | |
| 282 | + * or .zip extension) if it exists on disk. | |
| 283 | + ***************************************************************************/ | |
| 284 | +void GFilename::remove(void) const | |
| 285 | +{ | |
| 286 | + // Set compressed file name variants | |
| 287 | + GFilename name_gz = url()+".gz"; | |
| 288 | + GFilename name_Z = url()+".Z"; | |
| 289 | + GFilename name_z = url()+".z"; | |
| 290 | + GFilename name_zip = url()+".zip"; | |
| 291 | + | |
| 292 | + // Remove file if one of the variants exists on disk | |
| 293 | + if (name_gz.exists()) { | |
| 294 | + std::remove(name_gz.url().c_str()); | |
| 295 | + } | |
| 296 | + else if (name_Z.exists()) { | |
| 297 | + std::remove(name_Z.url().c_str()); | |
| 298 | + } | |
| 299 | + else if (name_z.exists()) { | |
| 300 | + std::remove(name_z.url().c_str()); | |
| 301 | + } | |
| 302 | + else if (name_zip.exists()) { | |
| 303 | + std::remove(name_zip.url().c_str()); | |
| 304 | + } | |
| 305 | + else if (exists()) { | |
| 306 | + std::remove(url().c_str()); | |
| 307 | + } | |
| 308 | + | |
| 309 | + // Return | |
| 310 | + return; | |
| 311 | +} | |
| 312 | + | |
| 313 | + | |
| 314 | +/***********************************************************************//** | |
| 203 | 315 | * @brief Return extension name |
| 204 | 316 | * |
| 205 | 317 | * @param[in] defaultname Default extension name (default: ""). |
| ... | ... | @@ -291,10 +403,14 @@ std::string GFilename::print(const GChatter& chatter) const |
| 291 | 403 | // Append header |
| 292 | 404 | result.append("=== GFilename ==="); |
| 293 | 405 | |
| 294 | - // Append file name | |
| 406 | + // Append filename | |
| 295 | 407 | result.append("\n"+gammalib::parformat("File name")); |
| 296 | 408 | result.append(m_filename); |
| 297 | 409 | |
| 410 | + // Append URL | |
| 411 | + result.append("\n"+gammalib::parformat("URL")); | |
| 412 | + result.append(m_url); | |
| 413 | + | |
| 298 | 414 | // Append extension name |
| 299 | 415 | result.append("\n"+gammalib::parformat("Extension name")); |
| 300 | 416 | if (has_extname()) { |
| ... | ... | @@ -350,8 +466,8 @@ std::string GFilename::print(const GChatter& chatter) const |
| 350 | 466 | void GFilename::init_members(void) |
| 351 | 467 | { |
| 352 | 468 | // Initialise members |
| 353 | - m_fullname.clear(); | |
| 354 | 469 | m_filename.clear(); |
| 470 | + m_url.clear(); | |
| 355 | 471 | m_extname.clear(); |
| 356 | 472 | m_extno = -1; |
| 357 | 473 | m_extver = 0; |
| ... | ... | @@ -370,8 +486,8 @@ void GFilename::init_members(void) |
| 370 | 486 | void GFilename::copy_members(const GFilename& filename) |
| 371 | 487 | { |
| 372 | 488 | // Copy members |
| 373 | - m_fullname = filename.m_fullname; | |
| 374 | 489 | m_filename = filename.m_filename; |
| 490 | + m_url = filename.m_url; | |
| 375 | 491 | m_extname = filename.m_extname; |
| 376 | 492 | m_extno = filename.m_extno; |
| 377 | 493 | m_extver = filename.m_extver; |
| ... | ... | @@ -414,47 +530,47 @@ void GFilename::set_filename(const std::string& filename) |
| 414 | 530 | clear(); |
| 415 | 531 | |
| 416 | 532 | // Strip any whitespace and store the full name |
| 417 | - m_fullname = gammalib::strip_whitespace(filename); | |
| 533 | + m_filename = gammalib::strip_whitespace(filename); | |
| 418 | 534 | |
| 419 | 535 | // Continue only if filename is not empty |
| 420 | - if (!m_fullname.empty()) { | |
| 536 | + if (!m_filename.empty()) { | |
| 421 | 537 | |
| 422 | 538 | // Check for [ symbol in filename |
| 423 | - size_t start = m_fullname.find_first_of("["); | |
| 539 | + size_t start = m_filename.find_first_of("["); | |
| 424 | 540 | |
| 425 | 541 | // If we have an extension then separate filename into filename |
| 426 | 542 | // part and extension part and fill the information of th object |
| 427 | 543 | if (start != std::string::npos) { |
| 428 | 544 | |
| 429 | 545 | // Check for ] symbol in filename |
| 430 | - size_t stop = m_fullname.find_first_of("]", start); | |
| 546 | + size_t stop = m_filename.find_first_of("]", start); | |
| 431 | 547 | |
| 432 | 548 | // If there is no ] symbol then throw an exception |
| 433 | 549 | if (stop == std::string::npos) { |
| 434 | 550 | std::string msg = "Missing ] symbol in file name \""+ |
| 435 | - m_fullname+"\" extension. Please correct " | |
| 551 | + m_filename+"\" extension. Please correct " | |
| 436 | 552 | "the file name."; |
| 437 | 553 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 438 | 554 | } |
| 439 | 555 | |
| 440 | 556 | // If ] is not the last character then check if an opening bracket is next |
| 441 | - if (stop < m_fullname.length()-1) { | |
| 557 | + if (stop < m_filename.length()-1) { | |
| 442 | 558 | |
| 443 | 559 | // Get character |
| 444 | - std::string character = gammalib::strip_whitespace(m_fullname.substr(stop+1,1)); | |
| 560 | + std::string character = gammalib::strip_whitespace(m_filename.substr(stop+1,1)); | |
| 445 | 561 | |
| 446 | 562 | // If no bracket is coming, throw an exception |
| 447 | 563 | if (character != "[") { |
| 448 | 564 | std::string msg = "Non-bracket character \""+character+ |
| 449 | 565 | "\" beyond ] symbol in file name \""+ |
| 450 | - m_fullname+"\" expression. Please " | |
| 566 | + m_filename+"\" expression. Please " | |
| 451 | 567 | "correct the file name."; |
| 452 | 568 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 453 | 569 | } |
| 454 | 570 | } |
| 455 | 571 | |
| 456 | 572 | // Extract extension name |
| 457 | - std::string extname = gammalib::strip_whitespace(m_fullname.substr(start+1, stop-start-1)); | |
| 573 | + std::string extname = gammalib::strip_whitespace(m_filename.substr(start+1, stop-start-1)); | |
| 458 | 574 | |
| 459 | 575 | // Check if there is a , symbol that separates extension name |
| 460 | 576 | // and extension version |
| ... | ... | @@ -469,7 +585,7 @@ void GFilename::set_filename(const std::string& filename) |
| 469 | 585 | // If string is empty then throw an exception |
| 470 | 586 | if (extver.empty()) { |
| 471 | 587 | std::string msg = "No extension version found after , " |
| 472 | - "symbol in file name \""+m_fullname+ | |
| 588 | + "symbol in file name \""+m_filename+ | |
| 473 | 589 | "\". Please correct the file name."; |
| 474 | 590 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 475 | 591 | } |
| ... | ... | @@ -482,7 +598,7 @@ void GFilename::set_filename(const std::string& filename) |
| 482 | 598 | if (m_extver <= 0) { |
| 483 | 599 | std::string msg = "Non-positive extension version " |
| 484 | 600 | "encountered in file name \""+ |
| 485 | - m_fullname+ "\". Please correct the " | |
| 601 | + m_filename+ "\". Please correct the " | |
| 486 | 602 | "file name."; |
| 487 | 603 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 488 | 604 | } |
| ... | ... | @@ -495,7 +611,7 @@ void GFilename::set_filename(const std::string& filename) |
| 495 | 611 | // If we have an empty extension name then throw an exception |
| 496 | 612 | if (extname.empty()) { |
| 497 | 613 | std::string msg = "An empty extension has been specified in " |
| 498 | - "file name \""+m_fullname+"\". Please " | |
| 614 | + "file name \""+m_filename+"\". Please " | |
| 499 | 615 | "correct the file name."; |
| 500 | 616 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 501 | 617 | } |
| ... | ... | @@ -511,7 +627,7 @@ void GFilename::set_filename(const std::string& filename) |
| 511 | 627 | // positive |
| 512 | 628 | if (m_extno < 0) { |
| 513 | 629 | std::string msg = "Negative extension number encountered " |
| 514 | - "in file name \""+m_fullname+"\". " | |
| 630 | + "in file name \""+m_filename+"\". " | |
| 515 | 631 | "Please correct the file name."; |
| 516 | 632 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 517 | 633 | } |
| ... | ... | @@ -527,7 +643,7 @@ void GFilename::set_filename(const std::string& filename) |
| 527 | 643 | // Throw an exception if extension name is empty |
| 528 | 644 | if (m_extname.empty()) { |
| 529 | 645 | std::string msg = "Empty extension name encountered " |
| 530 | - "in file name \""+m_fullname+"\". " | |
| 646 | + "in file name \""+m_filename+"\". " | |
| 531 | 647 | "Please correct the file name."; |
| 532 | 648 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 533 | 649 | } |
| ... | ... | @@ -535,41 +651,41 @@ void GFilename::set_filename(const std::string& filename) |
| 535 | 651 | } // endelse: extension name provided |
| 536 | 652 | |
| 537 | 653 | // Store the file name |
| 538 | - m_filename = gammalib::strip_whitespace(m_fullname.substr(0,start)); | |
| 654 | + m_url = gammalib::strip_whitespace(m_filename.substr(0,start)); | |
| 539 | 655 | |
| 540 | 656 | // Check if there is an expression behind the extension name |
| 541 | - size_t expr_start = m_fullname.find_first_of("[", stop+1); | |
| 657 | + size_t expr_start = m_filename.find_first_of("[", stop+1); | |
| 542 | 658 | |
| 543 | 659 | if (expr_start != std::string::npos) { |
| 544 | 660 | |
| 545 | 661 | // Check for ] symbol in expression |
| 546 | - size_t expr_stop = m_fullname.find_first_of("]", expr_start); | |
| 662 | + size_t expr_stop = m_filename.find_first_of("]", expr_start); | |
| 547 | 663 | |
| 548 | 664 | // If there is no ] symbol then throw an exception |
| 549 | 665 | if (expr_stop == std::string::npos) { |
| 550 | 666 | std::string msg = "Missing ] symbol in file name \""+ |
| 551 | - m_fullname+"\" expression. Please " | |
| 667 | + m_filename+"\" expression. Please " | |
| 552 | 668 | "correct the file name."; |
| 553 | 669 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 554 | 670 | } |
| 555 | 671 | |
| 556 | 672 | // If ] is not the last character then throw an exception |
| 557 | - if (expr_stop < m_fullname.length()-1) { | |
| 673 | + if (expr_stop < m_filename.length()-1) { | |
| 558 | 674 | std::string msg = "Characters beyond ] symbol in file " |
| 559 | - "name \""+m_fullname+"\" expression. " | |
| 675 | + "name \""+m_filename+"\" expression. " | |
| 560 | 676 | "Please correct the file name."; |
| 561 | 677 | throw GException::invalid_argument(G_SET_FILENAME, msg); |
| 562 | 678 | } |
| 563 | 679 | |
| 564 | 680 | // Store expression |
| 565 | - m_expression = gammalib::strip_whitespace(m_fullname.substr(expr_start+1, expr_stop - expr_start-1)); | |
| 681 | + m_expression = gammalib::strip_whitespace(m_filename.substr(expr_start+1, expr_stop - expr_start-1)); | |
| 566 | 682 | } |
| 567 | 683 | |
| 568 | 684 | } // endif: had extension |
| 569 | 685 | |
| 570 | 686 | // ... otherwise simply set the filename |
| 571 | 687 | else { |
| 572 | - m_filename = m_fullname; | |
| 688 | + m_url = m_filename; | |
| 573 | 689 | } |
| 574 | 690 | |
| 575 | 691 | } // endif: filename was not empty | ... | ... |
src/support/GTools.cpp
| ... | ... | @@ -1002,58 +1002,6 @@ GEnergy gammalib::elogmean(const GEnergy& a, const GEnergy& b) |
| 1002 | 1002 | |
| 1003 | 1003 | |
| 1004 | 1004 | /***********************************************************************//** |
| 1005 | - * @brief Checks if file exists | |
| 1006 | - * | |
| 1007 | - * @param[in] filename File name. | |
| 1008 | - * @return True if file exists, false otherwise. | |
| 1009 | - * | |
| 1010 | - * Checks if a file exists. If a directory with the same name is found, false | |
| 1011 | - * is returned. The function expands any environment variable prior to | |
| 1012 | - * checking. | |
| 1013 | - ***************************************************************************/ | |
| 1014 | -bool gammalib::file_exists(const std::string& filename) | |
| 1015 | -{ | |
| 1016 | - // Initialise result | |
| 1017 | - bool result = false; | |
| 1018 | - | |
| 1019 | - // Allocate file information structure | |
| 1020 | - struct stat info; | |
| 1021 | - | |
| 1022 | - // Get file information structure | |
| 1023 | - int ret = stat(gammalib::expand_env(filename).c_str(), &info); | |
| 1024 | - | |
| 1025 | - // Check if file is a regular file | |
| 1026 | - if (ret == 0 && S_ISREG(info.st_mode)) { | |
| 1027 | - result = true; | |
| 1028 | - } | |
| 1029 | - | |
| 1030 | - // Return result | |
| 1031 | - return result; | |
| 1032 | -} | |
| 1033 | - | |
| 1034 | - | |
| 1035 | -/***********************************************************************//** | |
| 1036 | - * @brief Checks if either uncompressed or compressed file exists | |
| 1037 | - * | |
| 1038 | - * @param[in] filename Uncompressed file name (withput ".gz" extension) | |
| 1039 | - * @return True if file exists, false otherwise. | |
| 1040 | - * | |
| 1041 | - * Checks if a file exists, either as an uncompressed file or with a ".gz" | |
| 1042 | - * extension. If a directory with the same name is found, false is returned. | |
| 1043 | - * The function expands any environment variable prior to checking. | |
| 1044 | - ***************************************************************************/ | |
| 1045 | -bool gammalib::file_exists_gzip(const std::string& filename) | |
| 1046 | -{ | |
| 1047 | - // Get result | |
| 1048 | - bool result = gammalib::file_exists(filename) || | |
| 1049 | - gammalib::file_exists(filename+".gz"); | |
| 1050 | - | |
| 1051 | - // Return result | |
| 1052 | - return result; | |
| 1053 | -} | |
| 1054 | - | |
| 1055 | - | |
| 1056 | -/***********************************************************************//** | |
| 1057 | 1005 | * @brief Checks if directory exists |
| 1058 | 1006 | * |
| 1059 | 1007 | * @param[in] dirname Directory name. |
| ... | ... | @@ -1084,41 +1032,6 @@ bool gammalib::dir_exists(const std::string& dirname) |
| 1084 | 1032 | |
| 1085 | 1033 | |
| 1086 | 1034 | /***********************************************************************//** |
| 1087 | - * @brief Checks if file is a FITS file | |
| 1088 | - * | |
| 1089 | - * @param[in] filename File name. | |
| 1090 | - * @return True if file is a FITS file. | |
| 1091 | - * | |
| 1092 | - * Checks if the specified file is a FITS file. | |
| 1093 | - ***************************************************************************/ | |
| 1094 | -bool gammalib::is_fits(const std::string& filename) | |
| 1095 | -{ | |
| 1096 | - // Initialise result | |
| 1097 | - bool result = false; | |
| 1098 | - | |
| 1099 | - // Try opening file | |
| 1100 | - try { | |
| 1101 | - | |
| 1102 | - // Open file | |
| 1103 | - GFits fits(filename); | |
| 1104 | - | |
| 1105 | - // If we're still alive then close file and set result to true | |
| 1106 | - fits.close(); | |
| 1107 | - result = true; | |
| 1108 | - | |
| 1109 | - } | |
| 1110 | - | |
| 1111 | - // Catch any exceptions | |
| 1112 | - catch (...) { | |
| 1113 | - ; | |
| 1114 | - } | |
| 1115 | - | |
| 1116 | - // Return result | |
| 1117 | - return result; | |
| 1118 | -} | |
| 1119 | - | |
| 1120 | - | |
| 1121 | -/***********************************************************************//** | |
| 1122 | 1035 | * @brief Checks if a substring is in a string |
| 1123 | 1036 | * |
| 1124 | 1037 | * @param[in] str string you want to search in. | ... | ... |
src/xml/GXml.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GXml.cpp - XML class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2015 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -518,9 +518,9 @@ const GXmlElement* GXml::element(const std::string& name, const int& index) cons |
| 518 | 518 | ***************************************************************************/ |
| 519 | 519 | void GXml::load(const GFilename& filename) |
| 520 | 520 | { |
| 521 | - // Check if file exists | |
| 522 | - if (!gammalib::file_exists(filename.url())) { | |
| 523 | - throw GException::file_open_error(G_LOAD, filename.url()); | |
| 521 | + // Throw an exception if file does not exist | |
| 522 | + if (!filename.exists()) { | |
| 523 | + throw GException::file_open_error(G_LOAD, filename); | |
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | // Open XML URL as file for reading | ... | ... |