source: trunk/src/FitsIO/dataIO.cc @ 204

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

A large commit, based on improving memory usage, allocation, etc:

  • src/param.hh :
    • Added a delete command for the offsets array in Param. Keep track of size via new sizeOffsets variable.
    • Changed "wcsprm" to "struct wcsprm", for clarity.
    • Put wcsvfree in the FitsHeader? destructor so that memory is deallocated correctly.
  • src/param.cc :
    • Improved the FitsHeader? constructor functions, so that memory for the wcsprm structures is allocated appropriately.
    • Other wcsprm-related tweaks.
    • Included code for sizeOffsets -- the size of the offsets array in Param, so that we can properly deallocate its memory in the destructor function.
  • src/FitsIO/subsection.cc : Changed "wcsprm" to "struct wcsprm", for clarity, and added a sizeOffsets to track the memory allocation for offsets.
  • src/FitsIO/dataIO.cc : renamed the local variable array to pixarray so that there is no confusion. Added delete[] commands for local arrays.
  • src/FitsIO/wcsIO.cc : Improved the struct wcsprm memory allocation. Now using a local wcs variable so that we don't get confusion with the FitsHeader? one.
  • src/Utils/wcsFunctions.cc : changed "wcsprm" to "struct wcsprm", for clarity.
  • src/Cubes/CubicSearch.cc : removed two allocation calls (to new) that were not needed, as well as unused commented-out code.
  • src/Cubes/plotting.cc :
    • Corrected the way the detection map is worked out and the scale bar range calculated.
    • Changed "wcsprm" to "struct wcsprm", for clarity.
  • src/duchamp.hh : better implementation of the rewind() and remove() functions for ProgressBar?
  • src/Utils/getStats.cc : minor diffs
  • src/Utils/utils.hh : changed prototypes
  • src/Cubes/cubes.cc : Changed the way the setCubeStats() function works, so that stats aren't needlessly calculated if the threshold has already been specified.
  • src/Cubes/cubes.hh : minor presentation changes
  • src/Cubes/drawMomentCutout.cc : Tried to improve the scale-bar drawing function, to cope with very high angular resolution data. Not quite working properly yet.
  • src/Cubes/outputSpectra.cc : Corrected the flux labels so that the appropriate units are used, and not just Jy or Jy/beam.
File size: 4.0 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,lpixel,inc;
102
103  if(anynul==0){   
104    // no blank pixels, so don't bother with any trimming or checking...
105    if(this->par.getFlagBlankPix()) { 
106      // if user requested fixing, inform them of change.
107      duchampWarning("getFITSdata",
108                     "No blank pixels, so setting flagBlankPix to false.\n");
109    }
110    this->par.setFlagBlankPix(false);
111  }
112
113  this->initialiseCube(dimAxes);
114  this->saveArray(pixarray,npix);
115  delete [] pixarray,dimAxes;
116//   delete [] dimAxes;
117  this->par.setOffsets(this->head.getWCS());
118  //-------------------------------------------------------------
119  // Once the array is saved, change the value of the blank pixels from
120  // 0 (as they are set by fits_read_pixnull) to the correct blank value
121  // as determined by the above code.
122  for(int i=0; i<npix;i++){
123//     if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; 
124//     if(isnan(array[i])) this->array[i] = par.getBlankPixVal();
125    if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); 
126  }
127
128  delete [] nullarray;
129
130  return SUCCESS;
131
132}
Note: See TracBrowser for help on using the repository browser.