GException.cpp 22.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
/***************************************************************************
 *                   GException.cpp  -  exception handler                  *
 * ----------------------------------------------------------------------- *
 *  copyright            : (C) 2006 by Jurgen Knodlseder                   *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/* __ Includes ___________________________________________________________ */
#include "GException.hpp"
#include "GFitsCfitsio.hpp"


/***************************************************************************
 *                       Private conversion functions                      *
 ***************************************************************************/
std::string str(int value)
{
    std::ostringstream s_value;
    s_value << value;
    return  s_value.str();
}
std::string str(double value)
{
    std::ostringstream s_value;
    s_value << std::scientific << value;
    return  s_value.str();
}


/***************************************************************************
 *                  Exception handler base class definition                *
 ***************************************************************************/
const char* GExceptionHandler::what() const throw()
{
    std::string message = "*** ERROR in " + m_origin + ": " + m_message;
    return message.c_str();
}


/***************************************************************************
 *                        Memory allocation exception                      *
 ***************************************************************************/
GException::mem_alloc::mem_alloc(std::string origin, unsigned num)
{
    m_origin  = origin;
    m_message = "Memory allocation error (" + str((int)num) + " elements)";
}


/***************************************************************************
 *                           Empty object exception                        *
 ***************************************************************************/
GException::empty::empty(std::string origin)
{
    m_origin  = origin;
    m_message = "Zero-size allocation";
}


/***************************************************************************
 *                            Index out of range                           *
 ***************************************************************************/
GException::out_of_range::out_of_range(std::string origin, int inx, int min, int max)
{
    m_origin  = origin;
    m_message = "Index (" + str(inx) + ") out of range [" + str(min) +
                "," + str(max) + "]";
}


/***************************************************************************
 *                          Vector index out of range                      *
 ***************************************************************************/
GException::out_of_range::out_of_range(std::string origin, int inx, int elements)
{
    m_origin = origin;
    if (elements > 0) {
        m_message = "Vector index (" + str(inx) + ") out of range [0," +
                    str(elements-1) + "]";
    }
    else {
        m_message = "Empty vector";
    }
}


/***************************************************************************
 *                      Matrix row or column out of range                  *
 ***************************************************************************/
GException::out_of_range::out_of_range(std::string origin, int row, int col, int rows, int cols)
{
    m_origin  = origin;
    m_message = "Matrix element (" + str(row) + "," + str(col) +
                ") out of range ([0," + str(rows) + "], [0," +
                str(cols) + "])";
}


/***************************************************************************
 *                          Vector dimensions differ                       *
 ***************************************************************************/
GException::vector_mismatch::vector_mismatch(std::string origin, int size1, int size2)
{
    m_origin  = origin;
    m_message = "Vector dimensions differ (" + str(size1) + " <-> " +
                str(size2) + ")";
}


/***************************************************************************
 *                   Invalid vector dimension for cross product            *
 ***************************************************************************/
GException::vector_bad_cross_dim::vector_bad_cross_dim(std::string origin, int elements)
{
    m_origin  = origin;
    m_message = "Vector cross product only defined for 3 dimensions but vector size is " + 
                str(elements); 
}


/***************************************************************************
 *                     Mismatch between matrix and vector                  *
 ***************************************************************************/
GException::matrix_vector_mismatch::matrix_vector_mismatch(std::string origin, 
                                                           int         num, 
														   int         rows,
														   int         cols)
{
    m_origin  = origin;
    m_message = "Vector dimension [" + str(num) + 
                "] is incompatible with matrix size [" + 
                str(rows) + "," + str(cols) + "]";
}


/***************************************************************************
 *                       Matrix mismatch in operation                      *
 ***************************************************************************/
GException::matrix_mismatch::matrix_mismatch(std::string origin, int rows1, int cols1, int rows2, int cols2)
{
    m_origin  = origin;
    m_message = "Matrix mismatch: M1(" + str(rows1) + "," + str(cols1) +
                ") incompatible with M2(" + str(rows2) + "," + str(cols2) + ")";
}


/***************************************************************************
 *                           Matrix not rectangular                        *
 ***************************************************************************/
GException::matrix_not_rectangular::matrix_not_rectangular(std::string origin, int rows, int cols)
{
    m_origin  = origin;
    m_message = "Matrix is not rectangular [" + str(rows) + "," + str(cols) + "]";
}


/***************************************************************************
 *                     Matrix is not positive definite                     *
 ***************************************************************************/
GException::matrix_not_pos_definite::matrix_not_pos_definite(std::string origin, int row, double sum)
{
    m_origin  = origin;
    m_message = "Matrix is not positive definite (sum " + str(sum) + 
                " occured in row/column " + str(row) + ")";
}


/***************************************************************************
 *                         Matrix is not symmetric                         *
 ***************************************************************************/
GException::matrix_not_symmetric::matrix_not_symmetric(std::string origin, 
                                                       int         cols, 
													   int         rows)
{
    m_origin  = origin;
    m_message = "Matrix is not symmetric [" + str(rows) + "," + str(cols) + "]";
}


