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

Last change on this file since 793 was 791, checked in by MatthewWhiting, 13 years ago

Making sure it works with the redesigned needBeamSize function (ie. not using it before reading beam from file). Also removing a lot of redundant code.

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