source: trunk/src/Cubes/readRecon.cc @ 901

Last change on this file since 901 was 901, checked in by MatthewWhiting, 12 years ago

Final fixes for #127, getting it working for reading the smoothed image as well.

File size: 8.9 KB
Line 
1// -----------------------------------------------------------------------
2// readRecon.cc: Read in an existing wavelet-reconstructed 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// -----------------------------------------------------------------------
28#include <iostream>
29#include <sstream>
30#include <string>
31#include <wcslib/wcs.h>
32#include <wcslib/wcshdr.h>
33#include <wcslib/wcsunits.h>
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>
37#include <duchamp/duchamp.hh>
38#include <duchamp/Cubes/cubes.hh>
39
40namespace duchamp
41{
42
43  OUTCOME Cube::readReconCube()
44  {
45    ///  @details
46    ///  A way to read in a previously reconstructed array, corresponding
47    ///    to the requested parameters, or in the filename given by reconFile.
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.
52
53    int status = 0;
54
55    if(!this->par.getFlagReconExists()){
56      duchampWarning("readReconCube",
57                     "reconExists flag is not set. Not reading anything in!\n");
58      return FAILURE;
59    }
60    else if(!this->par.getFlagATrous()){
61      duchampWarning("readReconCube",
62                     "flagATrous is not set. Don't need to read in recon array!\n");
63      return FAILURE;
64    }
65    else {
66
67      // Check to see whether the parameters reconFile and/or residFile are defined
68      bool reconGood = false;
69      int exists;
70      std::stringstream errmsg;
71      if(this->par.getReconFile() != ""){
72        reconGood = true;
73        fits_file_exists(this->par.getReconFile().c_str(),&exists,&status);
74        if(exists<=0){
75          fits_report_error(stderr, status);
76          errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n"
77                << "Bad reconFile was: "<<this->par.getReconFile() << std::endl;
78          duchampWarning("readReconCube", errmsg.str());
79          reconGood = false;
80        }
81      }
82      else{
83        errmsg<< "ReconFile not specified. Working out name from parameters.\n";
84      }
85 
86      if(!reconGood){ // if bad, need to look at parameters
87
88        std::string reconFile = this->par.outputReconFile();
89        errmsg << "Trying file " << reconFile << std::endl;
90        duchampWarning("readReconCube", errmsg.str() );
91        reconGood = true;
92        fits_file_exists(reconFile.c_str(),&exists,&status);
93        if(exists<=0){
94          fits_report_error(stderr, status);
95          //    duchampWarning("readReconCube","ReconFile not present.\n");
96          reconGood = false;
97        }
98
99        if(reconGood){
100          // were able to open this new file -- use this, so reset the
101          //  relevant parameter
102          this->par.setReconFile(reconFile);
103        }
104        else { // if STILL bad, give error message and exit.
105          duchampError("readReconCube","Cannot find reconstructed file.\n");
106          return FAILURE;
107        }
108
109      }
110
111      // if we get to here, reconGood is true (ie. file is open);
112
113      status=0;
114      fitsfile *fptr;
115      fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
116      short int maxdim=3;
117      long *fpixel = new long[maxdim];
118      for(int i=0;i<maxdim;i++) fpixel[i]=1;
119      long *dimAxesNew = new long[maxdim];
120      for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
121      int bitpix,numAxesNew,anynul;
122
123      status = 0;
124      fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
125      if(status){
126        fits_report_error(stderr, status);
127        return FAILURE;
128      }
129
130      if(numAxesNew != this->numDim){
131        std::stringstream errmsg;
132        errmsg << "Reconstructed cube has a different number of axes to original!"
133               << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
134        duchampError("readReconCube", errmsg.str());
135        return FAILURE;
136      }
137
138      for(int i=0;i<numAxesNew;i++){
139        if(dimAxesNew[i]!=int(this->axisDim[i])){
140          std::stringstream errmsg;
141          errmsg << "Reconstructed cube has different axis dimensions to original!"
142                 << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
143                 << " cf. " << this->axisDim[i] <<" in original.\n";   
144          duchampError("readReconCube", errmsg.str());
145          return FAILURE;
146        }
147      }
148
149      char *comment = new char[80];
150
151      if(this->par.getFlagSubsection()){
152        char *subsection = new char[80];
153        status = 0;
154        fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
155                      subsection, comment, &status);
156        if(status){
157          duchampError("readReconCube",
158                       "subsection keyword not present in reconFile.\n");
159          return FAILURE;
160        }
161        else{
162          if(this->par.getSubsection() != subsection){
163            std::stringstream errmsg;
164            errmsg << "subsection keyword in reconFile (" << subsection
165                   << ") does not match that requested ("
166                   << this->par.getSubsection() << ").\n";
167            duchampError("readReconCube", errmsg.str());
168            return FAILURE;
169          }
170        }
171        delete subsection;
172      }
173
174      unsigned int scaleMin;
175      int filterCode,reconDim;
176      float snrRecon;
177      status = 0;
178      fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
179                    &reconDim, comment, &status);
180      if(reconDim != this->par.getReconDim()){
181        std::stringstream errmsg;
182        errmsg << "reconDim keyword in reconFile (" << reconDim
183               << ") does not match that requested ("
184               << this->par.getReconDim() << ").\n";
185        duchampError("readReconCube", errmsg.str());
186        return FAILURE;
187      }
188      status = 0;
189      fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
190                    &filterCode, comment, &status);
191      if(filterCode != this->par.getFilterCode()){
192        std::stringstream errmsg;
193        errmsg << "filterCode keyword in reconFile (" << filterCode
194               << ") does not match that requested ("
195               << this->par.getFilterCode() << ").\n";
196        duchampError("readReconCube", errmsg.str());
197        return FAILURE;
198      }
199      status = 0;
200      fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
201                    &snrRecon, comment, &status);
202      if(snrRecon != this->par.getAtrousCut()){
203        std::stringstream errmsg;
204        errmsg << "snrRecon keyword in reconFile (" << snrRecon
205               << ") does not match that requested ("
206               << this->par.getAtrousCut() << ").\n";
207        duchampError("readReconCube", errmsg.str());
208        return FAILURE;
209      }
210      status = 0;
211      fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
212                    &scaleMin, comment, &status);
213      if(scaleMin != this->par.getMinScale()){
214        std::stringstream errmsg;
215        errmsg << "scaleMin keyword in reconFile (" << scaleMin
216               << ") does not match that requested ("
217               << this->par.getMinScale() << ").\n";
218        duchampError("readReconCube", errmsg.str());
219        return FAILURE;
220      }
221
222      // Read the BUNIT keyword, and translate to standard unit format if needs be
223      std::string header("BUNIT");
224      char *unit = new char[FLEN_VALUE];
225      std::string fluxunits;
226      fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
227      if (status){
228        duchampWarning("Cube Reader","Error reading BUNIT keyword: ");
229        fits_report_error(stderr, status);
230        return FAILURE;
231      }
232      else{
233        wcsutrn(0,unit);
234        fluxunits = unit;
235      }
236 
237      //
238      // If we get to here, the reconFile exists and matches the atrous
239      //  parameters the user has requested.
240
241      status = 0;
242      fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
243                    this->recon, &anynul, &status);
244 
245      status = 0;
246      fits_close_file(fptr, &status);
247      if (status){
248        duchampWarning("readReconCube", "Error closing file: ");
249        fits_report_error(stderr, status);
250      }
251
252      // We don't want to write out the recon or resid files at the end
253      this->par.setFlagOutputRecon(false);
254      this->par.setFlagOutputResid(false);
255
256      this->convertFluxUnits(fluxunits,this->par.getNewFluxUnits(),RECON);
257
258      // The reconstruction is done -- set the appropriate flag
259      this->reconExists = true;
260
261      return SUCCESS;
262    }
263  }
264
265}
Note: See TracBrowser for help on using the repository browser.