[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // getImage.cc: Read in a FITS file to a Cube object. |
---|
| 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 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
| 29 | #include <string> |
---|
[142] | 30 | #include <string.h> |
---|
[394] | 31 | #include <wcslib/wcs.h> |
---|
| 32 | #include <wcslib/wcshdr.h> |
---|
[400] | 33 | #include <wcslib/fitshdr.h> |
---|
[394] | 34 | #include <wcslib/wcsfix.h> |
---|
| 35 | #include <wcslib/wcsunits.h> |
---|
[142] | 36 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
[321] | 37 | // wtbarr (this is a problem when using gcc v.4+) |
---|
[66] | 38 | #include <fitsio.h> |
---|
[69] | 39 | #include <math.h> |
---|
[393] | 40 | #include <duchamp/duchamp.hh> |
---|
| 41 | #include <duchamp/param.hh> |
---|
| 42 | #include <duchamp/fitsHeader.hh> |
---|
| 43 | #include <duchamp/Cubes/cubes.hh> |
---|
[3] | 44 | |
---|
[378] | 45 | namespace duchamp |
---|
[3] | 46 | { |
---|
| 47 | |
---|
[378] | 48 | std::string imageType[4] = {"point", "spectrum", "image", "cube"}; |
---|
[3] | 49 | |
---|
[698] | 50 | OUTCOME Cube::getMetadata() |
---|
[528] | 51 | { |
---|
| 52 | /// @details |
---|
| 53 | /// A front-end to the Cube::getMetadata() function, that does |
---|
| 54 | /// subsection checks. Does the flux unit conversion if the user |
---|
| 55 | /// has requested it. Assumes the Param is set up properly. |
---|
| 56 | |
---|
[910] | 57 | // int exists; |
---|
| 58 | // fits_file_exists(fname.c_str(),&exists,&status); |
---|
| 59 | // if(exists<=0){ |
---|
| 60 | // fits_report_error(stderr, status); |
---|
[913] | 61 | // DUCHAMPERROR("Cube Reader", Requested image (" << fname << ") does not exist!); |
---|
[910] | 62 | // return FAILURE; |
---|
| 63 | // } |
---|
| 64 | if(this->par.checkImageExists() == FAILURE) return FAILURE; |
---|
| 65 | |
---|
[442] | 66 | std::string fname = par.getImageFile(); |
---|
| 67 | if(par.getFlagSubsection()) fname+=par.getSubsection(); |
---|
[443] | 68 | |
---|
[698] | 69 | OUTCOME result = getMetadata(fname); |
---|
[443] | 70 | |
---|
| 71 | if(result==SUCCESS){ |
---|
| 72 | // Convert the flux Units if the user has so requested |
---|
[899] | 73 | this->convertFluxUnits(this->head.getFluxUnits(),this->par.getNewFluxUnits()); |
---|
[443] | 74 | } |
---|
| 75 | |
---|
| 76 | return result; |
---|
[442] | 77 | } |
---|
| 78 | //-------------------------------------------------------------------- |
---|
| 79 | |
---|
[698] | 80 | OUTCOME Cube::getMetadata(std::string fname) |
---|
[378] | 81 | { |
---|
[528] | 82 | /// @details |
---|
| 83 | /// Read in the metadata associated with the cube from the file |
---|
| 84 | /// fname (assumed to be in FITS format). Function is a front end |
---|
| 85 | /// to the I/O functions in the FitsIO/ directory. This function |
---|
| 86 | /// will check that the file exists, report the dimensions and |
---|
| 87 | /// then get other functions to read the WCS and necessary header |
---|
| 88 | /// keywords. This function does not read in the data array -- |
---|
| 89 | /// that is done by Cube::getCube(). Note that no conversion of |
---|
| 90 | /// flux units is done -- you need to call Cube::getMetadata() or |
---|
| 91 | /// do it separately. |
---|
| 92 | /// \param fname A std::string with name of FITS file. |
---|
| 93 | /// \return SUCCESS or FAILURE. |
---|
[168] | 94 | |
---|
[378] | 95 | int numAxes, status = 0; /* MUST initialize status */ |
---|
| 96 | fitsfile *fptr; |
---|
| 97 | |
---|
| 98 | // Open the FITS file -- make sure it exists |
---|
| 99 | status = 0; |
---|
| 100 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 101 | fits_report_error(stderr, status); |
---|
| 102 | if(((status==URL_PARSE_ERROR)||(status==BAD_NAXIS)) |
---|
| 103 | &&(this->pars().getFlagSubsection())) |
---|
[913] | 104 | DUCHAMPERROR("Cube Reader", "It may be that the subsection you've entered is invalid. Either it has the wrong number of axes, or one axis has too large a range."); |
---|
[378] | 105 | return FAILURE; |
---|
| 106 | } |
---|
[3] | 107 | |
---|
[378] | 108 | // Read the size information -- number and lengths of all axes. |
---|
| 109 | // Just use this for reporting purposes. |
---|
| 110 | status = 0; |
---|
| 111 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
| 112 | fits_report_error(stderr, status); |
---|
| 113 | return FAILURE; |
---|
| 114 | } |
---|
| 115 | long *dimAxes = new long[numAxes]; |
---|
| 116 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
| 117 | status = 0; |
---|
| 118 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
| 119 | fits_report_error(stderr, status); |
---|
| 120 | return FAILURE; |
---|
| 121 | } |
---|
[443] | 122 | |
---|
[378] | 123 | // Close the FITS file. |
---|
| 124 | status = 0; |
---|
| 125 | fits_close_file(fptr, &status); |
---|
| 126 | if (status){ |
---|
[913] | 127 | DUCHAMPWARN("Cube Reader","Error closing file: "); |
---|
[378] | 128 | fits_report_error(stderr, status); |
---|
| 129 | } |
---|
[3] | 130 | |
---|
[378] | 131 | // Report the size of the FITS file |
---|
| 132 | if(this->par.isVerbose()){ |
---|
| 133 | std::cout << "Dimensions of FITS file: "; |
---|
| 134 | int dim = 0; |
---|
| 135 | std::cout << dimAxes[dim]; |
---|
| 136 | while(dim+1<numAxes) std::cout << "x" << dimAxes[++dim]; |
---|
| 137 | std::cout << std::endl; |
---|
| 138 | } |
---|
[103] | 139 | |
---|
[570] | 140 | |
---|
[378] | 141 | // Get the WCS information |
---|
[700] | 142 | if(this->head.defineWCS(fname, this->par) == FAILURE) return FAILURE; |
---|
[103] | 143 | |
---|
[795] | 144 | // Read the necessary header information, and copy some of it into the Param. |
---|
| 145 | if(this->head.readHeaderInfo(fname, this->par) == FAILURE) return FAILURE; |
---|
| 146 | |
---|
[378] | 147 | if(!this->head.isWCS()) |
---|
[913] | 148 | DUCHAMPWARN("Cube Reader","WCS is not good enough to be used.\n"); |
---|
[3] | 149 | |
---|
[508] | 150 | // allocate the dimension array in the Cube. |
---|
[700] | 151 | if(this->initialiseCube(dimAxes,false) == FAILURE) return FAILURE; |
---|
[508] | 152 | |
---|
| 153 | delete [] dimAxes; |
---|
| 154 | |
---|
[442] | 155 | return SUCCESS; |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | //-------------------------------------------------------------------- |
---|
| 159 | |
---|
| 160 | |
---|
[698] | 161 | OUTCOME Cube::getCube(std::string fname) |
---|
[442] | 162 | { |
---|
[528] | 163 | /// @details |
---|
| 164 | /// Read in a cube from the file fname (assumed to be in FITS |
---|
| 165 | /// format). Function is a front end to the data I/O function in |
---|
| 166 | /// the FitsIO/ directory. This function calls the getMetadata() |
---|
| 167 | /// function, then reads in the actual data array. |
---|
| 168 | /// \param fname A std::string with name of FITS file. |
---|
| 169 | /// \return SUCCESS or FAILURE. |
---|
[442] | 170 | |
---|
[700] | 171 | if(this->getMetadata(fname) == FAILURE) return FAILURE; |
---|
[442] | 172 | |
---|
[378] | 173 | // Get the data array from the FITS file. |
---|
| 174 | // Report the dimensions of the data array that was read (this can be |
---|
| 175 | // different to the full FITS array). |
---|
| 176 | if(this->par.isVerbose()) std::cout << "Reading data ... "<<std::flush; |
---|
[700] | 177 | if(this->getFITSdata(fname) == FAILURE) return FAILURE; |
---|
[443] | 178 | |
---|
| 179 | if(this->axisDim[2] == 1){ |
---|
| 180 | this->par.setMinChannels(0); |
---|
| 181 | } |
---|
| 182 | |
---|
[378] | 183 | if(this->par.isVerbose()){ |
---|
| 184 | std::cout << "Done. Data array has dimensions: "; |
---|
| 185 | std::cout << this->axisDim[0]; |
---|
| 186 | if(this->axisDim[1]>1) std::cout <<"x"<< this->axisDim[1]; |
---|
| 187 | if(this->axisDim[2]>1) std::cout <<"x"<< this->axisDim[2]; |
---|
| 188 | std::cout << "\n"; |
---|
| 189 | } |
---|
[46] | 190 | |
---|
[434] | 191 | // Convert the flux Units if the user has so requested |
---|
[899] | 192 | this->convertFluxUnits(this->head.getFluxUnits(),this->par.getNewFluxUnits(),ARRAY); |
---|
[434] | 193 | |
---|
[442] | 194 | return SUCCESS; |
---|
[3] | 195 | |
---|
[378] | 196 | } |
---|
[442] | 197 | //-------------------------------------------------------------------- |
---|
[378] | 198 | |
---|
[434] | 199 | |
---|
[899] | 200 | void Cube::convertFluxUnits(std::string oldUnit, std::string newUnit, ARRAYREF whichArray) |
---|
[434] | 201 | { |
---|
[528] | 202 | /// @details |
---|
| 203 | /// If the user has requested new flux units (via the input |
---|
| 204 | /// parameter newFluxUnits), this function converts the units |
---|
| 205 | /// given by BUNIT to those given by newFluxUnits. If no simple |
---|
| 206 | /// conversion can be found (by using wcsunits()) then nothing is |
---|
| 207 | /// done and the user is informed, otherwise the |
---|
| 208 | /// FitsHeader::fluxUnits parameter is updated and the pixel array |
---|
| 209 | /// is converted accordingly. |
---|
[434] | 210 | |
---|
[899] | 211 | // if(this->par.getNewFluxUnits()!=""){ |
---|
| 212 | if(newUnit!=""){ |
---|
[434] | 213 | |
---|
[899] | 214 | // std::string oldUnit = this->head.getFluxUnits(); |
---|
| 215 | // std::string newUnit = this->par.getNewFluxUnits(); |
---|
[434] | 216 | |
---|
[443] | 217 | if(oldUnit != newUnit){ |
---|
[434] | 218 | |
---|
[443] | 219 | if(this->par.isVerbose()){ |
---|
| 220 | std::cout << "Converting flux units from " << oldUnit << " to " << newUnit << "... "; |
---|
| 221 | std::cout << std::flush; |
---|
| 222 | } |
---|
[434] | 223 | |
---|
[443] | 224 | double scale,offset,power; |
---|
| 225 | int status = wcsunits(oldUnit.c_str(), newUnit.c_str(), &scale, &offset, &power); |
---|
| 226 | |
---|
| 227 | if(status==0){ |
---|
[434] | 228 | |
---|
[443] | 229 | this->head.setFluxUnits( newUnit ); |
---|
| 230 | this->head.setIntFluxUnits(); |
---|
[434] | 231 | |
---|
[899] | 232 | if(whichArray == ARRAY){ |
---|
| 233 | if(this->arrayAllocated){ |
---|
| 234 | for(size_t i=0;i<this->numPixels;i++) |
---|
| 235 | if(!this->isBlank(i)) this->array[i] = pow(scale * this->array[i] + offset, power); |
---|
| 236 | } |
---|
[496] | 237 | } |
---|
[899] | 238 | else if(whichArray==RECON){ |
---|
| 239 | if(this->reconAllocated){ |
---|
| 240 | for(size_t i=0;i<this->numPixels;i++) |
---|
| 241 | if(!this->isBlank(i)) this->recon[i] = pow(scale * this->recon[i] + offset, power); |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | else if(whichArray==BASELINE){ |
---|
| 245 | if(this->baselineAllocated){ |
---|
| 246 | for(size_t i=0;i<this->numPixels;i++) |
---|
| 247 | if(!this->isBlank(i)) this->baseline[i] = pow(scale * this->baseline[i] + offset, power); |
---|
| 248 | } |
---|
| 249 | } |
---|
[443] | 250 | if(this->par.isVerbose()) { |
---|
| 251 | std::cout << " Done.\n"; |
---|
| 252 | } |
---|
[434] | 253 | } |
---|
[443] | 254 | else{ |
---|
| 255 | if(this->par.isVerbose()) std::cout << "\n"; |
---|
[913] | 256 | DUCHAMPWARN("convertFluxUnits", "Could not convert units from " << oldUnit << " to " << newUnit << ". Leaving BUNIT as " << oldUnit); |
---|
[443] | 257 | } |
---|
[434] | 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
[3] | 264 | } |
---|