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

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

Final bunch of changes for #95, where we remove the old duchampWarning & duchampError from duchamp.hh and get rid of the last references to them.

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        if(status == KEY_NO_EXIST){
163          DUCHAMPWARN("Cube Reader", "There is no BLANK keyword present. Not doing any trimming.");
164        }
165        else{
166          DUCHAMPWARN("Cube Reader", "Error reading BLANK keyword, so not doing any trimming.");
167          fits_report_error(stderr, status);
168        }
169      }
170      returnStatus = FAILURE;
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.setFlagBlankPix(true);
183      par.setBlankKeyword(blank);
184      par.setBscaleKeyword(bscale);
185      par.setBzeroKeyword(bzero);
186      par.setBlankPixVal( blank*bscale + bzero );
187    }
188 
189    // Close the FITS file.
190    status = 0;
191    fits_close_file(fptr, &status);
192    if (status){
193      DUCHAMPWARN("Cube Reader","Error closing file: ");
194      fits_report_error(stderr, status);
195    }
196 
197    delete [] comment;
198
199    return returnStatus;
200
201  }
202
203  //////////////////////////////////////////////////
204
205  OUTCOME FitsHeader::readBeamInfo(std::string fname, Param &par)
206  {
207    ///   Reading in the beam parameters from the header.
208    ///   Use these, plus the basic WCS parameters to calculate the size of
209    ///    the beam in pixels. Copy the beam size into the parameter set.
210    ///   If information not present in FITS header, use the parameter
211    ///    set to define the beam size.
212    /// \param fname The name of the FITS file.
213    /// \param par The Param set.
214
215    int status=0;
216    fitsfile *fptr;         
217
218    // Open the FITS file
219    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
220      fits_report_error(stderr, status);
221      return FAILURE;
222    }
223   
224    this->itsBeam.readFromFITS(fptr, par, this->getAvPixScale());
225   
226    // Close the FITS file.
227    status=0;
228    fits_close_file(fptr, &status);
229    if (status){
230      DUCHAMPWARN("Cube Reader","Error closing file: ");
231      fits_report_error(stderr, status);
232    }
233
234    return SUCCESS;
235  }
236
237}
Note: See TracBrowser for help on using the repository browser.