source: branches/fitshead-branch/Cubes/readRecon.cc @ 99

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

Added a #define WCSLIB_GETWCSTAB command to the headers of those files using
fitsio.h -- this enables cfitsio v.3 to be compiled with g++ v.4 (else it
complains about multiple definitions of wtbarr).
Minor typo fix for outputSpectra.cc.

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