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 | |
---|
40 | namespace duchamp |
---|
41 | { |
---|
42 | |
---|
43 | OUTCOME FitsHeader::readHeaderInfo(std::string fname, Param &par) |
---|
44 | { |
---|
45 | |
---|
46 | OUTCOME returnValue = SUCCESS; |
---|
47 | |
---|
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 | } |
---|
56 | else { |
---|
57 | returnValue = this->readHeaderInfo(fptr,par); |
---|
58 | |
---|
59 | // Close the FITS file. |
---|
60 | status = 0; |
---|
61 | fits_close_file(fptr, &status); |
---|
62 | if (status){ |
---|
63 | DUCHAMPWARN("Cube Reader","Error closing file: "); |
---|
64 | fits_report_error(stderr, status); |
---|
65 | } |
---|
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."); |
---|
77 | } |
---|
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 |
---|
94 | // integrated flux when we go to fixSpectralUnits. |
---|
95 | OUTCOME bunitResult = this->readBUNIT(fptr); |
---|
96 | |
---|
97 | // if(this->readBLANKinfo(fname, par)==FAILURE) |
---|
98 | OUTCOME blankResult = this->readBLANKinfo(fptr, par); |
---|
99 | |
---|
100 | OUTCOME beamResult = this->readBeamInfo(fptr, par); |
---|
101 | |
---|
102 | if(this->wcs->spec>=0) this->fixSpectralUnits(par.getSpectralUnits()); |
---|
103 | |
---|
104 | if(bunitResult == FAILURE || blankResult==FAILURE || beamResult==FAILURE ){ |
---|
105 | DUCHAMPWARN("Cube Reader","Header could not be read completely"); |
---|
106 | returnValue = FAILURE; |
---|
107 | } |
---|
108 | |
---|
109 | return returnValue; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | ////////////////////////////////////////////////// |
---|
114 | |
---|
115 | OUTCOME FitsHeader::readBUNIT(fitsfile *fptr) |
---|
116 | { |
---|
117 | /// Read the BUNIT header keyword, to store the units of brightness (flux). |
---|
118 | /// \param fname The name of the FITS file. |
---|
119 | |
---|
120 | char *comment = new char[FLEN_COMMENT]; |
---|
121 | char *unit = new char[FLEN_VALUE]; |
---|
122 | OUTCOME returnStatus=SUCCESS; |
---|
123 | int status = 0; |
---|
124 | |
---|
125 | // Read the BUNIT keyword, and translate to standard unit format if needs be |
---|
126 | std::string header("BUNIT"); |
---|
127 | fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status); |
---|
128 | if (status){ |
---|
129 | DUCHAMPWARN("Cube Reader","Error reading BUNIT keyword: "); |
---|
130 | fits_report_error(stderr, status); |
---|
131 | returnStatus = FAILURE; |
---|
132 | } |
---|
133 | else{ |
---|
134 | wcsutrn(0,unit); |
---|
135 | this->fluxUnits = unit; |
---|
136 | this->originalFluxUnits = unit; |
---|
137 | returnStatus = SUCCESS; |
---|
138 | } |
---|
139 | |
---|
140 | delete [] comment; |
---|
141 | delete [] unit; |
---|
142 | |
---|
143 | return returnStatus; |
---|
144 | } |
---|
145 | |
---|
146 | ////////////////////////////////////////////////// |
---|
147 | |
---|
148 | OUTCOME FitsHeader::readBLANKinfo(fitsfile *fptr, Param &par) |
---|
149 | { |
---|
150 | /// Reading in the Blank pixel value keywords, which is only done |
---|
151 | /// if requested via the flagBlankPix parameter. |
---|
152 | /// |
---|
153 | /// If the BLANK keyword is in the header, use that and store the relevant |
---|
154 | /// values. Also copy them into the parameter set. |
---|
155 | /// If not, use the default value (either the default from param.cc or |
---|
156 | /// from the param file) and assume simple values for the keywords: |
---|
157 | /// <ul><li> The scale keyword is the same as the blank value, |
---|
158 | /// <li> The blank keyword (which is an int) is 1 and |
---|
159 | /// <li> The bzero (offset) is 0. |
---|
160 | /// </ul> |
---|
161 | /// \param fname The name of the FITS file. |
---|
162 | /// \param par The Param set: to know the flagBlankPix value and to |
---|
163 | /// store the keywords. |
---|
164 | |
---|
165 | OUTCOME returnStatus = SUCCESS; |
---|
166 | int status = 0; |
---|
167 | |
---|
168 | char *comment = new char[FLEN_COMMENT]; |
---|
169 | int blank; |
---|
170 | float bscale, bzero; |
---|
171 | |
---|
172 | // Read the BLANK keyword. |
---|
173 | // If this isn't present, make sure flagTrim is false (if it is |
---|
174 | // currently true, let the user know you're doing this) and set |
---|
175 | // flagBlankPix to false so that the isBlank functions all return |
---|
176 | // false |
---|
177 | // If it is, read the other two necessary keywords, and then set |
---|
178 | // the values accordingly. |
---|
179 | |
---|
180 | std::string header("BLANK"); |
---|
181 | if(fits_read_key(fptr, TINT, (char *)header.c_str(), &blank, comment, &status)){ |
---|
182 | |
---|
183 | par.setFlagBlankPix(false); |
---|
184 | |
---|
185 | if(par.getFlagTrim()){ |
---|
186 | par.setFlagTrim(false); |
---|
187 | if(status == KEY_NO_EXIST){ |
---|
188 | DUCHAMPWARN("Cube Reader", "There is no BLANK keyword present. Not doing any trimming."); |
---|
189 | } |
---|
190 | else{ |
---|
191 | DUCHAMPWARN("Cube Reader", "Error reading BLANK keyword, so not doing any trimming."); |
---|
192 | fits_report_error(stderr, status); |
---|
193 | } |
---|
194 | returnStatus = FAILURE; //only return a failure if we care whether the BLANK keyword is present, which is only if the Trim flag is set. |
---|
195 | } |
---|
196 | } |
---|
197 | else{ |
---|
198 | status = 0; |
---|
199 | header="BZERO"; |
---|
200 | fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bzero, comment, &status); |
---|
201 | status = 0; |
---|
202 | header="BSCALE"; |
---|
203 | fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bscale, NULL, &status); |
---|
204 | this->blankKeyword = blank; |
---|
205 | this->bscaleKeyword = bscale; |
---|
206 | this->bzeroKeyword = bzero; |
---|
207 | par.setFlagBlankPix(true); |
---|
208 | par.setBlankKeyword(blank); |
---|
209 | par.setBscaleKeyword(bscale); |
---|
210 | par.setBzeroKeyword(bzero); |
---|
211 | par.setBlankPixVal( blank*bscale + bzero ); |
---|
212 | } |
---|
213 | |
---|
214 | delete [] comment; |
---|
215 | |
---|
216 | return returnStatus; |
---|
217 | |
---|
218 | } |
---|
219 | |
---|
220 | ////////////////////////////////////////////////// |
---|
221 | |
---|
222 | OUTCOME FitsHeader::readBeamInfo(fitsfile *fptr, Param &par) |
---|
223 | { |
---|
224 | /// Reading in the beam parameters from the header. |
---|
225 | /// Use these, plus the basic WCS parameters to calculate the size of |
---|
226 | /// the beam in pixels. Copy the beam size into the parameter set. |
---|
227 | /// If information not present in FITS header, use the parameter |
---|
228 | /// set to define the beam size. |
---|
229 | /// \param fname The name of the FITS file. |
---|
230 | /// \param par The Param set. |
---|
231 | |
---|
232 | this->itsBeam.readFromFITS(fptr, par, this->getAvPixScale()); |
---|
233 | |
---|
234 | |
---|
235 | return SUCCESS; |
---|
236 | } |
---|
237 | |
---|
238 | } |
---|