Commit ec9cf37f2b75219d8488fa2d0eba9f34140139b5

Authored by knodlseder
1 parent c6fd4023

implement image saving

include/GFitsCfitsio.hpp
... ... @@ -47,12 +47,16 @@
47 47 #define __ffgtcl(A, B, C, D, E, F) ffgtcl(A, B, C, D, E, F)
48 48 #define __ffgcv(A, B, C, D, E, F, G, H, I, J) ffgcv(A, B, C, D, E, F, G, H, I, J)
49 49 #define __ffgcvs(A, B, C, D, E, F, G, H, I) ffgcvs(A, B, C, D, E, F, G, H, I)
50   -#define __TLOGICAL TLOGICAL
51   -#define __TSTRING TSTRING
52   -#define __TSHORT TSHORT
53   -#define __TLONG TLONG
54   -#define __TFLOAT TFLOAT
55   -#define __TDOUBLE TDOUBLE
  50 +#define __ffpss(A, B, C, D, E, F) ffpss(A, B, C, D, E, F)
  51 +#define __TBIT TBIT
  52 +#define __TBYTE TBYTE
  53 +#define __TLOGICAL TLOGICAL
  54 +#define __TSTRING TSTRING
  55 +#define __TSHORT TSHORT
  56 +#define __TLONG TLONG
  57 +#define __TLONGLONG TLONGLONG
  58 +#define __TFLOAT TFLOAT
  59 +#define __TDOUBLE TDOUBLE
56 60  
57 61 /* __ Type definition ____________________________________________________ */
58 62 typedef fitsfile __fitsfile;
... ... @@ -81,8 +85,9 @@ typedef fitsfile __fitsfile;
81 85 #define __ffgtcl(A, B, C, D, E, F) __dummy()
82 86 #define __ffgcv(A, B, C, D, E, F, G, H, I, J) __dummy()
83 87 #define __ffgcvs(A, B, C, D, E, F, G, H, I) __dummy()
  88 +#define __ffpss(A, B, C, D, E, F) __dummy()
84 89 #define __TBIT 1
85   -#define __TSHORT 11
  90 +#define __TBYTE 11
86 91 #define __TLOGICAL 14
87 92 #define __TSTRING 16
88 93 #define __TSHORT 21
... ...
include/GFitsImage.hpp
... ... @@ -40,6 +40,7 @@ class GFitsImage : public GFitsData {
40 40 public:
41 41 // Constructors and destructors
42 42 GFitsImage();
  43 + GFitsImage(int naxis, const int* naxes, int bitpix = -64);
43 44 GFitsImage(const GFitsImage& image);
44 45 ~GFitsImage();
45 46  
... ... @@ -50,6 +51,7 @@ public:
50 51 void open(__fitsfile* fptr);
51 52 void save(void);
52 53 void close(void);
  54 + void link(const double* pixels);
53 55 GFitsImage* clone(void) const;
54 56  
55 57 private:
... ... @@ -60,16 +62,26 @@ private:
60 62 void connect(__fitsfile* fptr);
61 63  
62 64 // Private data area
63   - __fitsfile m_fitsfile;
64   - int m_bitpix;
65   - int m_naxis;
66   - long* m_naxes;
  65 + __fitsfile m_fitsfile; // FITS file
  66 + int m_bitpix; // Number of Bits/pixel
  67 + int m_naxis; // Image dimension
  68 + long* m_naxes; // Number of pixels in each dimension
  69 + int m_linked; // Pixels are linked (don't delete them!)
  70 + int m_type; // Pixel type
  71 + void* m_pixels; // Pixels
67 72 };
68 73  
69 74  
70 75 /***************************************************************************
71 76 * Inline methods *
72 77 ***************************************************************************/
  78 +inline
  79 +void GFitsImage::link(const double* pixels)
  80 +{
  81 + m_linked = 1;
  82 + m_type = __TDOUBLE;
  83 + m_pixels = (double*)pixels;
  84 +}