/***************************************************************************
 *                       Matrix has not been factorised                    *
 ***************************************************************************/
GException::matrix_not_factorised::matrix_not_factorised(std::string origin, 
                                                         std::string type)
{
    m_origin  = origin;
    m_message = "Matrix has not been factorised using " + type;
}


/***************************************************************************
 *                       All matrix elements are zero                      *
 ***************************************************************************/
GException::matrix_zero::matrix_zero(std::string origin)
{
    m_origin = origin;
    m_message = "All matrix elements are zero";
}


/***************************************************************************
 *                     Invalid ordering scheme requested                   *
 ***************************************************************************/
GException::invalid_order::invalid_order(std::string origin, int order, int min_order, int max_order)
{
    m_origin  = origin;
    m_message = "Invalid ordering type " + str(order) + 
                "requested; must be comprised in [" + str(min_order) +
                "," + str(max_order) + "]";
}


/***********************************************************************//**
 * @brief General FITS error
 *
 * @param[in] origin Method that throws the error.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_error::fits_error(std::string origin, int status)
{
    m_origin  = origin;
    std::ostringstream s_error;
    char err_text[31];
    __ffgerr(status, err_text);
    s_error << err_text << " (status=" << status << ")";
    m_message = s_error.str();
}


/***********************************************************************//**
 * @brief FITS error: unable to open FITS file
 *
 * @param[in] origin Method that throws the error.
 * @param[in] filename Name of the file for which opening was attempted.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_open_error::fits_open_error(std::string origin,
                                             std::string filename,
                                             int         status)
{
    m_origin  = origin;
    m_message = "Unable to open FITS file '" + filename + "'";
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: attempted to overwrite FITS file
 *
 * @param[in] origin Method that throws the error.
 * @param[in] filename Name of the file for which overwrite attempt was made.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_file_exist::fits_file_exist(std::string origin,
                                             std::string filename,
                                             int         status)
{
    m_origin  = origin;
    m_message = "Attempted to overwrite FITS file '" + filename + "'";
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: file already open
 *
 * @param[in] origin Method that throws the error.
 * @param[in] filename Name of file that is already open.
 ***************************************************************************/
GException::fits_already_opened::fits_already_opened(std::string origin,
                                                     std::string filename)
{
    m_origin  = origin;
    m_message = "FITS file '" + filename + "' is already open";
}


/***********************************************************************//**
 * @brief FITS error: Keyword not in header
 *
 * @param[in] origin Method that throws the error.
 * @param[in] keyname Name of keyword that was not found.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_key_not_found::fits_key_not_found(std::string origin,
                                                   std::string keyname,
                                                   int         status)
{
    m_origin  = origin;
    m_message = "Keyword '" + keyname + "' not found in header";
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: Table column not found
 *
 * @param[in] origin Method that throws the error.
 * @param[in] colname Name of the table column that was not found.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_column_not_found::fits_column_not_found(std::string origin,
                                                         std::string colname,
                                                         int         status)
{
    m_origin  = origin;
    m_message = "Column '" + colname + "' not found in table";
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: No header
 *
 * @param[in] origin Method that throws the error.
 * @param[in] message Error message.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_no_header::fits_no_header(std::string origin,
                                           std::string message,
                                           int         status)
{
    m_origin  = origin;
    m_message = message;
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: No data
 *
 * @param[in] origin Method that throws the error.
 * @param[in] message Error message.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_no_data::fits_no_data(std::string origin,
                                       std::string message,
                                       int         status)
{
    m_origin  = origin;
    m_message = message;
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: HDU not found in FITS file
 *
 * @param[in] origin Method that throws the error.
 * @param[in] extname Name of the extension that was not found.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_hdu_not_found::fits_hdu_not_found(std::string origin,
                                                   std::string extname,
                                                   int         status)
{
    m_origin  = origin;
    m_message = "HDU '" + extname + "' not found in FITS file";
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: HDU not found in FITS file
 *
 * @param[in] origin Method that throws the error.
 * @param[in] extno Number of the extension that was not found.
 * @param[in] status cfitsio status.
 ***************************************************************************/
GException::fits_hdu_not_found::fits_hdu_not_found(std::string origin,
                                                   int         extno,
                                                   int         status)
{
    m_origin  = origin;
    m_message = "HDU number '" + str(extno) + "' not found in FITS file";
    if (status != 0)
        m_message += " (status=" + str(status) + ")";
}


/***********************************************************************//**
 * @brief FITS error: HDU of unknown type found
 *
 * @param[in] origin Method that throws the error.
 * @param[in] type Specified HDU type.
 ***************************************************************************/
GException::fits_unknown_HDU_type::fits_unknown_HDU_type(std::string origin,
                                                         int         type)
{
    m_origin  = origin;
    m_message = "HDU type '" + str(type) + "' is not known";
}


/***********************************************************************//**
 * @brief FITS error: HDU is not a table
 *
 * @param[in] origin Method that throws the error.
 * @param[in] type Specified HDU type.
 ***************************************************************************/
