Commit 536084e1d72341b2c3f039310c03cd09011e4e63

Authored by Jürgen Knödlseder
1 parent 62e49649

Implement XML parameter reader and writer.

include/GEbounds.hpp
1 1 /***************************************************************************
2 2 * GEbounds.hpp - Energy boundaries class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009-2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2009-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -30,10 +30,13 @@
30 30 /* __ Includes ___________________________________________________________ */
31 31 #include <string>
32 32 #include "GContainer.hpp"
33   -#include "GFits.hpp"
34   -#include "GFitsTable.hpp"
35 33 #include "GEnergy.hpp"
36 34  
  35 +/* __ Forward declarations _______________________________________________ */
  36 +class GXmlElement;
  37 +class GFits;
  38 +class GFitsTable;
  39 +
37 40  
38 41 /***********************************************************************//**
39 42 * @class GEbounds
... ... @@ -53,6 +56,7 @@ public:
53 56 // Constructors and destructors
54 57 GEbounds(void);
55 58 GEbounds(const GEbounds& ebds);
  59 + explicit GEbounds(const GXmlElement& xml);
56 60 GEbounds(const GEnergy& emin, const GEnergy& emax);
57 61 GEbounds(const int& num, const GEnergy& emin, const GEnergy& emax,
58 62 const bool& log = true);
... ... @@ -88,6 +92,8 @@ public:
88 92 void write(GFits& file,
89 93 const std::string& extname = "EBOUNDS",
90 94 const std::string& unit = "keV") const;
  95 + void read(const GXmlElement& xml);
  96 + void write(GXmlElement& xml) const;
91 97 int index(const GEnergy& eng) const;
92 98 const GEnergy& emin(void) const;
93 99 const GEnergy& emax(void) const;
... ...
include/GGti.hpp
1 1 /***************************************************************************
2 2 * GGti.hpp - Good time interval class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2008-2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2008-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -30,11 +30,14 @@
30 30 /* __ Includes ___________________________________________________________ */
31 31 #include <string>
32 32 #include "GContainer.hpp"
33   -#include "GFits.hpp"
34   -#include "GFitsTable.hpp"
35 33 #include "GTime.hpp"
36 34 #include "GTimeReference.hpp"
37 35  
  36 +/* __ Forward declarations _______________________________________________ */
  37 +class GXmlElement;
  38 +class GFits;
  39 +class GFitsTable;
  40 +
38 41  
39 42 /***********************************************************************//**
40 43 * @class GGti
... ... @@ -56,6 +59,7 @@ public:
56 59 // Constructors and destructors
57 60 GGti(void);
58 61 GGti(const GGti& gti);
  62 + explicit GGti(const GXmlElement& xml);
59 63 explicit GGti(const GTimeReference& ref);
60 64 virtual ~GGti(void);
61 65  
... ... @@ -84,6 +88,8 @@ public:
84 88 void read(const GFitsTable& table);
85 89 void write(GFits& file,
86 90 const std::string& extname = "GTI") const;
  91 + void read(const GXmlElement& xml);
  92 + void write(GXmlElement& xml) const;
87 93 const GTime& tstart(void) const;
88 94 const GTime& tstop(void) const;
89 95 const GTime& tstart(const int& index) const;
... ...
include/GTimeReference.hpp
1 1 /***************************************************************************
2 2 * GTimeReference.hpp - Time reference class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2012-2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2012-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -30,7 +30,10 @@
30 30 /* __ Includes ___________________________________________________________ */
31 31 #include <string>
32 32 #include "GBase.hpp"
33   -#include "GFitsHDU.hpp"
  33 +
  34 +/* __ Forward declarations _______________________________________________ */
  35 +class GXmlElement;
  36 +class GFitsHDU;
