From 536084e1d72341b2c3f039310c03cd09011e4e63 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 21 Feb 2015 23:38:59 +0100 Subject: [PATCH] Implement XML parameter reader and writer. --- include/GEbounds.hpp | 12 +++++++++--- include/GGti.hpp | 12 +++++++++--- include/GTimeReference.hpp | 9 +++++++-- inst/cta/include/GCTARoi.hpp | 5 ++++- inst/cta/pyext/GCTARoi.i | 3 +++ inst/cta/src/GCTAPointing.cpp | 93 +++++++++++++++++++++++++-------------------------------------------------------------------- inst/cta/src/GCTARoi.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- pyext/GEbounds.i | 3 +++ pyext/GGti.i | 3 +++ pyext/GTimeReference.i | 2 ++ src/obs/GEbounds.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/obs/GGti.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/obs/GTimeReference.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 13 files changed, 481 insertions(+), 81 deletions(-) diff --git a/include/GEbounds.hpp b/include/GEbounds.hpp index 0bf2814..a36bd03 100644 --- a/include/GEbounds.hpp +++ b/include/GEbounds.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GEbounds.hpp - Energy boundaries class * * ----------------------------------------------------------------------- * - * copyright (C) 2009-2014 by Juergen Knoedlseder * + * copyright (C) 2009-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,10 +30,13 @@ /* __ Includes ___________________________________________________________ */ #include #include "GContainer.hpp" -#include "GFits.hpp" -#include "GFitsTable.hpp" #include "GEnergy.hpp" +/* __ Forward declarations _______________________________________________ */ +class GXmlElement; +class GFits; +class GFitsTable; + /***********************************************************************//** * @class GEbounds @@ -53,6 +56,7 @@ public: // Constructors and destructors GEbounds(void); GEbounds(const GEbounds& ebds); + explicit GEbounds(const GXmlElement& xml); GEbounds(const GEnergy& emin, const GEnergy& emax); GEbounds(const int& num, const GEnergy& emin, const GEnergy& emax, const bool& log = true); @@ -88,6 +92,8 @@ public: void write(GFits& file, const std::string& extname = "EBOUNDS", const std::string& unit = "keV") const; + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; int index(const GEnergy& eng) const; const GEnergy& emin(void) const; const GEnergy& emax(void) const; diff --git a/include/GGti.hpp b/include/GGti.hpp index 75d91e9..a319054 100644 --- a/include/GGti.hpp +++ b/include/GGti.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GGti.hpp - Good time interval class * * ----------------------------------------------------------------------- * - * copyright (C) 2008-2014 by Juergen Knoedlseder * + * copyright (C) 2008-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,11 +30,14 @@ /* __ Includes ___________________________________________________________ */ #include #include "GContainer.hpp" -#include "GFits.hpp" -#include "GFitsTable.hpp" #include "GTime.hpp" #include "GTimeReference.hpp" +/* __ Forward declarations _______________________________________________ */ +class GXmlElement; +class GFits; +class GFitsTable; + /***********************************************************************//** * @class GGti @@ -56,6 +59,7 @@ public: // Constructors and destructors GGti(void); GGti(const GGti& gti); + explicit GGti(const GXmlElement& xml); explicit GGti(const GTimeReference& ref); virtual ~GGti(void); @@ -84,6 +88,8 @@ public: void read(const GFitsTable& table); void write(GFits& file, const std::string& extname = "GTI") const; + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; const GTime& tstart(void) const; const GTime& tstop(void) const; const GTime& tstart(const int& index) const; diff --git a/include/GTimeReference.hpp b/include/GTimeReference.hpp index 6f3a52a..2e4ea87 100644 --- a/include/GTimeReference.hpp +++ b/include/GTimeReference.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GTimeReference.hpp - Time reference class * * ----------------------------------------------------------------------- * - * copyright (C) 2012-2014 by Juergen Knoedlseder * + * copyright (C) 2012-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,7 +30,10 @@ /* __ Includes ___________________________________________________________ */ #include #include "GBase.hpp" -#include "GFitsHDU.hpp" + +/* __ Forward declarations _______________________________________________ */ +class GXmlElement; +class GFitsHDU; /***********************************************************************//** @@ -77,6 +80,8 @@ public: std::string classname(void) const; void read(const GFitsHDU& hdu); void write(GFitsHDU& hdu) const; + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; void set(const double& mrdref, const std::string& timeunit, const std::string& timesys = "TT", diff --git a/inst/cta/include/GCTARoi.hpp b/inst/cta/include/GCTARoi.hpp index d1350d4..c24f81b 100644 --- a/inst/cta/include/GCTARoi.hpp +++ b/inst/cta/include/GCTARoi.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GCTARoi.hpp - CTA region of interest class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2014 by Juergen Knoedlseder * + * copyright (C) 2010-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -50,6 +50,7 @@ class GCTARoi : public GRoi { public: // Constructors and destructors GCTARoi(void); + explicit GCTARoi(const GXmlElement& xml); GCTARoi(const GCTAInstDir& centre, const double& radius); GCTARoi(const GCTARoi& roi); virtual ~GCTARoi(void); @@ -69,6 +70,8 @@ public: const double& radius(void) const; void centre(const GCTAInstDir& centre); void radius(const double& radius); + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; protected: // Protected methods diff --git a/inst/cta/pyext/GCTARoi.i b/inst/cta/pyext/GCTARoi.i index 1814711..3fb97a8 100644 --- a/inst/cta/pyext/GCTARoi.i +++ b/inst/cta/pyext/GCTARoi.i @@ -39,6 +39,7 @@ class GCTARoi : public GRoi { public: // Constructors and destructors GCTARoi(void); + explicit GCTARoi(const GXmlElement& xml); GCTARoi(const GCTAInstDir& centre, const double& radius); GCTARoi(const GCTARoi& roi); virtual ~GCTARoi(void); @@ -54,6 +55,8 @@ public: const double& radius(void) const; void centre(const GCTAInstDir& centre); void radius(const double& radius); + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; }; diff --git a/inst/cta/src/GCTAPointing.cpp b/inst/cta/src/GCTAPointing.cpp index 4b7287f..a6b620e 100644 --- a/inst/cta/src/GCTAPointing.cpp +++ b/inst/cta/src/GCTAPointing.cpp @@ -521,45 +521,26 @@ void GCTAPointing::read(const GXmlElement& xml) // Clear pointing clear(); - // Determine number of pointing elements in XML element - int nelements = xml.elements("parameter"); - - // Extract pointing parameter - int n_pointing = 0; - for (int i = 0; i < nelements; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle prefactor - if (par->attribute("name") == "Pointing") { - - // Extract position attributes - if (par->has_attribute("ra") && par->has_attribute("dec")) { - double ra = gammalib::todouble(par->attribute("ra")); - double dec = gammalib::todouble(par->attribute("dec")); - m_dir.radec_deg(ra,dec); - } - else if (par->has_attribute("lon") && par->has_attribute("lat")) { - double lon = gammalib::todouble(par->attribute("lon")); - double lat = gammalib::todouble(par->attribute("lat")); - m_dir.lb_deg(lon,lat); - } - else { - std::string msg = "Did not find attributes (ra,dec) or" - " (lon,lat) in XML parameter element." - " Please verify the XML format."; - throw GException::invalid_value(G_READ_XML, msg); - } - - // Signal that we found parameter - n_pointing++; - } - - } // endfor: looped over all parameters - - // Verify that parameter was found - gammalib::xml_parcheck(G_READ_XML, "Pointing", n_pointing); + // Get pointing parameter + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "Pointing"); + + // Extract position attributes + if (par->has_attribute("ra") && par->has_attribute("dec")) { + double ra = gammalib::todouble(par->attribute("ra")); + double dec = gammalib::todouble(par->attribute("dec")); + m_dir.radec_deg(ra,dec); + } + else if (par->has_attribute("lon") && par->has_attribute("lat")) { + double lon = gammalib::todouble(par->attribute("lon")); + double lat = gammalib::todouble(par->attribute("lat")); + m_dir.lb_deg(lon,lat); + } + else { + std::string msg = "Attributes \"ra\" and \"dec\" or \"lon\" and" + " \"lat\"not found in XML parameter \"Pointing\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } // Return return; @@ -579,36 +560,12 @@ void GCTAPointing::read(const GXmlElement& xml) ***************************************************************************/ void GCTAPointing::write(GXmlElement& xml) const { - // Determine number of parameter elements in XML element - int nelements = xml.elements("parameter"); + // Get parameter + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "Pointing"); - // If XML element has 0 parameter elements then add one - if (nelements == 0) { - xml.append(GXmlElement("parameter name=\"Pointing\"")); - nelements++; - } - - // Set or update model parameter attributes - int npar[] = {0}; - for (int i = 0; i < nelements; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); - - // Handle prefactor - if (par->attribute("name") == "Pointing") { - par->attribute("ra", gammalib::str(m_dir.ra_deg())); - par->attribute("dec", gammalib::str(m_dir.dec_deg())); - npar[0]++; - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1) { - std::string msg = "Require \"Pointing\" parameter."; - throw GException::invalid_value(G_WRITE_XML, msg); - } + // Write attributes + par->attribute("ra", gammalib::str(m_dir.ra_deg())); + par->attribute("dec", gammalib::str(m_dir.dec_deg())); // Return return; diff --git a/inst/cta/src/GCTARoi.cpp b/inst/cta/src/GCTARoi.cpp index ec27968..edb51f4 100644 --- a/inst/cta/src/GCTARoi.cpp +++ b/inst/cta/src/GCTARoi.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GCTARoi.cpp - CTA region of interest class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2013 by Juergen Knoedlseder * + * copyright (C) 2010-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -33,6 +33,8 @@ #include "GTools.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_READ "GCTARoi::read(GXmlElement&)" +#define G_WRITE "GCTARoi::write(GXmlElement&)" /* __ Macros _____________________________________________________________ */ @@ -62,6 +64,26 @@ GCTARoi::GCTARoi(void) : GRoi() /***********************************************************************//** + * @brief XML element constructor + * + * @param[in] xml XML element. + * + * Constructs region of interest from an XML element. + ***************************************************************************/ +GCTARoi::GCTARoi(const GXmlElement& xml) +{ + // Initialise members + init_members(); + + // Read region of interest from XML element + read(xml); + + // Return + return; +} + + +/***********************************************************************//** * @brief Region of interest constructor * * @param[in] centre Region of interest centre. @@ -210,6 +232,80 @@ bool GCTARoi::contains(const GEvent& event) const /***********************************************************************//** + * @brief Read region of interest from XML element + * + * @param[in] xml XML element. + * + * @exception GException::invalid_value + * Invalid XML format encountered. + * + * Read region of interest from an XML element. The format of the region + * of interest is + * + * + * + * The units are deg. + ***************************************************************************/ +void GCTARoi::read(const GXmlElement& xml) +{ + // Clear region of interest + clear(); + + // Get region of interest parameter + const GXmlElement* par = gammalib::xml_getpar(G_READ, xml, "RegionOfInterest"); + + // Extract position attributes + if (par->has_attribute("ra") && + par->has_attribute("dec") && + par->has_attribute("rad")) { + double ra = gammalib::todouble(par->attribute("ra")); + double dec = gammalib::todouble(par->attribute("dec")); + double rad = gammalib::todouble(par->attribute("rad")); + GSkyDir centre; + centre.radec_deg(ra,dec); + this->centre(GCTAInstDir(centre)); + this->radius(rad); + } + else { + std::string msg = "Attributes \"ra\", \"dec\" and/or \"rad\" not found" + " in XML parameter \"RegionOfInterest\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ, msg); + } + + // Return + return; +} + + +/***********************************************************************//** + * @brief Write region of interest into XML element + * + * @param[in] xml XML element. + * + * Writes region of interest into an XML element. The format of the region + * of interest is + * + * + * + * The units are deg. + ***************************************************************************/ +void GCTARoi::write(GXmlElement& xml) const +{ + // Get parameter + GXmlElement* par = gammalib::xml_needpar(G_WRITE, xml, "RegionOfInterest"); + + // Write attributes + par->attribute("ra", gammalib::str(m_centre.dir().ra_deg())); + par->attribute("dec", gammalib::str(m_centre.dir().dec_deg())); + par->attribute("rad", gammalib::str(m_radius)); + + // Return + return; +} + + +/***********************************************************************//** * @brief Print ROI information * * @param[in] chatter Chattiness (defaults to NORMAL). diff --git a/pyext/GEbounds.i b/pyext/GEbounds.i index b06fc0e..a63f84c 100644 --- a/pyext/GEbounds.i +++ b/pyext/GEbounds.i @@ -40,6 +40,7 @@ public: // Constructors and destructors GEbounds(void); GEbounds(const GEbounds& ebds); + explicit GEbounds(const GXmlElement& xml); GEbounds(const GEnergy& emin, const GEnergy& emax); GEbounds(const int& num, const GEnergy& emin, const GEnergy& emax, const bool& log = true); @@ -72,6 +73,8 @@ public: void write(GFits& file, const std::string& extname = "EBOUNDS", const std::string& unit = "keV") const; + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; int index(const GEnergy& eng) const; const GEnergy& emin(void) const; const GEnergy& emax(void) const; diff --git a/pyext/GGti.i b/pyext/GGti.i index 3f69423..4119c86 100644 --- a/pyext/GGti.i +++ b/pyext/GGti.i @@ -40,6 +40,7 @@ public: // Constructors and destructors GGti(void); GGti(const GGti& gti); + explicit GGti(const GXmlElement& xml); explicit GGti(const GTimeReference& ref); virtual ~GGti(void); @@ -65,6 +66,8 @@ public: void read(const GFitsTable& table); void write(GFits& file, const std::string& extname = "GTI") const; + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; const GTime& tstart(void) const; const GTime& tstop(void) const; const GTime& tstart(const int& index) const; diff --git a/pyext/GTimeReference.i b/pyext/GTimeReference.i index e01d9e6..23d5292 100644 --- a/pyext/GTimeReference.i +++ b/pyext/GTimeReference.i @@ -57,6 +57,8 @@ public: std::string classname(void) const; void read(const GFitsHDU& hdu); void write(GFitsHDU& hdu) const; + void read(const GXmlElement& xml); + void write(GXmlElement& xml) const; void set(const double& mrdref, const std::string& timeunit, const std::string& timesys = "TT", diff --git a/src/obs/GEbounds.cpp b/src/obs/GEbounds.cpp index 2cef51b..6df8fbb 100644 --- a/src/obs/GEbounds.cpp +++ b/src/obs/GEbounds.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GEbounds.cpp - Energy boundary class * * ----------------------------------------------------------------------- * - * copyright (C) 2009-2014 by Juergen Knoedlseder * + * copyright (C) 2009-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -37,8 +37,11 @@ #include "GFitsTable.hpp" #include "GFitsBinTable.hpp" #include "GFitsTableDoubleCol.hpp" +#include "GXmlElement.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_READ_XML "GEbounds::read(GXmlElement&)" +#define G_WRITE_XML "GEbounds::write(GXmlElement&)" #define G_REMOVE "GEbounds::remove(int&)" #define G_EMIN "GEbounds::emin(int&)" #define G_EMAX "GEbounds::emax(int&)" @@ -91,6 +94,26 @@ GEbounds::GEbounds(const GEbounds& ebds) /***********************************************************************//** + * @brief XML element constructor + * + * @param[in] xml XML element. + * + * Constructs energy bounds from an XML element. + ***************************************************************************/ +GEbounds::GEbounds(const GXmlElement& xml) +{ + // Initialise members + init_members(); + + // Read energy boundaries from XML element + read(xml); + + // Return + return; +} + + +/***********************************************************************//** * @brief Single energy band constructor * * @param[in] emin Minimum energy of the interval. @@ -103,7 +126,10 @@ GEbounds::GEbounds(const GEnergy& emin, const GEnergy& emax) // Initialise members init_members(); + // Append energies append(emin, emax); + + // Set attribues set_attributes(); // Return @@ -724,6 +750,76 @@ void GEbounds::write(GFits& file, /***********************************************************************//** + * @brief Read energy boundaries from XML element + * + * @param[in] xml XML element. + * + * @exception GException::invalid_value + * Invalid XML format encountered. + * + * Read energy boundaries from an XML element. The format of the energy + * boundaries is + * + * + * + * The units of the @a emin and @a emax parameters are MeV. + ***************************************************************************/ +void GEbounds::read(const GXmlElement& xml) +{ + // Clear energy boundaries + clear(); + + // Get energy boundaries parameter + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "EnergyBoundaries"); + + // Extract position attributes + if (par->has_attribute("emin") && par->has_attribute("emax")) { + double emin = gammalib::todouble(par->attribute("emin")); + double emax = gammalib::todouble(par->attribute("emax")); + append(GEnergy(emin, "MeV"), GEnergy(emax, "MeV")); + } + else { + std::string msg = "Attributes \"emin\" and/or \"emax\" not found" + " in XML parameter \"EnergyBoundaries\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + + // Set attribues + set_attributes(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Write energy boundaries into XML element + * + * @param[in] xml XML element. + * + * Writes energy boundaries into an XML element. The format of the energy + * boundaries is + * + * + * + * The units of the @a emin and @a emax parameters are MeV. + ***************************************************************************/ +void GEbounds::write(GXmlElement& xml) const +{ + // Get parameter + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "EnergyBoundaries"); + + // Write attributes + par->attribute("emin", gammalib::str(emin().MeV())); + par->attribute("emax", gammalib::str(emax().MeV())); + + // Return + return; +} + + +/***********************************************************************//** * @brief Returns energy bin index for a given energy * * @param[in] eng Energy. diff --git a/src/obs/GGti.cpp b/src/obs/GGti.cpp index 39ae369..0ea7f50 100644 --- a/src/obs/GGti.cpp +++ b/src/obs/GGti.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GGti.cpp - Good time interval class * * ----------------------------------------------------------------------- * - * copyright (C) 2008-2013 by Juergen Knoedlseder * + * copyright (C) 2008-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -35,9 +35,12 @@ #include "GFitsTable.hpp" #include "GFitsBinTable.hpp" #include "GFitsTableDoubleCol.hpp" +#include "GXmlElement.hpp" /* __ Method name definitions ____________________________________________ */ #define G_REMOVE "GGti::remove(int&)" +#define G_READ_XML "GGti::read(GXmlElement&)" +#define G_WRITE_XML "GGti::write(GXmlElement&)" #define G_TSTART "GGti::tstart(int&)" #define G_TSTOP "GGti::tstop(int&)" @@ -86,6 +89,26 @@ GGti::GGti(const GGti& gti) /***********************************************************************//** + * @brief XML element constructor + * + * @param[in] xml XML element. + * + * Constructs Good Time Intervals from an XML element. + ***************************************************************************/ +GGti::GGti(const GXmlElement& xml) +{ + // Initialise members + init_members(); + + // Read Good Time Intervals from XML element + read(xml); + + // Return + return; +} + + +/***********************************************************************//** * @brief Time reference constructor * * @param[in] ref Time reference. @@ -666,6 +689,85 @@ void GGti::write(GFits& file, const std::string& extname) const /***********************************************************************//** + * @brief Read Good Time Intervals from XML element + * + * @param[in] xml XML element. + * + * @exception GException::invalid_value + * Invalid XML format encountered. + * + * Read Good Time Intervals from an XML element. The format of the Good Time + * Intervals is + * + * + * + * The units of the @a tmin and @a tmax parameters are seconds. + * + * Note that the method also expects that the time reference is provided + * as parameter in the @p xml element. + ***************************************************************************/ +void GGti::read(const GXmlElement& xml) +{ + // Clear energy boundaries + clear(); + + // Read time reference + m_reference.read(xml); + + // Get energy boundaries parameter + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "GoodTimeIntervals"); + + // Extract tmin and tmax attributes + if (par->has_attribute("tmin") && par->has_attribute("tmax")) { + double tmin = gammalib::todouble(par->attribute("tmin")); + double tmax = gammalib::todouble(par->attribute("tmax")); + append(GTime(tmin, m_reference), GTime(tmax, m_reference)); + } + else { + std::string msg = "Attributes \"tmin\" and/or \"tmax\" not found" + " in XML parameter \"GoodTimeIntervals\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + + // Return + return; +} + + +/***********************************************************************//** + * @brief Write Good Time Intervals into XML element + * + * @param[in] xml XML element. + * + * Writes Good Time Intervals into an XML element. The format of the Good + * Time Intervals is + * + * + * + * The units of the @a tmin and @a tmax parameters are seconds. + * + * The method also writes the time reference as parameter in the @p xml + * element. + ***************************************************************************/ +void GGti::write(GXmlElement& xml) const +{ + // Get parameter + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "GoodTimeIntervals"); + + // Write time interval + par->attribute("tmin", gammalib::str(tstart().convert(m_reference))); + par->attribute("tmax", gammalib::str(tstop().convert(m_reference))); + + // Write time reference + m_reference.write(xml); + + // Return + return; +} + + +/***********************************************************************//** * @brief Returns earliest start time in Good Time Intervals * * @return Earliest start time in Good Time Intervals. diff --git a/src/obs/GTimeReference.cpp b/src/obs/GTimeReference.cpp index c0b26a6..00091d9 100644 --- a/src/obs/GTimeReference.cpp +++ b/src/obs/GTimeReference.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GTimeReference.cpp - Time reference class * * ----------------------------------------------------------------------- * - * copyright (C) 2012-2013 by Juergen Knoedlseder * + * copyright (C) 2012-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -31,11 +31,15 @@ #include "GTimeReference.hpp" #include "GTools.hpp" #include "GException.hpp" +#include "GFitsHDU.hpp" +#include "GXmlElement.hpp" /* __ Constants __________________________________________________________ */ /* __ Method name definitions ____________________________________________ */ #define G_READ "GTimeReference::read(GFitsHDU&)" +#define G_READ_XML "GTimeReference::read(GXmlElement&)" +#define G_WRITE_XML "GTimeReference::write(GXmlElement&)" #define G_SET "GTimeReference::set(double&, std::string&, std::string&, "\ "std::string&)" @@ -332,6 +336,120 @@ void GTimeReference::write(GFitsHDU& hdu) const /***********************************************************************//** + * @brief Read time reference from XML element + * + * @param[in] xml XML element. + * + * @exception GException::invalid_value + * Invalid XML format encountered. + * + * Reads the time reference from an XML element. The format of the time + * reference is + * + * + * + ***************************************************************************/ +void GTimeReference::read(const GXmlElement& xml) +{ + // Clear energy boundaries + clear(); + + // Get energy boundaries parameter + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "TimeReference"); + + // Initialise reference values + int mjdrefi; + double mjdreff; + std::string timeunit; + std::string timesys; + std::string timeref; + + // Extract attributes + if (par->has_attribute("mjdrefi")) { + mjdrefi = gammalib::toint(par->attribute("mjdrefi")); + } + else { + std::string msg = "Attribute \"mjdrefi\" not found in XML parameter" + " \"TimeReference\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + if (par->has_attribute("mjdreff")) { + mjdreff = gammalib::todouble(par->attribute("mjdreff")); + } + else { + std::string msg = "Attribute \"mjdreff\" not found in XML parameter" + " \"TimeReference\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + if (par->has_attribute("timeunit")) { + timeunit = par->attribute("timeunit"); + } + else { + std::string msg = "Attribute \"timeunit\" not found in XML parameter" + " \"TimeReference\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + if (par->has_attribute("timesys")) { + timesys = par->attribute("timesys"); + } + else { + std::string msg = "Attribute \"timesys\" not found in XML parameter" + " \"TimeReference\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + if (par->has_attribute("timeref")) { + timeref = par->attribute("timeref"); + } + else { + std::string msg = "Attribute \"timeref\" not found in XML parameter" + " \"TimeReference\"." + " Please verify the XML format."; + throw GException::invalid_value(G_READ_XML, msg); + } + + // Set time reference + set(mjdrefi, mjdreff, timeunit, timesys, timeref); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Write time reference into XML element + * + * @param[in] xml XML element. + * + * Writes the time reference into an XML element. The format of the time + * reference is + * + * + * + ***************************************************************************/ +void GTimeReference::write(GXmlElement& xml) const +{ + // Get parameter + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "TimeReference"); + + // Write time reference + par->attribute("mjdrefi", gammalib::str(mjdrefi())); + par->attribute("mjdreff", gammalib::str(mjdreff())); + par->attribute("timeunit", timeunit()); + par->attribute("timesys", timesys()); + par->attribute("timeref", timeref()); + + // Return + return; +} + + +/***********************************************************************//** * @brief Set time reference * * @param[in] mjdref Reference MJD (days). -- libgit2 0.21.4