source: tags/release-1.1/src/Cubes/readRecon.cc @ 1391

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

Updated the verification files again, plus detection.cc, trying to make sitar and delphinus agree on integrated fluxes (still can't).
Also commented out some un-necessary comments.

File size: 8.2 KB
Line 
1// -----------------------------------------------------------------------
2// readRecon.cc: Read in an existing wavelet-reconstructed FITS file.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <sstream>
30#include <string>
31#include <wcs.h>
32#include <wcshdr.h>
33#define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr
34                         // (this is a problem when using gcc v.4+
35#include <fitsio.h>
36#include <duchamp.hh>
37#include <Cubes/cubes.hh>
38
39int Cube::readReconCube()
40{
41  /**
42   *  A way to read in a previously reconstructed array, corresponding
43   *    to the requested parameters, or in the filename given by reconFile.
44   *   Performs various tests to make sure there are no clashes between
45   *    the requested parameters and those stored in the header of the
46   *    FITS file. Also test to make sure that the size (and subsection,
47   *    if applicable) of the array is the same.
48   */
49
50
51  int status = 0;
52
53  if(!this->par.getFlagReconExists()){
54    duchampWarning("readReconCube",
55                   "reconExists flag is not set. Not reading anything in!\n");
56    return FAILURE;
57  }
58  else if(!this->par.getFlagATrous()){
59    duchampWarning("readReconCube",
60                   "flagATrous is not set. Don't need to read in recon array!\n");
61    return FAILURE;
62  }
63  else {
64
65    // Check to see whether the parameters reconFile and/or residFile are defined
66    bool reconGood = false;
67    int exists;
68    std::stringstream errmsg;
69    if(this->par.getReconFile() != ""){
70      reconGood = true;
71      fits_file_exists(this->par.getReconFile().c_str(),&exists,&status);
72      if(exists<=0){
73        fits_report_error(stderr, status);
74        errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n"
75              << "Bad reconFile was: "<<this->par.getReconFile() << std::endl;
76        duchampWarning("readReconCube", errmsg.str());
77        reconGood = false;
78      }
79    }
80    else{
81      errmsg<< "ReconFile not specified. Working out name from parameters.\n";
82    }
83 
84    if(!reconGood){ // if bad, need to look at parameters
85
86      std::string reconFile = this->par.outputReconFile();
87      errmsg << "Trying file " << reconFile << std::endl;
88      duchampWarning("readReconCube", errmsg.str() );
89      reconGood = true;
90      fits_file_exists(reconFile.c_str(),&exists,&status);
91      if(exists<=0){
92        fits_report_error(stderr, status);
93//      duchampWarning("readReconCube","ReconFile not present.\n");
94        reconGood = false;
95      }
96
97      if(reconGood){
98        // were able to open this new file -- use this, so reset the
99        //  relevant parameter
100        this->par.setReconFile(reconFile);
101      }
102      else { // if STILL bad, give error message and exit.
103        duchampError("readReconCube","Cannot find reconstructed file.\n");
104        return FAILURE;
105      }
106
107    }
108
109    // if we get to here, reconGood is true (ie. file is open);
110
111    status=0;
112    fitsfile *fptr;
113    fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
114    short int maxdim=3;
115    long *fpixel = new long[maxdim];
116    for(int i=0;i<maxdim;i++) fpixel[i]=1;
117    long *dimAxesNew = new long[maxdim];
118    for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
119    int bitpix,numAxesNew,anynul;
120
121    status = 0;
122    fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
123    if(status){
124      fits_report_error(stderr, status);
125      return FAILURE;
126    }
127
128    if(numAxesNew != this->numDim){
129      std::stringstream errmsg;
130      errmsg << "Reconstructed cube has a different number of axes to original!"
131             << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
132      duchampError("readReconCube", errmsg.str());
133      return FAILURE;
134    }
135
136    for(int i=0;i<numAxesNew;i++){
137      if(dimAxesNew[i]!=this->axisDim[i]){
138        std::stringstream errmsg;
139        errmsg << "Reconstructed cube has different axis dimensions to original!"
140               << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
141               << " cf. " << this->axisDim[i] <<" in original.\n";     
142        duchampError("readReconCube", errmsg.str());
143        return FAILURE;
144      }
145    }
146
147    char *comment = new char[80];
148
149    if(this->par.getFlagSubsection()){
150      char *subsection = new char[80];
151      status = 0;
152      fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
153                    subsection, comment, &status);
154      if(status){
155        duchampError("readReconCube",
156                     "subsection keyword not present in reconFile.\n");
157        return FAILURE;
158      }
159      else{
160        if(this->par.getSubsection() != subsection){
161          std::stringstream errmsg;
162          errmsg << "subsection keyword in reconFile (" << subsection
163                 << ") does not match that requested ("
164                 << this->par.getSubsection() << ").\n";
165          duchampError("readReconCube", errmsg.str());
166          return FAILURE;
167        }
168      }
169      delete subsection;
170    }
171
172    int scaleMin,filterCode,reconDim;
173    float snrRecon;
174    status = 0;
175    fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
176                  &reconDim, comment, &status);
177    if(reconDim != this->par.getReconDim()){
178      std::stringstream errmsg;
179      errmsg << "reconDim keyword in reconFile (" << reconDim
180             << ") does not match that requested ("
181             << this->par.getReconDim() << ").\n";
182      duchampError("readReconCube", errmsg.str());
183      return FAILURE;
184    }
185    status = 0;
186    fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
187                  &filterCode, comment, &status);
188    if(filterCode != this->par.getFilterCode()){
189      std::stringstream errmsg;
190      errmsg << "filterCode keyword in reconFile (" << filterCode
191             << ") does not match that requested ("
192             << this->par.getFilterCode() << ").\n";
193      duchampError("readReconCube", errmsg.str());
194      return FAILURE;
195    }
196    status = 0;
197    fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
198                  &snrRecon, comment, &status);
199    if(snrRecon != this->par.getAtrousCut()){
200      std::stringstream errmsg;
201      errmsg << "snrRecon keyword in reconFile (" << snrRecon
202             << ") does not match that requested ("
203             << this->par.getAtrousCut() << ").\n";
204      duchampError("readReconCube", errmsg.str());
205      return FAILURE;
206    }
207    status = 0;
208    fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
209                  &scaleMin, comment, &status);
210    if(scaleMin != this->par.getMinScale()){
211      std::stringstream errmsg;
212      errmsg << "scaleMin keyword in reconFile (" << scaleMin
213             << ") does not match that requested ("
214             << this->par.getMinScale() << ").\n";
215      duchampError("readReconCube", errmsg.str());
216      return FAILURE;
217    }
218 
219    //
220    // If we get to here, the reconFile exists and matches the atrous
221    //  parameters the user has requested.
222
223    status = 0;
224    fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
225                  this->recon, &anynul, &status);
226 
227    status = 0;
228    fits_close_file(fptr, &status);
229    if (status){
230      duchampWarning("readReconCube", "Error closing file: ");
231      fits_report_error(stderr, status);
232    }
233
234    // We don't want to write out the recon or resid files at the end
235    this->par.setFlagOutputRecon(false);
236    this->par.setFlagOutputResid(false);
237
238    // The reconstruction is done -- set the appropriate flag
239    this->reconExists = true;
240
241    return SUCCESS;
242  }
243}
Note: See TracBrowser for help on using the repository browser.