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

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

Mostly adding the distribution text to the start of files, with a few additional comments added too.

File size: 8.3 KB
Line 
1// -----------------------------------------------------------------------
2// headerIO.cc: Read in various PHU keywords from a FITS file.
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#include <string.h>
31#include <wcsunits.h>
32#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
33                         //  wtbarr (this is a problem when using gcc v.4+
34#include <fitsio.h>
35#include <longnam.h>
36#include <math.h>
37#include <duchamp.hh>
38#include <Cubes/cubes.hh>
39
40int FitsHeader::readHeaderInfo(std::string fname, Param &par)
41{
42  /**
43   *  A simple front-end function to the three header access
44   *   functions defined below.
45   *
46   */
47
48  int returnValue = SUCCESS;
49
50//   if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE;
51 
52  if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
53 
54  if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
55
56  return returnValue;
57}
58
59
60//////////////////////////////////////////////////
61
62int FitsHeader::readBUNIT(std::string fname)
63{
64  /**
65   *   Read the BUNIT header keyword, to store the units of brightness (flux).
66   *  \param fname The name of the FITS file.
67   */
68  fitsfile *fptr;         
69  char *comment = new char[FLEN_COMMENT];
70  char *unit = new char[FLEN_VALUE];
71  int returnStatus = 0, status = 0;
72
73  // Open the FITS file
74  status = 0;
75  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
76    fits_report_error(stderr, status);
77    return FAILURE;
78  }
79
80  // Read the BUNIT keyword, and translate to standard unit format if needs be
81  fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus);
82  if (returnStatus){
83    duchampWarning("Cube Reader","Error reading BUNIT keyword: ");
84    fits_report_error(stderr, returnStatus);
85  }
86  else{
87    wcsutrn(0,unit);
88    this->fluxUnits = unit;
89  }
90
91  // Close the FITS file
92  status = 0;
93  fits_close_file(fptr, &status);
94  if (status){
95    duchampWarning("Cube Reader","Error closing file: ");
96    fits_report_error(stderr, status);
97  }
98
99  delete [] comment;
100  delete [] unit;
101
102  return returnStatus;
103}
104
105//////////////////////////////////////////////////
106
107int FitsHeader::readBLANKinfo(std::string fname, Param &par)
108{
109  /**
110   *    Reading in the Blank pixel value keywords, which is only done
111   *    if requested via the flagBlankPix parameter.
112   *
113   *    If the BLANK keyword is in the header, use that and store the relevant
114   *     values. Also copy them into the parameter set.
115   *    If not, use the default value (either the default from param.cc or
116   *     from the param file) and assume simple values for the keywords:
117   *     <ul><li> The scale keyword is the same as the blank value,
118   *         <li> The blank keyword (which is an int) is 1 and
119   *         <li> The bzero (offset) is 0.
120   *    </ul>
121   * \param fname The name of the FITS file.
122   * \param par The Param set: to know the flagBlankPix value and to
123   * store the keywords.
124   */
125  int returnStatus = 0, status = 0;
126
127  fitsfile *fptr;         
128  char *comment = new char[FLEN_COMMENT];
129  int blank;
130  float bscale, bzero;
131   
132  // Open the FITS file.
133  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
134    fits_report_error(stderr, status);
135    return FAILURE;
136  }
137
138  // Read the BLANK keyword.
139  //  If this isn't present, make sure flagTrim is false (if it is
140  //  currently true, let the user know you're doing this) and set
141  //  flagBlankPix to false so that the isBlank functions all return
142  //  false
143  //  If it is, read the other two necessary keywords, and then set
144  //     the values accordingly.
145
146  if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){
147
148    par.setFlagBlankPix(false);
149
150    if(par.getFlagTrim()){
151      par.setFlagTrim(false);
152      std::stringstream errmsg;
153      if(returnStatus == KEY_NO_EXIST)
154        duchampWarning("Cube Reader",
155                       "There is no BLANK keyword present. Not doing any trimming.\n");
156      else{
157        duchampWarning("Cube Reader",
158                       "Error reading BLANK keyword, so not doing any trimming.");
159        fits_report_error(stderr, returnStatus);
160      }
161    }
162  }
163  else{
164    status = 0;
165    fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
166    status = 0;
167    fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
168    this->blankKeyword  = blank;
169    this->bscaleKeyword = bscale;
170    this->bzeroKeyword  = bzero;
171    par.setBlankKeyword(blank);
172    par.setBscaleKeyword(bscale);
173    par.setBzeroKeyword(bzero);
174    par.setBlankPixVal( blank*bscale + bzero );
175  }
176 
177  // Close the FITS file.
178  status = 0;
179  fits_close_file(fptr, &status);
180  if (status){
181    duchampWarning("Cube Reader","Error closing file: ");
182    fits_report_error(stderr, status);
183  }
184 
185  delete [] comment;
186
187  return returnStatus;
188
189}
190
191//////////////////////////////////////////////////
192
193int FitsHeader::readBeamInfo(std::string fname, Param &par)
194{
195  /**
196   *   Reading in the beam parameters from the header.
197   *   Use these, plus the basic WCS parameters to calculate the size of
198   *    the beam in pixels. Copy the beam size into the parameter set.
199   *   If information not present in FITS header, use the parameter
200   *    set to define the beam size.
201   * \param fname The name of the FITS file.
202   * \param par The Param set.
203   */
204  char *comment = new char[80];
205  std::string keyword[5]={"BMAJ","BMIN","BPA","CDELT1","CDELT2"};
206  float bmaj,bmin,bpa,cdelt1,cdelt2;
207  int status[7];
208  fitsfile *fptr;         
209
210  for(int i=0;i<7;i++) status[i] = 0;
211
212  // Open the FITS file
213  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){
214    fits_report_error(stderr, status[0]);
215    return FAILURE;
216  }
217
218  // Read the Keywords -- first look for BMAJ. If it is present, read the
219  //   others, and calculate the beam size.
220  // If it is not, give warning and set beam size to nominal value.
221  fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj,
222                comment, &status[1]);
223  fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin,
224                comment, &status[2]);
225  fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa,
226                comment, &status[3]);
227  fits_read_key(fptr, TFLOAT, (char *)keyword[3].c_str(), &cdelt1,
228                comment, &status[4]);
229  fits_read_key(fptr, TFLOAT, (char *)keyword[4].c_str(), &cdelt2,
230                comment, &status[5]);
231
232  if(status[1]||status[2]||status[3]||status[4]||status[5]){ // error
233    std::stringstream errmsg;
234    errmsg << "Header keywords not present: ";
235    for(int i=0;i<5;i++) if(status[i+1]) errmsg<<keyword[i]<<" ";
236    errmsg << "\nUsing parameter beamSize to determine size of beam.\n";
237    duchampWarning("Cube Reader",errmsg.str());
238    this->setBeamSize(par.getBeamSize());
239    par.setFlagUsingBeam(true);
240  }
241  else{ // all keywords present
242    this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) );
243    this->setBmajKeyword(bmaj);
244    this->setBminKeyword(bmin);
245    this->setBpaKeyword(bpa);
246    par.setBeamSize(this->beamSize);
247  }
248   
249  // Close the FITS file.
250  fits_close_file(fptr, &status[6]);
251  if (status[6]){
252    duchampWarning("Cube Reader","Error closing file: ");
253    fits_report_error(stderr, status[6]);
254  }
255
256  delete [] comment;
257
258  int returnStatus = status[0];
259  for(int i=1;i<7;i++) if(status[i]>returnStatus) returnStatus=status[i];
260
261  return returnStatus;
262}
Note: See TracBrowser for help on using the repository browser.