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