[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 | |
---|
[378] | 43 | int FitsHeader::readHeaderInfo(std::string fname, Param &par) |
---|
| 44 | { |
---|
| 45 | /** |
---|
| 46 | * A simple front-end function to the three header access |
---|
| 47 | * functions defined below. |
---|
| 48 | * |
---|
| 49 | */ |
---|
[161] | 50 | |
---|
[378] | 51 | int returnValue = SUCCESS; |
---|
| 52 | |
---|
| 53 | // if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE; |
---|
[159] | 54 | |
---|
[378] | 55 | if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
[159] | 56 | |
---|
[378] | 57 | if(this->needBeamSize()) |
---|
| 58 | if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
[159] | 59 | |
---|
[378] | 60 | return returnValue; |
---|
| 61 | } |
---|
[159] | 62 | |
---|
| 63 | |
---|
[378] | 64 | ////////////////////////////////////////////////// |
---|
[159] | 65 | |
---|
[378] | 66 | int FitsHeader::readBUNIT(std::string fname) |
---|
| 67 | { |
---|
| 68 | /** |
---|
| 69 | * Read the BUNIT header keyword, to store the units of brightness (flux). |
---|
| 70 | * \param fname The name of the FITS file. |
---|
| 71 | */ |
---|
| 72 | fitsfile *fptr; |
---|
| 73 | char *comment = new char[FLEN_COMMENT]; |
---|
| 74 | char *unit = new char[FLEN_VALUE]; |
---|
| 75 | int returnStatus = 0, status = 0; |
---|
[161] | 76 | |
---|
[378] | 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 | } |
---|
[161] | 83 | |
---|
[378] | 84 | // Read the BUNIT keyword, and translate to standard unit format if needs be |
---|
| 85 | fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus); |
---|
| 86 | if (returnStatus){ |
---|
| 87 | duchampWarning("Cube Reader","Error reading BUNIT keyword: "); |
---|
| 88 | fits_report_error(stderr, returnStatus); |
---|
| 89 | } |
---|
| 90 | else{ |
---|
| 91 | wcsutrn(0,unit); |
---|
| 92 | this->fluxUnits = unit; |
---|
| 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 | |
---|
[378] | 111 | int FitsHeader::readBLANKinfo(std::string fname, Param &par) |
---|
| 112 | { |
---|
| 113 | /** |
---|
| 114 | * Reading in the Blank pixel value keywords, which is only done |
---|
| 115 | * if requested via the flagBlankPix parameter. |
---|
| 116 | * |
---|
| 117 | * If the BLANK keyword is in the header, use that and store the relevant |
---|
| 118 | * values. Also copy them into the parameter set. |
---|
| 119 | * If not, use the default value (either the default from param.cc or |
---|
| 120 | * from the param file) and assume simple values for the keywords: |
---|
| 121 | * <ul><li> The scale keyword is the same as the blank value, |
---|
| 122 | * <li> The blank keyword (which is an int) is 1 and |
---|
| 123 | * <li> The bzero (offset) is 0. |
---|
| 124 | * </ul> |
---|
| 125 | * \param fname The name of the FITS file. |
---|
| 126 | * \param par The Param set: to know the flagBlankPix value and to |
---|
| 127 | * store the keywords. |
---|
| 128 | */ |
---|
| 129 | int returnStatus = 0, 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 | |
---|
[378] | 150 | if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){ |
---|
[285] | 151 | |
---|
[378] | 152 | par.setFlagBlankPix(false); |
---|
[285] | 153 | |
---|
[378] | 154 | if(par.getFlagTrim()){ |
---|
| 155 | par.setFlagTrim(false); |
---|
| 156 | std::stringstream errmsg; |
---|
| 157 | if(returnStatus == KEY_NO_EXIST) |
---|
| 158 | duchampWarning("Cube Reader", |
---|
| 159 | "There is no BLANK keyword present. Not doing any trimming.\n"); |
---|
| 160 | else{ |
---|
| 161 | duchampWarning("Cube Reader", |
---|
| 162 | "Error reading BLANK keyword, so not doing any trimming."); |
---|
| 163 | fits_report_error(stderr, returnStatus); |
---|
| 164 | } |
---|
[285] | 165 | } |
---|
[160] | 166 | } |
---|
[378] | 167 | else{ |
---|
| 168 | status = 0; |
---|
| 169 | fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status); |
---|
| 170 | status = 0; |
---|
| 171 | fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status); |
---|
| 172 | this->blankKeyword = blank; |
---|
| 173 | this->bscaleKeyword = bscale; |
---|
| 174 | this->bzeroKeyword = bzero; |
---|
| 175 | par.setBlankKeyword(blank); |
---|
| 176 | par.setBscaleKeyword(bscale); |
---|
| 177 | par.setBzeroKeyword(bzero); |
---|
| 178 | par.setBlankPixVal( blank*bscale + bzero ); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | // Close the FITS file. |
---|
[160] | 182 | status = 0; |
---|
[378] | 183 | fits_close_file(fptr, &status); |
---|
| 184 | if (status){ |
---|
| 185 | duchampWarning("Cube Reader","Error closing file: "); |
---|
| 186 | fits_report_error(stderr, status); |
---|
| 187 | } |
---|
[160] | 188 | |
---|
[378] | 189 | delete [] comment; |
---|
[160] | 190 | |
---|
[378] | 191 | return returnStatus; |
---|
[159] | 192 | |
---|
[378] | 193 | } |
---|
[159] | 194 | |
---|
[378] | 195 | ////////////////////////////////////////////////// |
---|
[159] | 196 | |
---|
[378] | 197 | int FitsHeader::readBeamInfo(std::string fname, Param &par) |
---|
| 198 | { |
---|
| 199 | /** |
---|
| 200 | * Reading in the beam parameters from the header. |
---|
| 201 | * Use these, plus the basic WCS parameters to calculate the size of |
---|
| 202 | * the beam in pixels. Copy the beam size into the parameter set. |
---|
| 203 | * If information not present in FITS header, use the parameter |
---|
| 204 | * set to define the beam size. |
---|
| 205 | * \param fname The name of the FITS file. |
---|
| 206 | * \param par The Param set. |
---|
| 207 | */ |
---|
| 208 | char *comment = new char[80]; |
---|
[416] | 209 | std::string keyword[3]={"BMAJ","BMIN","BPA"}; |
---|
[415] | 210 | float bmaj,bmin,bpa; |
---|
[416] | 211 | int status[5]; |
---|
[378] | 212 | fitsfile *fptr; |
---|
[161] | 213 | |
---|
[416] | 214 | for(int i=0;i<5;i++) status[i] = 0; |
---|
[181] | 215 | |
---|
[378] | 216 | // Open the FITS file |
---|
| 217 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){ |
---|
| 218 | fits_report_error(stderr, status[0]); |
---|
| 219 | return FAILURE; |
---|
| 220 | } |
---|
[161] | 221 | |
---|
[378] | 222 | // Read the Keywords -- first look for BMAJ. If it is present, read the |
---|
| 223 | // others, and calculate the beam size. |
---|
| 224 | // If it is not, give warning and set beam size to nominal value. |
---|
| 225 | fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj, |
---|
| 226 | comment, &status[1]); |
---|
| 227 | fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin, |
---|
| 228 | comment, &status[2]); |
---|
| 229 | fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa, |
---|
| 230 | comment, &status[3]); |
---|
[181] | 231 | |
---|
[416] | 232 | if(status[1]||status[2]||status[3]){ // error |
---|
[378] | 233 | std::stringstream errmsg; |
---|
| 234 | errmsg << "Header keywords not present: "; |
---|
[415] | 235 | for(int i=0;i<3;i++) if(status[i+1]) errmsg<<keyword[i]<<" "; |
---|
[378] | 236 | errmsg << "\nUsing parameter beamSize to determine size of beam.\n"; |
---|
| 237 | duchampWarning("Cube Reader",errmsg.str()); |
---|
| 238 | this->setBeamSize(par.getBeamSize()); |
---|
| 239 | par.setFlagUsingBeam(true); |
---|
| 240 | } |
---|
| 241 | else{ // all keywords present |
---|
[415] | 242 | float pixScale = this->getAvPixScale(); |
---|
[428] | 243 | this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / (M_LN2*pixScale*pixScale) ); |
---|
[378] | 244 | this->setBmajKeyword(bmaj); |
---|
| 245 | this->setBminKeyword(bmin); |
---|
| 246 | this->setBpaKeyword(bpa); |
---|
| 247 | par.setBeamSize(this->beamSize); |
---|
| 248 | } |
---|
[181] | 249 | |
---|
[378] | 250 | // Close the FITS file. |
---|
[416] | 251 | fits_close_file(fptr, &status[4]); |
---|
| 252 | if (status[4]){ |
---|
[378] | 253 | duchampWarning("Cube Reader","Error closing file: "); |
---|
[416] | 254 | fits_report_error(stderr, status[4]); |
---|
[378] | 255 | } |
---|
[159] | 256 | |
---|
[378] | 257 | delete [] comment; |
---|
[159] | 258 | |
---|
[378] | 259 | int returnStatus = status[0]; |
---|
[416] | 260 | for(int i=1;i<5;i++) if(status[i]>returnStatus) returnStatus=status[i]; |
---|
[181] | 261 | |
---|
[378] | 262 | return returnStatus; |
---|
| 263 | } |
---|
| 264 | |
---|
[159] | 265 | } |
---|