source: trunk/src/FitsIO/headerIO.cc @ 258

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

Large raft of changes. Most are minor ones related to fixing up the use of std::string and std::vector (whether they are declared as using, or not...). Other changes include:

  • Moving the reconstruction filter class Filter into its own header/implementation files filter.{hh,cc}. As a result, the atrous.cc file is removed, but atrous.hh remains with just the function prototypes.
  • Incorporating a Filter object into the Param set, so that the reconstruction routines can see it without the messy "extern" call. The define() function is now called in both the Param() and Param::readParams() functions, and no longer in the main body.
  • Col functions in columns.cc moved into the Column namespace, while the template function printEntry is moved into the columns.hh file -- it would not compile on delphinus with it in the columns.cc, even though all the implementations were present.
  • Improved the introductory section of the Guide, with a bit more detail about each of the execution steps. This way the reader can read this section and have a reasonably good idea about what is happening.
  • Other minor changes to the Guide.
File size: 7.0 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(std::string fname, Param &par)
14{
15  /**
16   *  A simple front-end function to the three header access
17   *   functions defined below.
18   *
19   */
20
21  int returnValue = SUCCESS;
22
23  if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE;
24 
25  if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
26 
27  if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
28
29  return returnValue;
30}
31
32
33//////////////////////////////////////////////////
34
35int FitsHeader::readBUNIT(std::string fname)
36{
37  /**
38   *   Read the BUNIT header keyword, to store the units of brightness (flux).
39   *  \param fname The name of the FITS file.
40   */
41  fitsfile *fptr;         
42  char *comment = new char[FLEN_COMMENT];
43  char *unit = new char[FLEN_VALUE];
44  int returnStatus = 0, status = 0;
45
46  // Open the FITS file
47  status = 0;
48  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
49    fits_report_error(stderr, status);
50    return FAILURE;
51  }
52
53  // Read the BUNIT keyword, and translate to standard unit format if needs be
54  fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus);
55  if (returnStatus){
56    duchampWarning("readBUNIT","Error reading BUNIT keyword: ");
57    fits_report_error(stderr, returnStatus);
58  }
59  else{
60    wcsutrn(0,unit);
61    this->fluxUnits = unit;
62  }
63
64  // Close the FITS file
65  status = 0;
66  fits_close_file(fptr, &status);
67  if (status){
68    duchampWarning("readBUNIT","Error closing file: ");
69    fits_report_error(stderr, status);
70  }
71
72  delete [] comment;
73  delete [] unit;
74
75  return returnStatus;
76}
77
78//////////////////////////////////////////////////
79
80int FitsHeader::readBLANKinfo(std::string fname, Param &par)
81{
82  /**
83   *    Reading in the Blank pixel value keywords, which is only done
84   *    if requested via the flagBlankPix parameter.
85   *
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   *     <ul><li> The scale keyword is the same as the blank value,
91   *         <li> The blank keyword (which is an int) is 1 and
92   *         <li> The bzero (offset) is 0.
93   *    </ul>
94   * \param fname The name of the FITS file.
95   * \param par The Param set: to know the flagBlankPix value and to
96   * store the keywords.
97   */
98  int returnStatus = 0, status = 0;
99  if(par.getFlagBlankPix()){  // Only do this if we want the blank pix value
100
101    fitsfile *fptr;         
102    char *comment = new char[FLEN_COMMENT];
103    int blank;
104    float bscale, bzero;
105   
106    // Open the FITS file.
107    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
108      fits_report_error(stderr, status);
109      return FAILURE;
110    }
111
112    // Read the BLANK keyword.
113    //  If this isn't present, then report the error, and set to
114    //     the nominal values given in Param
115    //  If it is, read the other two necessary keywords, and then set
116    //     the values accordingly.
117    if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){
118      std::stringstream errmsg;
119      duchampWarning("readBLANKinfo", "Error reading BLANK keyword: ");
120      fits_report_error(stderr, returnStatus);
121      errmsg << "Using the BLANK value given as input parameter ("
122             << par.getBlankPixVal() << ").\n";
123      duchampWarning("readBLANKinfo", errmsg.str());
124      this->blankKeyword  = 1;
125      this->bscaleKeyword = par.getBlankPixVal();
126      this->bzeroKeyword  = 0;
127      par.setBlankKeyword(1);
128      par.setBzeroKeyword(0);
129      par.setFlagUsingBlank(true);
130    }
131    else{
132      status = 0;
133      fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
134      status = 0;
135      fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
136      this->blankKeyword  = blank;
137      this->bscaleKeyword = bscale;
138      this->bzeroKeyword  = bzero;
139      par.setBlankKeyword(blank);
140      par.setBscaleKeyword(bscale);
141      par.setBzeroKeyword(bzero);
142      par.setBlankPixVal( blank*bscale + bzero );
143    }
144 
145    // Close the FITS file.
146    status = 0;
147    fits_close_file(fptr, &status);
148    if (status){
149      duchampWarning("readBLANKinfo","Error closing file: ");
150      fits_report_error(stderr, status);
151    }
152 
153    delete [] comment;
154
155  }
156
157  return returnStatus;
158
159}
160
161//////////////////////////////////////////////////
162
163int FitsHeader::readBeamInfo(std::string fname, Param &par)
164{
165  /**
166   *   Reading in the beam parameters from the header.
167   *   Use these, plus the basic WCS parameters to calculate the size of
168   *    the beam in pixels. Copy the beam size into the parameter set.
169   *   If information not present in FITS header, use the parameter
170   *    set to define the beam size.
171   * \param fname The name of the FITS file.
172   * \param par The Param set.
173   */
174  char *comment = new char[80];
175  std::string keyword[4]={"BMAJ","BMIN","CDELT1","CDELT2"};
176  float bmaj,bmin,cdelt1,cdelt2;
177  int status[6];
178  fitsfile *fptr;         
179
180  for(int i=0;i<6;i++) status[i] = 0;
181
182  // Open the FITS file
183  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){
184    fits_report_error(stderr, status[0]);
185    return FAILURE;
186  }
187
188  // Read the Keywords -- first look for BMAJ. If it is present, read the
189  //   others, and calculate the beam size.
190  // If it is not, give warning and set beam size to nominal value.
191  fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj,
192                comment, &status[1]);
193  fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin,
194                comment, &status[2]);
195  fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &cdelt1,
196                comment, &status[3]);
197  fits_read_key(fptr, TFLOAT, (char *)keyword[3].c_str(), &cdelt2,
198                comment, &status[4]);
199
200  if(status[1]||status[2]||status[3]||status[4]){ // error
201    std::stringstream errmsg;
202    errmsg << "Header keywords not present: ";
203    for(int i=0;i<4;i++) if(status[i+1]) errmsg<<keyword[i]<<" ";
204    errmsg << "\nUsing parameter beamSize to determine size of beam.\n";
205    duchampWarning("readBeamInfo",errmsg.str());
206    this->setBeamSize(par.getBeamSize());
207    par.setFlagUsingBeam(true);
208  }
209  else{ // all keywords present
210    this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) );
211    this->setBmajKeyword(bmaj);
212    this->setBminKeyword(bmin);
213    par.setBeamSize(this->beamSize);
214  }
215   
216  // Close the FITS file.
217  fits_close_file(fptr, &status[5]);
218  if (status[5]){
219    duchampWarning("readBeamInfo","Error closing file: ");
220    fits_report_error(stderr, status[5]);
221  }
222
223  delete [] comment;
224
225  int returnStatus = status[0];
226  for(int i=1;i<6;i++) if(status[i]>returnStatus) returnStatus=status[i];
227
228  return returnStatus;
229}
Note: See TracBrowser for help on using the repository browser.