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
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  {
[161]45
[698]46    OUTCOME returnValue = SUCCESS;
[378]47
[970]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    }
[971]56    else {
57      returnValue = this->readHeaderInfo(fptr,par);
[159]58
[970]59      // Close the FITS file.
60      status = 0;
61      fits_close_file(fptr, &status);
62      if (status){
[971]63        DUCHAMPWARN("Cube Reader","Error closing file: ");
64        fits_report_error(stderr, status);
[970]65      }
[971]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.");
[945]77    }
[971]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
[999]94    //  integrated flux when we go to fixSpectralUnits.
[971]95    OUTCOME bunitResult = this->readBUNIT(fptr);
96     
97    // if(this->readBLANKinfo(fname, par)==FAILURE)
98    OUTCOME blankResult = this->readBLANKinfo(fptr, par);
[970]99 
[971]100    OUTCOME beamResult = this->readBeamInfo(fptr, par);
101
102    if(this->wcs->spec>=0) this->fixSpectralUnits(par.getSpectralUnits());
[1214]103   
104    this->setIntFluxUnits();
[971]105
106    if(bunitResult == FAILURE || blankResult==FAILURE || beamResult==FAILURE ){
107      DUCHAMPWARN("Cube Reader","Header could not be read completely");
108      returnValue = FAILURE;
109    }
110
[378]111    return returnValue;
112  }
[159]113
114
[378]115  //////////////////////////////////////////////////
[159]116
[970]117  OUTCOME FitsHeader::readBUNIT(fitsfile *fptr)
[378]118  {
[528]119    ///   Read the BUNIT header keyword, to store the units of brightness (flux).
120    ///  \param fname The name of the FITS file.
121
[378]122    char *comment = new char[FLEN_COMMENT];
123    char *unit = new char[FLEN_VALUE];
[698]124    OUTCOME returnStatus=SUCCESS;
125    int status = 0;
[161]126
[971]127    // Read the BUNIT keyword, and translate to standard unit format if needs be
[643]128    std::string header("BUNIT");
[698]129    fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
130    if (status){
[913]131      DUCHAMPWARN("Cube Reader","Error reading BUNIT keyword: ");
[698]132      fits_report_error(stderr, status);
[945]133      returnStatus =  FAILURE;
[378]134    }
135    else{
136      wcsutrn(0,unit);
137      this->fluxUnits = unit;
[757]138      this->originalFluxUnits = unit;
[945]139      returnStatus = SUCCESS;
[378]140    }
[161]141
[378]142    delete [] comment;
143    delete [] unit;
[159]144
[378]145    return returnStatus;
146  }
[159]147
[378]148  //////////////////////////////////////////////////
[159]149
[970]150  OUTCOME FitsHeader::readBLANKinfo(fitsfile *fptr, Param &par)
[378]151  {
[528]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
[698]167    OUTCOME returnStatus = SUCCESS;
168    int status = 0;
[161]169
[378]170    char *comment = new char[FLEN_COMMENT];
171    int blank;
172    float bscale, bzero;
[970]173 
[378]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.
[285]181
[643]182    std::string header("BLANK");
[698]183    if(fits_read_key(fptr, TINT, (char *)header.c_str(), &blank, comment, &status)){
[285]184
[378]185      par.setFlagBlankPix(false);
[285]186
[378]187      if(par.getFlagTrim()){
188        par.setFlagTrim(false);
[913]189        if(status == KEY_NO_EXIST){
190          DUCHAMPWARN("Cube Reader", "There is no BLANK keyword present. Not doing any trimming.");
191        }
[378]192        else{
[913]193          DUCHAMPWARN("Cube Reader", "Error reading BLANK keyword, so not doing any trimming.");
[698]194          fits_report_error(stderr, status);
[378]195        }
[945]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.
[285]197      }
[160]198    }
[378]199    else{
200      status = 0;
[643]201      header="BZERO";
202      fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bzero, comment, &status);
[378]203      status = 0;
[643]204      header="BSCALE";
205      fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bscale, NULL, &status);
[378]206      this->blankKeyword  = blank;
207      this->bscaleKeyword = bscale;
208      this->bzeroKeyword  = bzero;
[845]209      par.setFlagBlankPix(true);
[378]210      par.setBlankKeyword(blank);
211      par.setBscaleKeyword(bscale);
212      par.setBzeroKeyword(bzero);
213      par.setBlankPixVal( blank*bscale + bzero );
214    }
215 
216    delete [] comment;
[160]217
[378]218    return returnStatus;
[159]219
[378]220  }
[159]221
[378]222  //////////////////////////////////////////////////
[159]223
[970]224  OUTCOME FitsHeader::readBeamInfo(fitsfile *fptr, Param &par)
[378]225  {
[528]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
[788]234    this->itsBeam.readFromFITS(fptr, par, this->getAvPixScale());
[181]235   
[971]236 
[698]237    return SUCCESS;
[378]238  }
239
[159]240}
Note: See TracBrowser for help on using the repository browser.