34 37  
35 38  
36 39 /***********************************************************************//**
... ... @@ -77,6 +80,8 @@ public:
77 80 std::string classname(void) const;
78 81 void read(const GFitsHDU& hdu);
79 82 void write(GFitsHDU& hdu) const;
  83 + void read(const GXmlElement& xml);
  84 + void write(GXmlElement& xml) const;
80 85 void set(const double& mrdref,
81 86 const std::string& timeunit,
82 87 const std::string& timesys = "TT",
... ...
inst/cta/include/GCTARoi.hpp
1 1 /***************************************************************************
2 2 * GCTARoi.hpp - CTA region of interest class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -50,6 +50,7 @@ class GCTARoi : public GRoi {
50 50 public:
51 51 // Constructors and destructors
52 52 GCTARoi(void);
  53 + explicit GCTARoi(const GXmlElement& xml);
53 54 GCTARoi(const GCTAInstDir& centre, const double& radius);
54 55 GCTARoi(const GCTARoi& roi);
55 56 virtual ~GCTARoi(void);
... ... @@ -69,6 +70,8 @@ public:
69 70 const double& radius(void) const;
70 71 void centre(const GCTAInstDir& centre);
71 72 void radius(const double& radius);
  73 + void read(const GXmlElement& xml);
  74 + void write(GXmlElement& xml) const;
72 75  
73 76 protected:
74 77 // Protected methods
... ...
inst/cta/pyext/GCTARoi.i
... ... @@ -39,6 +39,7 @@ class GCTARoi : public GRoi {
39 39 public:
40 40 // Constructors and destructors
41 41 GCTARoi(void);
  42 + explicit GCTARoi(const GXmlElement& xml);
42 43 GCTARoi(const GCTAInstDir& centre, const double& radius);
43 44 GCTARoi(const GCTARoi& roi);
44 45 virtual ~GCTARoi(void);
... ... @@ -54,6 +55,8 @@ public:
54 55 const double& radius(void) const;
55 56 void centre(const GCTAInstDir& centre);
56 57 void radius(const double& radius);
  58 + void read(const GXmlElement& xml);
  59 + void write(GXmlElement& xml) const;
57 60 };
58 61  
59 62  
... ...
inst/cta/src/GCTAPointing.cpp
... ... @@ -521,45 +521,26 @@ void GCTAPointing::read(const GXmlElement&amp; xml)
521 521 // Clear pointing
522 522 clear();
523 523  
524   - // Determine number of pointing elements in XML element
525   - int nelements = xml.elements("parameter");
526   -
527   - // Extract pointing parameter
528   - int n_pointing = 0;
529   - for (int i = 0; i < nelements; ++i) {
530   -
531   - // Get parameter element
532   - const GXmlElement* par = xml.element("parameter", i);
533   -
534   - // Handle prefactor
535   - if (par->attribute("name") == "Pointing") {
536   -
537   - // Extract position attributes
538   - if (par->has_attribute("ra") && par->has_attribute("dec")) {
539   - double ra = gammalib::todouble(par->attribute("ra"));
540   - double dec = gammalib::todouble(par->attribute("dec"));
541   - m_dir.radec_deg(ra,dec);
542   - }
543   - else if (par->has_attribute("lon") && par->has_attribute("lat")) {
544   - double lon = gammalib::todouble(par->attribute("lon"));
545   - double lat = gammalib::todouble(par->attribute("lat"));
546   - m_dir.lb_deg(lon,lat);
547   - }
548   - else {
549   - std::string msg = "Did not find attributes (ra,dec) or"
550   - " (lon,lat) in XML parameter element."
551   - " Please verify the XML format.";
552   - throw GException::invalid_value(G_READ_XML, msg);
553   - }
554   -
555   - // Signal that we found parameter
556   - n_pointing++;
557   - }
558   -
559   - } // endfor: looped over all parameters
560   -
561   - // Verify that parameter was found
562   - gammalib::xml_parcheck(G_READ_XML, "Pointing", n_pointing);
  524 + // Get pointing parameter
  525 + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "Pointing");
  526 +
  527 + // Extract position attributes
  528 + if (par->has_attribute("ra") && par->has_attribute("dec")) {
  529 + double ra = gammalib::todouble(par->attribute("ra"));
  530 + double dec = gammalib::todouble(par->attribute("dec"));
  531 + m_dir.radec_deg(ra,dec);
  532 + }
  533 + else if (par->has_attribute("lon") && par->has_attribute("lat")) {
  534 + double lon = gammalib::todouble(par->attribute("lon"));
  535 + double lat = gammalib::todouble(par->attribute("lat"));
  536 + m_dir.lb_deg(lon,lat);
  537 + }
  538 + else {
  539 + std::string msg = "Attributes \"ra\" and \"dec\" or \"lon\" and"
  540 + " \"lat\"not found in XML parameter \"Pointing\"."
  541 + " Please verify the XML format.";
  542 + throw GException::invalid_value(G_READ_XML, msg);
  543 + }
563 544  
564 545 // Return
565 546 return;
... ... @@ -579,36 +560,12 @@ void GCTAPointing::read(const GXmlElement&amp; xml)
579 560 ***************************************************************************/
580 561 void GCTAPointing::write(GXmlElement& xml) const
581 562 {
582   - // Determine number of parameter elements in XML element
583   - int nelements = xml.elements("parameter");
  563 + // Get parameter
  564 + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "Pointing");
