source: tags/release-1.1.9/src/FitsIO/headerIO.cc @ 1441

Last change on this file since 1441 was 698, checked in by MatthewWhiting, 14 years ago

A bunch of changes aimed at improving the use of OUTCOME to report SUCCESS/FAILURE. When such a value is returned by a function, the returned type is duchamp::OUTCOME.

Also improved the error reporting in saveImage

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