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