584 565  
585   - // If XML element has 0 parameter elements then add one
586   - if (nelements == 0) {
587   - xml.append(GXmlElement("parameter name=\"Pointing\""));
588   - nelements++;
589   - }
590   -
591   - // Set or update model parameter attributes
592   - int npar[] = {0};
593   - for (int i = 0; i < nelements; ++i) {
594   -
595   - // Get parameter element
596   - GXmlElement* par = xml.element("parameter", i);
597   -
598   - // Handle prefactor
599   - if (par->attribute("name") == "Pointing") {
600   - par->attribute("ra", gammalib::str(m_dir.ra_deg()));
601   - par->attribute("dec", gammalib::str(m_dir.dec_deg()));
602   - npar[0]++;
603   - }
604   -
605   - } // endfor: looped over all parameters
606   -
607   - // Check of all required parameters are present
608   - if (npar[0] != 1) {
609   - std::string msg = "Require \"Pointing\" parameter.";
610   - throw GException::invalid_value(G_WRITE_XML, msg);
611   - }
  566 + // Write attributes
  567 + par->attribute("ra", gammalib::str(m_dir.ra_deg()));
  568 + par->attribute("dec", gammalib::str(m_dir.dec_deg()));
612 569  
613 570 // Return
614 571 return;
... ...
inst/cta/src/GCTARoi.cpp
1 1 /***************************************************************************
2 2 * GCTARoi.cpp - CTA region of interest class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -33,6 +33,8 @@
33 33 #include "GTools.hpp"
34 34  
35 35 /* __ Method name definitions ____________________________________________ */
  36 +#define G_READ "GCTARoi::read(GXmlElement&)"
  37 +#define G_WRITE "GCTARoi::write(GXmlElement&)"
