source: tags/release-0.9.2/Cubes/readRecon.cc @ 1323

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

Large commit, mostly removing dud commented code, and adding comments and
documentation to the existing code. No new features.

File size: 6.0 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <string>
4#include <wcs.h>
5#include <wcshdr.h>
6#include <fitsio.h>
7#include <duchamp.hh>
8#include <Cubes/cubes.hh>
9
10int Cube::readReconCube()
11{
12  /**
13   *  Cube::readReconCube()
14   *   
15   *   A way to read in a previously reconstructed array, corresponding to the
16   *    requested parameters, or in the filename given by reconFile.
17   *   Performs various tests to make sure there are no clashes between the requested
18   *    parameters and those stored in the header of the FITS file.
19   */
20
21
22  fitsfile *fptr;
23  int status = 0;
24
25  // Check to see whether the parameters reconFile and/or residFile are defined.
26  bool reconGood = false;
27  bool residGood = false;
28  int exists;
29  if(this->par.getReconFile() != ""){
30    reconGood = true;
31    fits_file_exists(this->par.getReconFile().c_str(),&exists,&status);
32    if(exists<=0){
33      fits_report_error(stderr, status);
34      std::cerr <<
35        "WARNING <readReconCube> : Cannot find requested ReconFile. Trying with parameters.\n";
36      std::cerr <<
37        "                          Bad reconFile was: "<<this->par.getReconFile()<<std::endl;
38      reconGood = false;
39    }
40  }
41  else{
42    std::cerr <<
43      "WARNING <readReconCube> : ReconFile not specified. Working out name from parameters.\n";
44  }
45 
46  if(!reconGood){ // if bad, need to look at parameters
47
48    string reconFile = this->par.getImageFile();
49    reconFile = reconFile.substr(0,reconFile.size()-5); // remove the ".fits" on the end.
50    std::stringstream ss;
51    ss << ".RECON"<<this->par.getAtrousCut()<<".fits";
52    reconFile += ss.str();
53    reconGood = true;
54    fits_file_exists(reconFile.c_str(),&exists,&status);
55    if(exists<=0){
56      fits_report_error(stderr, status);
57      std::cerr <<
58        "WARNING <readReconCube> : ReconFile based on reconstruction parameters is not present\n";
59      std::cerr <<
60        "                          Looking for: "<<this->par.getReconFile()<<std::endl;
61      reconGood = false;
62    }
63
64    if(reconGood){ // were able to open this file -- use this, so reset the relevant parameter
65      this->par.setReconFile(reconFile);
66    }
67    else { // if STILL bad, give error message and exit.
68      std::cerr << "WARNING <readReconCube> : Cannot find reconstructed file.\n";
69      return FAILURE;
70    }
71
72  }
73
74  // if we get to here, reconGood is true (ie. file is open);
75  status=0;
76  fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
77  short int maxdim=3;
78  long *fpixel = new long[maxdim];
79  for(int i=0;i<maxdim;i++) fpixel[i]=1;
80  long *dimAxesNew = new long[maxdim];
81  for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
82  long nelements;
83  int bitpix,numAxesNew,anynul;
84
85  status = 0;
86  fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
87  if(status){
88    fits_report_error(stderr, status);
89    return FAILURE;
90  }
91
92  if(numAxesNew != this->numDim){
93    std::cerr << "ERROR <readReconCube> : "
94              << "Reconstructed cube has a different number of axes to original! ("
95              << numAxesNew << " cf. " << this->numDim << ")\n";
96    return FAILURE;
97  }
98
99  for(int i=0;i<numAxesNew;i++){
100    if(dimAxesNew[i]!=this->axisDim[i]){
101      std::cerr << "ERROR <readReconCube> : "
102                << "Reconstructed cube has different axis dimensions to original!\n"
103                << "      Axis #" << i << " has size " << dimAxesNew[i]
104                << " cf. " << this->axisDim[i] <<" in original.\n";     
105      return FAILURE;
106    }
107  }     
108
109  char *comment = new char[80];
110  int minMW;
111  fits_read_key(fptr, TINT, (char *)keyword_minMW.c_str(), &minMW, comment, &status);
112  if(minMW != this->par.getMinMW()){
113    std::cerr << "ERROR <readReconCube> : Reconstructed cube has different minMW parameter to original!\n"
114              << "      Reconstructed cube has "<<minMW
115              << " cf. " <<this->par.getMinMW() << " as requested.\n";
116    return FAILURE;
117  }
118  int maxMW;
119  fits_read_key(fptr, TINT, (char *)keyword_maxMW.c_str(), &maxMW, comment, &status);
120  if(maxMW != this->par.getMaxMW()){
121    std::cerr << "ERROR <readReconCube> : Reconstructed cube has different maxMW parameter to original!\n"
122              << "      Reconstructed cube has "<<maxMW
123              << " cf. " <<this->par.getMaxMW() << " as requested.\n";
124    return FAILURE;
125  }
126
127  float *reconArray = new float[this->numPixels];
128  //  fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, this->recon, &anynul, &status);
129  fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, reconArray, &anynul, &status);
130  this->saveRecon(reconArray,this->numPixels);
131 
132  // Make the a trous parameters match what the recon file has.
133  int scaleMin,filterCode;
134  float snrRecon;
135  if(!this->par.getFlagATrous()){
136    this->par.setFlagATrous(true);
137    std::cerr << "WARNING <readReconCube> : Setting flagAtrous from false to true, as the reconstruction exists.\n";
138  }
139  fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), &scaleMin, comment, &status);
140  if(scaleMin != this->par.getMinScale()){
141    this->par.setMinScale(scaleMin);
142    std::cerr << "WARNING <readReconCube> : Changing scaleMin parameter to match ReconFile.\n";
143  }
144  fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(), &filterCode, comment, &status);
145  if(filterCode != this->par.getFilterCode()){
146    this->par.setFilterCode(filterCode);
147    std::cerr << "WARNING <readReconCube> : Changing filterCode parameter to match ReconFile.\n";
148  }
149  fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), &snrRecon, comment, &status);
150  if(snrRecon != this->par.getAtrousCut()){
151    this->par.setAtrousCut(snrRecon);
152    std::cerr << "WARNING <readReconCube> : Changing snrRecon parameter to match ReconFile.\n";
153  }
154
155  fits_close_file(fptr, &status);
156  if (status){
157    std::cerr << "WARNING <readReconCube> : Error closing file: ";
158    fits_report_error(stderr, status);
159    //    return FAILURE;
160  }
161
162  // We don't want to write out the recon or resid files at the end
163  this->par.setFlagOutputRecon(false);
164  this->par.setFlagOutputResid(false);
165
166  // The reconstruction is done -- set the appropriate flag
167  this->reconExists = true;
168
169  return SUCCESS;
170}
Note: See TracBrowser for help on using the repository browser.