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

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

A large swathe of changes aimed at fixing compilation warnings. These mostly refer to size_t conversions, although there is at least one instance of adding a return value to a function that needed it. Note there are still a few warnings - I need to decide how best to reconcile the size_t variables with integral values that could conceivably be negative...

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]!=int(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      unsigned int scaleMin;
174      int filterCode,reconDim;
175      float snrRecon;
176      status = 0;
177      fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
178                    &reconDim, comment, &status);
179      if(reconDim != this->par.getReconDim()){
180        std::stringstream errmsg;
181        errmsg << "reconDim keyword in reconFile (" << reconDim
182               << ") does not match that requested ("
183               << this->par.getReconDim() << ").\n";
184        duchampError("readReconCube", errmsg.str());
185        return FAILURE;
186      }
187      status = 0;
188      fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
189                    &filterCode, comment, &status);
190      if(filterCode != this->par.getFilterCode()){
191        std::stringstream errmsg;
192        errmsg << "filterCode keyword in reconFile (" << filterCode
193               << ") does not match that requested ("
194               << this->par.getFilterCode() << ").\n";
195        duchampError("readReconCube", errmsg.str());
196        return FAILURE;
197      }
198      status = 0;
199      fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
200                    &snrRecon, comment, &status);
201      if(snrRecon != this->par.getAtrousCut()){
202        std::stringstream errmsg;
203        errmsg << "snrRecon keyword in reconFile (" << snrRecon
204               << ") does not match that requested ("
205               << this->par.getAtrousCut() << ").\n";
206        duchampError("readReconCube", errmsg.str());
207        return FAILURE;
208      }
209      status = 0;
210      fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
211                    &scaleMin, comment, &status);
212      if(scaleMin != this->par.getMinScale()){
213        std::stringstream errmsg;
214        errmsg << "scaleMin keyword in reconFile (" << scaleMin
215               << ") does not match that requested ("
216               << this->par.getMinScale() << ").\n";
217        duchampError("readReconCube", errmsg.str());
218        return FAILURE;
219      }
220 
221      //
222      // If we get to here, the reconFile exists and matches the atrous
223      //  parameters the user has requested.
224
225      status = 0;
226      fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
227                    this->recon, &anynul, &status);
228 
229      status = 0;
230      fits_close_file(fptr, &status);
231      if (status){
232        duchampWarning("readReconCube", "Error closing file: ");
233        fits_report_error(stderr, status);
234      }
235
236      // We don't want to write out the recon or resid files at the end
237      this->par.setFlagOutputRecon(false);
238      this->par.setFlagOutputResid(false);
239
240      // The reconstruction is done -- set the appropriate flag
241      this->reconExists = true;
242
243      return SUCCESS;
244    }
245  }
246
247}
Note: See TracBrowser for help on using the repository browser.