[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] | 40 | namespace duchamp |
---|
[159] | 41 | { |
---|
| 42 | |
---|
[698] | 43 | OUTCOME FitsHeader::readHeaderInfo(std::string fname, Param &par) |
---|
[378] | 44 | { |
---|
[528] | 45 | /// A simple front-end function to the three header access |
---|
| 46 | /// functions defined below. |
---|
[161] | 47 | |
---|
[698] | 48 | OUTCOME returnValue = SUCCESS; |
---|
[378] | 49 | |
---|
| 50 | // if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE; |
---|
[159] | 51 | |
---|
[378] | 52 | if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
[159] | 53 | |
---|
[791] | 54 | // if(this->needBeamSize()) |
---|
[378] | 55 | if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
[159] | 56 | |
---|
[378] | 57 | return returnValue; |
---|
| 58 | } |
---|
[159] | 59 | |
---|
| 60 | |
---|
[378] | 61 | ////////////////////////////////////////////////// |
---|
[159] | 62 | |
---|
[698] | 63 | OUTCOME FitsHeader::readBUNIT(std::string fname) |
---|
[378] | 64 | { |
---|
[528] | 65 | /// Read the BUNIT header keyword, to store the units of brightness (flux). |
---|
| 66 | /// \param fname The name of the FITS file. |
---|
| 67 | |
---|
[378] | 68 | fitsfile *fptr; |
---|
| 69 | char *comment = new char[FLEN_COMMENT]; |
---|
| 70 | char *unit = new char[FLEN_VALUE]; |
---|
[698] | 71 | OUTCOME returnStatus=SUCCESS; |
---|
| 72 | int status = 0; |
---|
[161] | 73 | |
---|
[378] | 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 | } |
---|
[161] | 80 | |
---|
[378] | 81 | // Read the BUNIT keyword, and translate to standard unit format if needs be |
---|
[643] | 82 | std::string header("BUNIT"); |
---|
[698] | 83 | fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status); |
---|
| 84 | if (status){ |
---|
[378] | 85 | duchampWarning("Cube Reader","Error reading BUNIT keyword: "); |
---|
[698] | 86 | fits_report_error(stderr, status); |
---|
| 87 | return FAILURE; |
---|
[378] | 88 | } |
---|
| 89 | else{ |
---|
| 90 | wcsutrn(0,unit); |
---|
| 91 | this->fluxUnits = unit; |
---|
[757] | 92 | this->originalFluxUnits = unit; |
---|
[378] | 93 | } |
---|
[161] | 94 | |
---|
[378] | 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 | } |
---|
[159] | 102 | |
---|
[378] | 103 | delete [] comment; |
---|
| 104 | delete [] unit; |
---|
[159] | 105 | |
---|
[378] | 106 | return returnStatus; |
---|
| 107 | } |
---|
[159] | 108 | |
---|
[378] | 109 | ////////////////////////////////////////////////// |
---|
[159] | 110 | |
---|
[698] | 111 | OUTCOME FitsHeader::readBLANKinfo(std::string fname, Param &par) |
---|
[378] | 112 | { |
---|
[528] | 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 | |
---|
[698] | 128 | OUTCOME returnStatus = SUCCESS; |
---|
| 129 | int status = 0; |
---|
[161] | 130 | |
---|
[378] | 131 | fitsfile *fptr; |
---|
| 132 | char *comment = new char[FLEN_COMMENT]; |
---|
| 133 | int blank; |
---|
| 134 | float bscale, bzero; |
---|
[161] | 135 | |
---|
[378] | 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 | } |
---|
[161] | 141 | |
---|
[378] | 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. |
---|
[285] | 149 | |
---|
[643] | 150 | std::string header("BLANK"); |
---|
[698] | 151 | if(fits_read_key(fptr, TINT, (char *)header.c_str(), &blank, comment, &status)){ |
---|
[285] | 152 | |
---|
[378] | 153 | par.setFlagBlankPix(false); |
---|
[285] | 154 | |
---|
[378] | 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."); |
---|
[698] | 164 | fits_report_error(stderr, status); |
---|
| 165 | return FAILURE; |
---|
[378] | 166 | } |
---|
[285] | 167 | } |
---|
[160] | 168 | } |
---|
[378] | 169 | else{ |
---|
| 170 | status = 0; |
---|
[643] | 171 | header="BZERO"; |
---|
| 172 | fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bzero, comment, &status); |
---|
[378] | 173 | status = 0; |
---|
[643] | 174 | header="BSCALE"; |
---|
| 175 | fits_read_key(fptr, TFLOAT, (char *)header.c_str(), &bscale, NULL, &status); |
---|
[378] | 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. |
---|
[160] | 186 | status = 0; |
---|
[378] | 187 | fits_close_file(fptr, &status); |
---|
| 188 | if (status){ |
---|
| 189 | duchampWarning("Cube Reader","Error closing file: "); |
---|
| 190 | fits_report_error(stderr, status); |
---|
| 191 | } |
---|
[160] | 192 | |
---|
[378] | 193 | delete [] comment; |
---|
[160] | 194 | |
---|
[378] | 195 | return returnStatus; |
---|
[159] | 196 | |
---|
[378] | 197 | } |
---|
[159] | 198 | |
---|
[378] | 199 | ////////////////////////////////////////////////// |
---|
[159] | 200 | |
---|
[698] | 201 | OUTCOME FitsHeader::readBeamInfo(std::string fname, Param &par) |
---|
[378] | 202 | { |
---|
[528] | 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 | |
---|
[698] | 211 | int status=0; |
---|
[378] | 212 | fitsfile *fptr; |
---|
[161] | 213 | |
---|
[378] | 214 | // Open the FITS file |
---|
[698] | 215 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 216 | fits_report_error(stderr, status); |
---|
[378] | 217 | return FAILURE; |
---|
| 218 | } |
---|
[791] | 219 | |
---|
[788] | 220 | this->itsBeam.readFromFITS(fptr, par, this->getAvPixScale()); |
---|
[181] | 221 | |
---|
[378] | 222 | // Close the FITS file. |
---|
[698] | 223 | status=0; |
---|
| 224 | fits_close_file(fptr, &status); |
---|
| 225 | if (status){ |
---|
[378] | 226 | duchampWarning("Cube Reader","Error closing file: "); |
---|
[698] | 227 | fits_report_error(stderr, status); |
---|
[378] | 228 | } |
---|
[159] | 229 | |
---|
[698] | 230 | return SUCCESS; |
---|
[378] | 231 | } |
---|
| 232 | |
---|
[159] | 233 | } |
---|