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

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

Generally tidying up code and adding more useful warning/error messages.
Some improvement of the constructor and save array tasks in cubes.cc,
adding tests for negative sizes and changes in array size.

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