source: tags/release-1.1.5/src/Cubes/readRecon.cc @ 1441

Last change on this file since 1441 was 394, checked in by MatthewWhiting, 17 years ago

Fixing wcslib include calls so that it is a bit more robust.

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  int Cube::readReconCube()
43  {
44    /**
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
53
54    int status = 0;
55
56    if(!this->par.getFlagReconExists()){
57      duchampWarning("readReconCube",
58                     "reconExists flag is not set. Not reading anything in!\n");
59      return FAILURE;
60    }
61    else if(!this->par.getFlagATrous()){
62      duchampWarning("readReconCube",
63                     "flagATrous is not set. Don't need to read in recon array!\n");
64      return FAILURE;
65    }
66    else {
67
68      // Check to see whether the parameters reconFile and/or residFile are defined
69      bool reconGood = false;
70      int exists;
71      std::stringstream errmsg;
72      if(this->par.getReconFile() != ""){
73        reconGood = true;
74        fits_file_exists(this->par.getReconFile().c_str(),&exists,&status);
75        if(exists<=0){
76          fits_report_error(stderr, status);
77          errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n"
78                << "Bad reconFile was: "<<this->par.getReconFile() << std::endl;
79          duchampWarning("readReconCube", errmsg.str());
80          reconGood = false;
81        }
82      }
83      else{
84        errmsg<< "ReconFile not specified. Working out name from parameters.\n";
85      }
86 
87      if(!reconGood){ // if bad, need to look at parameters
88
89        std::string reconFile = this->par.outputReconFile();
90        errmsg << "Trying file " << reconFile << std::endl;
91        duchampWarning("readReconCube", errmsg.str() );
92        reconGood = true;
93        fits_file_exists(reconFile.c_str(),&exists,&status);
94        if(exists<=0){
95          fits_report_error(stderr, status);
96          //    duchampWarning("readReconCube","ReconFile not present.\n");
97          reconGood = false;
98        }
99
100        if(reconGood){
101          // were able to open this new file -- use this, so reset the
102          //  relevant parameter
103          this->par.setReconFile(reconFile);
104        }
105        else { // if STILL bad, give error message and exit.
106          duchampError("readReconCube","Cannot find reconstructed file.\n");
107          return FAILURE;
108        }
109
110      }
111
112      // if we get to here, reconGood is true (ie. file is open);
113
114      status=0;
115      fitsfile *fptr;
116      fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
117      short int maxdim=3;
118      long *fpixel = new long[maxdim];
119      for(int i=0;i<maxdim;i++) fpixel[i]=1;
120      long *dimAxesNew = new long[maxdim];
121      for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
122      int bitpix,numAxesNew,anynul;
123
124      status = 0;
125      fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
126      if(status){
127        fits_report_error(stderr, status);
128        return FAILURE;
129      }
130
131      if(numAxesNew != this->numDim){
132        std::stringstream errmsg;
133        errmsg << "Reconstructed cube has a different number of axes to original!"
134               << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
135        duchampError("readReconCube", errmsg.str());
136        return FAILURE;
137      }
138
139      for(int i=0;i<numAxesNew;i++){
140        if(dimAxesNew[i]!=this->axisDim[i]){
141          std::stringstream errmsg;
142          errmsg << "Reconstructed cube has different axis dimensions to original!"
143                 << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
144                 << " cf. " << this->axisDim[i] <<" in original.\n";   
145          duchampError("readReconCube", errmsg.str());
146          return FAILURE;
147        }
148      }
149
150      char *comment = new char[80];
151
152      if(this->par.getFlagSubsection()){
153        char *subsection = new char[80];
154        status = 0;
155        fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
156                      subsection, comment, &status);
157        if(status){
158          duchampError("readReconCube",
159                       "subsection keyword not present in reconFile.\n");
160          return FAILURE;
161        }
162        else{
163          if(this->par.getSubsection() != subsection){
164            std::stringstream errmsg;
165            errmsg << "subsection keyword in reconFile (" << subsection
166                   << ") does not match that requested ("
167                   << this->par.getSubsection() << ").\n";
168            duchampError("readReconCube", errmsg.str());
169            return FAILURE;
170          }
171        }
172        delete subsection;
173      }
174
175      int scaleMin,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      //
223      // If we get to here, the reconFile exists and matches the atrous
224      //  parameters the user has requested.
225
226      status = 0;
227      fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
228                    this->recon, &anynul, &status);
229 
230      status = 0;
231      fits_close_file(fptr, &status);
232      if (status){
233        duchampWarning("readReconCube", "Error closing file: ");
234        fits_report_error(stderr, status);
235      }
236
237      // We don't want to write out the recon or resid files at the end
238      this->par.setFlagOutputRecon(false);
239      this->par.setFlagOutputResid(false);
240
241      // The reconstruction is done -- set the appropriate flag
242      this->reconExists = true;
243
244      return SUCCESS;
245    }
246  }
247
248}
Note: See TracBrowser for help on using the repository browser.