36 38  
37 39 /* __ Macros _____________________________________________________________ */
38 40  
... ... @@ -62,6 +64,26 @@ GCTARoi::GCTARoi(void) : GRoi()
62 64  
63 65  
64 66 /***********************************************************************//**
  67 + * @brief XML element constructor
  68 + *
  69 + * @param[in] xml XML element.
  70 + *
  71 + * Constructs region of interest from an XML element.
  72 + ***************************************************************************/
  73 +GCTARoi::GCTARoi(const GXmlElement& xml)
  74 +{
  75 + // Initialise members
  76 + init_members();
  77 +
  78 + // Read region of interest from XML element
  79 + read(xml);
  80 +
  81 + // Return
  82 + return;
  83 +}
  84 +
  85 +
  86 +/***********************************************************************//**
65 87 * @brief Region of interest constructor
66 88 *
67 89 * @param[in] centre Region of interest centre.
... ... @@ -210,6 +232,80 @@ bool GCTARoi::contains(const GEvent&amp; event) const
210 232  
211 233  
212 234 /***********************************************************************//**
  235 + * @brief Read region of interest from XML element
  236 + *
  237 + * @param[in] xml XML element.
  238 + *
  239 + * @exception GException::invalid_value
  240 + * Invalid XML format encountered.
  241 + *
  242 + * Read region of interest from an XML element. The format of the region
  243 + * of interest is
  244 + *
  245 + * <parameter name="RegionOfInterest" ra="..." dec="..." rad="..."/>
  246 + *
  247 + * The units are deg.
  248 + ***************************************************************************/
  249 +void GCTARoi::read(const GXmlElement& xml)
  250 +{
  251 + // Clear region of interest
  252 + clear();
  253 +
  254 + // Get region of interest parameter
  255 + const GXmlElement* par = gammalib::xml_getpar(G_READ, xml, "RegionOfInterest");
  256 +
  257 + // Extract position attributes
  258 + if (par->has_attribute("ra") &&
  259 + par->has_attribute("dec") &&
  260 + par->has_attribute("rad")) {
  261 + double ra = gammalib::todouble(par->attribute("ra"));
  262 + double dec = gammalib::todouble(par->attribute("dec"));
  263 + double rad = gammalib::todouble(par->attribute("rad"));
  264 + GSkyDir centre;
  265 + centre.radec_deg(ra,dec);
  266 + this->centre(GCTAInstDir(centre));
  267 + this->radius(rad);
  268 + }
  269 + else {
  270 + std::string msg = "Attributes \"ra\", \"dec\" and/or \"rad\" not found"
  271 + " in XML parameter \"RegionOfInterest\"."
  272 + " Please verify the XML format.";
  273 + throw GException::invalid_value(G_READ, msg);
  274 + }
  275 +
  276 + // Return
  277 + return;
  278 +}
  279 +
  280 +
  281 +/***********************************************************************//**
  282 + * @brief Write region of interest into XML element
  283 + *
  284 + * @param[in] xml XML element.
  285 + *
  286 + * Writes region of interest into an XML element. The format of the region
  287 + * of interest is
  288 + *
  289 + * <parameter name="RegionOfInterest" ra="..." dec="..." rad="..."/>
  290 + *
  291 + * The units are deg.
  292 + ***************************************************************************/
  293 +void GCTARoi::write(GXmlElement& xml) const
  294 +{
  295 + // Get parameter
  296 + GXmlElement* par = gammalib::xml_needpar(G_WRITE, xml, "RegionOfInterest");
  297 +
  298 + // Write attributes
  299 + par->attribute("ra", gammalib::str(m_centre.dir().ra_deg()));
  300 + par->attribute("dec", gammalib::str(m_centre.dir().dec_deg()));
  301 + par->attribute("rad", gammalib::str(m_radius));
  302 +
  303 + // Return
  304 + return;
  305 +}
  306 +
  307 +
  308 +/***********************************************************************//**
213 309 * @brief Print ROI information
214 310 *
215 311 * @param[in] chatter Chattiness (defaults to NORMAL).
... ...
pyext/GEbounds.i
... ... @@ -40,6 +40,7 @@ public:
40 40 // Constructors and destructors
41 41 GEbounds(void);
42 42 GEbounds(const GEbounds& ebds);
  43 + explicit GEbounds(const GXmlElement& xml);
43 44 GEbounds(const GEnergy& emin, const GEnergy& emax);
44 45 GEbounds(const int& num, const GEnergy& emin, const GEnergy& emax,
45 46 const bool& log = true);
... ... @@ -72,6 +73,8 @@ public:
72 73 void write(GFits& file,
73 74 const std::string& extname = "EBOUNDS",
74 75 const std::string& unit = "keV") const;
  76 + void read(const GXmlElement& xml);
  77 + void write(GXmlElement& xml) const;
75 78 int index(const GEnergy& eng) const;
76 79 const GEnergy& emin(void) const;
77 80 const GEnergy& emax(void) const;
... ...
pyext/GGti.i
... ... @@ -40,6 +40,7 @@ public:
40 40 // Constructors and destructors
41 41 GGti(void);
42 42 GGti(const GGti& gti);
  43 + explicit GGti(const GXmlElement& xml);
43 44 explicit GGti(const GTimeReference& ref);
44 45 virtual ~GGti(void);
45 46  
... ... @@ -65,6 +66,8 @@ public:
65 66 void read(const GFitsTable& table);
66 67 void write(GFits& file,
67 68 const std::string& extname = "GTI") const;
  69 + void read(const GXmlElement& xml);
  70 + void write(GXmlElement& xml) const;
68 71 const GTime& tstart(void) const;
69 72 const GTime& tstop(void) const;
70 73 const GTime& tstart(const int& index) const;
... ...
pyext/GTimeReference.i
... ... @@ -57,6 +57,8 @@ public:
57 57 std::string classname(void) const;
58 58 void read(const GFitsHDU& hdu);
59 59 void write(GFitsHDU& hdu) const;
  60 + void read(const GXmlElement& xml);
  61 + void write(GXmlElement& xml) const;
60 62 void set(const double& mrdref,
61 63 const std::string& timeunit,
62 64 const std::string& timesys = "TT",
... ...
src/obs/GEbounds.cpp
1 1 /***************************************************************************
2 2 * GEbounds.cpp - Energy boundary class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009-2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2009-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -37,8 +37,11 @@
37 37 #include "GFitsTable.hpp"
38 38 #include "GFitsBinTable.hpp"
39 39 #include "GFitsTableDoubleCol.hpp"
  40 +#include "GXmlElement.hpp"
40 41  
41 42 /* __ Method name definitions ____________________________________________ */
  43 +#define G_READ_XML "GEbounds::read(GXmlElement&)"
  44 +#define G_WRITE_XML "GEbounds::write(GXmlElement&)"
