source: tags/release-1.0.5/src/Cubes/readRecon.cc @ 1455

Last change on this file since 1455 was 180, checked in by Matthew Whiting, 18 years ago

Fixed a minor problem with the formatting of an error message in Cube::readReconCube().

File size: 6.5 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  // Check to see whether the parameters reconFile and/or residFile are defined
29  bool reconGood = false;
30  int exists;
31  std::stringstream errmsg;
32  if(this->par.getReconFile() != ""){
33    reconGood = true;
34    fits_file_exists(this->par.getReconFile().c_str(),&exists,&status);
35    if(exists<=0){
36      fits_report_error(stderr, status);
37      errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n"
38            << "Bad reconFile was: "<<this->par.getReconFile() << std::endl;
39      duchampWarning("readReconCube", errmsg.str());
40      reconGood = false;
41    }
42  }
43  else{
44    errmsg<< "ReconFile not specified. Working out name from parameters.\n";
45  }
46 
47  if(!reconGood){ // if bad, need to look at parameters
48
49    string reconFile = this->par.outputReconFile();
50    errmsg << "Trying file " << reconFile << std::endl;
51    duchampWarning("readReconCube", errmsg.str() );
52    reconGood = true;
53    fits_file_exists(reconFile.c_str(),&exists,&status);
54    if(exists<=0){
55      fits_report_error(stderr, status);
56      duchampWarning("readReconCube","ReconFile not present.\n");
57      reconGood = false;
58    }
59
60    if(reconGood){
61      // were able to open this new file -- use this, so reset the
62      //  relevant parameter
63      this->par.setReconFile(reconFile);
64    }
65    else { // if STILL bad, give error message and exit.
66      duchampError("readReconCube","Cannot find reconstructed file.\n");
67      return FAILURE;
68    }
69
70  }
71
72  // if we get to here, reconGood is true (ie. file is open);
73  status=0;
74  fitsfile *fptr;
75  fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
76  short int maxdim=3;
77  long *fpixel = new long[maxdim];
78  for(int i=0;i<maxdim;i++) fpixel[i]=1;
79  long *dimAxesNew = new long[maxdim];
80  for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
81  long nelements;
82  int bitpix,numAxesNew,anynul;
83
84  status = 0;
85  fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
86  if(status){
87    fits_report_error(stderr, status);
88    return FAILURE;
89  }
90
91  if(numAxesNew != this->numDim){
92    std::stringstream errmsg;
93    errmsg << "Reconstructed cube has a different number of axes to original!"
94           << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
95    duchampError("readReconCube", errmsg.str());
96    return FAILURE;
97  }
98
99  for(int i=0;i<numAxesNew;i++){
100    if(dimAxesNew[i]!=this->axisDim[i]){
101      std::stringstream errmsg;
102      errmsg << "Reconstructed cube has different axis dimensions to original!"
103             << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
104             << " cf. " << this->axisDim[i] <<" in original.\n";       
105      duchampError("readReconCube", errmsg.str());
106      return FAILURE;
107    }
108  }
109
110  char *comment = new char[80];
111
112  if(this->par.getFlagSubsection()){
113    char *subsection = new char[80];
114    status = 0;
115    fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
116                  subsection, comment, &status);
117    if(status){
118      duchampError("readReconCube",
119                   "subsection keyword not present in reconFile.\n");
120      return FAILURE;
121    }
122    else{
123      if(this->par.getSubsection() != subsection){
124        std::stringstream errmsg;
125        errmsg << "subsection keyword in reconFile (" << subsection
126               << ") does not match that requested ("
127               << this->par.getSubsection() << ").\n";
128        duchampError("readReconCube", errmsg.str());
129        return FAILURE;
130      }
131    }
132    delete subsection;
133  }
134
135  int scaleMin,filterCode,reconDim;
136  float snrRecon;
137  status = 0;
138  fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
139                &reconDim, comment, &status);
140  if(reconDim != this->par.getReconDim()){
141    std::stringstream errmsg;
142    errmsg << "reconDim keyword in reconFile (" << reconDim
143           << ") does not match that requested ("
144           << this->par.getReconDim() << ").\n";
145    duchampError("readReconCube", errmsg.str());
146    return FAILURE;
147  }
148  status = 0;
149  fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
150                &filterCode, comment, &status);
151  if(filterCode != this->par.getFilterCode()){
152    std::stringstream errmsg;
153    errmsg << "filterCode keyword in reconFile (" << filterCode
154           << ") does not match that requested ("
155           << this->par.getFilterCode() << ").\n";
156    duchampError("readReconCube", errmsg.str());
157    return FAILURE;
158  }
159  status = 0;
160  fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
161                &snrRecon, comment, &status);
162  if(snrRecon != this->par.getAtrousCut()){
163    std::stringstream errmsg;
164    errmsg << "snrRecon keyword in reconFile (" << snrRecon
165           << ") does not match that requested ("
166           << this->par.getAtrousCut() << ").\n";
167    duchampError("readReconCube", errmsg.str());
168    return FAILURE;
169  }
170  status = 0;
171  fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
172                &scaleMin, comment, &status);
173  if(scaleMin != this->par.getMinScale()){
174    std::stringstream errmsg;
175    errmsg << "scaleMin keyword in reconFile (" << scaleMin
176           << ") does not match that requested ("
177           << this->par.getMinScale() << ").\n";
178    duchampError("readReconCube", errmsg.str());
179    return FAILURE;
180  }
181 
182  //
183  // If we get to here, the reconFile exists and matches the atrous parameters
184  // the user has requested.
185
186  float *reconArray = new float[this->numPixels];
187  status = 0;
188  fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, reconArray,
189                &anynul, &status);
190  this->saveRecon(reconArray,this->numPixels);
191 
192  status = 0;
193  fits_close_file(fptr, &status);
194  if (status){
195    duchampWarning("readReconCube", "Error closing file: ");
196    fits_report_error(stderr, status);
197  }
198
199  // We don't want to write out the recon or resid files at the end
200  this->par.setFlagOutputRecon(false);
201  this->par.setFlagOutputResid(false);
202
203  // The reconstruction is done -- set the appropriate flag
204  this->reconExists = true;
205
206  return SUCCESS;
207}
Note: See TracBrowser for help on using the repository browser.