source: tags/release-1.1.12/src/FitsIO/headerIO.cc

Last change on this file was 795, checked in by MatthewWhiting, 13 years ago

Getting the integrated flux units right - needed to reorder the way we called certain functions, so that we didn't set the integrated flux units too early.

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