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

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

Fixes from ASKAPsoft ticket 3386, where the reconstruction was resulting in a segfault. Two problems: first the flagBlankPix defaults to true, so it is true for the ASKAP case. Second, it was then
finding the minimum scale a second time, but it was just setting the minimum dimension to the adjusted xdim, rather than comparing to the previously-determined minimum (this is a problem since the
spectral dimension in the case under consideration is smaller than the spatial). This has been fixed. Also make sure we explicitly set the flagBlankPix to true when we read the data in (when there are blank pixels).

File size: 7.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  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    // 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;
53 
54    if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
55 
56    if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
57
58    if(this->wcs->spec>=0) this->fixUnits(par);
59
60    return returnValue;
61  }
62
63
64  //////////////////////////////////////////////////
65
66  OUTCOME FitsHeader::readBUNIT(std::string fname)
67  {
68    ///   Read the BUNIT header keyword, to store the units of brightness (flux).
69    ///  \param fname The name of the FITS file.
70
71    fitsfile *fptr;         
72    char *comment = new char[FLEN_COMMENT];
73    char *unit = new char[FLEN_VALUE];
74    OUTCOME returnStatus=SUCCESS;
75    int status = 0;
76
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    }
83
84    // Read the BUNIT keyword, and translate to standard unit format if needs be
85    std::string header("BUNIT");
86    fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
87    if (status){
88      duchampWarning("Cube Reader","Error reading BUNIT keyword: ");
89      fits_report_error(stderr, status);
90      return FAILURE;
91    }
92    else{
93      wcsutrn(0,unit);
94      this->fluxUnits = unit;
95      this->originalFluxUnits = unit;
96    }
97
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    }
105
106    delete [] comment;
107    delete [] unit;
108
109    return returnStatus;
110  }
111
112  //////////////////////////////////////////////////
113
114  OUTCOME FitsHeader::readBLANKinfo(std::string fname, Param &par)
115  {
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
131    OUTCOME returnStatus = SUCCESS;
132    int status = 0;
133
134    fitsfile *fptr;         
135    char *comment = new char[FLEN_COMMENT];
136    int blank;
137    float bscale, bzero;
138   
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    }
144
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.
152
153    std::string header("BLANK");
154    if(fits_read_key(fptr, TINT, (char *)header.c_str(), &blank, comment, &status)){
155
156      par.setFlagBlankPix(false);
157
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.");
167          fits_report_error(stderr, status);
168          return FAILURE;
169        }
170      }
171    }
172    else{
173      status = 0;
174      header="BZERO";
175      fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bzero, comment, &status);
176      status = 0;
177      header="BSCALE";
178      fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bscale, NULL, &status);
179      this->blankKeyword  = blank;
180      this->bscaleKeyword = bscale;
181      this->bzeroKeyword  = bzero;
182      par.setFlagBlankPix(true);
183      par.setBlankKeyword(blank);
184      par.setBscaleKeyword(bscale);
185      par.setBzeroKeyword(bzero);
186      par.setBlankPixVal( blank*bscale + bzero );
187    }
188 
189    // Close the FITS file.
190    status = 0;
191    fits_close_file(fptr, &status);
192    if (status){
193      duchampWarning("Cube Reader","Error closing file: ");
194      fits_report_error(stderr, status);
195    }
196 
197    delete [] comment;
198
199    return returnStatus;
200
201  }
202
203  //////////////////////////////////////////////////
204
205  OUTCOME FitsHeader::readBeamInfo(std::string fname, Param &par)
206  {
207    ///   Reading in the beam parameters from the header.
208    ///   Use these, plus the basic WCS parameters to calculate the size of
209    ///    the beam in pixels. Copy the beam size into the parameter set.
210    ///   If information not present in FITS header, use the parameter
211    ///    set to define the beam size.
212    /// \param fname The name of the FITS file.
213    /// \param par The Param set.
214
215    int status=0;
216    fitsfile *fptr;         
217
218    // Open the FITS file
219    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
220      fits_report_error(stderr, status);
221      return FAILURE;
222    }
223   
224    this->itsBeam.readFromFITS(fptr, par, this->getAvPixScale());
225   
226    // Close the FITS file.
227    status=0;
228    fits_close_file(fptr, &status);
229    if (status){
230      duchampWarning("Cube Reader","Error closing file: ");
231      fits_report_error(stderr, status);
232    }
233
234    return SUCCESS;
235  }
236
237}
Note: See TracBrowser for help on using the repository browser.