GammaLib  2.0.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GException_fits.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GException_fits.cpp - FITS exception handlers *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2006-2013 by Jurgen Knodlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 
22 /* __ Includes ___________________________________________________________ */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include "GException.hpp"
27 #include "GTools.hpp"
28 #include "GFitsCfitsio.hpp"
29 
30 
31 /***********************************************************************//**
32  * @brief General FITS error
33  *
34  * @param[in] origin Method that throws the error.
35  * @param[in] status cfitsio status.
36  * @param[in] message Optional error message.
37  ***************************************************************************/
38 GException::fits_error::fits_error(const std::string& origin,
39  const int& status,
40  const std::string& message)
41 {
42  // Set origin
43  m_origin = origin;
44 
45  // Set FITS message
46  char err_text[31];
47  __ffgerr(status, err_text);
48  m_message = std::string(err_text);
49  m_message += " (status=" + gammalib::str(status) + ").";
50 
51  // Set message
52  /*
53  std::ostringstream s_error;
54  char err_text[31];
55  __ffgerr(status, err_text);
56  s_error << err_text << " (status=" << status << ")";
57  m_message = s_error.str();
58  */
59 
60  // Add optional error message
61  if (message.length() > 0) {
62  m_message += " " + message;
63  }
64 
65  // Return
66  return;
67 }
68 
69 
70 /***********************************************************************//**
71  * @brief FITS error: unable to open FITS file
72  *
73  * @param[in] origin Method that throws the error.
74  * @param[in] filename Name of the file for which opening was attempted.
75  * @param[in] status cfitsio status.
76  * @param[in] message Optional error message.
77  ***************************************************************************/
79  std::string filename,
80  int status,
81  std::string message)
82 {
83  // Set origin
84  m_origin = origin;
85 
86  // Set message
87  m_message = "Unable to open FITS file \"" + filename + "\"";
88 
89  // Add status
90  if (status != 0)
91  m_message += " (status=" + gammalib::str(status) + ")";
92 
93  // Add optional error message
94  if (message.length() > 0)
95  m_message += ". " + message;
96 
97  // Return
98  return;
99 }
100 
101 
102 /***********************************************************************//**
103  * @brief FITS error: attempted to overwrite FITS file
104  *
105  * @param[in] origin Method that throws the error.
106  * @param[in] filename Name of the file for which overwrite attempt was made.
107  * @param[in] status cfitsio status.
108  ***************************************************************************/
110  std::string filename,
111  int status)
112 {
113  // Set origin
114  m_origin = origin;
115 
116  // Set message
117  m_message = "Attempted to overwrite FITS file \"" + filename + "\"";
118 
119  // Add status
120  if (status != 0)
121  m_message += " (status=" + gammalib::str(status) + ")";
122 
123  // Return
124  return;
125 }
126 
127 
128 /***********************************************************************//**
129  * @brief FITS error: file not open
130  *
131  * @param[in] origin Method that throws the error.
132  * @param[in] message Optional error message.
133  ***************************************************************************/
135  std::string message)
136 {
137  // Set origin
138  m_origin = origin;
139 
140  // Set message
141  m_message = "FITS file not open";
142 
143 
144  // Add optional error message
145  if (message.length() > 0)
146  m_message += ". " + message;
147 
148  // Return
149  return;
150 }
151 
152 
153 /***********************************************************************//**
154  * @brief FITS error: file already open
155  *
156  * @param[in] origin Method that throws the error.
157  * @param[in] filename Name of file that is already open.
158  ***************************************************************************/
160  std::string filename)
161 {
162  // Set origin
163  m_origin = origin;
164 
165  // Set message
166  m_message = "FITS file \"" + filename + "\" is already open";
167 
168  // Return
169  return;
170 }
171 
172 
173 /***********************************************************************//**
174  * @brief FITS error: Table column not found
175  *
176  * @param[in] origin Method that throws the error.
177  * @param[in] colname Name of the table column that was not found.
178  * @param[in] status cfitsio status.
179  ***************************************************************************/
181  std::string colname,
182  int status)
183 {
184  // Set origin
185  m_origin = origin;
186 
187  // Set message
188  m_message = "Column \"" + colname + "\" not found in table";
189 
190  // Add status
191  if (status != 0)
192  m_message += " (status=" + gammalib::str(status) + ")";
193 
194  // Return
195  return;
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief FITS error: No header
201  *
202  * @param[in] origin Method that throws the error.
203  * @param[in] message Error message.
204  * @param[in] status cfitsio status.
205  ***************************************************************************/
207  std::string message,
208  int status)
209 {
210  // Set origin
211  m_origin = origin;
212 
213  // Set message
214  m_message = message;
215 
216  // Add status
217  if (status != 0)
218  m_message += " (status=" + gammalib::str(status) + ")";
219 
220  // Return
221  return;
222 }
223 
224 
225 /***********************************************************************//**
226  * @brief FITS error: No data
227  *
228  * @param[in] origin Method that throws the error.
229  * @param[in] message Error message.
230  * @param[in] status cfitsio status.
231  ***************************************************************************/
233  std::string message,
234  int status)
235 {
236  // Set origin
237  m_origin = origin;
238 
239  // Set message
240  m_message = message;
241 
242  // Add status
243  if (status != 0)
244  m_message += " (status=" + gammalib::str(status) + ")";
245 
246  // Return
247  return;
248 }
249 
250 
251 /***********************************************************************//**
252  * @brief FITS error: HDU not found in FITS file
253  *
254  * @param[in] origin Method that throws the error.
255  * @param[in] extname Name of the extension that was not found.
256  * @param[in] status cfitsio status.
257  ***************************************************************************/
259  std::string extname,
260  int status)
261 {
262  // Set origin
263  m_origin = origin;
264 
265  // Set message
266  m_message = "HDU \"" + extname + "\" not found in FITS file";
267 
268  // Add status
269  if (status != 0)
270  m_message += " (status=" + gammalib::str(status) + ")";
271 
272  // Return
273  return;
274 }
275 
276 
277 /***********************************************************************//**
278  * @brief FITS error: HDU not found in FITS file
279  *
280  * @param[in] origin Method that throws the error.
281  * @param[in] extno Number of the extension that was not found.
282  * @param[in] status cfitsio status.
283  ***************************************************************************/
285  int extno,
286  int status)
287 {
288  // Set origin
289  m_origin = origin;
290 
291  // Set message
292  m_message = "HDU number " + gammalib::str(extno) + " not found in FITS file";
293 
294  // Add status
295  if (status != 0)
296  m_message += " (status=" + gammalib::str(status) + ")";
297 
298  // Return
299  return;
300 }
301 
302 
303 /***********************************************************************//**
304  * @brief FITS error: HDU not an image
305  *
306  * @param[in] origin Method that throws the error.
307  * @param[in] extname Extension name.
308  * @param[in] type Specified HDU type.
309  ***************************************************************************/
311  std::string extname,
312  int type)
313 {
314  // Set origin
315  m_origin = origin;
316 
317  // Set message
318  m_message = "HDU \""+extname+"\" is not an image (type="+gammalib::str(type)+")";
319 
320  // Return
321  return;
322 }
323 
324 
325 /***********************************************************************//**
326  * @brief FITS error: HDU not a table
327  *
328  * @param[in] origin Method that throws the error.
329  * @param[in] extname Extension name.
330  * @param[in] type Specified HDU type.
331  ***************************************************************************/
333  std::string extname,
334  int type)
335 {
336  // Set origin
337  m_origin = origin;
338 
339  // Set message
340  m_message = "HDU \""+extname+"\" is not a table (type="+gammalib::str(type)+")";
341 
342  // Return
343  return;
344 }
345 
346 
347 /***********************************************************************//**
348  * @brief FITS error: HDU of unknown type found
349  *
350  * @param[in] origin Method that throws the error.
351  * @param[in] type Specified HDU type.
352  ***************************************************************************/
354  int type)
355 {
356  // Set origin
357  m_origin = origin;
358 
359  // Set message
360  m_message = "HDU type (type="+gammalib::str(type)+") is not defined";
361 
362  // Return
363  return;
364 }
365 
366 
367 /***********************************************************************//**
368  * @brief FITS error: Invalid type
369  *
370  * @param[in] origin Method that throws the error.
371  * @param[in] message Error message.
372  ***************************************************************************/
374  std::string message)
375 {
376  // Set origin
377  m_origin = origin;
378 
379  // Set message
380  m_message = message;
381 
382  // Return
383  return;
384 }
385 
386 
387 /***********************************************************************//**
388  * @brief FITS error: Table type is unknow
389  *
390  * @param[in] origin Method that throws the error.
391  * @param[in] type Specified table type.
392  ***************************************************************************/
394  int type)
395 {
396  // Set origin
397  m_origin = origin;
398 
399  // Set message
400  m_message = "Table type \"" + gammalib::str(type) + "\" is unknown";
401 
402  // Return
403  return;
404 }
405 
406 
407 /***********************************************************************//**
408  * @brief FITS error: Column type is unknown
409  *
410  * @param[in] origin Method that throws the error.
411  * @param[in] colname Column name.
412  * @param[in] type Specified column type.
413  ***************************************************************************/
415  std::string colname,
416  int type)
417 {
418  // Set origin
419  m_origin = origin;
420 
421  // Set message
422  m_message = "Column \""+colname+"\" has unsupported typecode="+gammalib::str(type);
423 
424  // Return
425  return;
426 }
427 
428 
429 /***********************************************************************//**
430  * @brief FITS error: Bad column length
431  *
432  * @param[in] origin Method that throws the error.
433  * @param[in] length Length of the column.
434  * @param[in] rows Number of rows in table.
435  ***************************************************************************/
437  int length,
438  int rows)
439 {
440  // Set origin
441  m_origin = origin;
442 
443  // Set message
444  m_message = "Column length (" + gammalib::str(length) + ") is not compatible"
445  " with the number of rows (" + gammalib::str(rows) + ") in the"
446  " table";
447 
448  // Return
449  return;
450 }
451 
452 
453 /***********************************************************************//**
454  * @brief FITS error: invalid number of bits per pixel
455  *
456  * @param[in] origin Method that throws the error.
457  * @param[in] bitpix Bitpix value that was not 8,16,32,64,-32, or -64.
458  ***************************************************************************/
460  int bitpix)
461 {
462  // Set origin
463  m_origin = origin;
464 
465  // Set message
466  m_message = "Invalid number of bits per pixel (bitpix="+gammalib::str(bitpix)+")";
467 
468  // Return
469  return;
470 }
471 
472 
473 /***********************************************************************//**
474  * @brief FITS error: wrong image operator has been used
475  *
476  * @param[in] origin Method that throws the error.
477  * @param[in] naxis Dimension of image.
478  * @param[in] nargs Number of arguments of the image operator.
479  ***************************************************************************/
481  int naxis,
482  int nargs)
483 {
484  // Set origin
485  m_origin = origin;
486 
487  // Set message
488  m_message = "Wrong image pixel access operator has been used (dimension=" +
489  gammalib::str(naxis) + " < arguments=" + gammalib::str(nargs) + ")";
490 
491  // Return
492  return;
493 }
494 
495 
496 /***********************************************************************//**
497  * @brief FITS error: Invalid row number
498  *
499  * @param[in] origin Method that throws the error.
500  * @param[in] row Row number.
501  * @param[in] nrows Number of rows in table.
502  * @param[in] message Error message.
503  ***************************************************************************/
505  int row,
506  int nrows,
507  std::string message)
508 {
509  // Set origin
510  m_origin = origin;
511 
512  // Set message
513  m_message = "Invalid row number "+gammalib::str(row)+" specified";
514  m_message += " (must be within [0,"+gammalib::str(nrows)+"]).";
515 
516  // Set optional message
517  if (message.length() > 0)
518  m_message += " "+message;
519 
520  // Return
521  return;
522 }
523 
524 
525 /***********************************************************************//**
526  * @brief FITS error: Invalid number of rows
527  *
528  * @param[in] origin Method that throws the error.
529  * @param[in] nrows Number of rows.
530  * @param[in] max_rows Maximum number of rows allowed.
531  * @param[in] message Error message.
532  ***************************************************************************/
534  int nrows,
535  int max_rows,
536  std::string message)
537 {
538  // Set origin
539  m_origin = origin;
540 
541  // Set message
542  m_message = "Invalid number of rows ("+gammalib::str(nrows)+") specified";
543  m_message += " (must be within [0,"+gammalib::str(max_rows)+"]).";
544 
545  // Set optional message
546  if (message.length() > 0)
547  m_message += " "+message;
548 
549  // Return
550  return;
551 }
552 
553 
554 /***********************************************************************//**
555  * @brief FITS error: Inconsistent TDIM information
556  *
557  * @param[in] origin Method that throws the error.
558  * @param[in] tdim Number of rows.
559  * @param[in] number Expected total number of elements.
560  * @param[in] message Error message.
561  ***************************************************************************/
563  std::vector<int> tdim,
564  int number,
565  std::string message)
566 {
567  // Set origin
568  m_origin = origin;
569 
570  // Set message
571  if (tdim.size() < 1) {
572  m_message = "Empty TDIM keyword encountered.";
573  }
574  else {
575  // Compute expectation
576  std::string sdim = "("+gammalib::str(tdim[0]);
577  int num = tdim[0];
578  for (int k = 1; k < tdim.size(); ++k) {
579  sdim += ","+gammalib::str(tdim[k]);
580  num *= tdim[k];
581  }
582  sdim += ")";
583 
584  // Set message
585  m_message = "TDIM keyword "+sdim+" predicts "+gammalib::str(num)+" column"
586  " elements, while there are "+gammalib::str(number)+" elements"
587  " in the column.";
588  }
589 
590  // Set optional message
591  if (message.length() > 0)
592  m_message += " "+message;
593 
594  // Return
595  return;
596 }
597 
598 
fits_already_opened(std::string origin, std::string filename)
FITS error: file already open.
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition: GTools.cpp:1099
fits_file_not_open(std::string origin, std::string filename)
FITS error: file not open.
fits_unknown_coltype(std::string origin, std::string colname, int type)
FITS error: Column type is unknown.
fits_hdu_not_image(std::string origin, std::string extname, int type)
FITS error: HDU not an image.
fits_hdu_not_found(std::string origin, std::string extname, int status=0)
FITS error: HDU not found in FITS file.
Gammalib tools definition.
fits_bad_bitpix(std::string origin, int bitpix)
FITS error: invalid number of bits per pixel.
fits_invalid_type(std::string origin, std::string message)
FITS error: Invalid type.
fits_invalid_nrows(std::string origin, int nrows, int max_rows, std::string message="")
FITS error: Invalid number of rows.
#define __ffgerr(A, B)
fits_column_not_found(std::string origin, std::string colname, int status=0)
FITS error: Table column not found.
fits_no_data(std::string origin, std::string message, int status=0)
FITS error: No data.
fits_inconsistent_tdim(std::string origin, std::vector< int > tdim, int number, std::string message="")
FITS error: Inconsistent TDIM information.
CFITSIO interface header.
fits_wrong_image_operator(std::string origin, int naxis, int nargs)
FITS error: wrong image operator has been used.
std::string m_message
Definition: GException.hpp:51
fits_hdu_not_table(std::string origin, std::string extname, int type)
FITS error: HDU not a table.
fits_file_exist(std::string origin, std::string filename, int status=0)
FITS error: attempted to overwrite FITS file.
fits_bad_col_length(std::string origin, int length, int rows)
FITS error: Bad column length.
fits_invalid_row(std::string origin, int row, int nrows, std::string message="")
FITS error: Invalid row number.
fits_no_header(std::string origin, std::string message, int status=0)
FITS error: No header.
fits_unknown_tabtype(std::string origin, int type)
FITS error: Table type is unknow.
Exception handler interface definition.
std::string m_origin
Definition: GException.hpp:50
fits_open_error(std::string origin, std::string filename, int status, std::string message="")
FITS error: unable to open FITS file.
fits_unknown_HDU_type(std::string origin, int type)
FITS error: HDU of unknown type found.
fits_error(const std::string &origin, const int &status, const std::string &message="")
General FITS error.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:457