Commit 223e4b2e00a84b394ba3fcde217e300c2efbab48
Committed by
Jürgen Knödlseder
1 parent
74fcf2fd
added read and write methods for GSkyRegionCircle and added some exceptions
Showing
5 changed files
with
182 additions
and
57 deletions
include/GSkyRegion.hpp
| ... | ... | @@ -67,7 +67,7 @@ public: |
| 67 | 67 | // Pure virtual methods |
| 68 | 68 | virtual void clear(void) = 0; |
| 69 | 69 | virtual GSkyRegion* clone(void) const = 0; |
| 70 | - virtual void read(const std::string regstring) const = 0; | |
| 70 | + virtual void read(const std::string& regstring) = 0; | |
| 71 | 71 | virtual std::string write() const = 0; |
| 72 | 72 | virtual std::string print(const GChatter& chatter = NORMAL) const = 0; |
| 73 | 73 | virtual bool contains(const GSkyDir& dir) const = 0; | ... | ... |
include/GSkyRegionCircle.hpp
| ... | ... | @@ -36,7 +36,7 @@ |
| 36 | 36 | |
| 37 | 37 | |
| 38 | 38 | /***********************************************************************//** |
| 39 | -* @class GSkyRegionCIRCLE | |
| 39 | +* @class GSkyRegionCircle | |
| 40 | 40 | * |
| 41 | 41 | * @brief Abstract interface for the circular sky region class |
| 42 | 42 | * |
| ... | ... | @@ -76,7 +76,7 @@ public: |
| 76 | 76 | void centre(const GSkyDir& centre); |
| 77 | 77 | void centre(const double& ra,const double& dec); |
| 78 | 78 | std::string print(const GChatter& chatter = NORMAL) const; |
| 79 | - void read(const std::string line) const; | |
| 79 | + void read(const std::string& line); | |
| 80 | 80 | std::string write() const; |
| 81 | 81 | bool contains(const GSkyDir& dir) const; |
| 82 | 82 | bool contains(const GSkyRegion& reg) const; |
| ... | ... | @@ -110,8 +110,51 @@ double GSkyRegionCircle::radius(void) const |
| 110 | 110 | return (m_radius); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | +/***********************************************************************//** | |
| 114 | + * @brief Return region centre | |
| 115 | + * | |
| 116 | + * @return region centre | |
| 117 | + * | |
| 118 | + * Returns the region centre. | |
| 119 | + ***************************************************************************/ | |
| 120 | + | |
| 113 | 121 | inline |
| 114 | 122 | GSkyDir GSkyRegionCircle::centre(void) const |
| 115 | 123 | { |
| 116 | 124 | return (m_centre); |
| 117 | 125 | } |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | +/***********************************************************************//** | |
| 130 | + * @brief Set centre of region | |
| 131 | + * | |
| 132 | + * @param[in] dir Center of region. | |
| 133 | + ***************************************************************************/ | |
| 134 | +void GSkyRegionCircle::centre(const GSkyDir& dir) | |
| 135 | +{ | |
| 136 | + // Set centre | |
| 137 | + m_centre = dir; | |
| 138 | + | |
| 139 | + //Return | |
| 140 | + return; | |
| 141 | +} | |
| 142 | + | |
| 143 | + | |
| 144 | +/***********************************************************************//** | |
| 145 | + * @brief Set centre values | |
| 146 | + * | |
| 147 | + * @param[in] ra Right ascension value. | |
| 148 | + * @param[in] dec declination value. | |
| 149 | + ***************************************************************************/ | |
| 150 | +void GSkyRegionCircle::centre(const double& ra, const double& dec) | |
| 151 | +{ | |
| 152 | + // Set centre values | |
| 153 | + m_centre.radec_deg(ra,dec); | |
| 154 | + | |
| 155 | + //Return | |
| 156 | + return; | |
| 157 | +} | |
| 158 | + | |
| 159 | + | |
| 160 | + | ... | ... |
include/GTools.hpp
| ... | ... | @@ -87,6 +87,7 @@ namespace gammalib { |
| 87 | 87 | bool file_exists(const std::string& filename); |
| 88 | 88 | bool isinfinite(const double& x); |
| 89 | 89 | bool isnotanumber(const double& x); |
| 90 | + bool contains(std::string str1, std::string str2); | |
| 90 | 91 | } |
| 91 | 92 | |
| 92 | 93 | /***********************************************************************//** | ... | ... |
src/sky/GSkyRegionCircle.cpp
| ... | ... | @@ -33,6 +33,12 @@ |
| 33 | 33 | |
| 34 | 34 | /* __ Method name definitions ____________________________________________ */ |
| 35 | 35 | |
| 36 | +#define G_CONSTRUCTOR "GSkyRegionCircle::GSkyRegionCircle(GSkyDir&, double&)" | |
| 37 | +#define G_RADIUS "GSkyRegionCircle::radius(double&)" | |
| 38 | +#define G_READ "GSkyRegionCircle::read(std::string&)" | |
| 39 | +#define G_CONTAINS "GSkyRegionCircle::contains(GSkyRegion&)" | |
| 40 | +#define G_OVERLAPS "GSkyRegionCircle::overlaps(GSkyRegion&)" | |
| 41 | + | |
| 36 | 42 | /* __ Macros _____________________________________________________________ */ |
| 37 | 43 | |
| 38 | 44 | /* __ Coding definitions _________________________________________________ */ |
| ... | ... | @@ -76,6 +82,11 @@ GSkyRegionCircle::GSkyRegionCircle(GSkyDir &centre, const double &radius) |
| 76 | 82 | m_centre = centre; |
| 77 | 83 | m_radius = radius; |
| 78 | 84 | |
| 85 | + // Check if radius is valid | |
| 86 | + if (m_radius < 0.0) { | |
| 87 | + throw GException::invalid_value(G_CONSTRUCTOR, "Radius of a region can't be less than 0"); | |
| 88 | + } | |
| 89 | + | |
| 79 | 90 | // compute solid angle |
| 80 | 91 | compute_solid(); |
| 81 | 92 | |
| ... | ... | @@ -84,31 +95,10 @@ GSkyRegionCircle::GSkyRegionCircle(GSkyDir &centre, const double &radius) |
| 84 | 95 | } |
| 85 | 96 | |
| 86 | 97 | /***********************************************************************//** |
| 87 | - * @brief Direction constructor | |
| 98 | + * @brief string constructor | |
| 88 | 99 | * |
| 89 | - * @param[in] ra Right ascension of Circle center [deg]. | |
| 90 | - * @param[in] dec declination of Circle center [deg]. | |
| 91 | - * @param[in] radius Radius of the region [deg]. | |
| 100 | + * @param[in] line a string containing a line of a ds9 region file | |
| 92 | 101 | ***************************************************************************/ |
| 93 | -//GSkyRegionCircle::GSkyRegionCircle(const double& ra, const double& dec, const double& radius) | |
| 94 | -//{ | |
| 95 | -// // Initialise members | |
| 96 | -// init_members(); | |
| 97 | -// | |
| 98 | -// // Set parameters | |
| 99 | -// GSkyDir dir = GSkyDir(); | |
| 100 | -// dir.radec(ra,dec); | |
| 101 | -// m_centre = dir; | |
| 102 | -// m_radius = radius; | |
| 103 | -// | |
| 104 | -// // compute solid angle | |
| 105 | -// compute_solid(); | |
| 106 | -// | |
| 107 | -// // Return | |
| 108 | -// return; | |
| 109 | -//} | |
| 110 | - | |
| 111 | - | |
| 112 | 102 | GSkyRegionCircle::GSkyRegionCircle(const std::string &line) |
| 113 | 103 | { |
| 114 | 104 | // Initialise members |
| ... | ... | @@ -228,44 +218,117 @@ void GSkyRegionCircle::radius(const double& radius) |
| 228 | 218 | // Set radius value |
| 229 | 219 | m_radius = radius; |
| 230 | 220 | |
| 221 | + if (m_radius < 0.0) { | |
| 222 | + throw GException::invalid_value(G_RADIUS, "Radius of a region can't be less than 0"); | |
| 223 | + } | |
| 224 | + | |
| 231 | 225 | // Recompute the solid angle |
| 232 | 226 | compute_solid(); |
| 233 | 227 | } |
| 234 | 228 | |
| 235 | 229 | |
| 236 | 230 | /***********************************************************************//** |
| 237 | - * @brief Set centre of region | |
| 231 | + * @brief read region from string | |
| 238 | 232 | * |
| 239 | - * @param[in] dir Center of region. | |
| 233 | + * @param[in] line a string ds9 format | |
| 240 | 234 | ***************************************************************************/ |
| 241 | -void GSkyRegionCircle::centre(const GSkyDir& dir) | |
| 235 | +void GSkyRegionCircle::read(const std::string& line) | |
| 242 | 236 | { |
| 243 | - // Set y value | |
| 244 | - m_centre = dir; | |
| 237 | + // Clear the current instance | |
| 238 | + clear(); | |
| 239 | + | |
| 240 | + // split the string into 2 parts seperated by # | |
| 241 | + std::vector<std::string> substrings = gammalib::split(line,"#"); | |
| 242 | + | |
| 243 | + std::string region_def = substrings[0]; | |
| 244 | + std::string comment = substrings[1]; | |
| 245 | + | |
| 246 | + // Finding the circle | |
| 247 | + if (region_def.find("circle") == std::string::npos) { | |
| 248 | + throw GException::invalid_value(G_READ, "Cannnot find the key word \"circle\" in provided string"); | |
| 249 | + } | |
| 250 | + | |
| 251 | + // get the the coordinate system of the values | |
| 252 | + std::string system = gammalib::split(region_def,";")[0]; | |
| 253 | + | |
| 254 | + // get the substring of the important values | |
| 255 | + unsigned pos = region_def.find("circle("); | |
| 256 | + unsigned end = region_def.find(")"); | |
| 257 | + std::string circlestring = region_def.substr(pos+7,end); | |
| 258 | + circlestring.erase(circlestring.find(")"),1); | |
| 259 | + | |
| 260 | + // get the values of the region x,y,and radius | |
| 261 | + std::vector<std::string> values = gammalib::split(circlestring,","); | |
| 262 | + if (values.size()!=3) { | |
| 263 | + throw GException::invalid_argument(G_READ, "Invalid number of arguments, circle takes exactly 3 arguments"); | |
| 264 | + } | |
| 265 | + double x = gammalib::todouble(values[0]); | |
| 266 | + double y = gammalib::todouble(values[1]); | |
| 267 | + double radius = gammalib::todouble(values[2]); | |
| 268 | + | |
| 269 | + // Initialise sky dir | |
| 270 | + GSkyDir dir = GSkyDir(); | |
| 271 | + | |
| 272 | + // Set the values in the correct system | |
| 273 | + if(system == "fk5") { | |
| 274 | + dir.radec_deg(x,y); | |
| 275 | + } | |
| 276 | + else if (system == "galactic") { | |
| 277 | + dir.lb_deg(x,y); | |
| 278 | + } | |
| 279 | + else { | |
| 280 | + std::stringstream s; | |
| 281 | + s<<"provided coordinate system "<<system<<" is not supported"; | |
| 282 | + throw GException::invalid_argument(G_READ, s.str()); | |
| 283 | + } | |
| 284 | + | |
| 285 | + // Setting par values | |
| 286 | + m_centre = dir; | |
| 287 | + m_radius = radius; | |
| 288 | + | |
| 289 | + // throwing exception if radius is less than 0 | |
| 290 | + if (m_radius < 0.0) { | |
| 291 | + throw GException::invalid_value(G_READ, "Radius of a region can't be less than 0"); | |
| 292 | + } | |
| 293 | + | |
| 294 | + // compute solid angle | |
| 295 | + compute_solid(); | |
| 296 | + | |
| 297 | + // Check if there is a given name for the region and set it | |
| 298 | + std::vector<std::string>comments =gammalib::split(comment, " "); | |
| 299 | + for (int i = 0; i < comments.size(); i++) { | |
| 300 | + if (gammalib::contains(comments[i],"text")){ | |
| 301 | + std::vector<std::string> attributes = gammalib::split(comments[i],"="); | |
| 302 | + if (attributes.size()<2) { | |
| 303 | + throw GException::invalid_argument(G_READ, "Invalid number of arguments, type of attribute must be key=value"); | |
| 304 | + } | |
| 305 | + m_name = attributes[1]; | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 309 | + // Return | |
| 310 | + return; | |
| 245 | 311 | } |
| 246 | 312 | |
| 247 | 313 | |
| 248 | 314 | /***********************************************************************//** |
| 249 | - * @brief Set centre values | |
| 315 | + * @brief write region into a string | |
| 250 | 316 | * |
| 251 | - * @param[in] ra Right ascension value. | |
| 252 | - * @param[in] dec declination value. | |
| 317 | + * @return string to be written in a ds9 region file | |
| 253 | 318 | ***************************************************************************/ |
| 254 | -void GSkyRegionCircle::centre(const double& ra, const double& dec) | |
| 255 | -{ | |
| 256 | - // Set centre values | |
| 257 | - m_centre.radec_deg(ra,dec); | |
| 258 | -} | |
| 259 | - | |
| 260 | - | |
| 261 | -void GSkyRegionCircle::read(const std::string line) const | |
| 262 | -{ | |
| 263 | - std::cout<<"Reading of GSkyRegionCircle::read not implemented yet"<<std::endl; | |
| 264 | -} | |
| 265 | 319 | std::string GSkyRegionCircle::write() const |
| 266 | 320 | { |
| 267 | - std::cout<<"Writing of GSkyRegionCircle::read not implemented yet"<<std::endl; | |
| 268 | - return ""; | |
| 321 | + std::string result; | |
| 322 | + result.append("fk5;circle("); | |
| 323 | + result.append(gammalib::str(m_centre.ra_deg())); | |
| 324 | + result.append(","); | |
| 325 | + result.append(gammalib::str(m_centre.dec_deg())); | |
| 326 | + result.append(","); | |
| 327 | + result.append(gammalib::str(m_radius)); | |
| 328 | + result.append(") # text="); | |
| 329 | + result.append(m_name); | |
| 330 | + result.append("\n"); | |
| 331 | + return result; | |
| 269 | 332 | } |
| 270 | 333 | /***********************************************************************//** |
| 271 | 334 | * @brief Print circular region |
| ... | ... | @@ -330,8 +393,7 @@ bool GSkyRegionCircle::contains(const GSkyRegion& reg) const |
| 330 | 393 | bool fully_inside = false; |
| 331 | 394 | |
| 332 | 395 | // If other region is circle use a simple way to calculate |
| 333 | - if (reg.type() == "Circle") | |
| 334 | - { | |
| 396 | + if (reg.type() == "Circle") { | |
| 335 | 397 | // create circular region from reg |
| 336 | 398 | GSkyRegionCircle* regcirc = (GSkyRegionCircle*)reg.clone(); |
| 337 | 399 | |
| ... | ... | @@ -339,15 +401,13 @@ bool GSkyRegionCircle::contains(const GSkyRegion& reg) const |
| 339 | 401 | double ang_dist = m_centre.dist_deg(regcirc->centre()); |
| 340 | 402 | |
| 341 | 403 | // Check if the region is contained in this |
| 342 | - if ((ang_dist + regcirc->radius()) <= m_radius) | |
| 343 | - { | |
| 404 | + if ((ang_dist + regcirc->radius()) <= m_radius) { | |
| 344 | 405 | // Set return value to true |
| 345 | 406 | fully_inside = true; |
| 346 | 407 | } |
| 347 | 408 | } |
| 348 | - else | |
| 349 | - { | |
| 350 | - std::cout<<"ERROR: GSkyRegionCircle::contains(const GSkyRegion& reg) not implemented yet"<<std::endl; | |
| 409 | + else { | |
| 410 | + throw GException::feature_not_implemented(G_CONTAINS,"Cannot compare two different region types yet"); | |
| 351 | 411 | } |
| 352 | 412 | // Return value |
| 353 | 413 | return fully_inside; |
| ... | ... | @@ -380,9 +440,8 @@ bool GSkyRegionCircle::overlaps(const GSkyRegion& reg) const |
| 380 | 440 | } |
| 381 | 441 | } |
| 382 | 442 | |
| 383 | - else | |
| 384 | - { | |
| 385 | - std::cout<<"ERROR: GSkyRegionCircle::overlaps(const GSkyRegion& reg) not implemented yet"<<std::endl; | |
| 443 | + else { | |
| 444 | + throw GException::feature_not_implemented(G_OVERLAPS,"Cannot compare two different region types yet"); | |
| 386 | 445 | } |
| 387 | 446 | |
| 388 | 447 | // Return value | ... | ... |
src/support/GTools.cpp
| ... | ... | @@ -824,3 +824,25 @@ bool gammalib::file_exists(const std::string& filename) |
| 824 | 824 | // Return result |
| 825 | 825 | return result; |
| 826 | 826 | } |
| 827 | + | |
| 828 | +/***********************************************************************//** | |
| 829 | + * @brief Checks if a substring is in a string . | |
| 830 | + * | |
| 831 | + * @param[in] str string you want to search in. | |
| 832 | + * @param[in] substring string you are looking for in str. | |
| 833 | + * | |
| 834 | + * Checks if substring is contained in str | |
| 835 | + ***************************************************************************/ | |
| 836 | +bool gammalib::contains(std::string str, std::string substring) | |
| 837 | +{ | |
| 838 | + // Initialise result | |
| 839 | + bool result = false; | |
| 840 | + | |
| 841 | + // checks if substring is in str | |
| 842 | + if (str.find(substring) != std::string::npos){ | |
| 843 | + result = true; | |
| 844 | + } | |
| 845 | + | |
| 846 | + // Return result | |
| 847 | + return result; | |
| 848 | +} | ... | ... |