GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GFitsImage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GFitsImage.cpp - Abstract FITS image base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2008-2017 by Juergen Knoedlseder *
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  * @file GFitsImage.cpp
23  * @brief Abstract FITS image base class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GFitsCfitsio.hpp"
33 #include "GFits.hpp"
34 #include "GFitsImage.hpp"
35 #include "GTools.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_NAXES "GFitsImage::naxes(int)"
39 #define G_OPEN_IMAGE "GFitsImage::open(void*)"
40 #define G_LOAD_IMAGE "GFitsImage::load_image(int,void*,void*,int*)"
41 #define G_SAVE_IMAGE "GFitsImage::save_image(int,void*)"
42 #define G_OFFSET_1D "GFitsImage::offset(int&)"
43 #define G_OFFSET_2D "GFitsImage::offset(int&,int&)"
44 #define G_OFFSET_3D "GFitsImage::offset(int&,int&,int&)"
45 #define G_OFFSET_4D "GFitsImage::offset(int&,int&,int&,int&)"
46 
47 /* __ Macros _____________________________________________________________ */
48 
49 /* __ Coding definitions _________________________________________________ */
50 
51 /* __ Debug definitions __________________________________________________ */
52 
53 
54 /*==========================================================================
55  = =
56  = Constructors/destructors =
57  = =
58  ==========================================================================*/
59 
60 /***********************************************************************//**
61  * @brief Void constructor
62  *
63  * Construct instance of an empty image. No header cards are present in an
64  * empty image.
65  ***************************************************************************/
67 {
68  // Initialise class members for clean destruction
69  init_members();
70 
71  // Return
72  return;
73 }
74 
75 
76 /***********************************************************************//**
77  * @brief 1D image constructor
78  *
79  * @param[in] bitpix Number of Bits per pixel (negative is floating point).
80  * @param[in] nx Number of pixels.
81  *
82  * Construct 1D instance of GFitsImage by specifying the number of pixels.
83  * This method also adds the relevant header cards.
84  ***************************************************************************/
85 GFitsImage::GFitsImage(const int& bitpix, const int& nx) : GFitsHDU()
86 {
87  // Initialise class members for clean destruction
88  init_members();
89 
90  // Store number of Bits per pixel
91  m_bitpix = bitpix;
92 
93  // Set number of axes
94  m_naxis = 1;
95 
96  // Set image dimensions
97  m_naxes = new long[m_naxis];
98  m_naxes[0] = nx;
99  m_num_pixels = nx;
100 
101  // Initialise header
103 
104  // Return
105  return;
106 }
107 
108 
109 /***********************************************************************//**
110  * @brief 2D image constructor
111  *
112  * @param[in] bitpix Number of Bits per pixel (negative is floating point).
113  * @param[in] nx Number of pixels in first dimension.
114  * @param[in] ny Number of pixels in second dimension.
115  *
116  * Construct 2D instance of GFitsImage by specifying the number of pixels
117  * in each dimension. This method also adds the relevant header cards.
118  ***************************************************************************/
119 GFitsImage::GFitsImage(const int& bitpix, const int& nx, const int& ny) :
120  GFitsHDU()
121 {
122  // Initialise class members for clean destruction
123  init_members();
124 
125  // Store number of Bits per pixel
126  m_bitpix = bitpix;
127 
128  // Set number of axes
129  m_naxis = 2;
130 
131  // Set image dimensions
132  m_naxes = new long[m_naxis];
133  m_naxes[0] = nx;
134  m_naxes[1] = ny;
135  m_num_pixels = nx * ny;
136 
137  // Initialise header
139 
140  // Return
141  return;
142 }
143 
144 
145 /***********************************************************************//**
146  * @brief 3D image constructor
147  *
148  * @param[in] bitpix Number of Bits per pixel (negative is floating point).
149  * @param[in] nx Number of pixels in first dimension.
150  * @param[in] ny Number of pixels in second dimension.
151  * @param[in] nz Number of pixels in third dimension.
152  *
153  * Construct 3D instance of GFitsImage by specifying the number of pixels
154  * in each dimension. This method also adds the relevant header cards.
155  ***************************************************************************/
156 GFitsImage::GFitsImage(const int& bitpix, const int& nx, const int& ny,
157  const int& nz) : GFitsHDU()
158 {
159  // Initialise class members for clean destruction
160  init_members();
161 
162  // Store number of Bits per pixel
163  m_bitpix = bitpix;
164 
165  // Set number of axes
166  m_naxis = 3;
167 
168  // Set image dimensions
169  m_naxes = new long[m_naxis];
170  m_naxes[0] = nx;
171  m_naxes[1] = ny;
172  m_naxes[2] = nz;
173  m_num_pixels = nx * ny * nz;
174 
175  // Initialise header
177 
178  // Return
179  return;
180 }
181 
182 
183 /***********************************************************************//**
184  * @brief 4D image constructor
185  *
186  * @param[in] bitpix Number of Bits per pixel (negative is floating point).
187  * @param[in] nx Number of pixels in first dimension.
188  * @param[in] ny Number of pixels in second dimension.
189  * @param[in] nz Number of pixels in third dimension.
190  * @param[in] nt Number of pixels in forth dimension.
191  *
192  * Construct 4D instance of GFitsImage by specifying the number of pixels
193  * in each dimension. This method also adds the relevant header cards.
194  ***************************************************************************/
195 GFitsImage::GFitsImage(const int& bitpix, const int& nx, const int& ny,
196  const int& nz, const int& nt) : GFitsHDU()
197 {
198  // Initialise class members for clean destruction
199  init_members();
200 
201  // Store number of Bits per pixel
202  m_bitpix = bitpix;
203 
204  // Set number of axes
205  m_naxis = 4;
206 
207  // Set image dimensions
208  m_naxes = new long[m_naxis];
209  m_naxes[0] = nx;
210  m_naxes[1] = ny;
211  m_naxes[2] = nz;
212  m_naxes[3] = nt;
213  m_num_pixels = nx * ny * nz * nt;
214 
215  // Initialise header
217 
218  // Return
219  return;
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Constructor
225  *
226  * @param[in] bitpix Number of Bits per pixel (negative is floating point).
227  * @param[in] naxes Vector of number of pixels in each dimension.
228  *
229  * Construct instance of GFitsImage by specifying the image dimension and
230  * the number of pixels in each dimension. This method also adds the relevant
231  * header cards.
232  ***************************************************************************/
233 GFitsImage::GFitsImage(const int& bitpix, const std::vector<int>& naxes) : GFitsHDU()
234 {
235  // Initialise class members for clean destruction
236  init_members();
237 
238  // Store number of Bits per pixel
239  m_bitpix = bitpix;
240 
241  // Store number of axes
242  m_naxis = naxes.size();
243 
244  // Copy number of pixels in each dimension and calculate the total
245  // number of pixels
246  if (m_naxis > 0) {
247  m_naxes = new long[m_naxis];
248  m_num_pixels = 1;
249  for (int i = 0; i < m_naxis; ++i) {
250  m_naxes[i] = naxes[i];
251  m_num_pixels *= naxes[i];
252  }
253  }
254 
255  // Initialise header
257 
258  // Return
259  return;
260 }
261 
262 
263 /***********************************************************************//**
264  * @brief Copy constructor
265  *
266  * @param[in] image FITS image.
267  ***************************************************************************/
269 {
270  // Initialise class members for clean destruction
271  init_members();
272 
273  // Copy members
274  copy_members(image);
275 
276  // Return
277  return;
278 }
279 
280 
281 /***********************************************************************//**
282  * @brief Destructor
283  ***************************************************************************/
285 {
286  // Free members
287  free_members();
288 
289  // Return
290  return;
291 }
292 
293 
294 /*==========================================================================
295  = =
296  = Operators =
297  = =
298  ==========================================================================*/
299 
300 /***********************************************************************//**
301  * @brief Assignment operator
302  *
303  * @param[in] image FITS image.
304  * @return FITS image.
305  ***************************************************************************/
307 {
308  // Execute only if object is not identical
309  if (this != &image) {
310 
311  // Copy base class members
312  this->GFitsHDU::operator=(image);
313 
314  // Free members
315  free_members();
316 
317  // Initialise private members for clean destruction
318  init_members();
319 
320  // Copy members
321  copy_members(image);
322 
323  } // endif: object was not identical
324 
325  // Return this object
326  return *this;
327 }
328 
329 
330 /*==========================================================================
331  = =
332  = Public methods =
333  = =
334  ==========================================================================*/
335 
336 /***********************************************************************//**
337  * @brief Return dimension of an image axis
338  *
339  * @param[in] axis Image axis [0,...,naxis()-1].
340  *
341  * @exception GException::out_of_range
342  * Image axis not valid.
343  ***************************************************************************/
344 int GFitsImage::naxes(const int& axis) const
345 {
346  // Check if axis is within the range
347  #if defined(G_RANGE_CHECK)
348  if (axis < 0 || axis >= naxis()) {
349  throw GException::out_of_range(G_NAXES, "Image axis", axis, naxis());
350  }
351  #endif
352 
353  // Get axis dimension
354  int dim = m_naxes[axis];
355 
356  // Return axis dimension
357  return dim;
358 }
359 
360 
361 /***********************************************************************//**
362  * @brief Set nul value
363  *
364  * @param[in] value Nul value.
365  *
366  * @todo To correctly reflect the nul value in the data, the image should
367  * be reloaded. However, the image may have been changed, so in principle
368  * saving is needed. However, we may not want to store the image, hence saving
369  * is also not desired. We thus have to develop a method to update the
370  * image information for a new nul value in place ...
371  ***************************************************************************/
372 void GFitsImage::nulval(const void* value)
373 {
374  // Allocate nul value
375  alloc_nulval(value);
376 
377  // Update image
378  //TODO
379 
380  // Return
381  return;
382 }
383 
384 
385 /***********************************************************************//**
386  * @brief Print column information
387  *
388  * @param[in] chatter Chattiness.
389  * @return String containing column information.
390  *
391  * @todo Format and cfitsio information is mainly for debugging. This could
392  * be vanish in a more stable version of the code, or it could be compiled
393  * in conditionally using a debug option.
394  ***************************************************************************/
395 std::string GFitsImage::print(const GChatter& chatter) const
396 {
397  // Initialise result string
398  std::string result;
399 
400  // Continue only if chatter is not silent
401  if (chatter != SILENT) {
402 
403  // Append header
404  result.append("=== GFitsImage ===");
405 
406  // Append HDU information
407  result.append("\n"+print_hdu(chatter));
408 
409  // Append image dimensions
410  result.append("\n"+gammalib::parformat("Image type"));
411  result.append(typecode(type()));
412  result.append("\n"+gammalib::parformat("Number of dimensions"));
413  result.append(gammalib::str(naxis()));
414  result.append("\n"+gammalib::parformat("Number of image pixels"));
415  result.append(gammalib::str(npix()));
416  for (int i = 0; i < naxis(); ++i) {
417  result.append("\n"+gammalib::parformat("Number of bins in "+gammalib::str(i)));
418  result.append(gammalib::str(naxes(i)));
419  }
420 
421  // NORMAL: Append header information
422  if (chatter >= NORMAL) {
423  result.append(+"\n"+m_header.print(gammalib::reduce(chatter)));
424  }
425 
426  } // endif: chatter was not silent
427 
428  // Return result
429  return result;
430 }
431 
432 
433 /*==========================================================================
434  = =
435  = Protected methods =
436  = =
437  ==========================================================================*/
438 
439 /***********************************************************************//**
440  * @brief Initialise class members
441  ***************************************************************************/
443 {
444  // Initialise members
445  m_bitpix = 8;
446  m_naxis = 0;
447  m_naxes = NULL;
448  m_num_pixels = 0;
449  m_anynul = 0;
450 
451  // Return
452  return;
453 }
454 
455 
456 /***********************************************************************//**
457  * @brief Copy class members
458  *
459  * @param[in] image FITS image to copy
460  ***************************************************************************/
462 {
463  // Copy attributes
464  m_bitpix = image.m_bitpix;
465  m_naxis = image.m_naxis;
466  m_num_pixels = image.m_num_pixels;
467  m_anynul = image.m_anynul;
468 
469  // Copy axes
470  m_naxes = NULL;
471  if (image.m_naxes != NULL && m_naxis > 0) {
472  m_naxes = new long[m_naxis];
473  for (int i = 0; i < m_naxis; ++i) {
474  m_naxes[i] = image.m_naxes[i];
475  }
476  }
477 
478  // Return
479  return;
480 }
481 
482 
483 /***********************************************************************//**
484  * @brief Delete class members
485  ***************************************************************************/
487 {
488  // Free memory
489  if (m_naxes != NULL) delete [] m_naxes;
490 
491  // Mark memory as free
492  m_naxes = NULL;
493 
494  // Return
495  return;
496 }
497 
498 
499 /***********************************************************************//**
500  * @brief Open FITS image
501  *
502  * @param[in] vptr FITS file pointer
503  *
504  * Open FITS image in FITS file. Opening means connecting the FITS file
505  * pointer to the image and reading the image and axes dimensions.
506  ***************************************************************************/
507 void GFitsImage::data_open(void* vptr)
508 {
509  // Open image
510  open_image(vptr);
511 
512  // Return
513  return;
514 }
515 
516 
517 /***********************************************************************//**
518  * @brief Save FITS image
519  *
520  * Saves the image into the FITS file.
521  ***************************************************************************/
523 {
524  // Save image
525  save_image(type(), pixels());
526 
527  // Return
528  return;
529 }
530 
531 
532 /***********************************************************************//**
533  * @brief Close FITS image
534  *
535  * Closing a FITS image resets the object into its initial state. Closing
536  * does NOT save the image into the FITS file. Use the save method for this
537  * purpose.
538  *
539  * @todo Not sure that this is efficient at this level since the pixel array
540  * will not be deallocated!!!
541  ***************************************************************************/
543 {
544  // Free members
545  free_members();
546 
547  // Initialise members
548  init_members();
549 
550  // Return
551  return;
552 }
553 
554 
555 /***********************************************************************//**
556  * @brief Connect FITS image
557  *
558  * @param[in] vptr FITS file pointer
559  *
560  * Connects a FITS file pointer to an image. This method actually does
561  * nothing since any GFitsImage can directly access the FITS file pointer
562  * that is stored in the GFitsHDU base class.
563  ***************************************************************************/
564 void GFitsImage::data_connect(void* vptr)
565 {
566  // Return
567  return;
568 }
569 
570 
571 /***********************************************************************//**
572  * @brief Initialise image header
573  *
574  * Initialises the image header by setting the default header cards. This
575  * method requires the members m_bitpix, m_naxis, and m_naxes to be set
576  * previously.
577  ***************************************************************************/
579 {
580  // Set image header keywords
581  m_header.append(GFitsHeaderCard("XTENSION", "IMAGE ",
582  "IMAGE extension"));
584  "number of bits per data pixel"));
586  "number of data axes"));
587  for (int i = 0; i < naxis(); ++i) {
588  std::ostringstream s_key;
589  std::ostringstream s_comment;
590  s_key << "NAXIS" << (i+1);
591  s_comment << "length of data axis " << (i+1);
592  m_header.append(GFitsHeaderCard(s_key.str(), naxes(i),
593  s_comment.str()));
594  }
595  m_header.append(GFitsHeaderCard("PCOUNT", 0,
596  "required keyword; must = 0"));
597  m_header.append(GFitsHeaderCard("GCOUNT", 1,
598  "required keyword; must = 1"));
599 
600  // Return
601  return;
602 }
603 
604 
605 /***********************************************************************//**
606  * @brief Open Image
607  *
608  * @param[in] vptr FITS file void pointer.
609  *
610  * @exception GException::fits_error
611  * FITS error.
612  *
613  * Open FITS image in FITS file. Opening means connecting the FITS file
614  * pointer to the image and reading the image and axes dimensions.
615  ***************************************************************************/
616 void GFitsImage::open_image(void* vptr)
617 {
618  // Move to HDU
620 
621  // Save the FITS file pointer and the HDU number
622  FPTR_COPY(m_fitsfile, vptr);
623  m_hdunum = FPTR(vptr)->HDUposition;
624 
625  // Get the image dimensions
626  int status = 0;
627  status = __ffgidm(FPTR(m_fitsfile), &m_naxis, &status);
628  if (status != 0) {
629  throw GException::fits_error(G_OPEN_IMAGE, status);
630  }
631 
632  // Reset number of image pixels
633  m_num_pixels = 0;
634 
635  // Get the axes dimensions
636  if (m_naxis > 0) {
637 
638  // Allocate memory for axes dimensions
639  if (m_naxes != NULL) delete [] m_naxes;
640  m_naxes = new long[m_naxis];
641 
642  // Get the axes dimensions
643  status = __ffgisz(FPTR(m_fitsfile), m_naxis, m_naxes, &status);
644  if (status != 0) {
645  throw GException::fits_error(G_OPEN_IMAGE, status);
646  }
647 
648  // Calculate number of image pixels
649  m_num_pixels = 1;
650  for (int i = 0; i < m_naxis; ++i) {
651  m_num_pixels *= m_naxes[i];
652  }
653 
654  } // endif: there is an image
655 
656  // Return
657  return;
658 }
659 
660 
661 /***********************************************************************//**
662  * @brief Load FITS image
663  *
664  * @param[in] datatype Datatype of pixels to be saved.
665  * @param[in] pixels Pixel array to be saved.
666  * @param[in] nulval Pointer to pixel nul value.
667  * @param[out] anynul Number of nul values encountered during loading.
668  *
669  * @exception GException::fits_error
670  * FITS error.
671  *
672  * Load image pixels from FITS file.
673  ***************************************************************************/
674 void GFitsImage::load_image(int datatype, const void* pixels,
675  const void* nulval, int* anynul)
676 {
677  // Move to HDU
678  move_to_hdu();
679 
680  // Load the image pixels (if there are some ...)
681  if (m_naxis > 0) {
682  long* fpixel = new long[m_naxis];
683  long* lpixel = new long[m_naxis];
684  long* inc = new long[m_naxis];
685  for (int i = 0; i < m_naxis; ++i) {
686  fpixel[i] = 1;
687  lpixel[i] = m_naxes[i];
688  inc[i] = 1;
689  }
690  int status = 0;
691  status = __ffgsv(FPTR(m_fitsfile), datatype, fpixel, lpixel, inc,
692  (void*)nulval, (void*)pixels, anynul, &status);
693  delete [] fpixel;
694  delete [] lpixel;
695  delete [] inc;
696  if (status != 0) {
697  throw GException::fits_error(G_LOAD_IMAGE, status);
698  }
699  }
700 
701  // Return
702  return;
703 }
704 
705 
706 /***********************************************************************//**
707  * @brief Save FITS image
708  *
709  * @param[in] datatype Datatype of pixels to be saved
710  * @param[in] pixels Pixel array to be saved
711  *
712  * @exception GException::fits_file_not_open
713  * No FITS file has been opened.
714  * @exception GException::fits_error
715  * FITS error.
716  *
717  * Save image pixels into FITS file. In case that the HDU does not exist it
718  * is created. In case that the pixel array is empty no data are saved; all
719  * image pixels will be empty in this case.
720  ***************************************************************************/
721 void GFitsImage::save_image(int datatype, const void* pixels)
722 {
723  // Throw an exception if FITS file is not open
724  if (FPTR(m_fitsfile)->Fptr == NULL) {
726  "Open file before saving the image.");
727  }
728 
729  // Move to HDU. We use here an explicit cfitsio moveto function since we
730  // want to recover the error code ...
731  int status = 0;
732  int type = 0;
733  status = __ffmahd(FPTR(m_fitsfile), m_hdunum+1, &type, &status);
734 
735  // If move was successful but HDU type in file differs from HDU type
736  // of object then replace the HDU in the file
737  if (status == 0 && type != exttype()) {
738  status = __ffdhdu(FPTR(m_fitsfile), NULL, &status);
739  if (status != 0) {
740  throw GException::fits_error(G_SAVE_IMAGE, status);
741  }
742  status = __ffiimg(FPTR(m_fitsfile), m_bitpix, m_naxis, m_naxes, &status);
743  //status = __ffiimgll(FPTR(m_fitsfile), m_bitpix, m_naxis, m_naxes, &status);
744  if (status != 0) {
745  throw GException::fits_error(G_SAVE_IMAGE, status);
746  }
747  }
748 
749  // If HDU does not yet exist in file then create it now
750  if (status == 107) {
751  status = 0;
752  status = __ffcrim(FPTR(m_fitsfile), m_bitpix, m_naxis, m_naxes, &status);
753  if (status != 0) {
754  throw GException::fits_error(G_SAVE_IMAGE, status);
755  }
756  }
757  else if (status != 0) {
758  throw GException::fits_error(G_SAVE_IMAGE, status);
759  }
760 
761  // If HDU seems to be empty then create it now. This is only needed for the
762  // primary HDU, since __ffmahd gives no error if the primary HDU is empty.
763  // By checking the number of keywords in the HDU we detect an empty HDU ...
764  int num = 0;
765  status = __ffghsp(FPTR(m_fitsfile), &num, NULL, &status);
766  if (status != 0) {
767  throw GException::fits_error(G_SAVE_IMAGE, status);
768  }
769  if (num == 0) {
770  status = __ffcrim(FPTR(m_fitsfile), m_bitpix, m_naxis, m_naxes, &status);
771  if (status != 0) {
772  throw GException::fits_error(G_SAVE_IMAGE, status);
773  }
774  }
775 
776  // Make sure that the image on disk has the right size by resizing it
777  status = __ffrsim(FPTR(m_fitsfile), m_bitpix, m_naxis, m_naxes, &status);
778  if (status != 0) {
779  throw GException::fits_error(G_SAVE_IMAGE, status);
780  }
781 
782  // Save the image pixels (if there are some ...)
783  if (m_naxis > 0 && pixels != NULL) {
784  long* fpixel = new long[m_naxis];
785  long* lpixel = new long[m_naxis];
786  for (int i = 0; i < m_naxis; ++i) {
787  fpixel[i] = 1;
788  lpixel[i] = m_naxes[i];
789  }
790  status = __ffpss(FPTR(m_fitsfile), datatype, fpixel, lpixel,
791  (void*)pixels, &status);
792  delete [] fpixel;
793  delete [] lpixel;
794  if (status != 0) {
795  throw GException::fits_error(G_SAVE_IMAGE, status);
796  }
797  }
798 
799  // Return
800  return;
801 }
802 
803 
804 /***********************************************************************//**
805  * @brief Fetch image pixels
806  *
807  * Fetch the image pixels. This function is in general called if pixel
808  * values should be read or written yet no pixel array is allocated. In case
809  * that pixels existed already before they will be deleted before fetching
810  * new ones.
811  * There are two possibilities to fetch the pixels:
812  * (1) In case that a FITS file is attached to the image, the pixel array
813  * will be loaded from the FITS file using the load_image() method.
814  * (2) In case that no FITS file is attached, a new pixel array will be
815  * allocated that is initalised to zero.
816  ***************************************************************************/
818 {
819  // Fetch only if there are pixels in image
820  if (m_num_pixels > 0) {
821 
822  // Allocate and initialise fresh memory
823  alloc_data();
824  init_data();
825 
826  // If a FITS file is attached then load pixels from FITS file.
827  if (FPTR(m_fitsfile)->Fptr != NULL) {
829  }
830 
831  } // endif: there were pixels available
832 
833  // Return
834  return;
835 }
836 
837 
838 /***********************************************************************//**
839  * @brief Return pixel offset
840  *
841  * @param[in] ix Pixel index (starting from 0).
842  *
843  * @exception GException::out_of_range
844  * Pixel index is outside valid range.
845  ***************************************************************************/
846 int GFitsImage::offset(const int& ix) const
847 {
848  // Check if axis is within the range
849  #if defined(G_RANGE_CHECK)
850  if (ix < 0 || ix >= m_num_pixels) {
852  }
853  #endif
854 
855  // Return index
856  return ix;
857 }
858 
859 
860 /***********************************************************************//**
861  * @brief Return 2D pixel offset
862  *
863  * @param[in] ix Pixel index in first dimension (starting from 0).
864  * @param[in] iy Pixel index in second dimension (starting from 0).
865  *
866  * @exception GException::fits_wrong_image_operator
867  * Pixel array has less than 2 dimensions.
868  * @exception GException::out_of_range
869  * Image axis not valid.
870  *
871  * Computes of offset in the pixel array of a 2D coordinate. This method is
872  * only applicable to pixels arrays that have at least 2 dimenions.
873  ***************************************************************************/
874 int GFitsImage::offset(const int& ix, const int& iy) const
875 {
876  // Operator is only valid for 2D images
877  if (m_naxis < 2) {
879  }
880 
881  // Check if axis is within the range
882  #if defined(G_RANGE_CHECK)
883  if (ix < 0 || ix >= m_naxes[0]) {
884  throw GException::out_of_range(G_OFFSET_2D, ix, 0, m_naxes[0]-1);
885  }
886  if (iy < 0 || iy >= m_naxes[1]) {
887  throw GException::out_of_range(G_OFFSET_2D, iy, 0, m_naxes[1]-1);
888  }
889  #endif
890 
891  // Return offset
892  return (ix + iy * m_naxes[0]);
893 }
894 
895 
896 /***********************************************************************//**
897  * @brief Return 3D pixel offset
898  *
899  * @param[in] ix Pixel index in first dimension (starting from 0).
900  * @param[in] iy Pixel index in second dimension (starting from 0).
901  * @param[in] iz Pixel index in third dimension (starting from 0).
902  *
903  * @exception GException::fits_wrong_image_operator
904  * Pixel array has less than 3 dimensions.
905  * @exception GException::out_of_range
906  * Image axis not valid.
907  *
908  * Computes of offset in the pixel array of a 3D coordinate. This method is
909  * only applicable to pixels arrays that have at least 3 dimenions.
910  ***************************************************************************/
911 int GFitsImage::offset(const int& ix, const int& iy, const int& iz) const
912 {
913  // Operator is only valid for 3D images
914  if (m_naxis < 3) {
916  }
917 
918  // Check if axis is within the range
919  #if defined(G_RANGE_CHECK)
920  if (ix < 0 || ix >= m_naxes[0]) {
921  throw GException::out_of_range(G_OFFSET_3D, ix, 0, m_naxes[0]-1);
922  }
923  if (iy < 0 || iy >= m_naxes[1]) {
924  throw GException::out_of_range(G_OFFSET_3D, iy, 0, m_naxes[1]-1);
925  }
926  if (iz < 0 || iz >= m_naxes[2]) {
927  throw GException::out_of_range(G_OFFSET_3D, iz, 0, m_naxes[2]-1);
928  }
929  #endif
930 
931  // Return offset
932  return (ix + m_naxes[0] * (iy + iz * m_naxes[1]));
933 }
934 
935 
936 /***********************************************************************//**
937  * @brief Return 4D pixel offset
938  *
939  * @param[in] ix Pixel index in first dimension (starting from 0).
940  * @param[in] iy Pixel index in second dimension (starting from 0).
941  * @param[in] iz Pixel index in third dimension (starting from 0).
942  * @param[in] it Pixel index in forth dimension (starting from 0).
943  *
944  * @exception GException::fits_wrong_image_operator
945  * Pixel array has less than 4 dimensions.
946  * @exception GException::out_of_range
947  * Image axis not valid.
948  *
949  * Computes of offset in the pixel array of a 4D coordinate. This method is
950  * only applicable to pixels arrays that have at least 4 dimenions.
951  ***************************************************************************/
952 int GFitsImage::offset(const int& ix, const int& iy, const int& iz,
953  const int& it) const
954 {
955  // Operator is only valid for 4D images
956  if (m_naxis < 4) {
958  }
959 
960  // Check if axis is within the range
961  #if defined(G_RANGE_CHECK)
962  if (ix < 0 || ix >= m_naxes[0]) {
963  throw GException::out_of_range(G_OFFSET_4D, ix, 0, m_naxes[0]-1);
964  }
965  if (iy < 0 || iy >= m_naxes[1]) {
966  throw GException::out_of_range(G_OFFSET_4D, iy, 0, m_naxes[1]-1);
967  }
968  if (iz < 0 || iz >= m_naxes[2]) {
969  throw GException::out_of_range(G_OFFSET_4D, iz, 0, m_naxes[2]-1);
970  }
971  if (it < 0 || it >= m_naxes[3]) {
972  throw GException::out_of_range(G_OFFSET_4D, it, 0, m_naxes[3]-1);
973  }
974  #endif
975 
976  // Return offset
977  return (ix + m_naxes[0] * (iy + m_naxes[1] * (iz + it * m_naxes[2])));
978 }
const int & bitpix(void) const
Return number of Bits per pixel (negative=floating point)
Definition: GFitsImage.hpp:146
#define __ffghsp(A, B, C, D)
Abstract FITS image base class.
Definition: GFitsImage.hpp:43
#define __ffdhdu(A, B, C)
int offset(const int &ix) const
Return pixel offset.
Definition: GFitsImage.cpp:846
const int & naxis(void) const
Return dimension of image.
Definition: GFitsImage.hpp:156
#define G_SAVE_IMAGE
Definition: GFitsImage.cpp:41
#define FPTR(A)
void open_image(void *vptr)
Open Image.
Definition: GFitsImage.cpp:616
std::string print_hdu(const GChatter &chatter=NORMAL) const
Print basic HDU information.
Definition: GFitsHDU.cpp:371
#define __ffpss(A, B, C, D, E, F)
Abstract FITS extension base class.
Definition: GFitsHDU.hpp:51
void data_close(void)
Close FITS image.
Definition: GFitsImage.cpp:542
void move_to_hdu(void)
Move FITS file pointer to HDU.
Definition: GFitsHDU.cpp:222
void data_open(void *vptr)
Open FITS image.
Definition: GFitsImage.cpp:507
virtual void * ptr_nulval(void)=0
virtual void init_data(void)=0
void * m_fitsfile
FITS file pointer pointing on actual HDU.
Definition: GFitsHDU.hpp:133
#define __ffiimg(A, B, C, D, E)
Gammalib tools definition.
#define __ffcrim(A, B, C, D, E)
FITS file class interface definition.
virtual void alloc_nulval(const void *value)=0
virtual void alloc_data(void)=0
Abstract FITS image base class definition.
GFitsHDU & operator=(const GFitsHDU &hdu)
Assignment operator.
Definition: GFitsHDU.cpp:124
int m_num_pixels
Number of image pixels.
Definition: GFitsImage.hpp:115
void save_image(int datatype, const void *pixels)
Save FITS image.
Definition: GFitsImage.cpp:721
void data_save(void)
Save FITS image.
Definition: GFitsImage.cpp:522
#define G_OPEN_IMAGE
Definition: GFitsImage.cpp:39
Implements FITS header card interface.
#define G_NAXES
Definition: GFitsImage.cpp:38
#define __ffrsim(A, B, C, D, E)
void init_image_header(void)
Initialise image header.
Definition: GFitsImage.cpp:578
#define G_OFFSET_4D
Definition: GFitsImage.cpp:45
long * m_naxes
Number of pixels in each dimension.
Definition: GFitsImage.hpp:114
#define __ffmahd(A, B, C, D)
GFitsHeaderCard & append(const GFitsHeaderCard &card)
Append or update header card.
virtual void * pixels(void)=0
CFITSIO interface header.
virtual int type(void) const =0
void data_connect(void *vptr)
Connect FITS image.
Definition: GFitsImage.cpp:564
const void * nulval(void) const
Return nul value.
Definition: GFitsImage.hpp:176
GFitsImage(void)
Void constructor.
Definition: GFitsImage.cpp:66
#define G_OFFSET_3D
Definition: GFitsImage.cpp:44
#define __ffgsv(A, B, C, D, E, F, G, H, I)
std::string print(const GChatter &chatter=NORMAL) const
Print FITS header information.
GChatter
Definition: GTypemaps.hpp:33
#define __ffgisz(A, B, C, D)
int m_anynul
Number of NULLs encountered.
Definition: GFitsImage.hpp:116
void fetch_data(void)
Fetch image pixels.
Definition: GFitsImage.cpp:817
GFitsHeader m_header
HDU header.
Definition: GFitsHDU.hpp:136
void init_members(void)
Initialise class members.
Definition: GFitsImage.cpp:442
#define FPTR_COPY(A, B)
int m_naxis
Image dimension.
Definition: GFitsImage.hpp:113
#define G_OFFSET_2D
Definition: GFitsImage.cpp:43
int naxes(const int &axis) const
Return dimension of an image axis.
Definition: GFitsImage.cpp:344
std::string print(const GChatter &chatter=NORMAL) const
Print column information.
Definition: GFitsImage.cpp:395
#define G_LOAD_IMAGE
Definition: GFitsImage.cpp:40
std::string typecode(int type) const
Return typecode as string.
Definition: GFitsHDU.cpp:396
virtual ~GFitsImage(void)
Destructor.
Definition: GFitsImage.cpp:284
const int & npix(void) const
Return size of pixel array.
Definition: GFitsImage.hpp:136
#define G_OFFSET_1D
Definition: GFitsImage.cpp:42
GChatter reduce(const GChatter &chatter)
Reduce chattiness by one level.
Definition: GTypemaps.hpp:65
void copy_members(const GFitsImage &image)
Copy class members.
Definition: GFitsImage.cpp:461
int m_bitpix
Number of Bits/pixel.
Definition: GFitsImage.hpp:112
#define __ffgidm(A, B, C)
Exception handler interface definition.
void load_image(int datatype, const void *pixels, const void *nulval, int *anynul)
Load FITS image.
Definition: GFitsImage.cpp:674
GFitsImage & operator=(const GFitsImage &image)
Assignment operator.
Definition: GFitsImage.cpp:306
int fits_move_to_hdu(const std::string &caller, void *vptr, const int &hdunum=0)
Move to FITS extension.
Definition: GFits.cpp:1745
virtual void * ptr_data(void)=0
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
HDUType exttype(void) const
Return extension type.
Definition: GFitsImage.hpp:126
int m_hdunum
HDU number (starting from 0)
Definition: GFitsHDU.hpp:134
void free_members(void)
Delete class members.
Definition: GFitsImage.cpp:486
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413