source: tags/release-1.0.7/src/FitsIO/dataIO.cc @ 1455

Last change on this file since 1455 was 205, checked in by Matthew Whiting, 18 years ago

Changes here mostly aimed at reducing/removing memory leaks. These were one of:

  • Forgetting to delete something allocated with "new". The Cube destructor has improved with the use of bool variables to keep track of when recon & baseline ahave been allocated.
  • Incorrectly using the wcs->flag parameter (eg. wcsIO had it set to -1 *after* calling wcsini)
  • Not closing a fits file once finished with (dataIO)
  • Allocating the wcsprm structs with calloc before calling wcsini, so that wcsvfree can work (it calls "free", so the memory needs to be allocated with calloc or malloc).

The only other change was the following:

  • A new way of doing the Cube::setCubeStats -- rather than calling the functions in getStats.cc, we explicitly do the calculations. This means we can sort the tempArray that has the BLANKS etc removed. This saves a great deal of memory usage on large FITS files (such as Enno's 2Gb one)
  • The old setCubeStats function is still there but called setCubeStatsOld.
File size: 4.3 KB
Line 
1#include <iostream>
2#include <string>
3#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
4                         //  wtbarr (this is a problem when using gcc v.4+
5#include <fitsio.h>
6#include <math.h>
7#include <duchamp.hh>
8#include <Cubes/cubes.hh>
9
10int Cube::getFITSdata(string fname)
11{
12  /**
13   * Cube::getFITSdata(string fname)
14   *
15   *  This function retrieves the data array from the FITS file at the
16   *   location given by the string argument.
17   *  Only the two spatial axes and the one spectral axis are stored in the
18   *   data array. These axes are given by the wcsprm variables wcs->lng,
19   *   wcs->lat and wcs->spec.
20   *  All other axes are just read by their first pixel, using the
21   *   fits_read_subsetnull_ functions
22   */
23
24  long nelements;
25  int bitpix,numAxes;
26  int status = 0,  nkeys;  /* MUST initialize status */
27  fitsfile *fptr; 
28
29  // Open the FITS file
30  status = 0;
31  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
32    fits_report_error(stderr, status);
33    return FAILURE;
34  }
35
36  // Read the size of the FITS file -- number and sizes of the axes
37  status = 0;
38  if(fits_get_img_dim(fptr, &numAxes, &status)){
39    fits_report_error(stderr, status);
40    return FAILURE;
41  }
42  long *dimAxes = new long[numAxes];
43  for(int i=0;i<numAxes;i++) dimAxes[i]=1;
44  status = 0;
45  if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
46    fits_report_error(stderr, status);
47    return FAILURE;
48  }
49
50  // Identify which axes are the "interesting" ones
51  int lng,lat,spc;
52  if(this->head.isWCS()){
53    lng = this->head.getWCS()->lng;
54    lat = this->head.getWCS()->lat;
55    spc = this->head.getWCS()->spec;
56  }
57  else{
58    lng = 0;
59    lat = 1;
60    spc = 2;
61  }
62   
63
64  int anynul;
65  int npix = dimAxes[lng]*dimAxes[lat]*dimAxes[spc];
66
67  float *pixarray = new float[npix];// the array of pixel values
68  char *nullarray = new char[npix]; // the array of null pixels
69  long *inc = new long[numAxes];    // the data-sampling increment
70
71  // define the first and last pixels for each axis.
72  // set both to 1 for the axes we don't want, and to the full
73  //  range for the ones we do.
74  long *fpixel = new long[numAxes];
75  long *lpixel = new long[numAxes];
76  for(int i=0;i<numAxes;i++){
77    inc[i] = fpixel[i] = lpixel[i] = 1;
78  }
79  lpixel[lng] = dimAxes[lng];
80  lpixel[lat] = dimAxes[lat];
81  lpixel[spc] = dimAxes[spc];
82   
83  int colnum = 0;  // want the first dataset in the FITS file
84
85  // prepare the Cube's pixel array -- so that we don't have to use saveArray;
86//   if(this->numPixels>0) delete [] array;
87//   this->array = new float[npix];
88//   this->numPixels = npix;
89  // read the relevant subset, defined by the first & last pixel ranges
90  status = 0;
91  if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes,
92                              fpixel, lpixel, inc,
93//                            this->array, nullarray, &anynul, &status)){
94                              pixarray, nullarray, &anynul, &status)){
95    duchampError("getFITSdata",
96                 "There was an error reading in the data array:");
97    fits_report_error(stderr, status);
98    return FAILURE;
99  }
100
101  delete [] fpixel;
102  delete [] lpixel;
103  delete [] inc;
104
105  if(anynul==0){   
106    // no blank pixels, so don't bother with any trimming or checking...
107    if(this->par.getFlagBlankPix()) { 
108      // if user requested fixing, inform them of change.
109      duchampWarning("getFITSdata",
110                     "No blank pixels, so setting flagBlankPix to false.\n");
111    }
112    this->par.setFlagBlankPix(false);
113  }
114
115  this->initialiseCube(dimAxes);
116  this->saveArray(pixarray,npix);
117  delete [] pixarray;
118  delete [] dimAxes;
119  this->par.setOffsets(this->head.getWCS());
120  //-------------------------------------------------------------
121  // Once the array is saved, change the value of the blank pixels from
122  // 0 (as they are set by fits_read_pixnull) to the correct blank value
123  // as determined by the above code.
124  for(int i=0; i<npix;i++){
125//     if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; 
126//     if(isnan(array[i])) this->array[i] = par.getBlankPixVal();
127    if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); 
128  }
129
130  delete [] nullarray;
131  // Close the FITS file -- not needed any more in this function.
132  status = 0;
133  fits_close_file(fptr, &status);
134  if (status){
135    duchampWarning("defineWCS","Error closing file: ");
136    fits_report_error(stderr, status);
137  }
138
139  return SUCCESS;
140
141}
Note: See TracBrowser for help on using the repository browser.