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

Last change on this file since 987 was 913, checked in by MatthewWhiting, 12 years ago

A large swathe of changes aimed at improving warning/error/exception handling. Now make use of macros and streams. Also, there is now a distinction between DUCHAMPERROR and DUCHAMPTHROW.

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