From b7f38b1285a61effea561a32c625d2aa998be7f7 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Tue, 16 Jan 2018 12:40:00 +0100 Subject: [PATCH] Use XML support functions in all model classes (#1430) --- ChangeLog | 1 + NEWS | 3 +++ doc/source/admin/release_history/1.5.rst | 2 ++ src/model/GModelSpatialDiffuseConst.cpp | 6 +++--- src/model/GModelSpatialDiffuseCube.cpp | 1 - src/model/GModelSpatialDiffuseMap.cpp | 1 - src/model/GModelSpatialElliptical.cpp | 143 +++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------- src/model/GModelSpatialEllipticalDisk.cpp | 98 +++++++++++++------------------------------------------------------------------------------------- src/model/GModelSpatialEllipticalGauss.cpp | 100 ++++++++++++++-------------------------------------------------------------------------------------- src/model/GModelSpatialPointSource.cpp | 97 +++++++++++++++++++++++++++++-------------------------------------------------------------------- src/model/GModelSpatialRadial.cpp | 122 +++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------- src/model/GModelSpatialRadialDisk.cpp | 69 +++++++++------------------------------------------------------------ src/model/GModelSpatialRadialGauss.cpp | 69 +++++++++------------------------------------------------------------ src/model/GModelSpatialRadialProfileGauss.cpp | 6 +++--- src/model/GModelSpatialRadialShell.cpp | 95 +++++++++++++---------------------------------------------------------------------------------- src/model/GModelSpectralFunc.cpp | 2 -- src/model/GModelSpectralGauss.cpp | 109 +++++++++++++++++++------------------------------------------------------------------------------------------ src/model/GModelSpectralNodes.cpp | 39 +++++++-------------------------------- src/model/GModelTemporalConst.cpp | 66 ++++++++++++++---------------------------------------------------- 19 files changed, 209 insertions(+), 820 deletions(-) diff --git a/ChangeLog b/ChangeLog index e90839c..39b6fb3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -36,6 +36,7 @@ Add time parameter to GApplicationPar class (#1864) Add time string constructor and set method to GTime class (#1864) Use GNdarray to store GSkyMap pixels and add smooth() method (#1729) + Use XML support functions in all model classes (#1430) Add GSkyRegionMap class (#1121) diff --git a/NEWS b/NEWS index 918fca4..7a018ca 100644 --- a/NEWS +++ b/NEWS @@ -143,6 +143,9 @@ None A spectral model constructor was added to GModelSpectralNodes to allow conversion of any spectral model into a spectral node model (#2178). +XML support functions are now used in all model classes to simplify the +code (#1430). + 9. Numerics module ------------------ diff --git a/doc/source/admin/release_history/1.5.rst b/doc/source/admin/release_history/1.5.rst index 6a77802..5a1b1b6 100644 --- a/doc/source/admin/release_history/1.5.rst +++ b/doc/source/admin/release_history/1.5.rst @@ -70,6 +70,8 @@ Improvements Add time string constructor and set method to ``GTime`` class * [`1729 `_] - Use ``GNdarray`` to store ``GSkyMap`` pixels and add ``GSkyMap::smooth()`` method +* [`1430 `_] - + Use XML support functions in all model classes * [`1121 `_] - Add ``GSkyRegionMap`` class diff --git a/src/model/GModelSpatialDiffuseConst.cpp b/src/model/GModelSpatialDiffuseConst.cpp index e3528e8..3b733d7 100644 --- a/src/model/GModelSpatialDiffuseConst.cpp +++ b/src/model/GModelSpatialDiffuseConst.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialDiffuseConst.cpp - Spatial isotropic model class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2016 by Juergen Knoedlseder * + * copyright (C) 2010-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -422,7 +422,7 @@ double GModelSpatialDiffuseConst::mc_norm(const GSkyDir& dir, void GModelSpatialDiffuseConst::read(const GXmlElement& xml) { // Get value parameter - const GXmlElement* par = gammalib::xml_get_par(G_READ, xml, "Value"); + const GXmlElement* par = gammalib::xml_get_par(G_READ, xml, m_value.name()); // Read value parameter m_value.read(*par); @@ -462,7 +462,7 @@ void GModelSpatialDiffuseConst::write(GXmlElement& xml) const } // Get or create Value parameter - GXmlElement* par = gammalib::xml_need_par(G_WRITE, xml, "Value"); + GXmlElement* par = gammalib::xml_need_par(G_WRITE, xml, m_value.name()); // Write parameter m_value.write(*par); diff --git a/src/model/GModelSpatialDiffuseCube.cpp b/src/model/GModelSpatialDiffuseCube.cpp index 1120f6c..d370dd6 100644 --- a/src/model/GModelSpatialDiffuseCube.cpp +++ b/src/model/GModelSpatialDiffuseCube.cpp @@ -641,7 +641,6 @@ void GModelSpatialDiffuseCube::write(GXmlElement& xml) const } // Set filename - //xml.attribute("file", m_filename); xml.attribute("file", gammalib::xml_file_reduce(xml, m_filename)); // Return diff --git a/src/model/GModelSpatialDiffuseMap.cpp b/src/model/GModelSpatialDiffuseMap.cpp index eac8923..7b56286 100644 --- a/src/model/GModelSpatialDiffuseMap.cpp +++ b/src/model/GModelSpatialDiffuseMap.cpp @@ -608,7 +608,6 @@ void GModelSpatialDiffuseMap::write(GXmlElement& xml) const } // Set sky map file name - //xml.attribute("file", m_filename); xml.attribute("file", gammalib::xml_file_reduce(xml, m_filename)); // If XML element has 0 nodes then append parameter node. The name diff --git a/src/model/GModelSpatialElliptical.cpp b/src/model/GModelSpatialElliptical.cpp index 8c460ce..6d5f1df 100644 --- a/src/model/GModelSpatialElliptical.cpp +++ b/src/model/GModelSpatialElliptical.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialElliptical.cpp - Abstract elliptical spatial model class * * ----------------------------------------------------------------------- * - * copyright (C) 2013-2017 by Juergen Knoedlseder * + * copyright (C) 2013-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -218,74 +218,48 @@ double GModelSpatialElliptical::eval(const GPhoton& photon, ***************************************************************************/ void GModelSpatialElliptical::read(const GXmlElement& xml) { - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); + // Read RA/DEC parameters + if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) { + + // Get parameters + const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA"); + const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC"); + + // Read parameters + m_ra.read(*ra); + m_dec.read(*dec); - // Verify that XML element has at least 3 parameters - if (xml.elements() < 3 || npars < 3) { - throw GException::model_invalid_parnum(G_READ, xml, - "Elliptical model requires at least 3 parameters."); } - // Extract model parameters - bool has_glon = false; - bool has_glat = false; - int npar[3] = {0, 0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle RA/GLON - if (par->attribute("name") == "RA") { - m_ra.read(*par); - npar[0]++; - } - else if (par->attribute("name") == "GLON") { - m_ra.read(*par); - npar[0]++; - has_glon = true; - } - - // Handle DEC/GLAT - else if (par->attribute("name") == "DEC") { - m_dec.read(*par); - npar[1]++; - } - else if (par->attribute("name") == "GLAT") { - m_dec.read(*par); - npar[1]++; - has_glat = true; - } - - // Handle PA - else if (par->attribute("name") == "PA") { - m_posangle.read(*par); - npar[2]++; - } - - } // endfor: looped over all parameters - - // Check if we have to convert GLON/GLAT into RA/DEC - if (has_glon && has_glat) { + // ... otherwise read GLON/GLAT parameters + else { + + // Get parameters + const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON"); + const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT"); + + // Read parameters + m_ra.read(*glon); + m_dec.read(*glat); + + // Convert into RA/DEC GSkyDir dir; dir.lb_deg(ra(), dec()), m_ra.value(dir.ra_deg()); m_dec.value(dir.dec_deg()); + + // Set names to RA/DEC m_ra.name("RA"); m_dec.name("DEC"); - } - else if (has_glon || has_glat) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require either \"RA\"/\"DEC\" or \"GLON\"/\"GLAT\"."); - } - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1 || npar[2] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"RA\"/\"DEC\", \"GLON\"/\"GLAT\" and \"PA\" parameters."); } + // Get other parameters + const GXmlElement* pa = gammalib::xml_get_par(G_READ, xml, m_posangle.name()); + + // Read other parameters + m_posangle.read(*pa); + // Return return; } @@ -329,54 +303,15 @@ void GModelSpatialElliptical::write(GXmlElement& xml) const "Elliptical model is not of type \""+type()+"\"."); } - // If XML element has 0 nodes then append 3 parameter nodes - if (xml.elements() == 0) { - xml.append(GXmlElement("parameter name=\"RA\"")); - xml.append(GXmlElement("parameter name=\"DEC\"")); - xml.append(GXmlElement("parameter name=\"PA\"")); - } - - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); + // Get or create parameters + GXmlElement* ra = gammalib::xml_need_par(G_WRITE, xml, m_ra.name()); + GXmlElement* dec = gammalib::xml_need_par(G_WRITE, xml, m_dec.name()); + GXmlElement* pa = gammalib::xml_need_par(G_WRITE, xml, m_posangle.name()); - // Verify that XML element has at least 3 parameters - if (xml.elements() < 3 || npars < 3) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Elliptical source model requires at least 3 parameters."); - } - - // Set or update model parameter attributes - int npar[3] = {0, 0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); - - // Handle RA - if (par->attribute("name") == "RA") { - npar[0]++; - m_ra.write(*par); - } - - // Handle DEC - else if (par->attribute("name") == "DEC") { - npar[1]++; - m_dec.write(*par); - } - - // Handle PA - else if (par->attribute("name") == "PA") { - npar[2]++; - m_posangle.write(*par); - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1 || npar[1] != 1 || npar[2] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require \"RA\", \"DEC\" and \"PA\" parameters."); - } + // Write parameters + m_ra.write(*ra); + m_dec.write(*dec); + m_posangle.write(*pa); // Return return; diff --git a/src/model/GModelSpatialEllipticalDisk.cpp b/src/model/GModelSpatialEllipticalDisk.cpp index d65d3a9..51e0a96 100644 --- a/src/model/GModelSpatialEllipticalDisk.cpp +++ b/src/model/GModelSpatialEllipticalDisk.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialEllipticalDisk.cpp - Elliptical disk source model class * * ----------------------------------------------------------------------- * - * copyright (C) 2013-2016 by Michael Mayer * + * copyright (C) 2013-2018 by Michael Mayer * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -471,41 +471,13 @@ void GModelSpatialEllipticalDisk::read(const GXmlElement& xml) // Read disk location GModelSpatialElliptical::read(xml); - // Extract model parameters - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle semiminor radius - if (par->attribute("name") == "MinorRadius") { - - // Read parameter - m_semiminor.read(*par); - - // Increment parameter counter - npar[0]++; - } - - // Handle semimajor radius - else if (par->attribute("name") == "MajorRadius") { + // Get parameters + const GXmlElement* minor = gammalib::xml_get_par(G_READ, xml, m_semiminor.name()); + const GXmlElement* major = gammalib::xml_get_par(G_READ, xml, m_semimajor.name()); - // Read parameter - m_semimajor.read(*par); - - // Increment parameter counter - npar[1]++; - } - - } // endfor: looped over all parameters - - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Elliptical disk model requires \"MinorRadius\" and" - " \"MajorRadius\" parameters."); - } + // Read parameters + m_semiminor.read(*minor); + m_semimajor.read(*major); // Return return; @@ -539,57 +511,13 @@ void GModelSpatialEllipticalDisk::write(GXmlElement& xml) const // Write disk location GModelSpatialElliptical::write(xml); - // If XML element has 3 nodes (which should be the location and PA nodes) - // then append 2 parameter nodes - if (xml.elements() == 3) { - xml.append(GXmlElement("parameter name=\"MinorRadius\"")); - xml.append(GXmlElement("parameter name=\"MajorRadius\"")); - } - - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); + // Get or create parameters + GXmlElement* minor = gammalib::xml_need_par(G_WRITE, xml, m_semiminor.name()); + GXmlElement* major = gammalib::xml_need_par(G_WRITE, xml, m_semimajor.name()); - // Verify that XML element has exactly 5 parameters - if (xml.elements() != 5 || npars != 5) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Elliptical Disk model requires exactly 5 parameters."); - } - - // Set or update model parameter attributes - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); - - // Handle semiminor radius - if (par->attribute("name") == "MinorRadius") { - - // Write parameter - m_semiminor.write(*par); - - // Increment parameter counter - npar[0]++; - } - - // Handle semimajor radius - else if (par->attribute("name") == "MajorRadius") { - - // Write parameter - m_semimajor.write(*par); - - // Increment parameter counter - npar[1]++; - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Elliptical disk model requires \"MinorRadius\" and" - " \"MajorRadius\" parameters."); - } + // Write parameters + m_semiminor.write(*minor); + m_semimajor.write(*major); // Return return; diff --git a/src/model/GModelSpatialEllipticalGauss.cpp b/src/model/GModelSpatialEllipticalGauss.cpp index 45a1fb8..10c70fa 100644 --- a/src/model/GModelSpatialEllipticalGauss.cpp +++ b/src/model/GModelSpatialEllipticalGauss.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialEllipticalGauss.cpp - Elliptical gauss source model class * * ----------------------------------------------------------------------- * - * copyright (C) 2015-2016 by Michael Mayer * + * copyright (C) 2015-2018 by Michael Mayer * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -541,41 +541,13 @@ void GModelSpatialEllipticalGauss::read(const GXmlElement& xml) // Read gauss location GModelSpatialElliptical::read(xml); - // Extract model parameters - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle semiminor radius - if (par->attribute("name") == "MinorRadius") { - - // Read parameter - m_semiminor.read(*par); - - // Increment parameter counter - npar[0]++; - } - - // Handle semimajor radius - else if (par->attribute("name") == "MajorRadius") { + // Get parameters + const GXmlElement* minor = gammalib::xml_get_par(G_READ, xml, m_semiminor.name()); + const GXmlElement* major = gammalib::xml_get_par(G_READ, xml, m_semimajor.name()); - // Read parameter - m_semimajor.read(*par); - - // Increment parameter counter - npar[1]++; - } - - } // endfor: looped over all parameters - - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Elliptical disk model requires \"MinorRadius\" and" - " \"MajorRadius\" parameters."); - } + // Read parameters + m_semiminor.read(*minor); + m_semimajor.read(*major); // Return return; @@ -609,57 +581,13 @@ void GModelSpatialEllipticalGauss::write(GXmlElement& xml) const // Write disk location GModelSpatialElliptical::write(xml); - // If XML element has 3 nodes (which should be the location and PA nodes) - // then append 2 parameter nodes - if (xml.elements() == 3) { - xml.append(GXmlElement("parameter name=\"MinorRadius\"")); - xml.append(GXmlElement("parameter name=\"MajorRadius\"")); - } - - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); - - // Verify that XML element has exactly 5 parameters - if (xml.elements() != 5 || npars != 5) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Elliptical gauss model requires exactly 5 parameters."); - } - - // Set or update model parameter attributes - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); - - // Handle semiminor radius - if (par->attribute("name") == "MinorRadius") { - - // Write parameter - m_semiminor.write(*par); - - // Increment parameter counter - npar[0]++; - } - - // Handle semimajor radius - else if (par->attribute("name") == "MajorRadius") { + // Get or create parameters + GXmlElement* minor = gammalib::xml_need_par(G_WRITE, xml, m_semiminor.name()); + GXmlElement* major = gammalib::xml_need_par(G_WRITE, xml, m_semimajor.name()); - // Write parameter - m_semimajor.write(*par); - - // Increment parameter counter - npar[1]++; - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Elliptical gauss model requires \"MinorRadius\" and" - " \"MajorRadius\" parameters."); - } + // Write parameters + m_semiminor.write(*minor); + m_semimajor.write(*major); // Return return; @@ -669,7 +597,7 @@ void GModelSpatialEllipticalGauss::write(GXmlElement& xml) const /***********************************************************************//** * @brief Print information * - * @param[in] chatter Chattiness (defaults to NORMAL). + * @param[in] chatter Chattiness. * @return String containing model information. ***************************************************************************/ std::string GModelSpatialEllipticalGauss::print(const GChatter& chatter) const diff --git a/src/model/GModelSpatialPointSource.cpp b/src/model/GModelSpatialPointSource.cpp index d0bdacd..fde91c1 100644 --- a/src/model/GModelSpatialPointSource.cpp +++ b/src/model/GModelSpatialPointSource.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialPointSource.cpp - Spatial point source model class * * ----------------------------------------------------------------------- * - * copyright (C) 2009-2017 by Juergen Knoedlseder * + * copyright (C) 2009-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -371,57 +371,40 @@ bool GModelSpatialPointSource::contains(const GSkyDir& dir, ***************************************************************************/ void GModelSpatialPointSource::read(const GXmlElement& xml) { - // Verify that XML element has exactly 2 parameters - if (xml.elements() != 2 || xml.elements("parameter") != 2) { - throw GException::model_invalid_parnum(G_READ, xml, - "Point source model requires exactly 2 parameters."); - } + // Read RA/DEC parameters + if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) { - // Extract model parameters - bool has_glon = false; - bool has_glat = false; - int npar[] = {0, 0}; - for (int i = 0; i < 2; ++i) { + // Get parameters + const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA"); + const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC"); - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); + // Read parameters + m_ra.read(*ra); + m_dec.read(*dec); - // Handle RA/GLON - if (par->attribute("name") == "RA") { - m_ra.read(*par); - npar[0]++; - } - else if (par->attribute("name") == "GLON") { - m_ra.read(*par); - npar[0]++; - has_glon = true; - } + } - // Handle DEC/GLAT - else if (par->attribute("name") == "DEC") { - m_dec.read(*par); - npar[1]++; - } - else if (par->attribute("name") == "GLAT") { - m_dec.read(*par); - npar[1]++; - has_glat = true; - } + // ... otherwise read GLON/GLAT parameters + else { + + // Get parameters + const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON"); + const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT"); - } // endfor: looped over all parameters + // Read parameters + m_ra.read(*glon); + m_dec.read(*glat); - // Check if we have to convert GLON/GLAT into RA/DEC - if (has_glon && has_glat) { + // Convert into RA/DEC GSkyDir dir; dir.lb_deg(ra(), dec()), m_ra.value(dir.ra_deg()); m_dec.value(dir.dec_deg()); + + // Set names to RA/DEC m_ra.name("RA"); m_dec.name("DEC"); - } - else if (has_glon || has_glat) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require either RA/DEC or GLON/GLAT."); + } // Return @@ -465,35 +448,13 @@ void GModelSpatialPointSource::write(GXmlElement& xml) const "Spatial model is not of type \""+type()+"\"."); } - // If XML element has 0 nodes then append 2 parameter nodes - if (xml.elements() == 0) { - xml.append(GXmlElement("parameter name=\"RA\"")); - xml.append(GXmlElement("parameter name=\"DEC\"")); - } - - // Verify that XML element has exactly 2 parameters - if (xml.elements() != 2 || xml.elements("parameter") != 2) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Point source model requires exactly 2 parameters."); - } - - // Get pointers on both model parameters - GXmlElement* par1 = xml.element("parameter", 0); - GXmlElement* par2 = xml.element("parameter", 1); + // Get or create parameters + GXmlElement* ra = gammalib::xml_need_par(G_WRITE, xml, m_ra.name()); + GXmlElement* dec = gammalib::xml_need_par(G_WRITE, xml, m_dec.name()); - // Set or update sky direction - if (par1->attribute("name") == "RA" && par2->attribute("name") == "DEC") { - m_ra.write(*par1); - m_dec.write(*par2); - } - else if (par2->attribute("name") == "RA" && par1->attribute("name") == "DEC") { - m_ra.write(*par2); - m_dec.write(*par1); - } - else { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require RA and DEC parameters."); - } + // Write parameters + m_ra.write(*ra); + m_dec.write(*dec); // Return return; diff --git a/src/model/GModelSpatialRadial.cpp b/src/model/GModelSpatialRadial.cpp index 3829dd1..18bb7e3 100644 --- a/src/model/GModelSpatialRadial.cpp +++ b/src/model/GModelSpatialRadial.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRadial.cpp - Abstract radial spatial model base class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2017 by Juergen Knoedlseder * + * copyright (C) 2011-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -215,66 +215,40 @@ double GModelSpatialRadial::eval(const GPhoton& photon, ***************************************************************************/ void GModelSpatialRadial::read(const GXmlElement& xml) { - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); + // Read RA/DEC parameters + if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) { + + // Get parameters + const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA"); + const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC"); + + // Read parameters + m_ra.read(*ra); + m_dec.read(*dec); - // Verify that XML element has at least 2 parameters - if (xml.elements() < 2 || npars < 2) { - throw GException::model_invalid_parnum(G_READ, xml, - "Radial model requires at least 2 parameters."); } - // Extract model parameters - bool has_glon = false; - bool has_glat = false; - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle RA/GLON - if (par->attribute("name") == "RA") { - m_ra.read(*par); - npar[0]++; - } - else if (par->attribute("name") == "GLON") { - m_ra.read(*par); - npar[0]++; - has_glon = true; - } - - // Handle DEC/GLAT - else if (par->attribute("name") == "DEC") { - m_dec.read(*par); - npar[1]++; - } - else if (par->attribute("name") == "GLAT") { - m_dec.read(*par); - npar[1]++; - has_glat = true; - } - - } // endfor: looped over all parameters - - // Check if we have to convert GLON/GLAT into RA/DEC - if (has_glon && has_glat) { + // ... otherwise read GLON/GLAT parameters + else { + + // Get parameters + const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON"); + const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT"); + + // Read parameters + m_ra.read(*glon); + m_dec.read(*glat); + + // Convert into RA/DEC GSkyDir dir; dir.lb_deg(ra(), dec()), m_ra.value(dir.ra_deg()); m_dec.value(dir.dec_deg()); + + // Set names to RA/DEC m_ra.name("RA"); m_dec.name("DEC"); - } - else if (has_glon || has_glat) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require either \"RA\"/\"DEC\" or \"GLON\"/\"GLAT\"."); - } - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"RA\"/\"DEC\" and \"GLON\"/\"GLAT\" parameters."); } // Return @@ -319,47 +293,13 @@ void GModelSpatialRadial::write(GXmlElement& xml) const "Radial model is not of type \""+type()+"\"."); } - // If XML element has 0 nodes then append 2 parameter nodes - if (xml.elements() == 0) { - xml.append(GXmlElement("parameter name=\"RA\"")); - xml.append(GXmlElement("parameter name=\"DEC\"")); - } - - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); - - // Verify that XML element has at least 2 parameters - if (xml.elements() < 2 || npars < 2) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Radial source model requires at least 2 parameters."); - } - - // Set or update model parameter attributes - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); + // Get or create parameters + GXmlElement* ra = gammalib::xml_need_par(G_WRITE, xml, m_ra.name()); + GXmlElement* dec = gammalib::xml_need_par(G_WRITE, xml, m_dec.name()); - // Handle RA - if (par->attribute("name") == "RA") { - npar[0]++; - m_ra.write(*par); - } - - // Handle DEC - else if (par->attribute("name") == "DEC") { - npar[1]++; - m_dec.write(*par); - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require \"RA\" and \"DEC\" parameters."); - } + // Write parameters + m_ra.write(*ra); + m_dec.write(*dec); // Return return; diff --git a/src/model/GModelSpatialRadialDisk.cpp b/src/model/GModelSpatialRadialDisk.cpp index 3553bfa..9409142 100644 --- a/src/model/GModelSpatialRadialDisk.cpp +++ b/src/model/GModelSpatialRadialDisk.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRadialDisk.cpp - Radial disk source model class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2016 by Christoph Deil * + * copyright (C) 2011-2018 by Christoph Deil * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -421,32 +421,11 @@ void GModelSpatialRadialDisk::read(const GXmlElement& xml) // Read disk location GModelSpatialRadial::read(xml); - // Extract model parameters - int npar[1] = {0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle Radius - if (par->attribute("name") == "Radius") { - - // Read parameter - m_radius.read(*par); - - //TODO: Check parameter - - // Increment parameter counter - npar[0]++; - } - - } // endfor: looped over all parameters + // Get parameters + const GXmlElement* radius = gammalib::xml_get_par(G_READ, xml, m_radius.name()); - // Verify that all parameters were found - if (npar[0] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"Radius\" parameter."); - } + // Read parameters + m_radius.read(*radius); // Return return; @@ -478,41 +457,11 @@ void GModelSpatialRadialDisk::write(GXmlElement& xml) const // Write disk location GModelSpatialRadial::write(xml); - // If XML element has 2 nodes (which should be the location nodes) - // then append 1 parameter node - if (xml.elements() == 2) { - xml.append(GXmlElement("parameter name=\"Radius\"")); - } - - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); - - // Verify that XML element has exactly 3 parameters - if (xml.elements() != 3 || npars != 3) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Disk model requires exactly 3 parameters."); - } - - // Set or update model parameter attributes - int npar[1] = {0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); + // Get or create parameters + GXmlElement* radius = gammalib::xml_need_par(G_WRITE, xml, m_radius.name()); - // Handle Radius - if (par->attribute("name") == "Radius") { - m_radius.write(*par); - npar[0]++; - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require \"Radius\" parameter."); - } + // Write parameters + m_radius.write(*radius); // Return return; diff --git a/src/model/GModelSpatialRadialGauss.cpp b/src/model/GModelSpatialRadialGauss.cpp index 685cd93..946d4bc 100644 --- a/src/model/GModelSpatialRadialGauss.cpp +++ b/src/model/GModelSpatialRadialGauss.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRadialGauss.cpp - Radial Gaussian source model class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2016 by Juergen Knoedlseder * + * copyright (C) 2011-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -417,32 +417,11 @@ void GModelSpatialRadialGauss::read(const GXmlElement& xml) // Read Gaussian location GModelSpatialRadial::read(xml); - // Extract model parameters - int npar[1] = {0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle Gaussian width - if (par->attribute("name") == "Sigma") { - - // Read parameter - m_sigma.read(*par); - - //TODO: Check parameter - - // Increment parameter counter - npar[0]++; - } - - } // endfor: looped over all parameters + // Get parameters + const GXmlElement* sigma = gammalib::xml_get_par(G_READ, xml, m_sigma.name()); - // Verify that all parameters were found - if (npar[0] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"Sigma\" parameters."); - } + // Read parameters + m_sigma.read(*sigma); // Return return; @@ -476,41 +455,11 @@ void GModelSpatialRadialGauss::write(GXmlElement& xml) const // Write Gaussian location GModelSpatialRadial::write(xml); - // If XML element has 2 nodes (which should be the location nodes) - // then append 1 parameter node - if (xml.elements() == 2) { - xml.append(GXmlElement("parameter name=\"Sigma\"")); - } + // Get or create parameters + GXmlElement* sigma = gammalib::xml_need_par(G_WRITE, xml, m_sigma.name()); - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); - - // Verify that XML element has exactly 3 parameters - if (xml.elements() != 3 || npars != 3) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Point source model requires exactly 3 parameters."); - } - - // Set or update model parameter attributes - int npar[1] = {0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); - - // Handle Sigma - if (par->attribute("name") == "Sigma") { - m_sigma.write(*par); - npar[0]++; - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require \"Sigma\" parameter."); - } + // Write parameters + m_sigma.write(*sigma); // Return return; diff --git a/src/model/GModelSpatialRadialProfileGauss.cpp b/src/model/GModelSpatialRadialProfileGauss.cpp index 4438345..06883cc 100644 --- a/src/model/GModelSpatialRadialProfileGauss.cpp +++ b/src/model/GModelSpatialRadialProfileGauss.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRadialProfileGauss.cpp - Gaussian radial profile class * * ----------------------------------------------------------------------- * - * copyright (C) 2016 by Juergen Knoedlseder * + * copyright (C) 2016-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -265,7 +265,7 @@ void GModelSpatialRadialProfileGauss::read(const GXmlElement& xml) GModelSpatialRadial::read(xml); // Read Sigma parameter - const GXmlElement* par = gammalib::xml_get_par(G_READ, xml, "Sigma"); + const GXmlElement* par = gammalib::xml_get_par(G_READ, xml, m_sigma.name()); m_sigma.read(*par); // Return @@ -293,7 +293,7 @@ void GModelSpatialRadialProfileGauss::write(GXmlElement& xml) const GModelSpatialRadial::write(xml); // Write Sigma parameter - GXmlElement* par = gammalib::xml_need_par(G_WRITE, xml, "Sigma"); + GXmlElement* par = gammalib::xml_need_par(G_WRITE, xml, m_sigma.name()); m_sigma.write(*par); // Return diff --git a/src/model/GModelSpatialRadialShell.cpp b/src/model/GModelSpatialRadialShell.cpp index 538243f..25a65ed 100644 --- a/src/model/GModelSpatialRadialShell.cpp +++ b/src/model/GModelSpatialRadialShell.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRadialShell.cpp - Radial shell source model class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2017 by Christoph Deil * + * copyright (C) 2011-2018 by Christoph Deil * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -477,47 +477,13 @@ void GModelSpatialRadialShell::read(const GXmlElement& xml) // Read shell location GModelSpatialRadial::read(xml); - // Extract remaining model parameters. Note that we have to loop over - // all parameters to find the remaining 2 parameters that are - // implemented by this class (the location is implemented by the base - // class) - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle Radius - if (par->attribute("name") == "Radius") { - - // Read parameter - m_radius.read(*par); - - //TODO: Check parameter - - // Increment parameter counter - npar[0]++; - } - - // Handle Width - else if (par->attribute("name") == "Width") { - - // Read parameter - m_width.read(*par); - - //TODO: Check parameter - - // Increment parameter counter - npar[1]++; - } + // Get parameters + const GXmlElement* radius = gammalib::xml_get_par(G_READ, xml, m_radius.name()); + const GXmlElement* width = gammalib::xml_get_par(G_READ, xml, m_width.name()); - } // endfor: looped over all parameters - - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"Radius\" and \"Width\" parameters."); - } + // Read parameters + m_radius.read(*radius); + m_width.read(*width); // Return return; @@ -552,48 +518,13 @@ void GModelSpatialRadialShell::write(GXmlElement& xml) const // Write shell location GModelSpatialRadial::write(xml); - // If XML element has 2 nodes (which should be the location nodes) - // then append 2 parameter nodes - if (xml.elements() == 2) { - xml.append(GXmlElement("parameter name=\"Radius\"")); - xml.append(GXmlElement("parameter name=\"Width\"")); - } + // Get or create parameters + GXmlElement* radius = gammalib::xml_need_par(G_WRITE, xml, m_radius.name()); + GXmlElement* width = gammalib::xml_need_par(G_WRITE, xml, m_width.name()); - // Determine number of parameter nodes in XML element - int npars = xml.elements("parameter"); - - // Verify that XML element has exactly 4 parameters - if (xml.elements() != 4 || npars != 4) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Shell source model requires exactly 4 parameters."); - } - - // Set or update model parameter attributes - int npar[2] = {0, 0}; - for (int i = 0; i < npars; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); - - // Handle Radius - if (par->attribute("name") == "Radius") { - m_radius.write(*par); - npar[0]++; - } - - // Handle Width - else if (par->attribute("name") == "Width") { - m_width.write(*par); - npar[1]++; - } - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require \"Radius\" and \"Width\" parameters."); - } + // Write parameters + m_radius.write(*radius); + m_width.write(*width); // Return return; diff --git a/src/model/GModelSpectralFunc.cpp b/src/model/GModelSpectralFunc.cpp index 93ab416..c5f0ca1 100644 --- a/src/model/GModelSpectralFunc.cpp +++ b/src/model/GModelSpectralFunc.cpp @@ -579,7 +579,6 @@ void GModelSpectralFunc::read(const GXmlElement& xml) } // Load nodes from file - //load_nodes(xml.attribute("file")); load_nodes(gammalib::xml_file_expand(xml, xml.attribute("file"))); // Return @@ -646,7 +645,6 @@ void GModelSpectralFunc::write(GXmlElement& xml) const } // Set file attribute - //xml.attribute("file", m_filename.url()); xml.attribute("file", gammalib::xml_file_reduce(xml, m_filename)); // Return diff --git a/src/model/GModelSpectralGauss.cpp b/src/model/GModelSpectralGauss.cpp index fbb4fc3..f8bfb2f 100644 --- a/src/model/GModelSpectralGauss.cpp +++ b/src/model/GModelSpectralGauss.cpp @@ -503,48 +503,15 @@ GEnergy GModelSpectralGauss::mc(const GEnergy& emin, ***************************************************************************/ void GModelSpectralGauss::read(const GXmlElement& xml) { - // Set number of parameters - const int n_pars = 3; + // Get parameters + const GXmlElement* norm = gammalib::xml_get_par(G_READ, xml, m_norm.name()); + const GXmlElement* mean = gammalib::xml_get_par(G_READ, xml, m_mean.name()); + const GXmlElement* sigma = gammalib::xml_get_par(G_READ, xml, m_sigma.name()); - // Verify that XML element has exactly 3 parameters - if (xml.elements() != n_pars || xml.elements("parameter") != n_pars) { - throw GException::model_invalid_parnum(G_READ, xml, - "Gaussian model requires exactly 3 parameters."); - } - - // Extract model parameters - int npar[] = {0, 0, 0}; - for (int i = 0; i < n_pars; ++i) { - - // Get parameter element - const GXmlElement* par = xml.element("parameter", i); - - // Handle normalization - if (par->attribute("name") == "Normalization") { - m_norm.read(*par); - npar[0]++; - } - - // Handle mean - else if (par->attribute("name") == "Mean") { - m_mean.read(*par); - npar[1]++; - } - - // Handle sigma - else if (par->attribute("name") == "Sigma") { - m_sigma.read(*par); - npar[2]++; - } - - } // endfor: looped over all parameters - - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1 || npar[2] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"Normalization\", \"Mean\", and \"Sigma\"" - " parameters."); - } + // Read parameters + m_norm.read(*norm); + m_mean.read(*mean); + m_sigma.read(*sigma); // Return return; @@ -576,62 +543,24 @@ void GModelSpectralGauss::write(GXmlElement& xml) const { // Set model type if (xml.attribute("type") == "") { - xml.attribute("type", "Gaussian"); + xml.attribute("type", type()); } // Verify model type - if (xml.attribute("type") != "Gaussian") { + if (xml.attribute("type") != type()) { throw GException::model_invalid_spectral(G_WRITE, xml.attribute("type"), - "Spectral model is not of type \"Gaussian\"."); - } - - // If XML element has 0 nodes then append 3 parameter nodes - if (xml.elements() == 0) { - xml.append(GXmlElement("parameter name=\"Normalization\"")); - xml.append(GXmlElement("parameter name=\"Mean\"")); - xml.append(GXmlElement("parameter name=\"Sigma\"")); - } - - // Verify that XML element has exactly 3 parameters - if (xml.elements() != 3 || xml.elements("parameter") != 3) { - throw GException::model_invalid_parnum(G_WRITE, xml, - "Power law model requires exactly 3 parameters."); + "Spectral model is not of type \""+type()+"\"."); } - // Set or update model parameter attributes - int npar[] = {0, 0, 0}; - for (int i = 0; i < 3; ++i) { - - // Get parameter element - GXmlElement* par = xml.element("parameter", i); + // Get XML parameters + GXmlElement* norm = gammalib::xml_need_par(G_WRITE, xml, m_norm.name()); + GXmlElement* mean = gammalib::xml_need_par(G_WRITE, xml, m_mean.name()); + GXmlElement* sigma = gammalib::xml_need_par(G_WRITE, xml, m_sigma.name()); - // Handle normalization - if (par->attribute("name") == "Normalization") { - npar[0]++; - m_norm.write(*par); - } - - // Handle mean - else if (par->attribute("name") == "Mean") { - npar[1]++; - m_mean.write(*par); - } - - // Handle sigma - else if (par->attribute("name") == "Sigma") { - npar[2]++; - m_sigma.write(*par); - } - - - } // endfor: looped over all parameters - - // Check of all required parameters are present - if (npar[0] != 1 || npar[1] != 1 || npar[2] != 1 ) { - throw GException::model_invalid_parnames(G_WRITE, xml, - "Require \"Normalization\", \"Mean\" and \"Sigma\"" - " parameters."); - } + // Write parameters + m_norm.write(*norm); + m_mean.write(*mean); + m_sigma.write(*sigma); // Return return; diff --git a/src/model/GModelSpectralNodes.cpp b/src/model/GModelSpectralNodes.cpp index 99f0d76..fbdabc0 100644 --- a/src/model/GModelSpectralNodes.cpp +++ b/src/model/GModelSpectralNodes.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpectralNodes.cpp - Spectral nodes model class * * ----------------------------------------------------------------------- * - * copyright (C) 2012-2017 by Juergen Knoedlseder * + * copyright (C) 2012-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -645,38 +645,13 @@ void GModelSpectralNodes::read(const GXmlElement& xml) // Get node const GXmlElement* node = xml.element("node", i); - // Verify that node XML element has exactly 2 parameters - if (node->elements() != 2 || node->elements("parameter") != 2) { - throw GException::model_invalid_parnum(G_READ, xml, - "Node requires exactly 2 parameters."); - } - - // Extract node parameters - int npar[] = {0, 0}; - for (int k = 0; k < 2; ++k) { - - // Get parameter element - const GXmlElement* par = node->element("parameter", k); - - // Handle energy - if (par->attribute("name") == "Energy") { - energy.read(*par); - npar[0]++; - } + // Get parameters + const GXmlElement* epar = gammalib::xml_get_par(G_READ, *node, "Energy"); + const GXmlElement* ipar = gammalib::xml_get_par(G_READ, *node, "Intensity"); - // Handle intensity - else if (par->attribute("name") == "Intensity") { - intensity.read(*par); - npar[1]++; - } - - } // endfor: looped over parameters - - // Verify that all parameters were found - if (npar[0] != 1 || npar[1] != 1) { - throw GException::model_invalid_parnames(G_READ, xml, - "Require \"Energy\" and \"Intensity\" parameters."); - } + // Read parameters + energy.read(*epar); + intensity.read(*ipar); // Throw an exception if either energy or intensity is not positive if (energy.value() <= 0.0) { diff --git a/src/model/GModelTemporalConst.cpp b/src/model/GModelTemporalConst.cpp index a32a437..2974ef9 100644 --- a/src/model/GModelTemporalConst.cpp +++ b/src/model/GModelTemporalConst.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelTemporalConst.cpp - Temporal constant model class * * ----------------------------------------------------------------------- * - * copyright (C) 2009-2016 by Juergen Knoedlseder * + * copyright (C) 2009-2018 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -326,27 +326,11 @@ GTimes GModelTemporalConst::mc(const double& rate, const GTime& tmin, ***************************************************************************/ void GModelTemporalConst::read(const GXmlElement& xml) { - // Verify that XML element has exactly 1 parameter - if (xml.elements() != 1 || xml.elements("parameter") != 1) { - std::string msg = "Constant temporal model requires exactly 1 " - " tag but "+ - gammalib::str(xml.elements("parameter"))+ - " tags were found."; - throw GException::invalid_value(G_READ, msg); - } + // Get parameter pointers + const GXmlElement* norm = gammalib::xml_get_par(G_READ, xml, m_norm.name()); - // Get parameter element - const GXmlElement* par = xml.element("parameter", 0); - - // Get value - if (par->attribute("name") == "Normalization") { - m_norm.read(*par); - } - else { - std::string msg = "Constant temporal model parameter " - "\"Normalization\" not found in XML element."; - throw GException::invalid_value(G_READ, msg); - } + // Read parameters + m_norm.read(*norm); // Return return; @@ -371,42 +355,20 @@ void GModelTemporalConst::write(GXmlElement& xml) const { // Set model type if (xml.attribute("type") == "") { - xml.attribute("type", "Constant"); + xml.attribute("type", type()); } // Verify model type - if (xml.attribute("type") != "Constant") { - std::string msg = "Temporal model of type "+xml.attribute("type")+ - " encountered while \"Constant\" was expected."; - throw GException::invalid_value(G_WRITE, msg); - } - - // If XML element has 0 nodes then append parameter node. - if (xml.elements() == 0) { - xml.append(GXmlElement("parameter name=\"Normalization\"")); - } - - // Verify that XML element has exactly 1 parameter - if (xml.elements() != 1 || xml.elements("parameter") != 1) { - std::string msg = "Constant temporal model requires exactly 1 " - " tag but "+ - gammalib::str(xml.elements("parameter"))+ - " tags were found."; - throw GException::invalid_value(G_WRITE, msg); + if (xml.attribute("type") != type()) { + throw GException::model_invalid_spectral(G_WRITE, xml.attribute("type"), + "Temporal model is not of type \""+type()+"\"."); } - // Get parameter element - GXmlElement* par = xml.element("parameter", 0); + // Get XML parameters + GXmlElement* norm = gammalib::xml_need_par(G_WRITE, xml, m_norm.name()); - // Set parameyter - if (par->attribute("name") == "Normalization") { - m_norm.write(*par); - } - else { - std::string msg = "Constant temporal model parameter " - "\"Normalization\" not found in XML element."; - throw GException::invalid_value(G_WRITE, msg); - } + // Write parameters + m_norm.write(*norm); // Return return; @@ -416,7 +378,7 @@ void GModelTemporalConst::write(GXmlElement& xml) const /***********************************************************************//** * @brief Print constant information * - * @param[in] chatter Chattiness (defaults to NORMAL). + * @param[in] chatter Chattiness. * @return String containing model information. ***************************************************************************/ std::string GModelTemporalConst::print(const GChatter& chatter) const -- libgit2 0.21.4