source: tags/release-1.1.7/src/FitsIO/headerIO.cc @ 1455

Last change on this file since 1455 was 528, checked in by MatthewWhiting, 15 years ago

Changing the documentation comments to match the askapsoft style. Also have split ChanMap? and Object3D into separate files.

File size: 8.5 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  int 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    int 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  int 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    int returnStatus = 0, status = 0;
72
73    // Open the FITS file
74    status = 0;
75    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
76      fits_report_error(stderr, status);
77      return FAILURE;
78    }
79
80    // Read the BUNIT keyword, and translate to standard unit format if needs be
81    fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus);
82    if (returnStatus){
83      duchampWarning("Cube Reader","Error reading BUNIT keyword: ");
84      fits_report_error(stderr, returnStatus);
85    }
86    else{
87      wcsutrn(0,unit);
88      this->fluxUnits = unit;
89    }
90
91    // Close the FITS file
92    status = 0;
93    fits_close_file(fptr, &status);
94    if (status){
95      duchampWarning("Cube Reader","Error closing file: ");
96      fits_report_error(stderr, status);
97    }
98
99    delete [] comment;
100    delete [] unit;
101
102    return returnStatus;
103  }
104
105  //////////////////////////////////////////////////
106
107  int FitsHeader::readBLANKinfo(std::string fname, Param &par)
108  {
109    ///    Reading in the Blank pixel value keywords, which is only done
110    ///    if requested via the flagBlankPix parameter.
111    ///
112    ///    If the BLANK keyword is in the header, use that and store the relevant
113    ///     values. Also copy them into the parameter set.
114    ///    If not, use the default value (either the default from param.cc or
115    ///     from the param file) and assume simple values for the keywords:
116    ///     <ul><li> The scale keyword is the same as the blank value,
117    ///         <li> The blank keyword (which is an int) is 1 and
118    ///         <li> The bzero (offset) is 0.
119    ///    </ul>
120    /// \param fname The name of the FITS file.
121    /// \param par The Param set: to know the flagBlankPix value and to
122    /// store the keywords.
123
124    int returnStatus = 0, status = 0;
125
126    fitsfile *fptr;         
127    char *comment = new char[FLEN_COMMENT];
128    int blank;
129    float bscale, bzero;
130   
131    // Open the FITS file.
132    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
133      fits_report_error(stderr, status);
134      return FAILURE;
135    }
136
137    // Read the BLANK keyword.
138    //  If this isn't present, make sure flagTrim is false (if it is
139    //  currently true, let the user know you're doing this) and set
140    //  flagBlankPix to false so that the isBlank functions all return
141    //  false
142    //  If it is, read the other two necessary keywords, and then set
143    //     the values accordingly.
144
145    if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){
146
147      par.setFlagBlankPix(false);
148
149      if(par.getFlagTrim()){
150        par.setFlagTrim(false);
151        std::stringstream errmsg;
152        if(returnStatus == KEY_NO_EXIST)
153          duchampWarning("Cube Reader",
154                         "There is no BLANK keyword present. Not doing any trimming.\n");
155        else{
156          duchampWarning("Cube Reader",
157                         "Error reading BLANK keyword, so not doing any trimming.");
158          fits_report_error(stderr, returnStatus);
159        }
160      }
161    }
162    else{
163      status = 0;
164      fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
165      status = 0;
166      fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
167      this->blankKeyword  = blank;
168      this->bscaleKeyword = bscale;
169      this->bzeroKeyword  = bzero;
170      par.setBlankKeyword(blank);
171      par.setBscaleKeyword(bscale);
172      par.setBzeroKeyword(bzero);
173      par.setBlankPixVal( blank*bscale + bzero );
174    }
175 
176    // Close the FITS file.
177    status = 0;
178    fits_close_file(fptr, &status);
179    if (status){
180      duchampWarning("Cube Reader","Error closing file: ");
181      fits_report_error(stderr, status);
182    }
183 
184    delete [] comment;
185
186    return returnStatus;
187
188  }
189
190  //////////////////////////////////////////////////
191
192  int FitsHeader::readBeamInfo(std::string fname, Param &par)
193  {
194    ///   Reading in the beam parameters from the header.
195    ///   Use these, plus the basic WCS parameters to calculate the size of
196    ///    the beam in pixels. Copy the beam size into the parameter set.
197    ///   If information not present in FITS header, use the parameter
198    ///    set to define the beam size.
199    /// \param fname The name of the FITS file.
200    /// \param par The Param set.
201
202    char *comment = new char[80];
203    std::string keyword[3]={"BMAJ","BMIN","BPA"};
204    float bmaj,bmin,bpa;
205    int status[5];
206    fitsfile *fptr;         
207
208    for(int i=0;i<5;i++) status[i] = 0;
209
210    // Open the FITS file
211    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){
212      fits_report_error(stderr, status[0]);
213      return FAILURE;
214    }
215
216    // Read the Keywords -- first look for BMAJ. If it is present, read the
217    //   others, and calculate the beam size.
218    // If it is not, give warning and set beam size to nominal value.
219    fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj,
220                  comment, &status[1]);
221    fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin,
222                  comment, &status[2]);
223    fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa,
224                  comment, &status[3]);
225
226    if(status[1]||status[2]||status[3]){ // error
227      std::stringstream errmsg;
228      errmsg << "Header keywords not present: ";
229      for(int i=0;i<3;i++) if(status[i+1]) errmsg<<keyword[i]<<" ";
230      errmsg << "\nUsing parameter beamSize to determine size of beam.\n";
231      duchampWarning("Cube Reader",errmsg.str());
232      this->setBeamSize(par.getBeamSize());
233      par.setFlagUsingBeam(true);
234    }
235    else{ // all keywords present
236      float pixScale = this->getAvPixScale();
237      this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / (M_LN2*pixScale*pixScale) );
238      this->setBmajKeyword(bmaj);
239      this->setBminKeyword(bmin);
240      this->setBpaKeyword(bpa);
241      par.setBeamSize(this->beamSize);
242    }
243   
244    // Close the FITS file.
245    fits_close_file(fptr, &status[4]);
246    if (status[4]){
247      duchampWarning("Cube Reader","Error closing file: ");
248      fits_report_error(stderr, status[4]);
249    }
250
251    delete [] comment;
252
253    int returnStatus = status[0];
254    for(int i=1;i<5;i++) if(status[i]>returnStatus) returnStatus=status[i];
255
256    return returnStatus;
257  }
258
259}
Note: See TracBrowser for help on using the repository browser.