Commit 680d61ee1b72ab9d840b7725df38db48c1f0436e
1 parent
d90634bb
implement constuctor parameter checking
Showing
5 changed files
with
135 additions
and
41 deletions
include/GException.hpp
| ... | ... | @@ -239,6 +239,24 @@ public: |
| 239 | 239 | healpix(string origin, string message); |
| 240 | 240 | }; |
| 241 | 241 | |
| 242 | + // Healpix resolution invalid | |
| 243 | + class healpix_bad_nside : public GExceptionHandler { | |
| 244 | + public: | |
| 245 | + healpix_bad_nside(string origin, int nside); | |
| 246 | + }; | |
| 247 | + | |
| 248 | + // Healpix scheme invalid | |
| 249 | + class healpix_bad_scheme : public GExceptionHandler { | |
| 250 | + public: | |
| 251 | + healpix_bad_scheme(string origin, string scheme); | |
| 252 | + }; | |
| 253 | + | |
| 254 | + // Healpix coordinate system invalid | |
| 255 | + class healpix_bad_coords : public GExceptionHandler { | |
| 256 | + public: | |
| 257 | + healpix_bad_coords(string origin, string coordsys); | |
| 258 | + }; | |
| 259 | + | |
| 242 | 260 | }; |
| 243 | 261 | |
| 244 | 262 | #endif /* GEXCEPTION_HPP */ | ... | ... |
include/GHealpix.hpp
| ... | ... | @@ -41,7 +41,8 @@ class GHealpix { |
| 41 | 41 | |
| 42 | 42 | public: |
| 43 | 43 | // Constructors and destructors |
| 44 | - GHealpix(int nside, int scheme = 1, int coordsys = 1, int dimension = 1); | |
| 44 | + GHealpix(int nside, std::string scheme = "NESTED", | |
| 45 | + std::string coordsys = "GAL", int dimension = 1); | |
| 45 | 46 | GHealpix(const GFitsHDU* hdu); |
| 46 | 47 | GHealpix(const GHealpix& pixels); |
| 47 | 48 | virtual ~GHealpix(); |
| ... | ... | @@ -62,7 +63,7 @@ public: |
| 62 | 63 | double omega(void) const; |
| 63 | 64 | GSkyDir pix2ang(const int& ipix); |
| 64 | 65 | int ang2pix(GSkyDir dir) const; |
| 65 | - | |
| 66 | + | |
| 66 | 67 | private: |
| 67 | 68 | // Private methods |
| 68 | 69 | void init_members(void); |
| ... | ... | @@ -77,7 +78,7 @@ private: |
| 77 | 78 | void pix2ang_nest(int ipix, double* theta, double* phi); |
| 78 | 79 | int ang2pix_z_phi_ring(double z, double phi) const; |
| 79 | 80 | int ang2pix_z_phi_nest(double z, double phi) const; |
| 80 | - | |
| 81 | + | |
| 81 | 82 | // Private data area |
| 82 | 83 | int m_nside; //!< Number of divisions of each base pixel (1-8192) |
| 83 | 84 | int m_npface; //!< | ... | ... |
pyext/GHealpix.i
| ... | ... | @@ -19,6 +19,7 @@ |
| 19 | 19 | /* Put headers and other declarations here that are needed for compilation */ |
| 20 | 20 | #include "GHealpix.hpp" |
| 21 | 21 | %} |
| 22 | +%include stl.i | |
| 22 | 23 | |
| 23 | 24 | |
| 24 | 25 | /***********************************************************************//** |
| ... | ... | @@ -29,7 +30,8 @@ |
| 29 | 30 | class GHealpix { |
| 30 | 31 | public: |
| 31 | 32 | // Constructors and destructors |
| 32 | - GHealpix(int nside, int scheme = 1, int coordsys = 1, int dimension = 1); | |
| 33 | + GHealpix(int nside, std::string scheme = "NESTED", | |
| 34 | + std::string coordsys = "GAL", int dimension = 1); | |
| 33 | 35 | GHealpix(const GFitsHDU* hdu); |
| 34 | 36 | GHealpix(const GHealpix& pixels); |
| 35 | 37 | virtual ~GHealpix(); | ... | ... |
src/GException.cpp
| ... | ... | @@ -72,7 +72,7 @@ GException::empty::empty(string origin) |
| 72 | 72 | GException::out_of_range::out_of_range(string origin, int inx, int min, int max) |
| 73 | 73 | { |
| 74 | 74 | m_origin = origin; |
| 75 | - m_message = "Index (" + str(inx) + ") out of range [" + str(min) + | |
| 75 | + m_message = "Index (" + str(inx) + ") out of range [" + str(min) + | |
| 76 | 76 | "," + str(max) + "]"; |
| 77 | 77 | } |
| 78 | 78 | |
| ... | ... | @@ -84,7 +84,7 @@ GException::out_of_range::out_of_range(string origin, int inx, int elements) |
| 84 | 84 | { |
| 85 | 85 | m_origin = origin; |
| 86 | 86 | if (elements > 0) { |
| 87 | - m_message = "Vector index (" + str(inx) + ") out of range [0," + | |
| 87 | + m_message = "Vector index (" + str(inx) + ") out of range [0," + | |
| 88 | 88 | str(elements-1) + "]"; |
| 89 | 89 | } |
| 90 | 90 | else { |
| ... | ... | @@ -111,7 +111,7 @@ GException::out_of_range::out_of_range(string origin, int row, int col, int rows |
| 111 | 111 | GException::vector_mismatch::vector_mismatch(string origin, int size1, int size2) |
| 112 | 112 | { |
| 113 | 113 | m_origin = origin; |
| 114 | - m_message = "Vector dimensions differ (" + str(size1) + " <-> " + | |
| 114 | + m_message = "Vector dimensions differ (" + str(size1) + " <-> " + | |
| 115 | 115 | str(size2) + ")"; |
| 116 | 116 | } |
| 117 | 117 | |
| ... | ... | @@ -532,3 +532,44 @@ GException::healpix::healpix(string origin, string message) |
| 532 | 532 | m_origin = origin; |
| 533 | 533 | m_message = message; |
| 534 | 534 | } |
| 535 | + | |
| 536 | + | |
| 537 | +/***********************************************************************//** | |
| 538 | + * @brief Healpix resolution invalid | |
| 539 | + * | |
| 540 | + * @param[in] origin Method that throws the error. | |
| 541 | + * @param[in] nside Specified nside parameter. | |
| 542 | + ***************************************************************************/ | |
| 543 | +GException::healpix_bad_nside::healpix_bad_nside(string origin, int nside) | |
| 544 | +{ | |
| 545 | + m_origin = origin; | |
| 546 | + m_message = "Invalid nside "+str(nside)+" parameter (should be one of " | |
| 547 | + "1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192)."; | |
| 548 | +} | |
| 549 | + | |
| 550 | + | |
| 551 | +/***********************************************************************//** | |
| 552 | + * @brief Healpix scheme invalid | |
| 553 | + * | |
| 554 | + * @param[in] origin Method that throws the error. | |
| 555 | + * @param[in] scheme Specified scheme. | |
| 556 | + ***************************************************************************/ | |
| 557 | +GException::healpix_bad_scheme::healpix_bad_scheme(string origin, string scheme) | |
| 558 | +{ | |
| 559 | + m_origin = origin; | |
| 560 | + m_message = "Invalid scheme '"+scheme+"' (should be either 'RING' or 'NESTED')."; | |
| 561 | +} | |
| 562 | + | |
| 563 | + | |
| 564 | +/***********************************************************************//** | |
| 565 | + * @brief Healpix coordinate system invalid | |
| 566 | + * | |
| 567 | + * @param[in] origin Method that throws the error. | |
| 568 | + * @param[in] coordsys Specified coordinate system. | |
| 569 | + ***************************************************************************/ | |
| 570 | +GException::healpix_bad_coords::healpix_bad_coords(string origin, string coordsys) | |
| 571 | +{ | |
| 572 | + m_origin = origin; | |
| 573 | + m_message = "Invalid coordinate system '"+coordsys + | |
| 574 | + "' (should be either 'EQU' or 'GAL')."; | |
| 575 | +} | ... | ... |
src/GHealpix.cpp
| ... | ... | @@ -27,6 +27,7 @@ |
| 27 | 27 | /* __ Namespaces _________________________________________________________ */ |
| 28 | 28 | |
| 29 | 29 | /* __ Method name definitions ____________________________________________ */ |
| 30 | +#define G_CONSTRUCT "GHealpix::GHealpix(int,int,int,int)" | |
| 30 | 31 | #define G_OP_ACCESS "GHealpix::operator(int,int)" |
| 31 | 32 | #define G_LOAD "GHealpix::load(const GFitsHDU*)" |
| 32 | 33 | #define G_PIX2ANG_RING "GHealpix::pix2ang_ring(int,double*,double*)" |
| ... | ... | @@ -61,25 +62,50 @@ static short utab[0x100]; |
| 61 | 62 | * @brief Constructor |
| 62 | 63 | * |
| 63 | 64 | * @param[in] nside Number of sides |
| 64 | - * @param[in] scheme Ordering scheme (0=ring, 1=nested) | |
| 65 | - * @param[in] coordsys Coordinate system (0=equatorial, 1=galactic) | |
| 66 | - * @param[in] nside Number of sides | |
| 65 | + * @param[in] scheme Ordering scheme ('RING' or 'NESTED') | |
| 66 | + * @param[in] coordsys Coordinate system ('EQU' or 'GAL') | |
| 67 | + * @param[in] dimension Vector dimension of pixels | |
| 68 | + * | |
| 69 | + * @exception GException::healpix_bad_nside Invalid nside parameter. | |
| 70 | + * @exception GException::healpix_bad_scheme Invalid scheme parameter. | |
| 71 | + * @exception GException::healpix_bad_coords Invalid coordsys parameter. | |
| 67 | 72 | ***************************************************************************/ |
| 68 | -GHealpix::GHealpix(int nside, int scheme, int coordsys, int dimension) | |
| 73 | +GHealpix::GHealpix(int nside, std::string scheme, std::string coordsys, | |
| 74 | + int dimension) | |
| 69 | 75 | { |
| 70 | 76 | // Initialise class members |
| 71 | 77 | init_members(); |
| 72 | - | |
| 73 | - // Put parameters in valid range | |
| 74 | - if (nside < 1) nside = 1; | |
| 75 | - if (scheme != 0 && scheme != 1) scheme = 1; // Default to nested | |
| 76 | - if (coordsys != 0 && coordsys != 1) coordsys = 1; // Default to galactic | |
| 78 | + | |
| 79 | + // Check nside parameter (power of 2 between 1 and 8192) | |
| 80 | + int order = nside2order(nside); | |
| 81 | + if (order == -1) | |
| 82 | + throw GException::healpix_bad_nside(G_CONSTRUCT, nside); | |
| 83 | + | |
| 84 | + // Check scheme | |
| 85 | + int i_scheme = -1; | |
| 86 | + if (scheme == "RING") | |
| 87 | + i_scheme = 0; | |
| 88 | + else if (scheme == "NESTED") | |
| 89 | + i_scheme = 1; | |
| 90 | + else | |
| 91 | + throw GException::healpix_bad_scheme(G_CONSTRUCT, scheme); | |
| 92 | + | |
| 93 | + // Check coordinate system | |
| 94 | + int i_coordsys; | |
| 95 | + if (coordsys == "EQU") | |
| 96 | + i_coordsys = 0; | |
| 97 | + else if (coordsys == "GAL") | |
| 98 | + i_coordsys = 1; | |
| 99 | + else | |
| 100 | + throw GException::healpix_bad_coords(G_CONSTRUCT, coordsys); | |
| 101 | + | |
| 102 | + // Check dimension | |
| 77 | 103 | if (dimension < 1) dimension = 1; |
| 78 | - | |
| 104 | + | |
| 79 | 105 | // Set Healpix parameters |
| 80 | 106 | m_nside = nside; |
| 81 | - m_scheme = scheme; | |
| 82 | - m_coordsys = coordsys; | |
| 107 | + m_scheme = i_scheme; | |
| 108 | + m_coordsys = i_coordsys; | |
| 83 | 109 | m_size_pixels = dimension; |
| 84 | 110 | |
| 85 | 111 | // Derive Healpix parameters |
| ... | ... | @@ -231,11 +257,11 @@ void GHealpix::read(const GFitsHDU* hdu) |
| 231 | 257 | // Free memory and initialise members |
| 232 | 258 | free_members(); |
| 233 | 259 | init_members(); |
| 234 | - | |
| 260 | + | |
| 235 | 261 | // Check if we have a healpix representation |
| 236 | 262 | if (hdu->card("PIXTYPE")->string() != "HEALPIX") |
| 237 | 263 | throw GException::healpix(G_LOAD, "HDU does not contain Healpix data"); |
| 238 | - | |
| 264 | + | |
| 239 | 265 | // Get Healpix resolution and determine number of pixels and solid angle |
| 240 | 266 | m_nside = hdu->card("NSIDE")->integer(); |
| 241 | 267 | m_npface = m_nside * m_nside; |
| ... | ... | @@ -254,7 +280,7 @@ void GHealpix::read(const GFitsHDU* hdu) |
| 254 | 280 | catch (GException::fits_key_not_found &e) { |
| 255 | 281 | m_scheme = -1; // Flag unknown ordering |
| 256 | 282 | } |
| 257 | - | |
| 283 | + | |
| 258 | 284 | // Decode ordering scheme string |
| 259 | 285 | if (ordering == "RING") |
| 260 | 286 | m_scheme = 0; |
| ... | ... | @@ -278,7 +304,7 @@ void GHealpix::read(const GFitsHDU* hdu) |
| 278 | 304 | m_coordsys = -1; // Flag coordinate system as unknown |
| 279 | 305 | } |
| 280 | 306 | } |
| 281 | - | |
| 307 | + | |
| 282 | 308 | // Decode coordinate system string |
| 283 | 309 | if (coordsys.find("EQU") == 0) |
| 284 | 310 | m_coordsys = 0; |
| ... | ... | @@ -286,10 +312,10 @@ void GHealpix::read(const GFitsHDU* hdu) |
| 286 | 312 | m_coordsys = 1; |
| 287 | 313 | else |
| 288 | 314 | throw GException::healpix(G_LOAD, "Invalid coordinate system"); |
| 289 | - | |
| 315 | + | |
| 290 | 316 | // Continue only of we have pixels |
| 291 | 317 | if (m_num_pixels > 0) { |
| 292 | - | |
| 318 | + | |
| 293 | 319 | // Get first column |
| 294 | 320 | GFitsTableCol* col = hdu->column(0); |
| 295 | 321 | |
| ... | ... | @@ -300,7 +326,7 @@ void GHealpix::read(const GFitsHDU* hdu) |
| 300 | 326 | |
| 301 | 327 | // Extract vector size of each pixel |
| 302 | 328 | m_size_pixels = col->number(); |
| 303 | - | |
| 329 | + | |
| 304 | 330 | // Allocate pixels |
| 305 | 331 | alloc_members(); |
| 306 | 332 | |
| ... | ... | @@ -315,7 +341,7 @@ void GHealpix::read(const GFitsHDU* hdu) |
| 315 | 341 | } // endif: there were pixels to load |
| 316 | 342 | |
| 317 | 343 | } // endif: we had pixels |
| 318 | - | |
| 344 | + | |
| 319 | 345 | // Return |
| 320 | 346 | return; |
| 321 | 347 | } |
| ... | ... | @@ -331,7 +357,7 @@ void GHealpix::write(GFits* fits) |
| 331 | 357 | // Continue only if there are data |
| 332 | 358 | int size = m_num_pixels * m_size_pixels; |
| 333 | 359 | if (size > 0) { |
| 334 | - | |
| 360 | + | |
| 335 | 361 | // Create binary table with one column |
| 336 | 362 | GFitsBinTable table = GFitsBinTable(m_num_pixels); |
| 337 | 363 | GFitsTableDblCol column = GFitsTableDblCol("DATA", m_num_pixels, |
| ... | ... | @@ -343,16 +369,16 @@ void GHealpix::write(GFits* fits) |
| 343 | 369 | for (int inx = 0; inx < m_size_pixels; ++inx, ++ptr) |
| 344 | 370 | column(row,inx) = *ptr; |
| 345 | 371 | } |
| 346 | - | |
| 372 | + | |
| 347 | 373 | // Append column to table |
| 348 | 374 | table.append_column(column); |
| 349 | - | |
| 375 | + | |
| 350 | 376 | // Create HDU with table |
| 351 | 377 | GFitsHDU hdu(table); |
| 352 | - | |
| 378 | + | |
| 353 | 379 | // Set extension name |
| 354 | 380 | hdu.extname("HEALPIX"); |
| 355 | - | |
| 381 | + | |
| 356 | 382 | // Set ordering scheme keyword value |
| 357 | 383 | std::string s_scheme; |
| 358 | 384 | switch (m_scheme) { |
| ... | ... | @@ -380,24 +406,30 @@ void GHealpix::write(GFits* fits) |
| 380 | 406 | s_coordsys = "UNKNOWN"; |
| 381 | 407 | break; |
| 382 | 408 | } |
| 383 | - | |
| 409 | + | |
| 384 | 410 | // Set keywords |
| 385 | - GFitsHeaderCard pixtype = GFitsHeaderCard("PIXTYPE", "HEALPIX", "Pixel type"); | |
| 386 | - GFitsHeaderCard nside = GFitsHeaderCard("NSIDE", m_nside, "HEALPix resolution"); | |
| 387 | - GFitsHeaderCard ordering = GFitsHeaderCard("ORDERING", s_scheme, "Ordering scheme"); | |
| 388 | - GFitsHeaderCard coordsys = GFitsHeaderCard("COORDSYS", s_coordsys, "Coordinate system"); | |
| 389 | - | |
| 411 | + GFitsHeaderCard pixtype = GFitsHeaderCard("PIXTYPE", "HEALPIX", | |
| 412 | + "HEALPix pixelisation"); | |
| 413 | + GFitsHeaderCard nside = GFitsHeaderCard("NSIDE", m_nside, | |
| 414 | + "HEALPix resolution parameter"); | |
| 415 | + GFitsHeaderCard npix = GFitsHeaderCard("NPIX", m_num_pixels, | |
| 416 | + "Total number of pixels"); | |
| 417 | + GFitsHeaderCard ordering = GFitsHeaderCard("ORDERING", s_scheme, | |
| 418 | + "Pixel ordering scheme, either RING of NESTED"); | |
| 419 | + GFitsHeaderCard coordsys = GFitsHeaderCard("COORDSYS", s_coordsys, | |
| 420 | + "Coordinate system, either EQU or GAL"); | |
| 421 | + | |
| 390 | 422 | // Add cards to header |
| 391 | 423 | hdu.header()->update(pixtype); |
| 392 | 424 | hdu.header()->update(nside); |
| 393 | 425 | hdu.header()->update(ordering); |
| 394 | 426 | hdu.header()->update(coordsys); |
| 395 | - | |
| 427 | + | |
| 396 | 428 | // Append HDU to FITS file |
| 397 | 429 | fits->append_hdu(hdu); |
| 398 | - | |
| 430 | + | |
| 399 | 431 | } |
| 400 | - | |
| 432 | + | |
| 401 | 433 | // Return |
| 402 | 434 | return; |
| 403 | 435 | } | ... | ... |