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

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

Added more informative comments to the new functions.

File size: 5.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->getBUNIT(fname)==FAILURE) returnValue=FAILURE;
25 
26  if(this->getBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
27 
28  if(this->getBeamInfo(fname)==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.
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->setBlankKeyword(1);
121      this->setBscaleKeyword(par.getBlankPixVal());
122      this->setBzeroKeyword(0.);
123      par.setFlagUsingBlank(true);
124    }
125    else{
126      status = 0;
127      fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
128      status = 0;
129      fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
130      this->setBlankKeyword(blank);
131      this->setBscaleKeyword(bscale);
132      this->setBzeroKeyword(bzero);
133    }
134 
135    // Close the FITS file.
136    status = 0;
137    fits_close_file(fptr, &status);
138    if (status){
139      duchampWarning("getBLANKinfo","Error closing file: ");
140      fits_report_error(stderr, status);
141    }
142 
143    delete [] comment;
144
145  }
146
147  return returnStatus;
148
149}
150
151//////////////////////////////////////////////////
152
153int FitsHeader::getBeamInfo(string fname)
154{
155  /**
156   *  FitsHeader::getBeamInfo(string fname)
157   *   Reading in the beam parameters from the header.
158   *   Use these, plus the basic WCS parameters to calculate the size of
159   *    the beam in pixels.
160   */
161  char *comment = new char[80];
162  float bmaj,bmin,cdelt1,cdelt2;
163  int returnStatus = 0, status = 0;
164  fitsfile *fptr;         
165
166  // Open the FITS file
167  status = 0;
168  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
169    fits_report_error(stderr, status);
170    return FAILURE;
171  }
172
173  // Read the Keywords -- first look for BMAJ. If it is present, read the
174  //   others, and calculate the beam size.
175  // If it is not, give warning and set beam size to nominal value.
176  if( !fits_read_key(fptr, TFLOAT, "BMAJ", &bmaj, comment, &returnStatus) ){
177    fits_read_key(fptr, TFLOAT, "BMIN", &bmin, comment, &status);
178    fits_read_key(fptr, TFLOAT, "CDELT1", &cdelt1, comment, &status);
179    fits_read_key(fptr, TFLOAT, "CDELT2", &cdelt2, comment, &status);
180    this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) );
181    this->setBmajKeyword(bmaj);
182    this->setBminKeyword(bmin);
183  }
184  if (returnStatus){
185    duchampWarning("getBeamInfo",
186       "No beam information in header. Setting size to nominal 10 pixels.\n");
187    this->setBeamSize(10.);
188  }
189
190  // Close the FITS file.
191  status = 0;
192  fits_close_file(fptr, &status);
193  if (status){
194    duchampWarning("getBeamInfo","Error closing file: ");
195    fits_report_error(stderr, status);
196  }
197
198  delete [] comment;
199
200  return returnStatus;
201}
Note: See TracBrowser for help on using the repository browser.