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

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

Some crucial edits so that things will work when the cube's WCS is not well defined.

File size: 3.7 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 *array = new float[npix];   // the array of data 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  // read the relevant subset, defined by the first & last pixel ranges
86  status = 0;
87  if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes,
88                              fpixel, lpixel, inc,
89                              array, nullarray, &anynul, &status)){
90    duchampError("getFITSdata",
91                 "There was an error reading in the data array:");
92    fits_report_error(stderr, status);
93    return FAILURE;
94  }
95
96  if(anynul==0){   
97    // no blank pixels, so don't bother with any trimming or checking...
98    if(this->par.getFlagBlankPix()) { 
99      // if user requested fixing, inform them of change.
100      duchampWarning("getFITSdata",
101                     "No blank pixels, so setting flagBlankPix to false.\n");
102    }
103    this->par.setFlagBlankPix(false);
104  }
105
106  this->initialiseCube(dimAxes);
107  this->saveArray(array,npix);
108  this->par.setOffsets(this->head.getWCS());
109  //-------------------------------------------------------------
110  // Once the array is saved, change the value of the blank pixels from
111  // 0 (as they are set by fits_read_pixnull) to the correct blank value
112  // as determined by the above code.
113  for(int i=0; i<npix;i++){
114//     if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; 
115    if(isnan(array[i])) this->array[i] = par.getBlankPixVal();
116    if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); 
117  }
118
119  return SUCCESS;
120
121}
Note: See TracBrowser for help on using the repository browser.