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

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

Getting the integrated flux units right - needed to reorder the way we called certain functions, so that we didn't set the integrated flux units too early.

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