[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 | { |
---|
[528] | 45 | /// A simple front-end function to the three header access |
---|
| 46 | /// functions defined below. |
---|
[161] | 47 | |
---|
[378] | 48 | int returnValue = SUCCESS; |
---|
| 49 | |
---|
| 50 | // if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE; |
---|
[159] | 51 | |
---|
[378] | 52 | if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
[159] | 53 | |
---|
[378] | 54 | if(this->needBeamSize()) |
---|
| 55 | if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE; |
---|
[159] | 56 | |
---|
[378] | 57 | return returnValue; |
---|
| 58 | } |
---|
[159] | 59 | |
---|
| 60 | |
---|
[378] | 61 | ////////////////////////////////////////////////// |
---|
[159] | 62 | |
---|
[378] | 63 | int FitsHeader::readBUNIT(std::string fname) |
---|
| 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]; |
---|
| 71 | int returnStatus = 0, status = 0; |
---|
[161] | 72 | |
---|
[378] | 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 | } |
---|
[161] | 79 | |
---|
[378] | 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 | } |
---|
[161] | 90 | |
---|
[378] | 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 | } |
---|
[159] | 98 | |
---|
[378] | 99 | delete [] comment; |
---|
| 100 | delete [] unit; |
---|
[159] | 101 | |
---|
[378] | 102 | return returnStatus; |
---|
| 103 | } |
---|
[159] | 104 | |
---|
[378] | 105 | ////////////////////////////////////////////////// |
---|
[159] | 106 | |
---|
[378] | 107 | int FitsHeader::readBLANKinfo(std::string fname, Param &par) |
---|
| 108 | { |
---|
[528] | 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 | |
---|
[378] | 124 | int returnStatus = 0, status = 0; |
---|
[161] | 125 | |
---|
[378] | 126 | fitsfile *fptr; |
---|
| 127 | char *comment = new char[FLEN_COMMENT]; |
---|
| 128 | int blank; |
---|
| 129 | float bscale, bzero; |
---|
[161] | 130 | |
---|
[378] | 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 | } |
---|
[161] | 136 | |
---|
[378] | 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. |
---|
[285] | 144 | |
---|
[378] | 145 | if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){ |
---|
[285] | 146 | |
---|
[378] | 147 | par.setFlagBlankPix(false); |
---|
[285] | 148 | |
---|
[378] | 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 | } |
---|
[285] | 160 | } |
---|
[160] | 161 | } |
---|
[378] | 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. |
---|
[160] | 177 | status = 0; |
---|
[378] | 178 | fits_close_file(fptr, &status); |
---|
| 179 | if (status){ |
---|
| 180 | duchampWarning("Cube Reader","Error closing file: "); |
---|
| 181 | fits_report_error(stderr, status); |
---|
| 182 | } |
---|
[160] | 183 | |
---|
[378] | 184 | delete [] comment; |
---|
[160] | 185 | |
---|
[378] | 186 | return returnStatus; |
---|
[159] | 187 | |
---|
[378] | 188 | } |
---|
[159] | 189 | |
---|
[378] | 190 | ////////////////////////////////////////////////// |
---|
[159] | 191 | |
---|
[378] | 192 | int FitsHeader::readBeamInfo(std::string fname, Param &par) |
---|
| 193 | { |
---|
[528] | 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 | |
---|
[378] | 202 | char *comment = new char[80]; |
---|
[416] | 203 | std::string keyword[3]={"BMAJ","BMIN","BPA"}; |
---|
[415] | 204 | float bmaj,bmin,bpa; |
---|
[416] | 205 | int status[5]; |
---|
[378] | 206 | fitsfile *fptr; |
---|
[161] | 207 | |
---|
[416] | 208 | for(int i=0;i<5;i++) status[i] = 0; |
---|
[181] | 209 | |
---|
[378] | 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 | } |
---|
[161] | 215 | |
---|
[378] | 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]); |
---|
[181] | 225 | |
---|
[416] | 226 | if(status[1]||status[2]||status[3]){ // error |
---|
[378] | 227 | std::stringstream errmsg; |
---|
| 228 | errmsg << "Header keywords not present: "; |
---|
[415] | 229 | for(int i=0;i<3;i++) if(status[i+1]) errmsg<<keyword[i]<<" "; |
---|
[378] | 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 |
---|
[415] | 236 | float pixScale = this->getAvPixScale(); |
---|
[428] | 237 | this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / (M_LN2*pixScale*pixScale) ); |
---|
[378] | 238 | this->setBmajKeyword(bmaj); |
---|
| 239 | this->setBminKeyword(bmin); |
---|
| 240 | this->setBpaKeyword(bpa); |
---|
| 241 | par.setBeamSize(this->beamSize); |
---|
| 242 | } |
---|
[181] | 243 | |
---|
[378] | 244 | // Close the FITS file. |
---|
[416] | 245 | fits_close_file(fptr, &status[4]); |
---|
| 246 | if (status[4]){ |
---|
[378] | 247 | duchampWarning("Cube Reader","Error closing file: "); |
---|
[416] | 248 | fits_report_error(stderr, status[4]); |
---|
[378] | 249 | } |
---|
[159] | 250 | |
---|
[378] | 251 | delete [] comment; |
---|
[159] | 252 | |
---|
[378] | 253 | int returnStatus = status[0]; |
---|
[416] | 254 | for(int i=1;i<5;i++) if(status[i]>returnStatus) returnStatus=status[i]; |
---|
[181] | 255 | |
---|
[378] | 256 | return returnStatus; |
---|
| 257 | } |
---|
| 258 | |
---|
[159] | 259 | } |
---|