source: tags/release-1.1.10/src/FitsIO/headerIO.cc @ 1323

Last change on this file since 1323 was 757, checked in by MatthewWhiting, 14 years ago

A few changes to get the writing of recon cubes right - the problem was when the flux units were changed. We need to change them back before the cube is written.

File size: 8.8 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    char *comment = new char[80];
212    std::string keyword[3]={"BMAJ","BMIN","BPA"};
213    float bmaj,bmin,bpa;
214    int status=0;
215    fitsfile *fptr;         
216
217    // Open the FITS file
218    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
219      fits_report_error(stderr, status);
220      return FAILURE;
221    }
222
223    // Read the Keywords -- first look for BMAJ. If it is present, read the
224    //   others, and calculate the beam size.
225    // If it is not, give warning and set beam size to nominal value.
226    int bstatus[3]={0,0,0};
227    fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj, comment, &bstatus[0]);
228    fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin, comment, &bstatus[1]);
229    fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa, comment,  &bstatus[2]);
230
231    if(bstatus[0]||bstatus[1]||bstatus[2]){ // error
232      std::string paramName;
233      if(par.getBeamFWHM()>0.){
234        this->setBeamSize( getBeamArea(par.getBeamFWHM(),par.getBeamFWHM()) );
235        par.setBeamSize(this->beamSize);
236        paramName = "beamFWHM";
237      }
238      else{
239        this->setBeamSize(par.getBeamSize());
240        paramName = "beamArea";
241      }
242      par.setFlagUsingBeam(true);
243      std::stringstream errmsg;
244      errmsg << "Header keywords not present: ";
245      for(int i=0;i<3;i++) if(bstatus[i]) errmsg<<keyword[i]<<" ";
246      errmsg << "\nUsing parameter "<< paramName <<" to determine size of beam.\n";
247      duchampWarning("Cube Reader",errmsg.str());
248    }
249    else{ // all keywords present
250      float pixScale = this->getAvPixScale();
251      this->setBeamSize( getBeamArea(bmaj/pixScale, bmin/pixScale) );
252      this->setBmajKeyword(bmaj);
253      this->setBminKeyword(bmin);
254      this->setBpaKeyword(bpa);
255      par.setBeamSize(this->beamSize);
256    }
257   
258    // Close the FITS file.
259    status=0;
260    fits_close_file(fptr, &status);
261    if (status){
262      duchampWarning("Cube Reader","Error closing file: ");
263      fits_report_error(stderr, status);
264    }
265
266    delete [] comment;
267
268    return SUCCESS;
269  }
270
271}
Note: See TracBrowser for help on using the repository browser.