73 85 inline
74 86 GFitsImage* GFitsImage::clone(void) const
75 87 {
... ...
src/fits/GFitsImage.cpp
... ... @@ -51,6 +51,38 @@ GFitsImage::GFitsImage() : GFitsData()
51 51  
52 52  
53 53 /***********************************************************************//**
  54 + * @brief Constructor
  55 + *
  56 + * @param naxis Image dimensions (0,1,2,3,4)
  57 + * @param naxes Number of pixels in each dimension
  58 + * @param bitpix Number of Bits per pixel (8, 16, 32, 64, -32, -64=default)
  59 + *
  60 + * Construct instance of GFitsImage by specifying the image dimension and
  61 + * the number of pixels in each dimension. Note that this constructor does
  62 + * not allocate any memory for the actual image.
  63 + ***************************************************************************/
  64 +GFitsImage::GFitsImage(int naxis, const int* naxes, int bitpix) : GFitsData()
  65 +{
  66 + // Initialise class members for clean destruction
  67 + init_members();
  68 +
  69 + // Store number of axis and bitpix
  70 + m_naxis = naxis;
  71 + m_bitpix = bitpix;
  72 +
  73 + // Copy number of pixels in each dimension
  74 + if (m_naxis > 0) {
  75 + m_naxes = new long[m_naxis];
  76 + for (int i = 0; i < m_naxis; ++i)
  77 + m_naxes[i] = naxes[i];
  78 + }
  79 +
  80 + // Return
  81 + return;
  82 +}
  83 +
  84 +
  85 +/***********************************************************************//**
54 86 * @brief Copy constructor
55 87 *
56 88 * @param image FITS image which should be used for construction
... ... @@ -153,11 +185,11 @@ void GFitsImage::open(__fitsfile* fptr)
153 185 /***********************************************************************//**
154 186 * @brief Save Image
155 187 *
156   - * IMPLEMENTATION NOT COMPLETE (IMAGE CUBE SAVING MISSING)
  188 + * Save image pixels into FITS file. In case that the HDU does not exist it
  189 + * is created.
157 190 ***************************************************************************/
158 191 void GFitsImage::save(void)
159 192 {
160   -//cout << "GFitsImage::save " << m_fitsfile.Fptr << " " << (m_fitsfile.HDUposition)+1 << endl;
161 193 // Move to HDU
162 194 int status = 0;
163 195 status = __ffmahd(&m_fitsfile, (m_fitsfile.HDUposition)+1, NULL, &status);
... ... @@ -185,11 +217,20 @@ void GFitsImage::save(void)
185 217 throw GException::fits_error(G_SAVE, status);
186 218 }
187 219  
188   - // Now save the image
189   - // TBD
190   - //status = ffpss(&m_fitsfile, 0, NULL, NULL, NULL, &status);
191   - //if (status != 0)
192   - // throw GException::fits_error(G_SAVE, status);
  220 + // Save the image pixels (if there are some ...)
  221 + if (m_naxis > 0) {
  222 + long* fpixel = new long[m_naxis];
  223 + long* lpixel = new long[m_naxis];
  224 + for (int i = 0; i < m_naxis; ++i) {
  225 + fpixel[i] = 1;
  226 + lpixel[i] = m_naxes[i];
  227 + }
  228 + status = __ffpss(&m_fitsfile, m_type, fpixel, lpixel, m_pixels, &status);
  229 + if (status != 0)
  230 + throw GException::fits_error(G_SAVE, status);
  231 + delete [] fpixel;
  232 + delete [] lpixel;
  233 + }
193 234  
194 235 // Return
195 236 return;
... ... @@ -229,6 +270,9 @@ void GFitsImage::init_members(void)
229 270 m_bitpix = 8;
230 271 m_naxis = 0;
231 272 m_naxes = NULL;
  273 + m_linked = 0;
  274 + m_type = __TBYTE;
  275 + m_pixels = NULL;
232 276  
233 277 // Return
234 278 return;
... ... @@ -247,12 +291,21 @@ void GFitsImage::copy_members(const GFitsImage&amp; image)
247 291 m_bitpix = image.m_bitpix;
248 292 m_naxis = image.m_naxis;
249 293 m_naxes = NULL;
  294 + m_linked = image.m_linked;
  295 + m_type = image.m_type;
  296 + m_pixels = NULL;
250 297  
251   - // Copy data
  298 + // Copy axes
252 299 if (image.m_naxes != NULL && m_naxis > 0) {
253 300 m_naxes = new long[m_naxis];
254 301 memcpy(m_naxes, image.m_naxes, m_naxis*sizeof(long));
255 302 }
  303 +
  304 + // Copy pixels (not if they have been linked!!!)
  305 + if (m_linked)
  306 + m_pixels = image.m_pixels;
  307 + else {
  308 + }
256 309  
257 310 // Return
258 311 return;
... ... @@ -267,8 +320,23 @@ void GFitsImage::free_members(void)
267 320 // Free memory
268 321 if (m_naxes != NULL) delete [] m_naxes;
269 322  
  323 + // If we have proper pixels then free them now (type dependent!)
  324 + if (!m_linked) {
  325 + if (m_pixels != NULL) {
  326 + switch (m_type) {
  327 + case __TDOUBLE:
  328 + delete [] (double*)m_pixels;
  329 + break;
  330 + default:
  331 + break;
  332 + }
  333 + }
  334 + }
  335 +
270 336 // Mark memory as free
271   - m_naxes = NULL;
  337 + m_naxes = NULL;
  338 + m_pixels = NULL;
  339 + m_linked = 0;
272 340  
273 341 // Return
274 342 return;
... ...
test/test_GFits.cpp
... ... @@ -295,13 +295,26 @@ void test_create(void)
295 295 {
296 296 std::cout << "Test GFits: Create FITS file: ";
297 297 try {
  298 + // Allocate and open FITS file
298 299 GFits fits;
299 300 fits.open("test.fits");
300 301 cout << endl << fits;
301   - GFitsImage image;
  302 +
  303 + // Define image
  304 + int naxis = 2;
  305 + int naxes[] = {2,4};
  306 + double pixels[] = {0,1, 2,3, 4,5, 6,7};
  307 + GFitsImage image(naxis, naxes);
  308 + image.link(pixels);
  309 +
  310 + // Define and append HDU
302 311 GFitsHDU hdu(image);
303 312 fits.append(&hdu);
  313 +
  314 + // Dump FITS file
304 315 cout << endl << fits;
  316 +
  317 + // Save FITS file
305 318 fits.save();
306 319 }
307 320 catch (exception &e) {
... ...