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

Last change on this file since 224 was 224, checked in by Matthew Whiting, 17 years ago
  • Main change is to correct the problem reported in ticket #4, so that the velocity units are correctly dealt with. This entailed:
    • Changing the way wcssptr is called in FitsIO/wcsIO.cc, so that it is called when there is no CUNIT3 keyword, as well as when the type is different to that desired.
    • Changing the way fixUnits() will deal with missing units -- hopefully this will now not occur, and the error message has been changed accordingly.

Other changes include:

  • Making a new file cubes_extended.cc, that has those functions from cubes.cc that require external functions. This enables simpler compilation of test programs.
  • An additional clause in dataIO.cc to set the null value when the BLANK keyword is not present. However, this remains commented out, as I don't think it is necessary at this point, and there is a question about whether it is correct.
  • Other minor typo fixes.
File size: 4.8 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   * This function retrieves the data array from the FITS file at the
14   *   location given by the string argument.
15   *  Only the two spatial axes and the one spectral axis are stored in the
16   *   data array. These axes are given by the wcsprm variables wcs->lng,
17   *   wcs->lat and wcs->spec.
18   *  All other axes are just read by their first pixel, using the
19   *   fits_read_subsetnull_ functions
20   */
21
22  long nelements;
23  int bitpix,numAxes;
24  int status = 0,  nkeys;  /* MUST initialize status */
25  fitsfile *fptr; 
26
27  // Open the FITS file
28  status = 0;
29  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
30    fits_report_error(stderr, status);
31    return FAILURE;
32  }
33
34  // Read the size of the FITS file -- number and sizes of the axes
35  status = 0;
36  if(fits_get_img_dim(fptr, &numAxes, &status)){
37    fits_report_error(stderr, status);
38    return FAILURE;
39  }
40  long *dimAxes = new long[numAxes];
41  for(int i=0;i<numAxes;i++) dimAxes[i]=1;
42  status = 0;
43  if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
44    fits_report_error(stderr, status);
45    return FAILURE;
46  }
47
48  // Identify which axes are the "interesting" ones
49  int lng,lat,spc;
50  if(this->head.isWCS()){
51    lng = this->head.getWCS()->lng;
52    lat = this->head.getWCS()->lat;
53    spc = this->head.getWCS()->spec;
54  }
55  else{
56    lng = 0;
57    lat = 1;
58    spc = 2;
59  }
60   
61
62  int anynul;
63  int npix = dimAxes[lng]*dimAxes[lat]*dimAxes[spc];
64
65  float *pixarray = new float[npix];// the array of pixel values
66  char *nullarray = new char[npix]; // the array of null pixels
67  long *inc = new long[numAxes];    // the data-sampling increment
68
69  // define the first and last pixels for each axis.
70  // set both to 1 for the axes we don't want, and to the full
71  //  range for the ones we do.
72  long *fpixel = new long[numAxes];
73  long *lpixel = new long[numAxes];
74  for(int i=0;i<numAxes;i++){
75    inc[i] = fpixel[i] = lpixel[i] = 1;
76  }
77  lpixel[lng] = dimAxes[lng];
78  lpixel[lat] = dimAxes[lat];
79  lpixel[spc] = dimAxes[spc];
80
81  /*
82////////////
83// MAY NOT NEED THIS -- SHOULD BE ABLE TO SET IT WITH THE BLANKPIXVALUE
84// INPUT PARAMETER.
85////////////
86  // set the BLANK pixel scaling if there is no BLANK keyword present
87  if(this->par.getFlagUsingBlank()){
88    long nullval = long((this->par.getBlankPixVal()-this->par.getBzeroKeyword())
89                        / this->getBscaleKeyword());
90    status=0;
91    if(fits_set_imgnull(fptr, nullval, &status)){
92      duchampWarning("getFITSdata","There was an error setting the null pixel value:");
93      fits_report_error(stderr,status);
94    }
95  }
96*/
97   
98  int colnum = 0;  // want the first dataset in the FITS file
99
100  // prepare the Cube's pixel array -- so that we don't have to use saveArray;
101//   if(this->numPixels>0) delete [] array;
102//   this->array = new float[npix];
103//   this->numPixels = npix;
104  // read the relevant subset, defined by the first & last pixel ranges
105  status = 0;
106  if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes,
107                              fpixel, lpixel, inc,
108//                            this->array, nullarray, &anynul, &status)){
109                              pixarray, nullarray, &anynul, &status)){
110    duchampError("getFITSdata",
111                 "There was an error reading in the data array:");
112    fits_report_error(stderr, status);
113    return FAILURE;
114  }
115
116  delete [] fpixel;
117  delete [] lpixel;
118  delete [] inc;
119
120  if(anynul==0){   
121    // no blank pixels, so don't bother with any trimming or checking...
122    if(this->par.getFlagBlankPix()) { 
123      // if user requested fixing, inform them of change.
124      duchampWarning("getFITSdata",
125                     "No blank pixels, so setting flagBlankPix to false.\n");
126    }
127    this->par.setFlagBlankPix(false);
128  }
129
130  this->initialiseCube(dimAxes);
131  this->saveArray(pixarray,npix);
132  delete [] pixarray;
133  delete [] dimAxes;
134  this->par.setOffsets(this->head.getWCS());
135  //-------------------------------------------------------------
136  // Once the array is saved, change the value of the blank pixels from
137  // 0 (as they are set by fits_read_pixnull) to the correct blank value
138  // as determined by the above code.
139  for(int i=0; i<npix;i++){
140//     if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; 
141//     if(isnan(array[i])) this->array[i] = par.getBlankPixVal();
142    if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); 
143  }
144
145  delete [] nullarray;
146  // Close the FITS file -- not needed any more in this function.
147  status = 0;
148  fits_close_file(fptr, &status);
149  if (status){
150    duchampWarning("defineWCS","Error closing file: ");
151    fits_report_error(stderr, status);
152  }
153
154  return SUCCESS;
155
156}
Note: See TracBrowser for help on using the repository browser.