source: tags/release-1.1.1/src/FitsIO/dataIO.cc @ 1213

Last change on this file since 1213 was 309, checked in by Matthew Whiting, 17 years ago

Mostly changes to fix some memory allocation/deallocation issues raised by valgrind. Minor changes to the Guide.

File size: 5.7 KB
Line 
1// -----------------------------------------------------------------------
2// dataIO.cc: Read the data array from a FITS file into a Cube object.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <string>
30#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
31                         //  wtbarr (this is a problem when using gcc v.4+
32#include <fitsio.h>
33#include <math.h>
34#include <duchamp.hh>
35#include <param.hh>
36#include <fitsHeader.hh>
37#include <Cubes/cubes.hh>
38
39int Cube::getFITSdata(std::string fname)
40{
41  /**
42   * This function retrieves the data array from the FITS file at the
43   *   location given by the string argument.
44   *  Only the two spatial axes and the one spectral axis are stored in the
45   *   data array. These axes are given by the wcsprm variables wcs->lng,
46   *   wcs->lat and wcs->spec.
47   *  All other axes are just read by their first pixel, using the
48   *   fits_read_subsetnull_ functions
49   */
50
51  int numAxes, status = 0;  /* MUST initialize status */
52  fitsfile *fptr; 
53
54  // Open the FITS file
55  status = 0;
56  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
57    fits_report_error(stderr, status);
58    return FAILURE;
59  }
60
61  // Read the size of the FITS file -- number and sizes of the axes
62  status = 0;
63  if(fits_get_img_dim(fptr, &numAxes, &status)){
64    fits_report_error(stderr, status);
65    return FAILURE;
66  }
67
68  long *dimAxes = new long[numAxes];
69  for(int i=0;i<numAxes;i++) dimAxes[i]=1;
70  status = 0;
71  if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
72    fits_report_error(stderr, status);
73    return FAILURE;
74  }
75
76  // Identify which axes are the "interesting" ones
77  int lng,lat,spc;
78  if(this->head.isWCS()){
79    lng = this->head.WCS().lng;
80    lat = this->head.WCS().lat;
81    spc = this->head.WCS().spec;
82  }
83  else{
84    lng = 0;
85    lat = 1;
86    spc = 2;
87  }
88   
89
90  int anynul;
91  int npix = dimAxes[lng];
92  if(numAxes>1) npix *= dimAxes[lat];
93  if(numAxes>2) npix *= dimAxes[spc];
94
95  float *pixarray = new float[npix];// the array of pixel values
96  char *nullarray = new char[npix]; // the array of null pixels
97  long *inc = new long[numAxes];    // the data-sampling increment
98
99  // define the first and last pixels for each axis.
100  // set both to 1 for the axes we don't want, and to the full
101  //  range for the ones we do.
102  long *fpixel = new long[numAxes];
103  long *lpixel = new long[numAxes];
104  for(int i=0;i<numAxes;i++){
105    inc[i] = fpixel[i] = lpixel[i] = 1;
106  }
107  lpixel[lng] = dimAxes[lng];
108  if(numAxes>1) lpixel[lat] = dimAxes[lat];
109  if(numAxes>2) lpixel[spc] = dimAxes[spc];
110
111   
112  int colnum = 0;  // want the first dataset in the FITS file
113
114  // prepare the Cube's pixel array -- so that we don't have to use saveArray;
115//   if(this->numPixels>0) delete [] array;
116//   this->array = new float[npix];
117//   this->numPixels = npix;
118  // read the relevant subset, defined by the first & last pixel ranges
119  status = 0;
120  if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes,
121                              fpixel, lpixel, inc,
122//                            this->array, nullarray, &anynul, &status)){
123                              pixarray, nullarray, &anynul, &status)){
124    duchampError("Cube Reader",
125                 "There was an error reading in the data array:");
126    fits_report_error(stderr, status);
127    return FAILURE;
128  }
129
130  delete [] fpixel;
131  delete [] lpixel;
132  delete [] inc;
133
134  if(anynul==0){   
135    // no blank pixels, so don't bother with any trimming or checking...
136    if(this->par.getFlagTrim()) { 
137      // if user requested fixing, inform them of change.
138      duchampWarning("Cube Reader",
139                     "No blank pixels, so setting flagTrim to false.\n");
140    }
141    this->par.setFlagBlankPix(false);
142    this->par.setFlagTrim(false);
143  }
144
145  this->initialiseCube(dimAxes);
146  this->saveArray(pixarray,npix);
147  delete [] pixarray;
148  delete [] dimAxes;
149  this->par.setOffsets(this->head.WCS());
150  //-------------------------------------------------------------
151  // Once the array is saved, change the value of the blank pixels from
152  // 0 (as they are set by fits_read_pixnull) to the correct blank value
153  // as determined by the above code.
154  for(int i=0; i<npix;i++){
155//     if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; 
156//     if(isnan(array[i])) this->array[i] = par.getBlankPixVal();
157    if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); 
158  }
159
160  delete [] nullarray;
161  // Close the FITS file -- not needed any more in this function.
162  status = 0;
163  fits_close_file(fptr, &status);
164  if (status){
165    duchampWarning("Cube Reader","Error closing file: ");
166    fits_report_error(stderr, status);
167  }
168
169  return SUCCESS;
170
171}
Note: See TracBrowser for help on using the repository browser.