From 32989d1fee1fa3da444a9c3682d49ea70e2df265 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 17 Feb 2016 22:40:11 +0100 Subject: [PATCH] Put back extension handling in GFits::open(). --- inst/com/src/GCOMEventCube.cpp | 8 ++++---- inst/com/src/GCOMResponse.cpp | 6 +++--- inst/cta/src/GCTAAeff2D.cpp | 8 ++++---- inst/cta/src/GCTAAeffArf.cpp | 6 +++--- inst/cta/src/GCTAEdisp2D.cpp | 6 +++--- inst/cta/src/GCTAObservation.cpp | 31 +++++++++++++++++-------------- inst/cta/src/GCTAPointing.cpp | 6 +++--- inst/lat/src/GLATEventCube.cpp | 6 +++--- inst/lat/src/GLATEventList.cpp | 6 +++--- inst/lat/src/GLATLtCube.cpp | 12 ++++++------ inst/mwl/src/GMWLSpectrum.cpp | 10 +++++----- src/fits/GFits.cpp | 12 ++++-------- src/model/GModelSpatialDiffuseCube.cpp | 4 ++-- src/model/GModelSpatialDiffuseMap.cpp | 6 +++--- src/obs/GEbounds.cpp | 11 +++-------- src/obs/GEnergies.cpp | 11 +++-------- src/obs/GGti.cpp | 11 +++-------- src/xspec/GArf.cpp | 33 ++++++++++++++++++--------------- src/xspec/GPha.cpp | 25 +++++++++++++++++++++---- src/xspec/GRmf.cpp | 14 +++++++++++--- 20 files changed, 122 insertions(+), 110 deletions(-) diff --git a/inst/com/src/GCOMEventCube.cpp b/inst/com/src/GCOMEventCube.cpp index 9c16d83..a82c60c 100644 --- a/inst/com/src/GCOMEventCube.cpp +++ b/inst/com/src/GCOMEventCube.cpp @@ -366,13 +366,13 @@ int GCOMEventCube::naxis(const int& axis) const void GCOMEventCube::load(const GFilename& filename) { // Open DRE FITS file - GFits file(filename); + GFits fits(filename); // Load DRE cube from FITS file - read(file); + read(fits); // Close FITS file - file.close(); + fits.close(); // Return return; @@ -383,7 +383,7 @@ void GCOMEventCube::load(const GFilename& filename) * @brief Save COMPTEL event cube into FITS file * * @param[in] filename FITS filename. - * @param[in] clobber Overwrite existing FITS file (default=false). + * @param[in] clobber Overwrite existing FITS file? (default: false). * * Save the COMPTEL event cube into FITS file. ***************************************************************************/ diff --git a/inst/com/src/GCOMResponse.cpp b/inst/com/src/GCOMResponse.cpp index d7444cc..e6a73d9 100644 --- a/inst/com/src/GCOMResponse.cpp +++ b/inst/com/src/GCOMResponse.cpp @@ -480,16 +480,16 @@ void GCOMResponse::load(const std::string& rspname) } // Open FITS file - GFits file(filename); + GFits fits(filename); // Get IAQ image - const GFitsImage& iaq = *file.image(0); + const GFitsImage& iaq = *fits.image(0); // Read IAQ read(iaq); // Close ARF FITS file - file.close(); + fits.close(); // Return return; diff --git a/inst/cta/src/GCTAAeff2D.cpp b/inst/cta/src/GCTAAeff2D.cpp index 479a507..11544f2 100644 --- a/inst/cta/src/GCTAAeff2D.cpp +++ b/inst/cta/src/GCTAAeff2D.cpp @@ -355,16 +355,16 @@ void GCTAAeff2D::write(GFitsBinTable& table) const void GCTAAeff2D::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get effective area table - const GFitsTable& table = *file.table(filename.extname("EFFECTIVE AREA")); + const GFitsTable& table = *fits.table(filename.extname("EFFECTIVE AREA")); // Read effective area from table read(table); // Close FITS file - file.close(); + fits.close(); // Store filename m_filename = filename; @@ -395,7 +395,7 @@ void GCTAAeff2D::save(const GFilename& filename, const bool& clobber) const // Get extension name std::string extname = filename.extname("EFFECTIVE AREA"); - // Open or create FITS file + // Open or create FITS file (without extension) GFits fits(filename, true); // Remove extension if it exists already diff --git a/inst/cta/src/GCTAAeffArf.cpp b/inst/cta/src/GCTAAeffArf.cpp index 34ea74a..92a0cc7 100644 --- a/inst/cta/src/GCTAAeffArf.cpp +++ b/inst/cta/src/GCTAAeffArf.cpp @@ -250,16 +250,16 @@ GCTAAeffArf* GCTAAeffArf::clone(void) const void GCTAAeffArf::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get ARF table - const GFitsTable& table = *file.table(filename.extname("SPECRESP")); + const GFitsTable& table = *fits.table(filename.extname("SPECRESP")); // Read ARF from table read(table); // Close FITS file - file.close(); + fits.close(); // Store filename m_filename = filename; diff --git a/inst/cta/src/GCTAEdisp2D.cpp b/inst/cta/src/GCTAEdisp2D.cpp index da6808a..9c47b5d 100644 --- a/inst/cta/src/GCTAEdisp2D.cpp +++ b/inst/cta/src/GCTAEdisp2D.cpp @@ -421,16 +421,16 @@ void GCTAEdisp2D::write(GFitsBinTable& table) const void GCTAEdisp2D::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get energy dispersion table - const GFitsTable& table = *file.table(filename.extname("ENERGY DISPERSION")); + const GFitsTable& table = *fits.table(filename.extname("ENERGY DISPERSION")); // Read energy dispersion from table read(table); // Close FITS file - file.close(); + fits.close(); // Store filename m_filename = filename; diff --git a/inst/cta/src/GCTAObservation.cpp b/inst/cta/src/GCTAObservation.cpp index 5e093f3..c65ad87 100644 --- a/inst/cta/src/GCTAObservation.cpp +++ b/inst/cta/src/GCTAObservation.cpp @@ -896,11 +896,11 @@ void GCTAObservation::read(const GFits& fits) if (m_events != NULL) delete m_events; m_events = NULL; - // Initialise file name from FITS file - GFilename fname(fits.filename()); + // Retrieve file name from FITS file + GFilename filename(fits.filename()); // Get extension name - std::string extname = fname.extname("EVENTS"); + std::string extname = filename.extname("EVENTS"); // If FITS file contains an EVENTS extension we have an unbinned // observation ... @@ -973,6 +973,14 @@ void GCTAObservation::write(GFits& fits, const std::string& evtname, // Case A: Observation contains an event list if (list != NULL) { + // Remove HDUs if they exist already + if (fits.contains(evtname)) { + fits.remove(evtname); + } + if (fits.contains(gtiname)) { + fits.remove(gtiname); + } + // Write event list and Good Time Intervals into FITS file list->write(fits, evtname, gtiname); @@ -1096,7 +1104,8 @@ void GCTAObservation::load(const GFilename& cntcube, * energy boundaries and the Good Time Intervals. The extension names of * these binary tables are "EBOUNDS" and "GTI", and cannot be modified. ***************************************************************************/ -void GCTAObservation::save(const GFilename& filename, const bool& clobber) const +void GCTAObservation::save(const GFilename& filename, + const bool& clobber) const { // Set default events and Good Time Intervals extension name and extract // a possible overwrite from the extension name argument of the filename. @@ -1115,16 +1124,10 @@ void GCTAObservation::save(const GFilename& filename, const bool& clobber) const } } - // Open or create FITS file - GFits fits(filename, true); - - // Remove HDUs if they exist already - if (fits.contains(evtname)) { - fits.remove(evtname); - } - if (fits.contains(gtiname)) { - fits.remove(gtiname); - } + // Open or create FITS file. Since we accept here a special structure + // for the extension that cfitsio does not understand we only pass + // the URL without extension name to the GFits constructor. + GFits fits(filename.url(), true); // Write data into FITS file write(fits, evtname, gtiname); diff --git a/inst/cta/src/GCTAPointing.cpp b/inst/cta/src/GCTAPointing.cpp index 96a2755..1a787db 100644 --- a/inst/cta/src/GCTAPointing.cpp +++ b/inst/cta/src/GCTAPointing.cpp @@ -418,16 +418,16 @@ GHorizDir GCTAPointing::dir_horiz(const GTime& time) const void GCTAPointing::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get pointing table - const GFitsTable& table = *file.table(filename.extname("POINTING")); + const GFitsTable& table = *fits.table(filename.extname("POINTING")); // Read pointing from table read(table); // Close FITS file - file.close(); + fits.close(); // Return return; diff --git a/inst/lat/src/GLATEventCube.cpp b/inst/lat/src/GLATEventCube.cpp index 890767d..3f35131 100644 --- a/inst/lat/src/GLATEventCube.cpp +++ b/inst/lat/src/GLATEventCube.cpp @@ -310,13 +310,13 @@ int GLATEventCube::naxis(const int& axis) const void GLATEventCube::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Read event cube from FITS file - read(file); + read(fits); // Close FITS file - file.close(); + fits.close(); // Return return; diff --git a/inst/lat/src/GLATEventList.cpp b/inst/lat/src/GLATEventList.cpp index fb45021..b4a494f 100644 --- a/inst/lat/src/GLATEventList.cpp +++ b/inst/lat/src/GLATEventList.cpp @@ -252,13 +252,13 @@ GLATEventList* GLATEventList::clone(void) const void GLATEventList::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Read event list from FITS file - read(file); + read(fits); // Close FITS file - file.close(); + fits.close(); // Return return; diff --git a/inst/lat/src/GLATLtCube.cpp b/inst/lat/src/GLATLtCube.cpp index 5eec5c6..55b5c92 100644 --- a/inst/lat/src/GLATLtCube.cpp +++ b/inst/lat/src/GLATLtCube.cpp @@ -396,13 +396,13 @@ void GLATLtCube::load(const GFilename& filename) clear(); // Open livetime cube FITS file - GFits file(filename); + GFits fits(filename); // Get HDUs - const GFitsTable& hdu_exposure = *file.table("EXPOSURE"); - const GFitsTable& hdu_weighted_exposure = *file.table("WEIGHTED_EXPOSURE"); - //const GFitsTable& hdu_cthetabounds = *file.table("CTHETABOUNDS"); - const GFitsTable& hdu_gti = *file.table("GTI"); + const GFitsTable& hdu_exposure = *fits.table("EXPOSURE"); + const GFitsTable& hdu_weighted_exposure = *fits.table("WEIGHTED_EXPOSURE"); + //const GFitsTable& hdu_cthetabounds = *fits.table("CTHETABOUNDS"); + const GFitsTable& hdu_gti = *fits.table("GTI"); // Load exposure m_exposure.read(hdu_exposure); @@ -416,7 +416,7 @@ void GLATLtCube::load(const GFilename& filename) m_gti.read(hdu_gti); // Close FITS file - file.close(); + fits.close(); // Return return; diff --git a/inst/mwl/src/GMWLSpectrum.cpp b/inst/mwl/src/GMWLSpectrum.cpp index c6d4295..f868593 100644 --- a/inst/mwl/src/GMWLSpectrum.cpp +++ b/inst/mwl/src/GMWLSpectrum.cpp @@ -264,21 +264,21 @@ void GMWLSpectrum::load(const GFilename& filename) clear(); // Open FITS file - GFits file(filename); + GFits fits(filename); // Read spectrum if (filename.has_extno()) { - read(file, filename.extno()); + read(fits, filename.extno()); } else if (filename.has_extname()) { - read(file, filename.extname()); + read(fits, filename.extname()); } else { - read(file); + read(fits); } // Close FITS file - file.close(); + fits.close(); // Return return; diff --git a/src/fits/GFits.cpp b/src/fits/GFits.cpp index 90e83fe..bb04b66 100644 --- a/src/fits/GFits.cpp +++ b/src/fits/GFits.cpp @@ -1145,7 +1145,7 @@ void GFits::save(const bool& clobber) // If we attempt to save an existing file without overwriting permission // then throw an error if (!m_created && !clobber) { - throw GException::fits_file_exist(G_SAVE, m_filename.url()); + throw GException::fits_file_exist(G_SAVE, m_filename); } // If no FITS file has been opened then throw an error @@ -1219,14 +1219,10 @@ void GFits::save(const bool& clobber) * Specified file exists already. Overwriting requires * clobber=true. * - * Saves object into a specific FITS file. - * Any environment variable present in the filename will be expanded. + * Saves the FITS object into a specific file. ***************************************************************************/ void GFits::saveto(const GFilename& filename, const bool& clobber) { - // Create gzipped file name version - std::string gzfname = filename + ".gz"; - // Debug header #if defined(G_DEBUG) std::cout << "GFits::saveto(\"" << fname << "\", " << clobber << ")" @@ -1243,7 +1239,7 @@ void GFits::saveto(const GFilename& filename, const bool& clobber) } // Create or open FITS file - GFits fits(filename, true);; + GFits fits(filename, true); // Append all headers for (int i = 0; i < size(); ++i) { @@ -1304,7 +1300,7 @@ std::string GFits::print(const GChatter& chatter) const // Append file information result.append("\n"+gammalib::parformat("Filename")); - result.append(m_filename.url()); + result.append(m_filename); result.append("\n"+gammalib::parformat("History")); if (m_created) { result.append("new file"); diff --git a/src/model/GModelSpatialDiffuseCube.cpp b/src/model/GModelSpatialDiffuseCube.cpp index 6c6b74e..3aceda1 100644 --- a/src/model/GModelSpatialDiffuseCube.cpp +++ b/src/model/GModelSpatialDiffuseCube.cpp @@ -649,7 +649,7 @@ void GModelSpatialDiffuseCube::write(GXmlElement& xml) const } // Set filename - xml.attribute("file", m_filename.url()); + xml.attribute("file", m_filename); // Return return; @@ -1069,7 +1069,7 @@ std::string GModelSpatialDiffuseCube::print(const GChatter& chatter) const // Append parameters result.append("\n"+gammalib::parformat("Map cube file")); - result.append(m_filename.url()); + result.append(m_filename); if (m_loaded) { result.append(" [loaded]"); } diff --git a/src/model/GModelSpatialDiffuseMap.cpp b/src/model/GModelSpatialDiffuseMap.cpp index d7f0867..ee81c6e 100644 --- a/src/model/GModelSpatialDiffuseMap.cpp +++ b/src/model/GModelSpatialDiffuseMap.cpp @@ -585,7 +585,7 @@ void GModelSpatialDiffuseMap::write(GXmlElement& xml) const } // Set model filename - xml.attribute("file", m_filename.url()); + xml.attribute("file", m_filename); // Verify model type if (xml.attribute("type") != "SpatialMap") { @@ -840,7 +840,7 @@ std::string GModelSpatialDiffuseMap::print(const GChatter& chatter) const // Append parameters result.append("\n"+gammalib::parformat("Sky map file")); - result.append(m_filename.url()); + result.append(m_filename); result.append("\n"+gammalib::parformat("Map normalization")); result.append(gammalib::str(m_mc_norm)+" ph/cm2/s"); if (normalize()) { @@ -883,7 +883,7 @@ void GModelSpatialDiffuseMap::load(const GFilename& filename) m_filename = filename; // Load skymap - m_map.load(m_filename.url()); + m_map.load(m_filename); // Prepare sky map prepare_map(); diff --git a/src/obs/GEbounds.cpp b/src/obs/GEbounds.cpp index b321cb0..6028a36 100644 --- a/src/obs/GEbounds.cpp +++ b/src/obs/GEbounds.cpp @@ -579,16 +579,16 @@ void GEbounds::set_log(const int& num, const GEnergy& emin, const GEnergy& emax) void GEbounds::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get energy boundary table - const GFitsTable& table = *file.table(filename.extname("EBOUNDS")); + const GFitsTable& table = *fits.table(filename.extname("EBOUNDS")); // Read energy boundaries from table read(table); // Close FITS file - file.close(); + fits.close(); // Return return; @@ -622,11 +622,6 @@ void GEbounds::save(const GFilename& filename, // Open or create FITS file GFits fits(filename, true); - // Remove extension if it exists already - if (fits.contains(extname)) { - fits.remove(extname); - } - // Write energy boundaries to FITS file write(fits, extname, unit); diff --git a/src/obs/GEnergies.cpp b/src/obs/GEnergies.cpp index 48b9720..8d0251d 100644 --- a/src/obs/GEnergies.cpp +++ b/src/obs/GEnergies.cpp @@ -548,16 +548,16 @@ void GEnergies::set_log(const int& num, void GEnergies::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get energies table - const GFitsTable& table = *file.table(filename.extname("ENERGIES")); + const GFitsTable& table = *fits.table(filename.extname("ENERGIES")); // Read energies from table read(table); // Close FITS file - file.close(); + fits.close(); // Return return; @@ -587,11 +587,6 @@ void GEnergies::save(const GFilename& filename, const bool& clobber) const // Open or create FITS file GFits fits(filename, true); - // Remove extension if it exists already - if (fits.contains(extname)) { - fits.remove(extname); - } - // Write energies to FITS file write(fits, extname); diff --git a/src/obs/GGti.cpp b/src/obs/GGti.cpp index 2e67f0a..b4ee105 100644 --- a/src/obs/GGti.cpp +++ b/src/obs/GGti.cpp @@ -585,16 +585,16 @@ void GGti::extend(const GGti& gti) void GGti::load(const GFilename& filename) { // Open FITS file - GFits file(filename); + GFits fits(filename); // Get GTI table - const GFitsTable& table = *file.table(filename.extname("GTI")); + const GFitsTable& table = *fits.table(filename.extname("GTI")); // Read GTI from table read(table); // Close FITS file - file.close(); + fits.close(); // Return return; @@ -625,11 +625,6 @@ void GGti::save(const GFilename& filename, const bool& clobber) const // Open or create FITS file GFits fits(filename, true); - // Remove extension if it exists already - if (fits.contains(extname)) { - fits.remove(extname); - } - // Write GTI to FITS object write(fits, extname); diff --git a/src/xspec/GArf.cpp b/src/xspec/GArf.cpp index cd311ba..d63f52e 100644 --- a/src/xspec/GArf.cpp +++ b/src/xspec/GArf.cpp @@ -255,20 +255,17 @@ const double& GArf::at(const int& index) const ***************************************************************************/ void GArf::load(const GFilename& filename) { - // Clear any existing models - clear(); - // Open FITS file - GFits file(filename); + GFits fits(filename); // Get ARF table - const GFitsTable& table = *file.table("SPECRESP"); + const GFitsTable& table = *fits.table("SPECRESP"); // Read ARF data read(table); // Close FITS file - file.close(); + fits.close(); // Store filename m_filename = filename; @@ -310,6 +307,9 @@ void GArf::save(const GFilename& filename, const bool& clobber) const ***************************************************************************/ void GArf::read(const GFitsTable& table) { + // Clear members + clear(); + // Get pointer to data columns const GFitsTableCol* energy_lo = table["ENERG_LO"]; const GFitsTableCol* energy_hi = table["ENERG_HI"]; @@ -352,6 +352,12 @@ void GArf::read(const GFitsTable& table) ***************************************************************************/ void GArf::write(GFits& fits) const { + // If the FITS object contains already an extension with the same + // name then remove now this extension + if (fits.contains("SPECRESP")) { + fits.remove("SPECRESP"); + } + // Set column length int length = size(); @@ -359,7 +365,7 @@ void GArf::write(GFits& fits) const if (length > 0) { // Create new binary table - GFitsBinTable* hdu = new GFitsBinTable; + GFitsBinTable hdu; // Allocate floating point vector columns GFitsTableFloatCol energy_lo("ENERG_LO", length); @@ -379,18 +385,15 @@ void GArf::write(GFits& fits) const specresp.unit("cm^2"); // Set table attributes - hdu->extname("SPECRESP"); + hdu.extname("SPECRESP"); // Append columns to table - hdu->append(energy_lo); - hdu->append(energy_hi); - hdu->append(specresp); + hdu.append(energy_lo); + hdu.append(energy_hi); + hdu.append(specresp); // Append HDU to FITS file - fits.append(*hdu); - - // Free binary table - delete hdu; + fits.append(hdu); } // endif: there were data to write diff --git a/src/xspec/GPha.cpp b/src/xspec/GPha.cpp index a9e1c6e..93d7ff1 100644 --- a/src/xspec/GPha.cpp +++ b/src/xspec/GPha.cpp @@ -373,18 +373,27 @@ void GPha::load(const GFilename& filename) * @brief Save Pulse Height Analyzer spectrum * * @param[in] filename File name. - * @param[in] clobber Overwrite existing file? (defaults to true) + * @param[in] clobber Overwrite existing file? (default: false) + * + * Saves Pulse Height Analyzer spectrum and energy boundaries into a FITS + * file. If a file with the given @p filename does not yet exist it will be + * created, otherwise the method opens the existing file. The method will + * create (or replace an existing) Pulse Height Analyzer spectrum and energy + * boundary extensions. + * + * An existing file will only be modified if the @p clobber flag is set to + * true. ***************************************************************************/ void GPha::save(const GFilename& filename, const bool& clobber) const { - // Open FITS file - GFits fits; + // Open or create FITS file + GFits fits(filename, true); // Write PHA into file write(fits); // Close FITS file - fits.saveto(filename, clobber); + fits.save(clobber); // Store filename m_filename = filename; @@ -443,6 +452,14 @@ void GPha::read(const GFitsTable& table) ***************************************************************************/ void GPha::write(GFits& fits) const { + // Remove extensions if they exist already + if (fits.contains("EBOUNDS")) { + fits.remove("EBOUNDS"); + } + if (fits.contains("SPECTRUM")) { + fits.remove("SPECTRUM"); + } + // Set column length int length = size(); diff --git a/src/xspec/GRmf.cpp b/src/xspec/GRmf.cpp index e7515ba..0bcf91d 100644 --- a/src/xspec/GRmf.cpp +++ b/src/xspec/GRmf.cpp @@ -376,19 +376,19 @@ void GRmf::load(const GFilename& filename) clear(); // Open FITS file - GFits file(filename); + GFits fits(filename); // Read measured energy boundaries m_ebds_measured.load(filename); // Get RMF table - const GFitsTable& table = *file.table(filename.extname("MATRIX")); + const GFitsTable& table = *fits.table(filename.extname("MATRIX")); // Read RMF data read(table); // Close FITS file - file.close(); + fits.close(); // Store filename m_filename = filename; @@ -501,6 +501,14 @@ void GRmf::read(const GFitsTable& table) ***************************************************************************/ void GRmf::write(GFits& fits, const std::string& unit) const { + // Remove extensions if they exist already + if (fits.contains("MATRIX")) { + fits.remove("MATRIX"); + } + if (fits.contains("EBOUNDS")) { + fits.remove("EBOUNDS"); + } + // Set table length int length = ntrue(); -- libgit2 0.21.4