42 45 #define G_REMOVE "GEbounds::remove(int&)"
43 46 #define G_EMIN "GEbounds::emin(int&)"
44 47 #define G_EMAX "GEbounds::emax(int&)"
... ... @@ -91,6 +94,26 @@ GEbounds::GEbounds(const GEbounds&amp; ebds)
91 94  
92 95  
93 96 /***********************************************************************//**
  97 + * @brief XML element constructor
  98 + *
  99 + * @param[in] xml XML element.
  100 + *
  101 + * Constructs energy bounds from an XML element.
  102 + ***************************************************************************/
  103 +GEbounds::GEbounds(const GXmlElement& xml)
  104 +{
  105 + // Initialise members
  106 + init_members();
  107 +
  108 + // Read energy boundaries from XML element
  109 + read(xml);
  110 +
  111 + // Return
  112 + return;
  113 +}
  114 +
  115 +
  116 +/***********************************************************************//**
94 117 * @brief Single energy band constructor
95 118 *
96 119 * @param[in] emin Minimum energy of the interval.
... ... @@ -103,7 +126,10 @@ GEbounds::GEbounds(const GEnergy&amp; emin, const GEnergy&amp; emax)
103 126 // Initialise members
104 127 init_members();
105 128  
  129 + // Append energies
106 130 append(emin, emax);
  131 +
  132 + // Set attribues
107 133 set_attributes();
108 134  
109 135 // Return
... ... @@ -724,6 +750,76 @@ void GEbounds::write(GFits&amp; file,
724 750  
725 751  
726 752 /***********************************************************************//**
  753 + * @brief Read energy boundaries from XML element
  754 + *
  755 + * @param[in] xml XML element.
  756 + *
  757 + * @exception GException::invalid_value
  758 + * Invalid XML format encountered.
  759 + *
  760 + * Read energy boundaries from an XML element. The format of the energy
  761 + * boundaries is
  762 + *
  763 + * <parameter name="EnergyBoundaries" emin="0.1" emax="10.0"/>
  764 + *
  765 + * The units of the @a emin and @a emax parameters are MeV.
  766 + ***************************************************************************/
  767 +void GEbounds::read(const GXmlElement& xml)
  768 +{
  769 + // Clear energy boundaries
  770 + clear();
  771 +
  772 + // Get energy boundaries parameter
  773 + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "EnergyBoundaries");
  774 +
  775 + // Extract position attributes
  776 + if (par->has_attribute("emin") && par->has_attribute("emax")) {
  777 + double emin = gammalib::todouble(par->attribute("emin"));
  778 + double emax = gammalib::todouble(par->attribute("emax"));
  779 + append(GEnergy(emin, "MeV"), GEnergy(emax, "MeV"));
  780 + }
  781 + else {
  782 + std::string msg = "Attributes \"emin\" and/or \"emax\" not found"
  783 + " in XML parameter \"EnergyBoundaries\"."
  784 + " Please verify the XML format.";
  785 + throw GException::invalid_value(G_READ_XML, msg);
  786 + }
  787 +
  788 + // Set attribues
  789 + set_attributes();
  790 +
  791 + // Return
  792 + return;
  793 +}
  794 +
  795 +
  796 +/***********************************************************************//**
  797 + * @brief Write energy boundaries into XML element
  798 + *
  799 + * @param[in] xml XML element.
  800 + *
  801 + * Writes energy boundaries into an XML element. The format of the energy
  802 + * boundaries is
  803 + *
  804 + * <parameter name="EnergyBoundaries" emin="0.1" emax="10.0"/>
  805 + *
  806 + * The units of the @a emin and @a emax parameters are MeV.
  807 + ***************************************************************************/
  808 +void GEbounds::write(GXmlElement& xml) const
  809 +{
  810 + // Get parameter
  811 + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "EnergyBoundaries");
  812 +
  813 + // Write attributes
  814 + par->attribute("emin", gammalib::str(emin().MeV()));
  815 + par->attribute("emax", gammalib::str(emax().MeV()));
  816 +
  817 + // Return
  818 + return;
  819 +}
  820 +
  821 +
  822 +/***********************************************************************//**
727 823 * @brief Returns energy bin index for a given energy
728 824 *
729 825 * @param[in] eng Energy.
... ...
src/obs/GGti.cpp
1 1 /***************************************************************************
2 2 * GGti.cpp - Good time interval class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2008-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2008-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -35,9 +35,12 @@
35 35 #include "GFitsTable.hpp"
36 36 #include "GFitsBinTable.hpp"
37 37 #include "GFitsTableDoubleCol.hpp"
  38 +#include "GXmlElement.hpp"
38 39  
39 40 /* __ Method name definitions ____________________________________________ */
40 41 #define G_REMOVE "GGti::remove(int&)"
  42 +#define G_READ_XML "GGti::read(GXmlElement&)"
  43 +#define G_WRITE_XML "GGti::write(GXmlElement&)"
