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

Last change on this file since 1210 was 1210, checked in by MatthewWhiting, 11 years ago

Fixing bug where the integrated flux units were not being set. This occurred when there was no spectral axis present, and we ended up with "counts" as the unit string. Have removed the initialisation of "counts" as well.

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