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

Last change on this file since 913 was 913, checked in by MatthewWhiting, 12 years ago

A large swathe of changes aimed at improving warning/error/exception handling. Now make use of macros and streams. Also, there is now a distinction between DUCHAMPERROR and DUCHAMPTHROW.

File size: 7.6 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    // Get the brightness unit, so that we can set the units for the
51    //  integrated flux when we go to fixUnits.
52    if(this->readBUNIT(fname) == FAILURE) return FAILURE;
53 
54    // if(this->readBLANKinfo(fname, par)==FAILURE)
55    //   duchampWarning("Cube Reader", "Reading BLANK info failed\n");
56    OUTCOME blankResult=this->readBLANKinfo(fname,par);
57 
58    if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
59
60    if(this->wcs->spec>=0) this->fixUnits(par);
61
62    return returnValue;
63  }
64
65
66  //////////////////////////////////////////////////
67
68  OUTCOME FitsHeader::readBUNIT(std::string fname)
69  {
70    ///   Read the BUNIT header keyword, to store the units of brightness (flux).
71    ///  \param fname The name of the FITS file.
72
73    fitsfile *fptr;         
74    char *comment = new char[FLEN_COMMENT];
75    char *unit = new char[FLEN_VALUE];
76    OUTCOME returnStatus=SUCCESS;
77    int status = 0;
78
79    // Open the FITS file
80    status = 0;
81    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
82      fits_report_error(stderr, status);
83      return FAILURE;
84    }
85
86    // Read the BUNIT keyword, and translate to standard unit format if needs be
87    std::string header("BUNIT");
88    fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
89    if (status){
90      DUCHAMPWARN("Cube Reader","Error reading BUNIT keyword: ");
91      fits_report_error(stderr, status);
92      return FAILURE;
93    }
94    else{
95      wcsutrn(0,unit);
96      this->fluxUnits = unit;
97      this->originalFluxUnits = unit;
98    }
99
100    // Close the FITS file
101    status = 0;
102    fits_close_file(fptr, &status);
103    if (status){
104      DUCHAMPWARN("Cube Reader","Error closing file: ");
105      fits_report_error(stderr, status);
106    }
107
108    delete [] comment;
109    delete [] unit;
110
111    return returnStatus;
112  }
113
114  //////////////////////////////////////////////////
115
116  OUTCOME FitsHeader::readBLANKinfo(std::string fname, Param &par)
117  {
118    ///    Reading in the Blank pixel value keywords, which is only done
119    ///    if requested via the flagBlankPix parameter.
120    ///
121    ///    If the BLANK keyword is in the header, use that and store the relevant
122    ///     values. Also copy them into the parameter set.
123    ///    If not, use the default value (either the default from param.cc or
124    ///     from the param file) and assume simple values for the keywords:
125    ///     <ul><li> The scale keyword is the same as the blank value,
126    ///         <li> The blank keyword (which is an int) is 1 and
127    ///         <li> The bzero (offset) is 0.
128    ///    </ul>
129    /// \param fname The name of the FITS file.
130    /// \param par The Param set: to know the flagBlankPix value and to
131    /// store the keywords.
132
133    OUTCOME returnStatus = SUCCESS;
134    int status = 0;
135
136    fitsfile *fptr;         
137    char *comment = new char[FLEN_COMMENT];
138    int blank;
139    float bscale, bzero;
140   
141    // Open the FITS file.
142    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
143      fits_report_error(stderr, status);
144      return FAILURE;
145    }
146
147    // Read the BLANK keyword.
148    //  If this isn't present, make sure flagTrim is false (if it is
149    //  currently true, let the user know you're doing this) and set
150    //  flagBlankPix to false so that the isBlank functions all return
151    //  false
152    //  If it is, read the other two necessary keywords, and then set
153    //     the values accordingly.
154
155    std::string header("BLANK");
156    if(fits_read_key(fptr, TINT, (char *)header.c_str(), &blank, comment, &status)){
157
158      par.setFlagBlankPix(false);
159
160      if(par.getFlagTrim()){
161        par.setFlagTrim(false);
162        std::stringstream errmsg;
163        if(status == KEY_NO_EXIST){
164          DUCHAMPWARN("Cube Reader", "There is no BLANK keyword present. Not doing any trimming.");
165        }
166        else{
167          DUCHAMPWARN("Cube Reader", "Error reading BLANK keyword, so not doing any trimming.");
168          fits_report_error(stderr, status);
169        }
170      }
171      returnStatus = FAILURE;
172    }
173    else{
174      status = 0;
175      header="BZERO";
176      fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bzero, comment, &status);
177      status = 0;
178      header="BSCALE";
179      fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bscale, NULL, &status);
180      this->blankKeyword  = blank;
181      this->bscaleKeyword = bscale;
182      this->bzeroKeyword  = bzero;
183      par.setFlagBlankPix(true);
184      par.setBlankKeyword(blank);
185      par.setBscaleKeyword(bscale);
186      par.setBzeroKeyword(bzero);
187      par.setBlankPixVal( blank*bscale + bzero );
188    }
189 
190    // Close the FITS file.
191    status = 0;
192    fits_close_file(fptr, &status);
193    if (status){
194      DUCHAMPWARN("Cube Reader","Error closing file: ");
195      fits_report_error(stderr, status);
196    }
197 
198    delete [] comment;
199
200    return returnStatus;
201
202  }
203
204  //////////////////////////////////////////////////
205
206  OUTCOME FitsHeader::readBeamInfo(std::string fname, Param &par)
207  {
208    ///   Reading in the beam parameters from the header.
209    ///   Use these, plus the basic WCS parameters to calculate the size of
210    ///    the beam in pixels. Copy the beam size into the parameter set.
211    ///   If information not present in FITS header, use the parameter
212    ///    set to define the beam size.
213    /// \param fname The name of the FITS file.
214    /// \param par The Param set.
215
216    int status=0;
217    fitsfile *fptr;         
218
219    // Open the FITS file
220    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
221      fits_report_error(stderr, status);
222      return FAILURE;
223    }
224   
225    this->itsBeam.readFromFITS(fptr, par, this->getAvPixScale());
226   
227    // Close the FITS file.
228    status=0;
229    fits_close_file(fptr, &status);
230    if (status){
231      DUCHAMPWARN("Cube Reader","Error closing file: ");
232      fits_report_error(stderr, status);
233    }
234
235    return SUCCESS;
236  }
237
238}
Note: See TracBrowser for help on using the repository browser.