source: tags/release-1.0.7/src/FitsIO/headerIO.cc

Last change on this file 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: 6.8 KB
Line 
1#include <iostream>
2#include <string>
3#include <string.h>
4#include <wcsunits.h>
5#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
6                         //  wtbarr (this is a problem when using gcc v.4+
7#include <fitsio.h>
8#include <longnam.h>
9#include <math.h>
10#include <duchamp.hh>
11#include <Cubes/cubes.hh>
12
13int FitsHeader::readHeaderInfo(string fname, Param &par)
14{
15  /**
16   * FitsHeader::readHeaderInfo
17   *  A simple front-end function to the three header access
18   *   functions defined below.
19   *
20   */
21
22  int returnValue = SUCCESS;
23
24  if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE;
25 
26  if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
27 
28  if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
29
30  return returnValue;
31}
32
33
34//////////////////////////////////////////////////
35
36int FitsHeader::readBUNIT(string fname)
37{
38  /**
39   *  FitsHeader::readBUNIT(string fname)
40   *   Read the BUNIT header keyword, to store the units
41   *    of brightness (flux).
42   */
43  fitsfile *fptr;         
44  char *comment = new char[FLEN_COMMENT];
45  char *unit = new char[FLEN_VALUE];
46  int returnStatus = 0, status = 0;
47
48  // Open the FITS file
49  status = 0;
50  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
51    fits_report_error(stderr, status);
52    return FAILURE;
53  }
54
55  // Read the BUNIT keyword, and translate to standard unit format if needs be
56  fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus);
57  if (returnStatus){
58    duchampWarning("readBUNIT","Error reading BUNIT keyword: ");
59    fits_report_error(stderr, returnStatus);
60  }
61  else{
62    wcsutrn(0,unit);
63    this->fluxUnits = unit;
64  }
65
66  // Close the FITS file
67  status = 0;
68  fits_close_file(fptr, &status);
69  if (status){
70    duchampWarning("readBUNIT","Error closing file: ");
71    fits_report_error(stderr, status);
72  }
73
74  delete [] comment;
75  delete [] unit;
76
77  return returnStatus;
78}
79
80//////////////////////////////////////////////////
81
82int FitsHeader::readBLANKinfo(string fname, Param &par)
83{
84  /**
85   *   FitsHeader::readBLANKinfo(string fname, Param &par)
86   *    Reading in the Blank pixel value keywords.
87   *    If the BLANK keyword is in the header, use that and store the relevant
88   *     values. Also copy them into the parameter set.
89   *    If not, use the default value (either the default from param.cc or
90   *     from the param file) and assume simple values for the keywords
91   *        --> the scale keyword is the same as the blank value,
92   *            the blank keyword (which is an int) is 1 and
93   *            the bzero (offset) is 0.
94   */
95  int returnStatus = 0, status = 0;
96  if(par.getFlagBlankPix()){  // Only do this if we want the blank pix value
97
98    fitsfile *fptr;         
99    char *comment = new char[FLEN_COMMENT];
100    int blank;
101    float bscale, bzero;
102   
103    // Open the FITS file.
104    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
105      fits_report_error(stderr, status);
106      return FAILURE;
107    }
108
109    // Read the BLANK keyword.
110    //  If this isn't present, then report the error, and set to
111    //     the nominal values given in Param
112    //  If it is, read the other two necessary keywords, and then set
113    //     the values accordingly.
114    if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){
115      duchampWarning("readBLANKinfo","Error reading BLANK keyword: ");
116      fits_report_error(stderr, returnStatus);
117      std::stringstream errmsg;
118      errmsg << "Using default BLANK value ("
119             << par.getBlankPixVal() << ").\n";
120      duchampWarning("readBLANKinfo", errmsg.str());
121      this->blankKeyword  = 1;
122      this->bscaleKeyword = par.getBlankPixVal();
123      this->bzeroKeyword  = 0;
124      par.setBlankKeyword(1);
125      par.setBzeroKeyword(0);
126      par.setFlagUsingBlank(true);
127    }
128    else{
129      status = 0;
130      fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
131      status = 0;
132      fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
133      this->blankKeyword  = blank;
134      this->bscaleKeyword = bscale;
135      this->bzeroKeyword  = bzero;
136      par.setBlankKeyword(blank);
137      par.setBscaleKeyword(bscale);
138      par.setBzeroKeyword(bzero);
139      par.setBlankPixVal( blank*bscale + bzero );
140    }
141 
142    // Close the FITS file.
143    status = 0;
144    fits_close_file(fptr, &status);
145    if (status){
146      duchampWarning("readBLANKinfo","Error closing file: ");
147      fits_report_error(stderr, status);
148    }
149 
150    delete [] comment;
151
152  }
153
154  return returnStatus;
155
156}
157
158//////////////////////////////////////////////////
159
160int FitsHeader::readBeamInfo(string fname, Param &par)
161{
162  /**
163   *  FitsHeader::readBeamInfo(string fname, Param &par)
164   *   Reading in the beam parameters from the header.
165   *   Use these, plus the basic WCS parameters to calculate the size of
166   *    the beam in pixels. Copy the beam size into the parameter set.
167   *   If information not present in FITS header, use the parameter
168   *    set to define the beam size.
169   */
170  char *comment = new char[80];
171  string keyword[4]={"BMAJ","BMIN","CDELT1","CDELT2"};
172  float bmaj,bmin,cdelt1,cdelt2;
173  int status[6];
174  fitsfile *fptr;         
175
176  for(int i=0;i<6;i++) status[i] = 0;
177
178  // Open the FITS file
179  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){
180    fits_report_error(stderr, status[0]);
181    return FAILURE;
182  }
183
184  // Read the Keywords -- first look for BMAJ. If it is present, read the
185  //   others, and calculate the beam size.
186  // If it is not, give warning and set beam size to nominal value.
187  fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj,
188                comment, &status[1]);
189  fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin,
190                comment, &status[2]);
191  fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &cdelt1,
192                comment, &status[3]);
193  fits_read_key(fptr, TFLOAT, (char *)keyword[3].c_str(), &cdelt2,
194                comment, &status[4]);
195
196  if(status[1]||status[2]||status[3]||status[4]){ // error
197    stringstream errmsg;
198    errmsg << "Header keywords not present: ";
199    for(int i=0;i<4;i++) if(status[i+1]) errmsg<<keyword[i]<<" ";
200    errmsg << "\nUsing parameter beamSize to determine size of beam.\n";
201    duchampWarning("readBeamInfo",errmsg.str());
202    this->setBeamSize(par.getBeamSize());
203    par.setFlagUsingBeam(true);
204  }
205  else{ // all keywords present
206    this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) );
207    this->setBmajKeyword(bmaj);
208    this->setBminKeyword(bmin);
209    par.setBeamSize(this->beamSize);
210  }
211   
212  // Close the FITS file.
213  fits_close_file(fptr, &status[5]);
214  if (status[5]){
215    duchampWarning("readBeamInfo","Error closing file: ");
216    fits_report_error(stderr, status[5]);
217  }
218
219  delete [] comment;
220
221  int returnStatus = status[0];
222  for(int i=1;i<6;i++) if(status[i]>returnStatus) returnStatus=status[i];
223
224  return returnStatus;
225}
Note: See TracBrowser for help on using the repository browser.