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

Last change on this file since 272 was 270, checked in by Matthew Whiting, 17 years ago

A large set of changes, each of which small ones from compiling with the -Wall flag (plus the -Wno-sign-compare flag -- as we don't care about warnings about comparing ints and unsigned ints).

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