41 44 #define G_TSTART "GGti::tstart(int&)"
42 45 #define G_TSTOP "GGti::tstop(int&)"
43 46  
... ... @@ -86,6 +89,26 @@ GGti::GGti(const GGti&amp; gti)
86 89  
87 90  
88 91 /***********************************************************************//**
  92 + * @brief XML element constructor
  93 + *
  94 + * @param[in] xml XML element.
  95 + *
  96 + * Constructs Good Time Intervals from an XML element.
  97 + ***************************************************************************/
  98 +GGti::GGti(const GXmlElement& xml)
  99 +{
  100 + // Initialise members
  101 + init_members();
  102 +
  103 + // Read Good Time Intervals from XML element
  104 + read(xml);
  105 +
  106 + // Return
  107 + return;
  108 +}
  109 +
  110 +
  111 +/***********************************************************************//**
89 112 * @brief Time reference constructor
90 113 *
91 114 * @param[in] ref Time reference.
... ... @@ -666,6 +689,85 @@ void GGti::write(GFits&amp; file, const std::string&amp; extname) const
666 689  
667 690  
668 691 /***********************************************************************//**
  692 + * @brief Read Good Time Intervals from XML element
  693 + *
  694 + * @param[in] xml XML element.
  695 + *
  696 + * @exception GException::invalid_value
  697 + * Invalid XML format encountered.
  698 + *
  699 + * Read Good Time Intervals from an XML element. The format of the Good Time
  700 + * Intervals is
  701 + *
  702 + * <parameter name="GoodTimeIntervals" tmin="..." tmax="..."/>
  703 + *
  704 + * The units of the @a tmin and @a tmax parameters are seconds.
  705 + *
  706 + * Note that the method also expects that the time reference is provided
  707 + * as parameter in the @p xml element.
  708 + ***************************************************************************/
  709 +void GGti::read(const GXmlElement& xml)
  710 +{
  711 + // Clear energy boundaries
  712 + clear();
  713 +
  714 + // Read time reference
  715 + m_reference.read(xml);
  716 +
  717 + // Get energy boundaries parameter
  718 + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "GoodTimeIntervals");
  719 +
  720 + // Extract tmin and tmax attributes
  721 + if (par->has_attribute("tmin") && par->has_attribute("tmax")) {
  722 + double tmin = gammalib::todouble(par->attribute("tmin"));
  723 + double tmax = gammalib::todouble(par->attribute("tmax"));
  724 + append(GTime(tmin, m_reference), GTime(tmax, m_reference));
  725 + }
  726 + else {
  727 + std::string msg = "Attributes \"tmin\" and/or \"tmax\" not found"
  728 + " in XML parameter \"GoodTimeIntervals\"."
  729 + " Please verify the XML format.";
  730 + throw GException::invalid_value(G_READ_XML, msg);
  731 + }
  732 +
  733 + // Return
  734 + return;
  735 +}
  736 +
  737 +
  738 +/***********************************************************************//**
  739 + * @brief Write Good Time Intervals into XML element
  740 + *
  741 + * @param[in] xml XML element.
  742 + *
  743 + * Writes Good Time Intervals into an XML element. The format of the Good
  744 + * Time Intervals is
  745 + *
  746 + * <parameter name="GoodTimeIntervals" tmin="..." tmax="..."/>
  747 + *
  748 + * The units of the @a tmin and @a tmax parameters are seconds.
  749 + *
  750 + * The method also writes the time reference as parameter in the @p xml
  751 + * element.
  752 + ***************************************************************************/
  753 +void GGti::write(GXmlElement& xml) const
  754 +{
  755 + // Get parameter
  756 + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "GoodTimeIntervals");
  757 +
  758 + // Write time interval
  759 + par->attribute("tmin", gammalib::str(tstart().convert(m_reference)));
  760 + par->attribute("tmax", gammalib::str(tstop().convert(m_reference)));
  761 +
  762 + // Write time reference
  763 + m_reference.write(xml);
  764 +
  765 + // Return
  766 + return;
  767 +}
  768 +
  769 +
  770 +/***********************************************************************//**
669 771 * @brief Returns earliest start time in Good Time Intervals
670 772 *
671 773 * @return Earliest start time in Good Time Intervals.
... ...
src/obs/GTimeReference.cpp
1 1 /***************************************************************************
2 2 * GTimeReference.cpp - Time reference class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2012-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2012-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -31,11 +31,15 @@
31 31 #include "GTimeReference.hpp"
32 32 #include "GTools.hpp"
33 33 #include "GException.hpp"
  34 +#include "GFitsHDU.hpp"
  35 +#include "GXmlElement.hpp"
34 36  
35 37 /* __ Constants __________________________________________________________ */
36 38  
37 39 /* __ Method name definitions ____________________________________________ */
38 40 #define G_READ "GTimeReference::read(GFitsHDU&)"
  41 +#define G_READ_XML "GTimeReference::read(GXmlElement&)"
  42 +#define G_WRITE_XML "GTimeReference::write(GXmlElement&)"
