Commit 93244a3ddb0bfc8978fab3af6067e7e8483ef79f
1 parent
588cea23
Replace std::string type by GFilename for GFits::m_filename data member (fixes #1649).
Showing
6 changed files
with
93 additions
and
85 deletions
ChangeLog
| ... | ... | @@ -3,7 +3,8 @@ |
| 3 | 3 | * Version 1.1.0 released |
| 4 | 4 | ========================= |
| 5 | 5 | |
| 6 | - Load CTA GTI extension from the HDU name provided by the DSS keyword (#1598). | |
| 6 | + Use GFilename in GFits to store filename (#1649) | |
| 7 | + Load CTA GTI extension from the HDU name provided by the DSS keyword (#1598) | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | 2016-01-13 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> | ... | ... |
NEWS
| ... | ... | @@ -13,7 +13,8 @@ made since the last release of GammaLib. |
| 13 | 13 | |
| 14 | 14 | 2. Public interface modifications |
| 15 | 15 | --------------------------------- |
| 16 | -None | |
| 16 | +The following methods return now GFilename instead of std::string: | |
| 17 | +- GFits::filename() | |
| 17 | 18 | |
| 18 | 19 | |
| 19 | 20 | 3. Configuration |
| ... | ... | @@ -33,7 +34,7 @@ None |
| 33 | 34 | |
| 34 | 35 | 6. FITS I/O Interface |
| 35 | 36 | --------------------- |
| 36 | -None | |
| 37 | +GFits now uses the GFilename class to store the FITS filename (fixes #1649). | |
| 37 | 38 | |
| 38 | 39 | |
| 39 | 40 | 7. Linear algebra module | ... | ... |
include/GFits.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GFits.hpp - FITS file class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2015 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -34,6 +34,7 @@ |
| 34 | 34 | #include "GFitsHDU.hpp" |
| 35 | 35 | #include "GFitsImage.hpp" |
| 36 | 36 | #include "GFitsTable.hpp" |
| 37 | +#include "GFilename.hpp" | |
| 37 | 38 | |
| 38 | 39 | /* __ Prototypes _________________________________________________________ */ |
| 39 | 40 | namespace gammalib { |
| ... | ... | @@ -64,7 +65,7 @@ class GFits : public GContainer { |
| 64 | 65 | public: |
| 65 | 66 | // Constructors and destructors |
| 66 | 67 | GFits(void); |
| 67 | - explicit GFits(const std::string& filename, const bool& create = false); | |
| 68 | + GFits(const std::string& filename, const bool& create = false); | |
| 68 | 69 | GFits(const GFits& fits); |
| 69 | 70 | virtual ~GFits(void); |
| 70 | 71 | |
| ... | ... | @@ -104,7 +105,7 @@ public: |
| 104 | 105 | void extend(const GFits& fits); |
| 105 | 106 | bool contains(const int& extno) const; |
| 106 | 107 | bool contains(const std::string& extname) const; |
| 107 | - const std::string& filename(void) const; | |
| 108 | + const GFilename& filename(void) const; | |
| 108 | 109 | int extno(const std::string& extname) const; |
| 109 | 110 | void open(const std::string& filename, |
| 110 | 111 | const bool& create = false); |
| ... | ... | @@ -136,7 +137,7 @@ private: |
| 136 | 137 | |
| 137 | 138 | // Private data area |
| 138 | 139 | std::vector<GFitsHDU*> m_hdu; //!< Pointers to HDUs |
| 139 | - std::string m_filename; //!< FITS file name | |
| 140 | + GFilename m_filename; //!< FITS file name | |
| 140 | 141 | void* m_fitsfile; //!< FITS file pointer |
| 141 | 142 | bool m_readwrite; //!< FITS file is readwrite (true/false) |
| 142 | 143 | bool m_created; //!< FITS file has been created (true/false) |
| ... | ... | @@ -299,13 +300,13 @@ bool GFits::contains(const std::string& extname) const |
| 299 | 300 | /***********************************************************************//** |
| 300 | 301 | * @brief Return FITS filename |
| 301 | 302 | * |
| 302 | - * @return FITS filename. | |
| 303 | + * @return FITS file name. | |
| 303 | 304 | * |
| 304 | - * Returns the FITS filename. If the object is not yet associated to a file | |
| 305 | - * an empty string will be returned. | |
| 305 | + * Returns the FITS file name. If the object is not yet associated to a file | |
| 306 | + * the file name will be empty. | |
| 306 | 307 | ***************************************************************************/ |
| 307 | 308 | inline |
| 308 | -const std::string& GFits::filename(void) const | |
| 309 | +const GFilename& GFits::filename(void) const | |
| 309 | 310 | { |
| 310 | 311 | return (m_filename); |
| 311 | 312 | } | ... | ... |
inst/mwl/src/GMWLSpectrum.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GMWLSpectrum.cpp - Multi-wavelength spectrum 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 * |
| ... | ... | @@ -380,7 +380,7 @@ void GMWLSpectrum::read(const GFits& fits, const int& extno) |
| 380 | 380 | |
| 381 | 381 | // If we found no table then throw an exception |
| 382 | 382 | if (extension == 0) { |
| 383 | - throw GMWLException::file_open_error(G_READ, fits.filename(), | |
| 383 | + throw GMWLException::file_open_error(G_READ, fits.filename().filename(), | |
| 384 | 384 | "No table found in file."); |
| 385 | 385 | } |
| 386 | 386 | ... | ... |
pyext/GFits.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GFits.i - FITS file class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2015 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -53,41 +53,41 @@ class GFits : public GContainer { |
| 53 | 53 | public: |
| 54 | 54 | // Constructors and destructors |
| 55 | 55 | GFits(void); |
| 56 | - explicit GFits(const std::string& filename, const bool& create = false); | |
| 56 | + GFits(const std::string& filename, const bool& create = false); | |
| 57 | 57 | GFits(const GFits& fits); |
| 58 | 58 | virtual ~GFits(void); |
| 59 | 59 | |
| 60 | 60 | // Methods |
| 61 | - void clear(void); | |
| 62 | - GFits* clone(void) const; | |
| 63 | - std::string classname(void) const; | |
| 64 | - GFitsHDU* at(const int& extno); | |
| 65 | - GFitsHDU* at(const std::string& extname); | |
| 66 | - GFitsImage* image(const int& extno); | |
| 67 | - GFitsImage* image(const std::string& extname); | |
| 68 | - GFitsTable* table(const int& extno); | |
| 69 | - GFitsTable* table(const std::string& extname); | |
| 70 | - int size(void) const; | |
| 71 | - bool is_empty(void) const; | |
| 72 | - GFitsHDU* set(const int& extno, const GFitsHDU& hdu); | |
| 73 | - GFitsHDU* set(const std::string& extname, const GFitsHDU& hdu); | |
| 74 | - GFitsHDU* append(const GFitsHDU& hdu); | |
| 75 | - GFitsHDU* insert(const int& extno, const GFitsHDU& hdu); | |
| 76 | - GFitsHDU* insert(const std::string& extname, const GFitsHDU& hdu); | |
| 77 | - void remove(const int& extno); | |
| 78 | - void remove(const std::string& extname); | |
| 79 | - void reserve(const int& num); | |
| 80 | - void extend(const GFits& fits); | |
| 81 | - bool contains(const int& extno) const; | |
| 82 | - bool contains(const std::string& extname) const; | |
| 83 | - const std::string& filename(void) const; | |
| 84 | - int extno(const std::string& extname) const; | |
| 85 | - void open(const std::string& filename, | |
| 86 | - const bool& create = false); | |
| 87 | - void save(const bool& clobber = false); | |
| 88 | - void saveto(const std::string& filename, | |
| 89 | - const bool& clobber = false); | |
| 90 | - void close(void); | |
| 61 | + void clear(void); | |
| 62 | + GFits* clone(void) const; | |
| 63 | + std::string classname(void) const; | |
| 64 | + GFitsHDU* at(const int& extno); | |
| 65 | + GFitsHDU* at(const std::string& extname); | |
| 66 | + GFitsImage* image(const int& extno); | |
| 67 | + GFitsImage* image(const std::string& extname); | |
| 68 | + GFitsTable* table(const int& extno); | |
| 69 | + GFitsTable* table(const std::string& extname); | |
| 70 | + int size(void) const; | |
| 71 | + bool is_empty(void) const; | |
| 72 | + GFitsHDU* set(const int& extno, const GFitsHDU& hdu); | |
| 73 | + GFitsHDU* set(const std::string& extname, const GFitsHDU& hdu); | |
| 74 | + GFitsHDU* append(const GFitsHDU& hdu); | |
| 75 | + GFitsHDU* insert(const int& extno, const GFitsHDU& hdu); | |
| 76 | + GFitsHDU* insert(const std::string& extname, const GFitsHDU& hdu); | |
| 77 | + void remove(const int& extno); | |
| 78 | + void remove(const std::string& extname); | |
| 79 | + void reserve(const int& num); | |
| 80 | + void extend(const GFits& fits); | |
| 81 | + bool contains(const int& extno) const; | |
| 82 | + bool contains(const std::string& extname) const; | |
| 83 | + const GFilename& filename(void) const; | |
| 84 | + int extno(const std::string& extname) const; | |
| 85 | + void open(const std::string& filename, | |
| 86 | + const bool& create = false); | |
| 87 | + void save(const bool& clobber = false); | |
| 88 | + void saveto(const std::string& filename, | |
| 89 | + const bool& clobber = false); | |
| 90 | + void close(void); | |
| 91 | 91 | }; |
| 92 | 92 | |
| 93 | 93 | ... | ... |
src/fits/GFits.cpp
| ... | ... | @@ -95,7 +95,7 @@ GFits::GFits(void) |
| 95 | 95 | * @brief FITS file constructor |
| 96 | 96 | * |
| 97 | 97 | * @param[in] filename FITS file name. |
| 98 | - * @param[in] create Create file if it does not exist (default=false)? | |
| 98 | + * @param[in] create Create file if it does not exist? (default: false) | |
| 99 | 99 | * |
| 100 | 100 | * Construct an object by opening a FITS file. If the file does not exist it |
| 101 | 101 | * will be created if @p create is set to true. |
| ... | ... | @@ -632,9 +632,8 @@ GFitsHDU* GFits::set(const std::string& extname, const GFitsHDU& hdu) |
| 632 | 632 | // Get extenion number |
| 633 | 633 | int extno = this->extno(extname); |
| 634 | 634 | if (extno == -1) { |
| 635 | - std::string msg = "No extension with name \""+extname+"\" found in" | |
| 636 | - " FITS file.\n" | |
| 637 | - "Please specify a valid extension name."; | |
| 635 | + std::string msg = "No extension with name \""+extname+"\" found in " | |
| 636 | + "FITS file. Please specify a valid extension name."; | |
| 638 | 637 | throw GException::invalid_argument(G_SET2, msg); |
| 639 | 638 | } |
| 640 | 639 | |
| ... | ... | @@ -764,11 +763,10 @@ GFitsHDU* GFits::insert(const int& extno, const GFitsHDU& hdu) |
| 764 | 763 | // Throw an exception if a non-image extension should be inserted in the |
| 765 | 764 | // first slot |
| 766 | 765 | if (extno == 0 && hdu.exttype() != GFitsHDU::HT_IMAGE) { |
| 767 | - std::string msg = "Attempt to insert a table extension as the first" | |
| 768 | - " extension of a FITS file.\n" | |
| 769 | - "The first extension of a FITS file must be an" | |
| 770 | - " image, hence use the next slot to insert a" | |
| 771 | - " table."; | |
| 766 | + std::string msg = "Attempt to insert a table extension as the first " | |
| 767 | + "extension of a FITS file. The first extension of " | |
| 768 | + "a FITS file must be an image, hence use the next " | |
| 769 | + "slot to insert a table."; | |
| 772 | 770 | throw GException::invalid_argument(G_INSERT1, msg); |
| 773 | 771 | } |
| 774 | 772 | |
| ... | ... | @@ -816,9 +814,8 @@ GFitsHDU* GFits::insert(const std::string& extname, const GFitsHDU& hdu) |
| 816 | 814 | // Get extenion number |
| 817 | 815 | int extno = this->extno(extname); |
| 818 | 816 | if (extno == -1) { |
| 819 | - std::string msg = "No extension with name \""+extname+"\" found in" | |
| 820 | - " FITS file.\n" | |
| 821 | - "Please specify a valid extension name."; | |
| 817 | + std::string msg = "No extension with name \""+extname+"\" found in " | |
| 818 | + "FITS file. Please specify a valid extension name."; | |
| 822 | 819 | throw GException::invalid_argument(G_INSERT2, msg); |
| 823 | 820 | } |
| 824 | 821 | |
| ... | ... | @@ -849,11 +846,11 @@ void GFits::remove(const int& extno) |
| 849 | 846 | // Throw an exception if removal would lead to a non-image extension |
| 850 | 847 | // in the first slot |
| 851 | 848 | if (extno == 0 && size() > 1 && m_hdu[1]->exttype() != GFitsHDU::HT_IMAGE) { |
| 852 | - std::string msg = "Attempt to remove primary image from a FITS file" | |
| 853 | - " with a table extension as second extension.\n" | |
| 854 | - "The removal of the primary image would result in" | |
| 855 | - " having a table as the first extension of the FITS" | |
| 856 | - " file, which is not a valid FITS file."; | |
| 849 | + std::string msg = "Attempt to remove primary image from a FITS file " | |
| 850 | + "with a table extension as second extension. The " | |
| 851 | + "removal of the primary image would result in " | |
| 852 | + "having a table as the first extension of the FITS " | |
| 853 | + "file, which is not a valid FITS file."; | |
| 857 | 854 | throw GException::invalid_argument(G_REMOVE1, msg); |
| 858 | 855 | } |
| 859 | 856 | |
| ... | ... | @@ -886,9 +883,8 @@ void GFits::remove(const std::string& extname) |
| 886 | 883 | // Get extenion number |
| 887 | 884 | int extno = this->extno(extname); |
| 888 | 885 | if (extno == -1) { |
| 889 | - std::string msg = "No extension with name \""+extname+"\" found in" | |
| 890 | - " FITS file.\n" | |
| 891 | - "Please specify a valid extension name."; | |
| 886 | + std::string msg = "No extension with name \""+extname+"\" found in " | |
| 887 | + "FITS file. Please specify a valid extension name."; | |
| 892 | 888 | throw GException::invalid_argument(G_REMOVE2, msg); |
| 893 | 889 | } |
| 894 | 890 | |
| ... | ... | @@ -996,7 +992,7 @@ int GFits::extno(const std::string& extname) const |
| 996 | 992 | * @param[in] filename Name of FITS file to be opened. |
| 997 | 993 | * @param[in] create Create FITS file if it does not exist (default=false) |
| 998 | 994 | * |
| 999 | - * @exception GException::fits_already_opened | |
| 995 | + * @exception GException::invalid_argument | |
| 1000 | 996 | * Class instance contains already an opened FITS file. |
| 1001 | 997 | * Close file before opening a new one using GFits::close(). |
| 1002 | 998 | * @exception GException::fits_open_error |
| ... | ... | @@ -1018,26 +1014,37 @@ void GFits::open(const std::string& filename, const bool& create) |
| 1018 | 1014 | // Remove any HDUs |
| 1019 | 1015 | m_hdu.clear(); |
| 1020 | 1016 | |
| 1021 | - // Don't allow opening if another file is already open | |
| 1017 | + // Create file name | |
| 1018 | + GFilename fname(gammalib::expand_env(filename)); | |
| 1019 | + | |
| 1020 | + // Don't allow opening if a file is already open | |
| 1022 | 1021 | if (m_fitsfile != NULL) { |
| 1023 | - throw GException::fits_already_opened(G_OPEN, m_filename); | |
| 1022 | + std::string msg; | |
| 1023 | + if (m_filename.filename() == fname.filename()) { | |
| 1024 | + msg = "FITS file \""+m_filename.filename()+"\" has already been " | |
| 1025 | + "opened, cannot open it again."; | |
| 1026 | + } | |
| 1027 | + else { | |
| 1028 | + msg = "A FITS file \""+m_filename.filename()+"\" has already " | |
| 1029 | + "been opened, cannot open another FITS file \""+ | |
| 1030 | + fname.filename()+"\" before closing the existing " | |
| 1031 | + "one."; | |
| 1032 | + } | |
| 1033 | + throw GException::invalid_argument(G_OPEN, msg); | |
| 1024 | 1034 | } |
| 1025 | 1035 | |
| 1026 | - // Expand environment variables | |
| 1027 | - std::string fname = gammalib::expand_env(filename); | |
| 1028 | - | |
| 1029 | 1036 | // Initialise FITS file as readwrite and non created |
| 1030 | 1037 | m_readwrite = true; |
| 1031 | 1038 | m_created = false; |
| 1032 | 1039 | |
| 1033 | 1040 | // Try opening FITS file with readwrite access |
| 1034 | 1041 | int status = 0; |
| 1035 | - status = __ffopen(FHANDLE(m_fitsfile), fname.c_str(), 1, &status); | |
| 1042 | + status = __ffopen(FHANDLE(m_fitsfile), fname.filename().c_str(), 1, &status); | |
| 1036 | 1043 | |
| 1037 | 1044 | // If failed then try opening as readonly |
| 1038 | 1045 | if (status == 104 || status == 112) { |
| 1039 | 1046 | status = 0; |
| 1040 | - status = __ffopen(FHANDLE(m_fitsfile), fname.c_str(), 0, &status); | |
| 1047 | + status = __ffopen(FHANDLE(m_fitsfile), fname.filename().c_str(), 0, &status); | |
| 1041 | 1048 | m_readwrite = false; |
| 1042 | 1049 | } |
| 1043 | 1050 | |
| ... | ... | @@ -1045,7 +1052,7 @@ void GFits::open(const std::string& filename, const bool& create) |
| 1045 | 1052 | // FITS file now |
| 1046 | 1053 | if (create && status == 104) { |
| 1047 | 1054 | status = 0; |
| 1048 | - status = __ffinit(FHANDLE(m_fitsfile), fname.c_str(), &status); | |
| 1055 | + status = __ffinit(FHANDLE(m_fitsfile), fname.filename().c_str(), &status); | |
| 1049 | 1056 | m_readwrite = true; |
| 1050 | 1057 | m_created = true; |
| 1051 | 1058 | } |
| ... | ... | @@ -1053,16 +1060,16 @@ void GFits::open(const std::string& filename, const bool& create) |
| 1053 | 1060 | // Throw special exception if status=202 (keyword not found). This error |
| 1054 | 1061 | // may occur if the file is opened with an expression |
| 1055 | 1062 | if (status == 202) { |
| 1056 | - throw GException::fits_open_error(G_OPEN, fname, status, | |
| 1063 | + throw GException::fits_open_error(G_OPEN, fname.filename(), status, | |
| 1057 | 1064 | "Keyword not found when opening file."); |
| 1058 | 1065 | } |
| 1059 | 1066 | |
| 1060 | 1067 | // Throw any other error |
| 1061 | 1068 | else if (status != 0) { |
| 1062 | - throw GException::fits_open_error(G_OPEN, fname, status); | |
| 1069 | + throw GException::fits_open_error(G_OPEN, fname.filename(), status); | |
| 1063 | 1070 | } |
| 1064 | 1071 | |
| 1065 | - // Store FITS file attributes | |
| 1072 | + // Store FITS file name | |
| 1066 | 1073 | m_filename = fname; |
| 1067 | 1074 | |
| 1068 | 1075 | // Determine number of HDUs |
| ... | ... | @@ -1140,7 +1147,7 @@ void GFits::save(const bool& clobber) |
| 1140 | 1147 | // If we attempt to save an existing file without overwriting permission |
| 1141 | 1148 | // then throw an error |
| 1142 | 1149 | if (!m_created && !clobber) { |
| 1143 | - throw GException::fits_file_exist(G_SAVE, m_filename); | |
| 1150 | + throw GException::fits_file_exist(G_SAVE, m_filename.filename()); | |
| 1144 | 1151 | } |
| 1145 | 1152 | |
| 1146 | 1153 | // If no FITS file has been opened then throw an error |
| ... | ... | @@ -1313,7 +1320,8 @@ std::string GFits::print(const GChatter& chatter) const |
| 1313 | 1320 | result.append("=== GFits ==="); |
| 1314 | 1321 | |
| 1315 | 1322 | // Append file information |
| 1316 | - result.append("\n"+gammalib::parformat("Filename")+m_filename); | |
| 1323 | + result.append("\n"+gammalib::parformat("Filename")); | |
| 1324 | + result.append(m_filename.filename()); | |
| 1317 | 1325 | result.append("\n"+gammalib::parformat("History")); |
| 1318 | 1326 | if (m_created) { |
| 1319 | 1327 | result.append("new file"); |
| ... | ... | @@ -1374,7 +1382,7 @@ void GFits::init_members(void) |
| 1374 | 1382 | * |
| 1375 | 1383 | * @param fits Object to be copied |
| 1376 | 1384 | * |
| 1377 | - * The method does not copy the FITS filename and FITS file pointer. | |
| 1385 | + * The method does not copy the FITS file name and FITS file pointer. | |
| 1378 | 1386 | * This prevents that several copies of the FITS file pointer exist in |
| 1379 | 1387 | * different instances of GFits, which would lead to confusion since one |
| 1380 | 1388 | * instance could close the file while for another it still would be |
| ... | ... | @@ -1493,8 +1501,6 @@ GFitsImage* GFits::new_image(void) |
| 1493 | 1501 | // Get number of bits per pixel |
| 1494 | 1502 | int status = 0; |
| 1495 | 1503 | int bitpix = -64; |
| 1496 | - //status = __ffgipr(FPTR(m_fitsfile), 0, &bitpix, NULL, NULL, &status); | |
| 1497 | - //status = __ffgidt(FPTR(m_fitsfile), &bitpix, &status); | |
| 1498 | 1504 | status = __ffgiet(FPTR(m_fitsfile), &bitpix, &status); |
| 1499 | 1505 | if (status != 0) { |
| 1500 | 1506 | throw GException::fits_error(G_NEW_IMAGE, status); |
| ... | ... | @@ -1632,9 +1638,8 @@ int gammalib::fits_move_to_hdu(const std::string& caller, void* vptr, |
| 1632 | 1638 | |
| 1633 | 1639 | // Throw exception in case of an error |
| 1634 | 1640 | if (status != 0) { |
| 1635 | - std::string msg = "Unable to move FITS file pointer to extension" | |
| 1636 | - " number "+ | |
| 1637 | - gammalib::str(position-1)+"."; | |
| 1641 | + std::string msg = "Unable to move FITS file pointer to extension " | |
| 1642 | + "number "+gammalib::str(position-1)+"."; | |
| 1638 | 1643 | throw GException::fits_error(caller, status, msg); |
| 1639 | 1644 | } |
| 1640 | 1645 | ... | ... |