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

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

Removing the call to setIntFluxUnits from fixSpectralUnits - they need to be considered separately.

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