[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); |
---|
| 35 | std::cerr << |
---|
[103] | 36 | " WARNING <readReconCube> : Cannot find requested ReconFile. Trying with parameters.\n"; |
---|
[71] | 37 | std::cerr << |
---|
[103] | 38 | " Bad reconFile was: "<<this->par.getReconFile()<<std::endl; |
---|
[71] | 39 | reconGood = false; |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | else{ |
---|
| 43 | std::cerr << |
---|
[103] | 44 | " WARNING <readReconCube> : 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); |
---|
| 55 | std::cerr << |
---|
[103] | 56 | " WARNING <readReconCube> : ReconFile not present\n"; |
---|
[71] | 57 | reconGood = false; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | if(reconGood){ // were able to open this file -- use this, so reset the relevant parameter |
---|
| 61 | this->par.setReconFile(reconFile); |
---|
| 62 | } |
---|
| 63 | else { // if STILL bad, give error message and exit. |
---|
[103] | 64 | std::cerr << " ERROR <readReconCube> : Cannot find reconstructed file.\n"; |
---|
[71] | 65 | return FAILURE; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // if we get to here, reconGood is true (ie. file is open); |
---|
| 71 | status=0; |
---|
[104] | 72 | fitsfile *fptr; |
---|
[71] | 73 | fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status); |
---|
| 74 | short int maxdim=3; |
---|
| 75 | long *fpixel = new long[maxdim]; |
---|
| 76 | for(int i=0;i<maxdim;i++) fpixel[i]=1; |
---|
| 77 | long *dimAxesNew = new long[maxdim]; |
---|
| 78 | for(int i=0;i<maxdim;i++) dimAxesNew[i]=1; |
---|
| 79 | long nelements; |
---|
| 80 | int bitpix,numAxesNew,anynul; |
---|
| 81 | |
---|
| 82 | status = 0; |
---|
| 83 | fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status); |
---|
| 84 | if(status){ |
---|
| 85 | fits_report_error(stderr, status); |
---|
| 86 | return FAILURE; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if(numAxesNew != this->numDim){ |
---|
[103] | 90 | std::cerr << " ERROR <readReconCube> : " |
---|
| 91 | << " Reconstructed cube has a different number of axes to original! (" |
---|
[71] | 92 | << numAxesNew << " cf. " << this->numDim << ")\n"; |
---|
| 93 | return FAILURE; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | for(int i=0;i<numAxesNew;i++){ |
---|
| 97 | if(dimAxesNew[i]!=this->axisDim[i]){ |
---|
[103] | 98 | std::cerr << " ERROR <readReconCube> : " |
---|
[105] | 99 | << "Reconstructed cube has different axis dimensions to original!\n" |
---|
| 100 | << " Axis #" << i+1 << " has size " << dimAxesNew[i] |
---|
[103] | 101 | << " cf. " << this->axisDim[i] <<" in original.\n"; |
---|
[71] | 102 | return FAILURE; |
---|
| 103 | } |
---|
[105] | 104 | } |
---|
[71] | 105 | |
---|
[104] | 106 | char *comment = new char[80]; |
---|
[105] | 107 | |
---|
| 108 | if(this->par.getFlagSubsection()){ |
---|
| 109 | char *subsection = new char[80]; |
---|
| 110 | status = 0; |
---|
| 111 | fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), subsection, comment, &status); |
---|
| 112 | if(status){ |
---|
| 113 | std::cerr <<" ERROR <readReconCube> : subsection keyword not present in reconFile.\n"; |
---|
| 114 | return FAILURE; |
---|
| 115 | } |
---|
| 116 | else{ |
---|
| 117 | if(this->par.getSubsection() != subsection){ |
---|
| 118 | std::cerr <<" ERROR <readReconCube> : subsection keyword in reconFile (" << subsection |
---|
| 119 | <<") does not match that requested (" << this->par.getSubsection() <<").\n"; |
---|
| 120 | return FAILURE; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | delete subsection; |
---|
| 124 | } |
---|
| 125 | |
---|
[104] | 126 | int scaleMin,filterCode,reconDim; |
---|
| 127 | float snrRecon; |
---|
| 128 | status = 0; |
---|
| 129 | fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(), &reconDim, comment, &status); |
---|
| 130 | if(reconDim != this->par.getReconDim()){ |
---|
| 131 | std::cerr << " ERROR <readReconCube> : reconDim keyword in reconFile (" << reconDim |
---|
| 132 | << ") does not match that requested (" << this->par.getReconDim() << ").\n"; |
---|
| 133 | return FAILURE; |
---|
| 134 | } |
---|
| 135 | status = 0; |
---|
| 136 | fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(), &filterCode, comment, &status); |
---|
| 137 | if(filterCode != this->par.getFilterCode()){ |
---|
| 138 | std::cerr << " ERROR <readReconCube> : filterCode keyword in reconFile (" << filterCode |
---|
| 139 | << ") does not match that requested (" << this->par.getFilterCode() << ").\n"; |
---|
| 140 | return FAILURE; |
---|
| 141 | } |
---|
| 142 | status = 0; |
---|
| 143 | fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), &snrRecon, comment, &status); |
---|
| 144 | if(snrRecon != this->par.getAtrousCut()){ |
---|
| 145 | std::cerr << " ERROR <readReconCube> : snrRecon keyword in reconFile (" << snrRecon |
---|
| 146 | << ") does not match that requested (" << this->par.getAtrousCut() << ").\n"; |
---|
| 147 | return FAILURE; |
---|
| 148 | } |
---|
| 149 | status = 0; |
---|
| 150 | fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), &scaleMin, comment, &status); |
---|
| 151 | if(scaleMin != this->par.getMinScale()){ |
---|
| 152 | std::cerr << " ERROR <readReconCube> : scaleMin keyword in reconFile (" << scaleMin |
---|
| 153 | << ") does not match that requested (" << this->par.getMinScale() << ").\n"; |
---|
| 154 | return FAILURE; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // |
---|
| 158 | // If we get to here, the reconFile exists and matches the atrous parameters the user has requested. |
---|
| 159 | // |
---|
| 160 | |
---|
[71] | 161 | float *reconArray = new float[this->numPixels]; |
---|
[103] | 162 | status = 0; |
---|
[71] | 163 | fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, reconArray, &anynul, &status); |
---|
| 164 | this->saveRecon(reconArray,this->numPixels); |
---|
| 165 | |
---|
[103] | 166 | status = 0; |
---|
[71] | 167 | fits_close_file(fptr, &status); |
---|
| 168 | if (status){ |
---|
[103] | 169 | std::cerr << " WARNING <readReconCube> : Error closing file: "; |
---|
[71] | 170 | fits_report_error(stderr, status); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // We don't want to write out the recon or resid files at the end |
---|
| 174 | this->par.setFlagOutputRecon(false); |
---|
| 175 | this->par.setFlagOutputResid(false); |
---|
| 176 | |
---|
| 177 | // The reconstruction is done -- set the appropriate flag |
---|
| 178 | this->reconExists = true; |
---|
| 179 | |
---|
| 180 | return SUCCESS; |
---|
| 181 | } |
---|