source: tags/release-1.1.12/src/Cubes/readRecon.cc

Last change on this file was 698, checked in by MatthewWhiting, 14 years ago

A bunch of changes aimed at improving the use of OUTCOME to report SUCCESS/FAILURE. When such a value is returned by a function, the returned type is duchamp::OUTCOME.

Also improved the error reporting in saveImage

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