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