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

Last change on this file since 187 was 179, 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
RevLine 
[71]1#include <iostream>
2#include <sstream>
3#include <string>
4#include <wcs.h>
5#include <wcshdr.h>
[179]6#define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr
[103]7                         // (this is a problem when using gcc v.4+
[71]8#include <fitsio.h>
9#include <duchamp.hh>
10#include <Cubes/cubes.hh>
11
12int Cube::readReconCube()
13{
[86]14  /**
15   *  Cube::readReconCube()
16   *   
[139]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.
[86]23   */
[71]24
[86]25
[71]26  int status = 0;
27
[179]28  // Check to see whether the parameters reconFile and/or residFile are defined
[71]29  bool reconGood = false;
30  int exists;
[139]31  std::stringstream errmsg;
[71]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);
[120]37      errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n"
38            << "Bad reconFile was: "<<this->par.getReconFile() << std::endl;
39      duchampWarning("readReconCube", errmsg.str());
[71]40      reconGood = false;
41    }
42  }
43  else{
[139]44    errmsg<< "ReconFile not specified. Working out name from parameters.\n";
[71]45  }
46 
47  if(!reconGood){ // if bad, need to look at parameters
48
[103]49    string reconFile = this->par.outputReconFile();
[139]50    errmsg << "Trying file " << reconFile << std::endl;
51    duchampWarning("readReconCube", errmsg.str() );
[71]52    reconGood = true;
53    fits_file_exists(reconFile.c_str(),&exists,&status);
54    if(exists<=0){
55      fits_report_error(stderr, status);
[120]56      duchampWarning("readReconCube","ReconFile not present.\n");
[71]57      reconGood = false;
58    }
59
[139]60    if(reconGood){
[179]61      // were able to open this new file -- use this, so reset the
62      //  relevant parameter
[71]63      this->par.setReconFile(reconFile);
64    }
65    else { // if STILL bad, give error message and exit.
[120]66      duchampError("readReconCube","Cannot find reconstructed file.\n");
[71]67      return FAILURE;
68    }
69
70  }
71
72  // if we get to here, reconGood is true (ie. file is open);
73  status=0;
[104]74  fitsfile *fptr;
[71]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){
[120]92    std::stringstream errmsg;
[179]93    errmsg << "Reconstructed cube has a different number of axes to original!"
94           << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
[120]95    duchampError("readReconCube", errmsg.str());
[71]96    return FAILURE;
97  }
98
99  for(int i=0;i<numAxesNew;i++){
100    if(dimAxesNew[i]!=this->axisDim[i]){
[120]101      std::stringstream errmsg;
[179]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";       
[120]105      duchampError("readReconCube", errmsg.str());
[71]106      return FAILURE;
107    }
[105]108  }
[71]109
[104]110  char *comment = new char[80];
[105]111
112  if(this->par.getFlagSubsection()){
113    char *subsection = new char[80];
114    status = 0;
[179]115    fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
116                  subsection, comment, &status);
[105]117    if(status){
[179]118      duchampError("readReconCube",
119                   "subsection keyword not present in reconFile.\n");
[105]120      return FAILURE;
121    }
122    else{
123      if(this->par.getSubsection() != subsection){
[120]124        std::stringstream errmsg;
125        errmsg << "subsection keyword in reconFile (" << subsection
[179]126               << ") does not match that requested ("
127               << this->par.getSubsection() << ").\n";
[120]128        duchampError("readReconCube", errmsg.str());
[105]129        return FAILURE;
130      }
131    }
132    delete subsection;
133  }
134
[104]135  int scaleMin,filterCode,reconDim;
136  float snrRecon;
137  status = 0;
[179]138  fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
139                &reconDim, comment, &status);
[104]140  if(reconDim != this->par.getReconDim()){
[120]141    std::stringstream errmsg;
142    errmsg << "reconDim keyword in reconFile (" << reconDim
[179]143           << ") does not match that requested ("
144           << this->par.getReconDim() << ").\n";
[120]145    duchampError("readReconCube", errmsg.str());
[104]146    return FAILURE;
147  }
148  status = 0;
[179]149  fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
150                &filterCode, comment, &status);
[104]151  if(filterCode != this->par.getFilterCode()){
[120]152    std::stringstream errmsg;
153    errmsg << "filterCode keyword in reconFile (" << filterCode
[179]154           << ") does not match that requested ("
155           << this->par.getFilterCode() << ").\n";
[120]156    duchampError("readReconCube", errmsg.str());
[104]157    return FAILURE;
158  }
159  status = 0;
[179]160  fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
161                &snrRecon, comment, &status);
[104]162  if(snrRecon != this->par.getAtrousCut()){
[120]163    std::stringstream errmsg;
164    errmsg << "snrRecon keyword in reconFile (" << snrRecon
[179]165           << ") does not match that requested ("
166           << this->par.getAtrousCut() << ").\n";
[120]167    duchampError("readReconCube", errmsg.str());
[104]168    return FAILURE;
169  }
170  status = 0;
[179]171  fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
172                &scaleMin, comment, &status);
[104]173  if(scaleMin != this->par.getMinScale()){
[120]174    std::stringstream errmsg;
175    errmsg << "scaleMin keyword in reconFile (" << scaleMin
[179]176           << ") does not match that requested ("
177           << this->par.getMinScale() << ").\n";
[120]178    duchampError("readReconCube", errmsg.str());
[104]179    return FAILURE;
180  }
181 
182  //
[179]183  // If we get to here, the reconFile exists and matches the atrous parameters
184  // the user has requested.
[104]185
[71]186  float *reconArray = new float[this->numPixels];
[103]187  status = 0;
[179]188  fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, reconArray,
189                &anynul, &status);
[71]190  this->saveRecon(reconArray,this->numPixels);
191 
[103]192  status = 0;
[71]193  fits_close_file(fptr, &status);
194  if (status){
[120]195    duchampWarning("readReconCube", "Error closing file: ");
[71]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.