From 87e9392e155cd09d013f8ff8764aa9b40eb449be Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 18 Jan 2016 19:06:50 +0100 Subject: [PATCH] Introduced m_extname member to GCTAEventList to support writing to files including an extension name. Method extname() has also been added to set the extension name externally. GCTAEventList::save extracts and sets the extension name according to the input file name --- inst/cta/include/GCTAEventList.hpp | 18 ++++++++++++++++++ inst/cta/pyext/GCTAEventList.i | 2 ++ inst/cta/src/GCTAEventList.cpp | 25 +++++++++++++++++-------- inst/cta/src/GCTAObservation.cpp | 14 +++++++------- inst/cta/test/data/obs_unbinned_gti.xml | 6 ------ inst/cta/test/test_CTA.cpp | 32 +++++++++++++++++++++++++++++++- 6 files changed, 75 insertions(+), 22 deletions(-) delete mode 100644 inst/cta/test/data/obs_unbinned_gti.xml diff --git a/inst/cta/include/GCTAEventList.hpp b/inst/cta/include/GCTAEventList.hpp index 24676a9..ce72b21 100644 --- a/inst/cta/include/GCTAEventList.hpp +++ b/inst/cta/include/GCTAEventList.hpp @@ -74,6 +74,7 @@ public: virtual int number(void) const; virtual void roi(const GRoi& roi); virtual const GCTARoi& roi(void) const; + void extname(const std::string& extname); std::string print(const GChatter& chatter = NORMAL) const; // Implement other methods @@ -101,9 +102,11 @@ protected: std::vector m_events; //!< Events std::vector m_columns; //!< Pointers to optional columns std::string m_gti_extname; //!< GTI extension name + std::string m_extname; //!< Name of FITS extension bool m_has_phase; //!< Signal presence of phase bool m_has_detxy; //!< Signal presence of detector coordinates + // IRF cache for diffuse models mutable std::vector m_irf_names; //!< Model names mutable std::vector > m_irf_values; //!< IRF values @@ -159,6 +162,21 @@ const GCTARoi& GCTAEventList::roi(void) const /***********************************************************************//** + * @brief Set extension name for event list + * + * @param[in] extname Name of FITS extension + * + * Sets the FITS extension name for writing + ***************************************************************************/ +inline +void GCTAEventList::extname(const std::string& extname) +{ + m_extname = extname; + return; +} + + +/***********************************************************************//** * @brief Reserves space for events * * @param[in] number Number of events. diff --git a/inst/cta/pyext/GCTAEventList.i b/inst/cta/pyext/GCTAEventList.i index 2900dbb..737a37e 100644 --- a/inst/cta/pyext/GCTAEventList.i +++ b/inst/cta/pyext/GCTAEventList.i @@ -56,6 +56,8 @@ public: virtual int number(void) const; virtual void roi(const GRoi& roi); virtual const GCTARoi& roi(void) const; + void extname(const std::string& extname); + // Implement other methods void append(const GCTAEventAtom& event); diff --git a/inst/cta/src/GCTAEventList.cpp b/inst/cta/src/GCTAEventList.cpp index 12cb144..ac270cf 100644 --- a/inst/cta/src/GCTAEventList.cpp +++ b/inst/cta/src/GCTAEventList.cpp @@ -293,14 +293,20 @@ void GCTAEventList::load(const std::string& filename) void GCTAEventList::save(const std::string& filename, const bool& clobber) const { + // Initialise filename + GFilename fname(filename); + + // Set extension name (default: previous extension name) + const_cast(this)->extname(fname.extname(m_extname)); + // Create empty FITS file - GFits fits; + GFits fits(fname.filename(), true); // Write event list write(fits); // Save FITS file - fits.saveto(filename, clobber); + fits.save(clobber); // Return return; @@ -338,11 +344,11 @@ void GCTAEventList::read(const GFits& fits) // Initialise filename from fits file GFilename fname = GFilename(fits.filename()); - // Initialise extension name - std::string extname = fname.extname("EVENTS"); + // Store extension name + m_extname = fname.extname("EVENTS"); // Get event list HDU - const GFitsTable& events = *fits.table(extname); + const GFitsTable& events = *fits.table(m_extname); // Load event data read_events(events); @@ -406,9 +412,13 @@ void GCTAEventList::read(const GFits& fits) ***************************************************************************/ void GCTAEventList::write(GFits& file) const { + // Allocate FITS binary table HDU GFitsBinTable* events = new GFitsBinTable; + // Set extension name + events->extname(m_extname); + // Write events write_events(*events); @@ -594,6 +604,7 @@ void GCTAEventList::init_members(void) m_events.clear(); m_columns.clear(); m_gti_extname = "GTI"; //!< Default GTI extension name + m_extname = "EVENTS"; //!< Default events extension name m_has_phase = false; m_has_detxy = true; @@ -617,6 +628,7 @@ void GCTAEventList::copy_members(const GCTAEventList& list) m_roi = list.m_roi; m_events = list.m_events; m_gti_extname = list.m_gti_extname; + m_extname = list.m_extname; m_has_phase = list.m_has_phase; m_has_detxy = list.m_has_detxy; @@ -806,9 +818,6 @@ void GCTAEventList::read_events(const GFitsTable& table) ***************************************************************************/ void GCTAEventList::write_events(GFitsBinTable& hdu) const { - // Set extension name - hdu.extname("EVENTS"); - // If there are events then write them now if (size() > 0) { diff --git a/inst/cta/src/GCTAObservation.cpp b/inst/cta/src/GCTAObservation.cpp index ee2dc78..bf2caf3 100644 --- a/inst/cta/src/GCTAObservation.cpp +++ b/inst/cta/src/GCTAObservation.cpp @@ -1227,21 +1227,21 @@ void GCTAObservation::read(const GFits& fits) void GCTAObservation::write(GFits& fits, const std::string& extname) const { // Get pointers on event list - const GCTAEventList* list = dynamic_cast(events()); + GCTAEventList* list = dynamic_cast(const_cast(events())); const GCTAEventCube* cube = dynamic_cast(events()); // Case A: Observation contains an event list if (list != NULL) { + // Set event list extension name + list->extname(extname); + // Write event list into FITS file. This method also writes // the GTI if provided on loading. list->write(fits); // Get reference to EVENTS HDU - GFitsHDU& hdu = *fits.at("EVENTS"); - - // Change extension name - hdu.extname(extname); + GFitsHDU& hdu = *fits.at(extname); // Write observation attributes write_attributes(hdu); @@ -1379,13 +1379,13 @@ void GCTAObservation::save(const std::string& filename, const bool& clobber) con std::string extname = fname.extname("EVENTS"); // Create FITS file - GFits fits; + GFits fits(fname.filename(), true); // Write data into FITS file write(fits, extname); // Save FITS file - fits.saveto(fname.filename(), clobber); + fits.save(clobber); // Return return; diff --git a/inst/cta/test/data/obs_unbinned_gti.xml b/inst/cta/test/data/obs_unbinned_gti.xml deleted file mode 100644 index c392156..0000000 --- a/inst/cta/test/data/obs_unbinned_gti.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/inst/cta/test/test_CTA.cpp b/inst/cta/test/test_CTA.cpp index 0fb5219..2d852b3 100644 --- a/inst/cta/test/test_CTA.cpp +++ b/inst/cta/test/test_CTA.cpp @@ -1300,7 +1300,10 @@ void TestGCTAObservation::test_unbinned_obs(void) { // Set filenames const std::string file1 = "test_cta_obs_unbinned.xml"; - const std::string file2 = "test_cta_obs_unbinned_gti.xml"; + const std::string evfile = "test_cta_obs_unbinned.fits[TESTEVENTS]"; +// const std::string file2 = "test_cta_obs_unbinned_gti.xml"; + + /* const std::string evfile = "test_cta_obs_unbinned.fits[TESTEVENTS]"; const std::string gtifile = "test_cta_obs_unbinned.fits[TESTGTI]"; @@ -1362,6 +1365,33 @@ void TestGCTAObservation::test_unbinned_obs(void) test_try_failure(e); } + // Save unbinned CTA observation with non-default extension names + test_try("Save unbinned CTA observation with non-default extension names"); + try { + run.load(cta_events); + run.save(evfile, true); + run.load(evfile); + test_value(run.roi().centre().dir().ra_deg(), 83.63, 1.0e-10); + test_value(run.roi().centre().dir().dec_deg(), 22.01, 1.0e-10); + test_value(run.roi().radius(), 5.0, 1.0e-10); + test_value(run.ebounds().emin().TeV(), 0.1, 1.0e-10); + test_value(run.ebounds().emax().TeV(), 100.0, 1.0e-10); + test_value(run.gti().tstart().convert(run.gti().reference()), 0.0, 1.0e-10); + test_value(run.gti().tstop().convert(run.gti().reference()), 1800.0, 1.0e-10); + test_value(run.ontime(), 1800.0, 1.0e-10); + test_value(run.livetime(), 1710.0, 1.0e-10); + test_value(run.deadc(), 0.95, 1.0e-10); + test_value(run.ra_obj(), 0.0, 1.0e-10); + test_value(run.dec_obj(), 0.0, 1.0e-10); + test_value(run.obs_id(), 0); + test_value(run.pointing().dir().ra_deg(), 83.63, 1.0e-10); + test_value(run.pointing().dir().dec_deg(), 22.01, 1.0e-10); + test_try_success(); + } + catch (std::exception &e) { + test_try_failure(e); + } + /* test_try("Save and load unbinned CTA observation"); try { -- libgit2 0.21.4