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

Last change on this file since 208 was 188, checked in by Matthew Whiting, 18 years ago
  • Changed the way the ReconSearch?-ing is done, with a new function ReconCube? that does the reconstruction, and is called by ReconSearch?.
  • Added default case for the atrous Filter class.
  • Improved the readRecon file, with checks for relevant flags, and saving from the fits call straight into the recon array (without an intermediate array).
File size: 6.9 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <string>
4#include <wcs.h>
5#include <wcshdr.h>
6#define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr
7                         // (this is a problem when using gcc v.4+
8#include <fitsio.h>
9#include <duchamp.hh>
10#include <Cubes/cubes.hh>
11
12int Cube::readReconCube()
13{
14  /**
15   *  Cube::readReconCube()
16   *   
17   *   A way to read in a previously reconstructed array, corresponding
18   *    to the requested parameters, or in the filename given by reconFile.
19   *   Performs various tests to make sure there are no clashes between
20   *    the requested parameters and those stored in the header of the
21   *    FITS file. Also test to make sure that the size (and subsection,
22   *    if applicable) of the array is the same.
23   */
24
25
26  int status = 0;
27
28  if(!this->par.getFlagReconExists()){
29    duchampWarning("readReconCube",
30                   "reconExists flag is not set. Not reading anything in!\n");
31    return FAILURE;
32  }
33  else if(!this->par.getFlagATrous()){
34    duchampWarning("readReconCube",
35                   "flagATrous is not set. Don't need to read in recon array!\n");
36    return FAILURE;
37  }
38  else {
39
40    // Check to see whether the parameters reconFile and/or residFile are defined
41    bool reconGood = false;
42    int exists;
43    std::stringstream errmsg;
44    if(this->par.getReconFile() != ""){
45      reconGood = true;
46      fits_file_exists(this->par.getReconFile().c_str(),&exists,&status);
47      if(exists<=0){
48        fits_report_error(stderr, status);
49        errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n"
50              << "Bad reconFile was: "<<this->par.getReconFile() << std::endl;
51        duchampWarning("readReconCube", errmsg.str());
52        reconGood = false;
53      }
54    }
55    else{
56      errmsg<< "ReconFile not specified. Working out name from parameters.\n";
57    }
58 
59    if(!reconGood){ // if bad, need to look at parameters
60
61      string reconFile = this->par.outputReconFile();
62      errmsg << "Trying file " << reconFile << std::endl;
63      duchampWarning("readReconCube", errmsg.str() );
64      reconGood = true;
65      fits_file_exists(reconFile.c_str(),&exists,&status);
66      if(exists<=0){
67        fits_report_error(stderr, status);
68        duchampWarning("readReconCube","ReconFile not present.\n");
69        reconGood = false;
70      }
71
72      if(reconGood){
73        // were able to open this new file -- use this, so reset the
74        //  relevant parameter
75        this->par.setReconFile(reconFile);
76      }
77      else { // if STILL bad, give error message and exit.
78        duchampError("readReconCube","Cannot find reconstructed file.\n");
79        return FAILURE;
80      }
81
82    }
83
84    // if we get to here, reconGood is true (ie. file is open);
85
86    status=0;
87    fitsfile *fptr;
88    fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
89    short int maxdim=3;
90    long *fpixel = new long[maxdim];
91    for(int i=0;i<maxdim;i++) fpixel[i]=1;
92    long *dimAxesNew = new long[maxdim];
93    for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
94    long nelements;
95    int bitpix,numAxesNew,anynul;
96
97    status = 0;
98    fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
99    if(status){
100      fits_report_error(stderr, status);
101      return FAILURE;
102    }
103
104    if(numAxesNew != this->numDim){
105      std::stringstream errmsg;
106      errmsg << "Reconstructed cube has a different number of axes to original!"
107             << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
108      duchampError("readReconCube", errmsg.str());
109      return FAILURE;
110    }
111
112    for(int i=0;i<numAxesNew;i++){
113      if(dimAxesNew[i]!=this->axisDim[i]){
114        std::stringstream errmsg;
115        errmsg << "Reconstructed cube has different axis dimensions to original!"
116               << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
117               << " cf. " << this->axisDim[i] <<" in original.\n";     
118        duchampError("readReconCube", errmsg.str());
119        return FAILURE;
120      }
121    }
122
123    char *comment = new char[80];
124
125    if(this->par.getFlagSubsection()){
126      char *subsection = new char[80];
127      status = 0;
128      fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
129                    subsection, comment, &status);
130      if(status){
131        duchampError("readReconCube",
132                     "subsection keyword not present in reconFile.\n");
133        return FAILURE;
134      }
135      else{
136        if(this->par.getSubsection() != subsection){
137          std::stringstream errmsg;
138          errmsg << "subsection keyword in reconFile (" << subsection
139                 << ") does not match that requested ("
140                 << this->par.getSubsection() << ").\n";
141          duchampError("readReconCube", errmsg.str());
142          return FAILURE;
143        }
144      }
145      delete subsection;
146    }
147
148    int scaleMin,filterCode,reconDim;
149    float snrRecon;
150    status = 0;
151    fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
152                  &reconDim, comment, &status);
153    if(reconDim != this->par.getReconDim()){
154      std::stringstream errmsg;
155      errmsg << "reconDim keyword in reconFile (" << reconDim
156             << ") does not match that requested ("
157             << this->par.getReconDim() << ").\n";
158      duchampError("readReconCube", errmsg.str());
159      return FAILURE;
160    }
161    status = 0;
162    fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
163                  &filterCode, comment, &status);
164    if(filterCode != this->par.getFilterCode()){
165      std::stringstream errmsg;
166      errmsg << "filterCode keyword in reconFile (" << filterCode
167             << ") does not match that requested ("
168             << this->par.getFilterCode() << ").\n";
169      duchampError("readReconCube", errmsg.str());
170      return FAILURE;
171    }
172    status = 0;
173    fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
174                  &snrRecon, comment, &status);
175    if(snrRecon != this->par.getAtrousCut()){
176      std::stringstream errmsg;
177      errmsg << "snrRecon keyword in reconFile (" << snrRecon
178             << ") does not match that requested ("
179             << this->par.getAtrousCut() << ").\n";
180      duchampError("readReconCube", errmsg.str());
181      return FAILURE;
182    }
183    status = 0;
184    fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
185                  &scaleMin, comment, &status);
186    if(scaleMin != this->par.getMinScale()){
187      std::stringstream errmsg;
188      errmsg << "scaleMin keyword in reconFile (" << scaleMin
189             << ") does not match that requested ("
190             << this->par.getMinScale() << ").\n";
191      duchampError("readReconCube", errmsg.str());
192      return FAILURE;
193    }
194 
195    //
196    // If we get to here, the reconFile exists and matches the atrous
197    //  parameters the user has requested.
198
199    status = 0;
200    fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
201                  this->recon, &anynul, &status);
202 
203    status = 0;
204    fits_close_file(fptr, &status);
205    if (status){
206      duchampWarning("readReconCube", "Error closing file: ");
207      fits_report_error(stderr, status);
208    }
209
210    // We don't want to write out the recon or resid files at the end
211    this->par.setFlagOutputRecon(false);
212    this->par.setFlagOutputResid(false);
213
214    // The reconstruction is done -- set the appropriate flag
215    this->reconExists = true;
216
217    return SUCCESS;
218  }
219}
Note: See TracBrowser for help on using the repository browser.