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