39 43 #define G_SET "GTimeReference::set(double&, std::string&, std::string&, "\
40 44 "std::string&)"
41 45  
... ... @@ -332,6 +336,120 @@ void GTimeReference::write(GFitsHDU&amp; hdu) const
332 336  
333 337  
334 338 /***********************************************************************//**
  339 + * @brief Read time reference from XML element
  340 + *
  341 + * @param[in] xml XML element.
  342 + *
  343 + * @exception GException::invalid_value
  344 + * Invalid XML format encountered.
  345 + *
  346 + * Reads the time reference from an XML element. The format of the time
  347 + * reference is
  348 + *
  349 + * <parameter name="TimeReference" mjdrefi="..." mjdreff="..."
  350 + * timeunit="..." timesys="..." timeref="..."/>
  351 + *
  352 + ***************************************************************************/
  353 +void GTimeReference::read(const GXmlElement& xml)
  354 +{
  355 + // Clear energy boundaries
  356 + clear();
  357 +
  358 + // Get energy boundaries parameter
  359 + const GXmlElement* par = gammalib::xml_getpar(G_READ_XML, xml, "TimeReference");
  360 +
  361 + // Initialise reference values
  362 + int mjdrefi;
  363 + double mjdreff;
  364 + std::string timeunit;
  365 + std::string timesys;
  366 + std::string timeref;
  367 +
  368 + // Extract attributes
  369 + if (par->has_attribute("mjdrefi")) {
  370 + mjdrefi = gammalib::toint(par->attribute("mjdrefi"));
  371 + }
  372 + else {
  373 + std::string msg = "Attribute \"mjdrefi\" not found in XML parameter"
  374 + " \"TimeReference\"."
  375 + " Please verify the XML format.";
  376 + throw GException::invalid_value(G_READ_XML, msg);
  377 + }
  378 + if (par->has_attribute("mjdreff")) {
  379 + mjdreff = gammalib::todouble(par->attribute("mjdreff"));
  380 + }
  381 + else {
  382 + std::string msg = "Attribute \"mjdreff\" not found in XML parameter"
  383 + " \"TimeReference\"."
  384 + " Please verify the XML format.";
  385 + throw GException::invalid_value(G_READ_XML, msg);
  386 + }
  387 + if (par->has_attribute("timeunit")) {
  388 + timeunit = par->attribute("timeunit");
  389 + }
  390 + else {
  391 + std::string msg = "Attribute \"timeunit\" not found in XML parameter"
  392 + " \"TimeReference\"."
  393 + " Please verify the XML format.";
  394 + throw GException::invalid_value(G_READ_XML, msg);
  395 + }
  396 + if (par->has_attribute("timesys")) {
  397 + timesys = par->attribute("timesys");
  398 + }
  399 + else {
  400 + std::string msg = "Attribute \"timesys\" not found in XML parameter"
  401 + " \"TimeReference\"."
  402 + " Please verify the XML format.";
  403 + throw GException::invalid_value(G_READ_XML, msg);
  404 + }
  405 + if (par->has_attribute("timeref")) {
  406 + timeref = par->attribute("timeref");
  407 + }
  408 + else {
  409 + std::string msg = "Attribute \"timeref\" not found in XML parameter"
  410 + " \"TimeReference\"."
  411 + " Please verify the XML format.";
  412 + throw GException::invalid_value(G_READ_XML, msg);
  413 + }
  414 +
  415 + // Set time reference
  416 + set(mjdrefi, mjdreff, timeunit, timesys, timeref);
  417 +
  418 + // Return
  419 + return;
  420 +}
  421 +
  422 +
  423 +/***********************************************************************//**
  424 + * @brief Write time reference into XML element
  425 + *
  426 + * @param[in] xml XML element.
  427 + *
  428 + * Writes the time reference into an XML element. The format of the time
  429 + * reference is
  430 + *
  431 + * <parameter name="TimeReference" mjdrefi="..." mjdreff="..."
  432 + * timeunit="..." timesys="..." timeref="..."/>
  433 + *
  434 + ***************************************************************************/
  435 +void GTimeReference::write(GXmlElement& xml) const
  436 +{
  437 + // Get parameter
  438 + GXmlElement* par = gammalib::xml_needpar(G_WRITE_XML, xml, "TimeReference");
  439 +
  440 + // Write time reference
  441 + par->attribute("mjdrefi", gammalib::str(mjdrefi()));
  442 + par->attribute("mjdreff", gammalib::str(mjdreff()));
  443 + par->attribute("timeunit", timeunit());
  444 + par->attribute("timesys", timesys());
  445 + par->attribute("timeref", timeref());
  446 +
  447 + // Return
  448 + return;
  449 +}
  450 +
  451 +
  452 +/***********************************************************************//**
335 453 * @brief Set time reference
336 454 *
337 455 * @param[in] mjdref Reference MJD (days).
... ...