[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // readRecon.cc: Read in an existing wavelet-reconstructed 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 | // ----------------------------------------------------------------------- |
---|
[71] | 28 | #include <iostream> |
---|
| 29 | #include <sstream> |
---|
| 30 | #include <string> |
---|
[394] | 31 | #include <wcslib/wcs.h> |
---|
| 32 | #include <wcslib/wcshdr.h> |
---|
[899] | 33 | #include <wcslib/wcsunits.h> |
---|
[179] | 34 | #define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr |
---|
[103] | 35 | // (this is a problem when using gcc v.4+ |
---|
[71] | 36 | #include <fitsio.h> |
---|
[393] | 37 | #include <duchamp/duchamp.hh> |
---|
| 38 | #include <duchamp/Cubes/cubes.hh> |
---|
[71] | 39 | |
---|
[378] | 40 | namespace duchamp |
---|
[71] | 41 | { |
---|
| 42 | |
---|
[698] | 43 | OUTCOME Cube::readReconCube() |
---|
[378] | 44 | { |
---|
[528] | 45 | /// @details |
---|
| 46 | /// A way to read in a previously reconstructed array, corresponding |
---|
| 47 | /// to the requested parameters, or in the filename given by reconFile. |
---|
| 48 | /// Performs various tests to make sure there are no clashes between |
---|
| 49 | /// the requested parameters and those stored in the header of the |
---|
| 50 | /// FITS file. Also test to make sure that the size (and subsection, |
---|
| 51 | /// if applicable) of the array is the same. |
---|
[86] | 52 | |
---|
[378] | 53 | int status = 0; |
---|
[188] | 54 | |
---|
[378] | 55 | if(!this->par.getFlagReconExists()){ |
---|
| 56 | duchampWarning("readReconCube", |
---|
| 57 | "reconExists flag is not set. Not reading anything in!\n"); |
---|
| 58 | return FAILURE; |
---|
[188] | 59 | } |
---|
[378] | 60 | else if(!this->par.getFlagATrous()){ |
---|
| 61 | duchampWarning("readReconCube", |
---|
| 62 | "flagATrous is not set. Don't need to read in recon array!\n"); |
---|
| 63 | return FAILURE; |
---|
[188] | 64 | } |
---|
[378] | 65 | else { |
---|
[71] | 66 | |
---|
[378] | 67 | // Check to see whether the parameters reconFile and/or residFile are defined |
---|
| 68 | bool reconGood = false; |
---|
| 69 | int exists; |
---|
| 70 | std::stringstream errmsg; |
---|
| 71 | if(this->par.getReconFile() != ""){ |
---|
| 72 | reconGood = true; |
---|
| 73 | fits_file_exists(this->par.getReconFile().c_str(),&exists,&status); |
---|
| 74 | if(exists<=0){ |
---|
| 75 | fits_report_error(stderr, status); |
---|
| 76 | errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n" |
---|
| 77 | << "Bad reconFile was: "<<this->par.getReconFile() << std::endl; |
---|
| 78 | duchampWarning("readReconCube", errmsg.str()); |
---|
| 79 | reconGood = false; |
---|
| 80 | } |
---|
[188] | 81 | } |
---|
[378] | 82 | else{ |
---|
| 83 | errmsg<< "ReconFile not specified. Working out name from parameters.\n"; |
---|
[188] | 84 | } |
---|
[378] | 85 | |
---|
| 86 | if(!reconGood){ // if bad, need to look at parameters |
---|
[188] | 87 | |
---|
[378] | 88 | std::string reconFile = this->par.outputReconFile(); |
---|
| 89 | errmsg << "Trying file " << reconFile << std::endl; |
---|
| 90 | duchampWarning("readReconCube", errmsg.str() ); |
---|
| 91 | reconGood = true; |
---|
| 92 | fits_file_exists(reconFile.c_str(),&exists,&status); |
---|
| 93 | if(exists<=0){ |
---|
| 94 | fits_report_error(stderr, status); |
---|
| 95 | // duchampWarning("readReconCube","ReconFile not present.\n"); |
---|
| 96 | reconGood = false; |
---|
| 97 | } |
---|
[188] | 98 | |
---|
[378] | 99 | if(reconGood){ |
---|
| 100 | // were able to open this new file -- use this, so reset the |
---|
| 101 | // relevant parameter |
---|
| 102 | this->par.setReconFile(reconFile); |
---|
| 103 | } |
---|
| 104 | else { // if STILL bad, give error message and exit. |
---|
| 105 | duchampError("readReconCube","Cannot find reconstructed file.\n"); |
---|
| 106 | return FAILURE; |
---|
| 107 | } |
---|
[188] | 108 | |
---|
[378] | 109 | } |
---|
[188] | 110 | |
---|
[378] | 111 | // if we get to here, reconGood is true (ie. file is open); |
---|
[71] | 112 | |
---|
[378] | 113 | status=0; |
---|
| 114 | fitsfile *fptr; |
---|
| 115 | fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status); |
---|
| 116 | short int maxdim=3; |
---|
| 117 | long *fpixel = new long[maxdim]; |
---|
| 118 | for(int i=0;i<maxdim;i++) fpixel[i]=1; |
---|
| 119 | long *dimAxesNew = new long[maxdim]; |
---|
| 120 | for(int i=0;i<maxdim;i++) dimAxesNew[i]=1; |
---|
| 121 | int bitpix,numAxesNew,anynul; |
---|
[71] | 122 | |
---|
[378] | 123 | status = 0; |
---|
| 124 | fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status); |
---|
| 125 | if(status){ |
---|
| 126 | fits_report_error(stderr, status); |
---|
| 127 | return FAILURE; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if(numAxesNew != this->numDim){ |
---|
[188] | 131 | std::stringstream errmsg; |
---|
[378] | 132 | errmsg << "Reconstructed cube has a different number of axes to original!" |
---|
| 133 | << " (" << numAxesNew << " cf. " << this->numDim << ")\n"; |
---|
[188] | 134 | duchampError("readReconCube", errmsg.str()); |
---|
| 135 | return FAILURE; |
---|
| 136 | } |
---|
[71] | 137 | |
---|
[378] | 138 | for(int i=0;i<numAxesNew;i++){ |
---|
[894] | 139 | if(dimAxesNew[i]!=int(this->axisDim[i])){ |
---|
[188] | 140 | std::stringstream errmsg; |
---|
[378] | 141 | errmsg << "Reconstructed cube has different axis dimensions to original!" |
---|
| 142 | << "\nAxis #" << i+1 << " has size " << dimAxesNew[i] |
---|
| 143 | << " cf. " << this->axisDim[i] <<" in original.\n"; |
---|
[188] | 144 | duchampError("readReconCube", errmsg.str()); |
---|
| 145 | return FAILURE; |
---|
| 146 | } |
---|
| 147 | } |
---|
[71] | 148 | |
---|
[378] | 149 | char *comment = new char[80]; |
---|
| 150 | |
---|
| 151 | if(this->par.getFlagSubsection()){ |
---|
| 152 | char *subsection = new char[80]; |
---|
| 153 | status = 0; |
---|
| 154 | fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
| 155 | subsection, comment, &status); |
---|
| 156 | if(status){ |
---|
| 157 | duchampError("readReconCube", |
---|
| 158 | "subsection keyword not present in reconFile.\n"); |
---|
| 159 | return FAILURE; |
---|
| 160 | } |
---|
| 161 | else{ |
---|
| 162 | if(this->par.getSubsection() != subsection){ |
---|
| 163 | std::stringstream errmsg; |
---|
| 164 | errmsg << "subsection keyword in reconFile (" << subsection |
---|
| 165 | << ") does not match that requested (" |
---|
| 166 | << this->par.getSubsection() << ").\n"; |
---|
| 167 | duchampError("readReconCube", errmsg.str()); |
---|
| 168 | return FAILURE; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | delete subsection; |
---|
| 172 | } |
---|
| 173 | |
---|
[894] | 174 | unsigned int scaleMin; |
---|
| 175 | int filterCode,reconDim; |
---|
[378] | 176 | float snrRecon; |
---|
| 177 | status = 0; |
---|
| 178 | fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(), |
---|
| 179 | &reconDim, comment, &status); |
---|
| 180 | if(reconDim != this->par.getReconDim()){ |
---|
| 181 | std::stringstream errmsg; |
---|
| 182 | errmsg << "reconDim keyword in reconFile (" << reconDim |
---|
| 183 | << ") does not match that requested (" |
---|
| 184 | << this->par.getReconDim() << ").\n"; |
---|
| 185 | duchampError("readReconCube", errmsg.str()); |
---|
| 186 | return FAILURE; |
---|
| 187 | } |
---|
| 188 | status = 0; |
---|
| 189 | fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(), |
---|
| 190 | &filterCode, comment, &status); |
---|
| 191 | if(filterCode != this->par.getFilterCode()){ |
---|
| 192 | std::stringstream errmsg; |
---|
| 193 | errmsg << "filterCode keyword in reconFile (" << filterCode |
---|
| 194 | << ") does not match that requested (" |
---|
| 195 | << this->par.getFilterCode() << ").\n"; |
---|
| 196 | duchampError("readReconCube", errmsg.str()); |
---|
| 197 | return FAILURE; |
---|
| 198 | } |
---|
| 199 | status = 0; |
---|
| 200 | fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), |
---|
| 201 | &snrRecon, comment, &status); |
---|
| 202 | if(snrRecon != this->par.getAtrousCut()){ |
---|
| 203 | std::stringstream errmsg; |
---|
| 204 | errmsg << "snrRecon keyword in reconFile (" << snrRecon |
---|
| 205 | << ") does not match that requested (" |
---|
| 206 | << this->par.getAtrousCut() << ").\n"; |
---|
| 207 | duchampError("readReconCube", errmsg.str()); |
---|
| 208 | return FAILURE; |
---|
| 209 | } |
---|
| 210 | status = 0; |
---|
| 211 | fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), |
---|
| 212 | &scaleMin, comment, &status); |
---|
| 213 | if(scaleMin != this->par.getMinScale()){ |
---|
| 214 | std::stringstream errmsg; |
---|
| 215 | errmsg << "scaleMin keyword in reconFile (" << scaleMin |
---|
| 216 | << ") does not match that requested (" |
---|
| 217 | << this->par.getMinScale() << ").\n"; |
---|
| 218 | duchampError("readReconCube", errmsg.str()); |
---|
| 219 | return FAILURE; |
---|
| 220 | } |
---|
[899] | 221 | |
---|
| 222 | // Read the BUNIT keyword, and translate to standard unit format if needs be |
---|
| 223 | std::string header("BUNIT"); |
---|
| 224 | char *unit = new char[FLEN_VALUE]; |
---|
| 225 | std::string fluxunits; |
---|
| 226 | fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status); |
---|
| 227 | if (status){ |
---|
| 228 | duchampWarning("Cube Reader","Error reading BUNIT keyword: "); |
---|
| 229 | fits_report_error(stderr, status); |
---|
| 230 | return FAILURE; |
---|
| 231 | } |
---|
| 232 | else{ |
---|
| 233 | wcsutrn(0,unit); |
---|
| 234 | fluxunits = unit; |
---|
| 235 | } |
---|
[104] | 236 | |
---|
[378] | 237 | // |
---|
| 238 | // If we get to here, the reconFile exists and matches the atrous |
---|
| 239 | // parameters the user has requested. |
---|
[104] | 240 | |
---|
[378] | 241 | status = 0; |
---|
| 242 | fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, |
---|
| 243 | this->recon, &anynul, &status); |
---|
[71] | 244 | |
---|
[378] | 245 | status = 0; |
---|
| 246 | fits_close_file(fptr, &status); |
---|
| 247 | if (status){ |
---|
| 248 | duchampWarning("readReconCube", "Error closing file: "); |
---|
| 249 | fits_report_error(stderr, status); |
---|
| 250 | } |
---|
[71] | 251 | |
---|
[378] | 252 | // We don't want to write out the recon or resid files at the end |
---|
| 253 | this->par.setFlagOutputRecon(false); |
---|
| 254 | this->par.setFlagOutputResid(false); |
---|
[71] | 255 | |
---|
[899] | 256 | this->convertFluxUnits(fluxunits,this->par.getNewFluxUnits(),RECON); |
---|
| 257 | |
---|
[378] | 258 | // The reconstruction is done -- set the appropriate flag |
---|
| 259 | this->reconExists = true; |
---|
[71] | 260 | |
---|
[378] | 261 | return SUCCESS; |
---|
| 262 | } |
---|
[188] | 263 | } |
---|
[378] | 264 | |
---|
[71] | 265 | } |
---|