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

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

Getting the spectral units conversion working properly:

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