From 223e4b2e00a84b394ba3fcde217e300c2efbab48 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 27 Jun 2013 12:01:12 +0200 Subject: [PATCH] added read and write methods for GSkyRegionCircle and added some exceptions --- include/GSkyRegion.hpp | 2 +- include/GSkyRegionCircle.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++-- include/GTools.hpp | 1 + src/sky/GSkyRegionCircle.cpp | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------ src/support/GTools.cpp | 22 ++++++++++++++++++++++ 5 files changed, 182 insertions(+), 57 deletions(-) diff --git a/include/GSkyRegion.hpp b/include/GSkyRegion.hpp index 9c2cba8..c1b5894 100644 --- a/include/GSkyRegion.hpp +++ b/include/GSkyRegion.hpp @@ -67,7 +67,7 @@ public: // Pure virtual methods virtual void clear(void) = 0; virtual GSkyRegion* clone(void) const = 0; - virtual void read(const std::string regstring) const = 0; + virtual void read(const std::string& regstring) = 0; virtual std::string write() const = 0; virtual std::string print(const GChatter& chatter = NORMAL) const = 0; virtual bool contains(const GSkyDir& dir) const = 0; diff --git a/include/GSkyRegionCircle.hpp b/include/GSkyRegionCircle.hpp index 3147cc0..3b6f8d1 100644 --- a/include/GSkyRegionCircle.hpp +++ b/include/GSkyRegionCircle.hpp @@ -36,7 +36,7 @@ /***********************************************************************//** -* @class GSkyRegionCIRCLE +* @class GSkyRegionCircle * * @brief Abstract interface for the circular sky region class * @@ -76,7 +76,7 @@ public: void centre(const GSkyDir& centre); void centre(const double& ra,const double& dec); std::string print(const GChatter& chatter = NORMAL) const; - void read(const std::string line) const; + void read(const std::string& line); std::string write() const; bool contains(const GSkyDir& dir) const; bool contains(const GSkyRegion& reg) const; @@ -110,8 +110,51 @@ double GSkyRegionCircle::radius(void) const return (m_radius); } +/***********************************************************************//** + * @brief Return region centre + * + * @return region centre + * + * Returns the region centre. + ***************************************************************************/ + inline GSkyDir GSkyRegionCircle::centre(void) const { return (m_centre); } + + + +/***********************************************************************//** + * @brief Set centre of region + * + * @param[in] dir Center of region. + ***************************************************************************/ +void GSkyRegionCircle::centre(const GSkyDir& dir) +{ + // Set centre + m_centre = dir; + + //Return + return; +} + + +/***********************************************************************//** + * @brief Set centre values + * + * @param[in] ra Right ascension value. + * @param[in] dec declination value. + ***************************************************************************/ +void GSkyRegionCircle::centre(const double& ra, const double& dec) +{ + // Set centre values + m_centre.radec_deg(ra,dec); + + //Return + return; +} + + + diff --git a/include/GTools.hpp b/include/GTools.hpp index 1e05e71..48026cf 100644 --- a/include/GTools.hpp +++ b/include/GTools.hpp @@ -87,6 +87,7 @@ namespace gammalib { bool file_exists(const std::string& filename); bool isinfinite(const double& x); bool isnotanumber(const double& x); + bool contains(std::string str1, std::string str2); } /***********************************************************************//** diff --git a/src/sky/GSkyRegionCircle.cpp b/src/sky/GSkyRegionCircle.cpp index 1ea841d..beffda2 100644 --- a/src/sky/GSkyRegionCircle.cpp +++ b/src/sky/GSkyRegionCircle.cpp @@ -33,6 +33,12 @@ /* __ Method name definitions ____________________________________________ */ +#define G_CONSTRUCTOR "GSkyRegionCircle::GSkyRegionCircle(GSkyDir&, double&)" +#define G_RADIUS "GSkyRegionCircle::radius(double&)" +#define G_READ "GSkyRegionCircle::read(std::string&)" +#define G_CONTAINS "GSkyRegionCircle::contains(GSkyRegion&)" +#define G_OVERLAPS "GSkyRegionCircle::overlaps(GSkyRegion&)" + /* __ Macros _____________________________________________________________ */ /* __ Coding definitions _________________________________________________ */ @@ -76,6 +82,11 @@ GSkyRegionCircle::GSkyRegionCircle(GSkyDir ¢re, const double &radius) m_centre = centre; m_radius = radius; + // Check if radius is valid + if (m_radius < 0.0) { + throw GException::invalid_value(G_CONSTRUCTOR, "Radius of a region can't be less than 0"); + } + // compute solid angle compute_solid(); @@ -84,31 +95,10 @@ GSkyRegionCircle::GSkyRegionCircle(GSkyDir ¢re, const double &radius) } /***********************************************************************//** - * @brief Direction constructor + * @brief string constructor * - * @param[in] ra Right ascension of Circle center [deg]. - * @param[in] dec declination of Circle center [deg]. - * @param[in] radius Radius of the region [deg]. + * @param[in] line a string containing a line of a ds9 region file ***************************************************************************/ -//GSkyRegionCircle::GSkyRegionCircle(const double& ra, const double& dec, const double& radius) -//{ -// // Initialise members -// init_members(); -// -// // Set parameters -// GSkyDir dir = GSkyDir(); -// dir.radec(ra,dec); -// m_centre = dir; -// m_radius = radius; -// -// // compute solid angle -// compute_solid(); -// -// // Return -// return; -//} - - GSkyRegionCircle::GSkyRegionCircle(const std::string &line) { // Initialise members @@ -228,44 +218,117 @@ void GSkyRegionCircle::radius(const double& radius) // Set radius value m_radius = radius; + if (m_radius < 0.0) { + throw GException::invalid_value(G_RADIUS, "Radius of a region can't be less than 0"); + } + // Recompute the solid angle compute_solid(); } /***********************************************************************//** - * @brief Set centre of region + * @brief read region from string * - * @param[in] dir Center of region. + * @param[in] line a string ds9 format ***************************************************************************/ -void GSkyRegionCircle::centre(const GSkyDir& dir) +void GSkyRegionCircle::read(const std::string& line) { - // Set y value - m_centre = dir; + // Clear the current instance + clear(); + + // split the string into 2 parts seperated by # + std::vector substrings = gammalib::split(line,"#"); + + std::string region_def = substrings[0]; + std::string comment = substrings[1]; + + // Finding the circle + if (region_def.find("circle") == std::string::npos) { + throw GException::invalid_value(G_READ, "Cannnot find the key word \"circle\" in provided string"); + } + + // get the the coordinate system of the values + std::string system = gammalib::split(region_def,";")[0]; + + // get the substring of the important values + unsigned pos = region_def.find("circle("); + unsigned end = region_def.find(")"); + std::string circlestring = region_def.substr(pos+7,end); + circlestring.erase(circlestring.find(")"),1); + + // get the values of the region x,y,and radius + std::vector values = gammalib::split(circlestring,","); + if (values.size()!=3) { + throw GException::invalid_argument(G_READ, "Invalid number of arguments, circle takes exactly 3 arguments"); + } + double x = gammalib::todouble(values[0]); + double y = gammalib::todouble(values[1]); + double radius = gammalib::todouble(values[2]); + + // Initialise sky dir + GSkyDir dir = GSkyDir(); + + // Set the values in the correct system + if(system == "fk5") { + dir.radec_deg(x,y); + } + else if (system == "galactic") { + dir.lb_deg(x,y); + } + else { + std::stringstream s; + s<<"provided coordinate system "<comments =gammalib::split(comment, " "); + for (int i = 0; i < comments.size(); i++) { + if (gammalib::contains(comments[i],"text")){ + std::vector attributes = gammalib::split(comments[i],"="); + if (attributes.size()<2) { + throw GException::invalid_argument(G_READ, "Invalid number of arguments, type of attribute must be key=value"); + } + m_name = attributes[1]; + } + } + + // Return + return; } /***********************************************************************//** - * @brief Set centre values + * @brief write region into a string * - * @param[in] ra Right ascension value. - * @param[in] dec declination value. + * @return string to be written in a ds9 region file ***************************************************************************/ -void GSkyRegionCircle::centre(const double& ra, const double& dec) -{ - // Set centre values - m_centre.radec_deg(ra,dec); -} - - -void GSkyRegionCircle::read(const std::string line) const -{ - std::cout<<"Reading of GSkyRegionCircle::read not implemented yet"<centre()); // Check if the region is contained in this - if ((ang_dist + regcirc->radius()) <= m_radius) - { + if ((ang_dist + regcirc->radius()) <= m_radius) { // Set return value to true fully_inside = true; } } - else - { - std::cout<<"ERROR: GSkyRegionCircle::contains(const GSkyRegion& reg) not implemented yet"<