GException::fits_HDU_not_a_table::fits_HDU_not_a_table(std::string origin,
                                                       int         type)
{
    m_origin  = origin;
    m_message = "HDU is not of type 'table' (type=" + str(type) + ")";
}


/***********************************************************************//**
 * @brief FITS error: Invalid type
 *
 * @param[in] origin Method that throws the error.
 * @param[in] message Error message.
 ***************************************************************************/
GException::fits_invalid_type::fits_invalid_type(std::string origin,
                                                 std::string message)
{
    m_origin  = origin;
    m_message = message;
}


/***********************************************************************//**
 * @brief FITS error: Table type is unknow
 *
 * @param[in] origin Method that throws the error.
 * @param[in] type Specified table type.
 ***************************************************************************/
GException::fits_unknown_tabtype::fits_unknown_tabtype(std::string origin,
                                                       int         type)
{
    m_origin  = origin;
    m_message = "Table type '" + str(type) + "' is unknown";
}


/***********************************************************************//**
 * @brief FITS error: Column type is unknown
 *
 * @param[in] origin Method that throws the error.
 * @param[in] type Specified column type.
 ***************************************************************************/
GException::fits_unknown_coltype::fits_unknown_coltype(std::string origin,
                                                       int         type)
{
    m_origin  = origin;
    m_message = "Column type '" + str(type) + "' is unknown";
}


/***********************************************************************//**
 * @brief FITS error: Bad column length
 *
 * @param[in] origin Method that throws the error.
 * @param[in] length Length of the column.
 * @param[in] rows Number of rows in table.
 ***************************************************************************/
GException::fits_bad_col_length::fits_bad_col_length(std::string origin,
                                                     int         length,
                                                     int         rows)
{
    m_origin  = origin;
    m_message = "Column length '" + str(length) + "' is not compatible"
                " with the number of rows '" + str(rows) + "' in the"
                " table";
}


/***********************************************************************//**
 * @brief FITS error: invalid number of bits per pixel
 *
 * @param[in] origin Method that throws the error.
 * @param[in] bitpix Bitpix value that was not 8,16,32,64,-32, or -64.
 ***************************************************************************/
GException::fits_bad_bitpix::fits_bad_bitpix(std::string origin,
                                             int         bitpix)
{
    m_origin  = origin;
    m_message = "Invalid number of bits per pixel (bitpix="+str(bitpix)+")";
}


/***********************************************************************//**
 * @brief FITS error: wrong image operator has been used
 *
 * @param[in] origin Method that throws the error.
 * @param[in] naxis Dimension of image.
 * @param[in] nargs Number of arguments of the image operator.
 ***************************************************************************/
GException::fits_wrong_image_operator::fits_wrong_image_operator(std::string origin,
                                                                 int         naxis,
                                                                 int         nargs)
{
    m_origin  = origin;
    m_message = "Wrong image pixel access operator has been used (dimension=" +
                str(naxis) + " <=> arguments=" + str(nargs) + ")";
}


/***********************************************************************//**
 * @brief Response error: invalid response type specified
 *
 * @param[in] origin Method that throws the error.
 * @param[in] type Specified response type.
 ***************************************************************************/
GException::rsp_invalid_type::rsp_invalid_type(std::string origin,
                                               std::string type)
{
    m_origin  = origin;
    m_message = "Invalid response type '"+type+"' specified";
}


/***********************************************************************//**
 * @brief General Healpix error
 *
 * @param[in] origin Method that throws the error.
 * @param[in] message Error message.
 ***************************************************************************/
GException::healpix::healpix(std::string origin, 
                             std::string message)
{
    m_origin  = origin;
    m_message = message;
}


/***********************************************************************//**
 * @brief Healpix resolution invalid
 *
 * @param[in] origin Method that throws the error.
 * @param[in] nside Specified nside parameter.
 ***************************************************************************/
GException::healpix_bad_nside::healpix_bad_nside(std::string origin, 
                                                 int         nside)
{
    m_origin  = origin;
    m_message = "Invalid nside "+str(nside)+" parameter (should be one of "
                "1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192).";
}


/***********************************************************************//**
 * @brief Healpix scheme invalid
 *
 * @param[in] origin Method that throws the error.
 * @param[in] scheme Specified scheme.
 ***************************************************************************/
GException::healpix_bad_scheme::healpix_bad_scheme(std::string origin, 
                                                   std::string scheme)
{
    m_origin  = origin;
    m_message = "Invalid scheme '"+scheme+"' (should be either 'RING' or 'NESTED').";
}


/***********************************************************************//**
 * @brief Healpix coordinate system invalid
 *
 * @param[in] origin Method that throws the error.
 * @param[in] coordsys Specified coordinate system.
 ***************************************************************************/
GException::healpix_bad_coords::healpix_bad_coords(std::string origin, 
                                                   std::string coordsys)
{
    m_origin  = origin;
    m_message = "Invalid coordinate system '"+coordsys +
                "' (should be either 'EQU' or 'GAL').";
}