source: tags/release-1.0.5/src/FitsIO/headerIO.cc @ 178

Last change on this file since 178 was 172, checked in by Matthew Whiting, 18 years ago
  • Improved the way the beam size is dealt with, utilising the parameter beamSize better when the necessary info is not in the FITS header.
  • Also fixed the way this is reported to the user in the parameter listing.
  • Updated the CHANGES document.
  • Other minor fixes to the headerIO code, so that a subsequent call to Param::copyHeaderInfo is not necessary.
File size: 6.3 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->getBUNIT(fname)==FAILURE) returnValue=FAILURE;
25 
26  if(this->getBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
27 
28  if(this->getBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
29
30  return returnValue;
31}
32
33
34//////////////////////////////////////////////////
35
36int FitsHeader::getBUNIT(string fname)
37{
38  /**
39   *  FitsHeader::getBUNIT(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("getBUNIT","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("getBUNIT","Error closing file: ");
71    fits_report_error(stderr, status);
72  }
73
74  delete [] comment, unit;
75
76  return returnStatus;
77}
78
79//////////////////////////////////////////////////
80
81int FitsHeader::getBLANKinfo(string fname, Param &par)
82{
83  /**
84   *   FitsHeader::getBLANKinfo(string fname, Param &par)
85   *    Reading in the Blank pixel value keywords.
86   *    If the BLANK keyword is in the header, use that and store the relevant
87   *     values. Also copy them into the parameter set.
88   *    If not, use the default value (either the default from param.cc or
89   *     from the param file) and assume simple values for the keywords
90   *        --> the scale keyword is the same as the blank value,
91   *            the blank keyword (which is an int) is 1 and
92   *            the bzero (offset) is 0.
93   */
94  int returnStatus = 0, status = 0;
95  if(par.getFlagBlankPix()){  // Only do this if we want the blank pix value
96
97    fitsfile *fptr;         
98    char *comment = new char[FLEN_COMMENT];
99    int blank;
100    float bscale, bzero;
101   
102    // Open the FITS file.
103    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
104      fits_report_error(stderr, status);
105      return FAILURE;
106    }
107
108    // Read the BLANK keyword.
109    //  If this isn't present, then report the error, and set to
110    //     the nominal values given in Param
111    //  If it is, read the other two necessary keywords, and then set
112    //     the values accordingly.
113    if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){
114      duchampWarning("getBLANKinfo","Error reading BLANK keyword: ");
115      fits_report_error(stderr, returnStatus);
116      std::stringstream errmsg;
117      errmsg << "Using default BLANK value ("
118             << par.getBlankPixVal() << ").\n";
119      duchampWarning("getBLANKinfo", errmsg.str());
120      this->blankKeyword  = 1;
121      this->bscaleKeyword = par.getBlankPixVal();
122      this->bzeroKeyword  = 0;
123      par.setBlankKeyword(1);
124      par.setBzeroKeyword(0);
125      par.setFlagUsingBlank(true);
126    }
127    else{
128      status = 0;
129      fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
130      status = 0;
131      fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
132      this->blankKeyword  = blank;
133      this->bscaleKeyword = bscale;
134      this->bzeroKeyword  = bzero;
135      par.setBlankKeyword(blank);
136      par.setBscaleKeyword(bscale);
137      par.setBzeroKeyword(bzero);
138      par.setBlankPixVal( blank*bscale + bzero );
139    }
140 
141    // Close the FITS file.
142    status = 0;
143    fits_close_file(fptr, &status);
144    if (status){
145      duchampWarning("getBLANKinfo","Error closing file: ");
146      fits_report_error(stderr, status);
147    }
148 
149    delete [] comment;
150
151  }
152
153  return returnStatus;
154
155}
156
157//////////////////////////////////////////////////
158
159int FitsHeader::getBeamInfo(string fname, Param &par)
160{
161  /**
162   *  FitsHeader::getBeamInfo(string fname, Param &par)
163   *   Reading in the beam parameters from the header.
164   *   Use these, plus the basic WCS parameters to calculate the size of
165   *    the beam in pixels. Copy the beam size into the parameter set.
166   *   If information not present in FITS header, use the parameter
167   *    set to define the beam size.
168   */
169  char *comment = new char[80];
170  float bmaj,bmin,cdelt1,cdelt2;
171  int returnStatus = 0, status = 0;
172  fitsfile *fptr;         
173
174  // Open the FITS file
175  status = 0;
176  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
177    fits_report_error(stderr, status);
178    return FAILURE;
179  }
180
181  // Read the Keywords -- first look for BMAJ. If it is present, read the
182  //   others, and calculate the beam size.
183  // If it is not, give warning and set beam size to nominal value.
184  if( !fits_read_key(fptr, TFLOAT, "BMAJ", &bmaj, comment, &returnStatus) ){
185    fits_read_key(fptr, TFLOAT, "BMIN", &bmin, comment, &status);
186    fits_read_key(fptr, TFLOAT, "CDELT1", &cdelt1, comment, &status);
187    fits_read_key(fptr, TFLOAT, "CDELT2", &cdelt2, comment, &status);
188    this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) );
189    this->setBmajKeyword(bmaj);
190    this->setBminKeyword(bmin);
191    par.setBeamSize(this->beamSize);
192  }
193  if (returnStatus){
194    duchampWarning("getBeamInfo",
195       "No beam information in header.\n\
196Using parameter beamSize to determine size of beam.\n");
197    this->setBeamSize(par.getBeamSize());
198    par.setFlagUsingBeam(true);
199  }
200
201  // Close the FITS file.
202  status = 0;
203  fits_close_file(fptr, &status);
204  if (status){
205    duchampWarning("getBeamInfo","Error closing file: ");
206    fits_report_error(stderr, status);
207  }
208
209  delete [] comment;
210
211  return returnStatus;
212}
Note: See TracBrowser for help on using the repository browser.