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

Last change on this file since 1095 was 1094, checked in by MatthewWhiting, 12 years ago

Ticket #164 - was saving to wrong array

File size: 9.0 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 <wcslib/wcs.h>
32#include <wcslib/wcshdr.h>
33#include <wcslib/wcsunits.h>
34#define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr
35                         // (this is a problem when using gcc v.4+
36#include <fitsio.h>
37#include <duchamp/duchamp.hh>
38#include <duchamp/Cubes/cubes.hh>
39
40namespace duchamp
41{
42
43  OUTCOME Cube::readReconCube()
44  {
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.
52
53    int status = 0;
54
55    if(!this->par.getFlagReconExists()){
56      DUCHAMPWARN("readReconCube", "reconExists flag is not set. Not reading anything in!");
57      return FAILURE;
58    }
59    else if(!this->par.getFlagATrous()){
60      DUCHAMPWARN("readReconCube","flagATrous is not set. Don't need to read in recon array!");
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      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);
73          DUCHAMPWARN("readReconCube", "Cannot find requested ReconFile. Trying with parameters. Bad reconFile was: "<<this->par.getReconFile());
74          reconGood = false;
75        }
76      }
77      else{
78        DUCHAMPWARN("readReconCube", "ReconFile not specified. Working out name from parameters.");
79      }
80 
81      if(!reconGood){ // if bad, need to look at parameters
82
83        std::string reconFile = this->par.outputReconFile();
84        DUCHAMPWARN("readReconCube", "Trying file " << reconFile );
85        reconGood = true;
86        fits_file_exists(reconFile.c_str(),&exists,&status);
87        if(exists<=0){
88          fits_report_error(stderr, status);
89          //    DUCHAMPWARNING("readReconCube","ReconFile not present.");
90          reconGood = false;
91        }
92
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.
99          DUCHAMPERROR("readReconCube","Cannot find reconstructed file.");
100          return FAILURE;
101        }
102
103      }
104
105      // if we get to here, reconGood is true (ie. file is open);
106
107      // Identify which axes are the "interesting" ones
108      int lng,lat,spc;
109      if(this->head.isWCS() && (this->head.WCS().spec>=0)){
110        lng = this->head.WCS().lng;
111        lat = this->head.WCS().lat;
112        spc = this->head.WCS().spec;
113      }
114      else{
115        lng = 0;
116        lat = 1;
117        spc = 2;
118      }
119
120      status=0;
121      fitsfile *fptr;
122      fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status);
123      short int maxdim=3;
124      long *fpixel = new long[maxdim];
125      for(int i=0;i<maxdim;i++) fpixel[i]=1;
126      long *lpixel = new long[maxdim];
127      for(int i=0;i<maxdim;i++) lpixel[i]=this->axisDim[i];
128      long *inc = new long[maxdim];
129      for(int i=0;i<maxdim;i++) inc[i]=1;
130      long *dimAxesNew = new long[maxdim];
131      for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
132      int bitpix,numAxesNew,anynul;
133
134      status = 0;
135      fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
136      if(status){
137        fits_report_error(stderr, status);
138        return FAILURE;
139      }
140
141      if(numAxesNew != this->numDim){
142        DUCHAMPERROR("readReconCube", "Reconstructed cube has a different number of axes to original!" << " (" << numAxesNew << " cf. " << this->numDim << ")");
143        return FAILURE;
144      }
145
146      for(int i=0;i<numAxesNew;i++){
147        if(dimAxesNew[i]!=int(this->axisDim[i])){
148          DUCHAMPERROR("readReconCube", "Reconstructed cube has different axis dimensions to original! Axis #" << i+1 << " has size " << dimAxesNew[i] << " cf. " << this->axisDim[i] <<" in original.");
149          return FAILURE;
150        }
151      }
152
153      char *comment = new char[80];
154
155      if(this->par.getFlagSubsection()){
156        char *subsection = new char[80];
157        status = 0;
158        fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
159                      subsection, comment, &status);
160        if(status){
161          DUCHAMPERROR("readReconCube", "subsection keyword not present in reconFile.");
162          return FAILURE;
163        }
164        else{
165          if(this->par.getSubsection() != subsection){
166            DUCHAMPERROR("readReconCube", "subsection keyword in reconFile (" << subsection << ") does not match that requested (" << this->par.getSubsection() << ").");
167            return FAILURE;
168          }
169        }
170        delete subsection;
171      }
172
173      unsigned int scaleMin;
174      int filterCode,reconDim;
175      float snrRecon;
176      status = 0;
177      fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(),
178                    &reconDim, comment, &status);
179      if(reconDim != this->par.getReconDim()){
180        DUCHAMPERROR("readReconCube", "reconDim keyword in reconFile (" << reconDim << ") does not match that requested (" << this->par.getReconDim() << ").");
181        return FAILURE;
182      }
183      status = 0;
184      fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(),
185                    &filterCode, comment, &status);
186      if(filterCode != this->par.getFilterCode()){
187        DUCHAMPERROR("readReconCube", "filterCode keyword in reconFile (" << filterCode << ") does not match that requested (" << this->par.getFilterCode() << ").");
188        return FAILURE;
189      }
190      status = 0;
191      fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(),
192                    &snrRecon, comment, &status);
193      if(snrRecon != this->par.getAtrousCut()){
194        DUCHAMPERROR("readReconCube", "snrRecon keyword in reconFile (" << snrRecon << ") does not match that requested (" << this->par.getAtrousCut() << ").");
195        return FAILURE;
196      }
197      status = 0;
198      fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(),
199                    &scaleMin, comment, &status);
200      if(scaleMin != this->par.getMinScale()){
201        DUCHAMPERROR("readReconCube", "scaleMin keyword in reconFile (" << scaleMin << ") does not match that requested (" << this->par.getMinScale() << ").");
202        return FAILURE;
203      }
204
205      // Read the BUNIT keyword, and translate to standard unit format if needs be
206      std::string header("BUNIT");
207      char *unit = new char[FLEN_VALUE];
208      std::string fluxunits;
209      fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
210      if (status){
211        DUCHAMPWARN("Recon Reader","Error reading BUNIT keyword: ");
212        fits_report_error(stderr, status);
213        return FAILURE;
214      }
215      else{
216        wcsutrn(0,unit);
217        fluxunits = unit;
218      }
219 
220      //
221      // If we get to here, the reconFile exists and matches the atrous
222      //  parameters the user has requested.
223
224      int colnum = 0;  // want the first dataset in the FITS file
225      status = 0;
226      // fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
227      //                    this->recon, &anynul, &status);
228      if(fits_read_subset_flt(fptr, colnum, 3, dimAxesNew,
229                              fpixel, lpixel, inc,
230                              this->par.getBlankPixVal(), this->recon, &anynul, &status)){
231        DUCHAMPERROR("Recon Reader", "There was an error reading in the data array:");
232        fits_report_error(stderr, status);
233        return FAILURE;
234      }
235
236      status = 0;
237      fits_close_file(fptr, &status);
238      if (status){
239        DUCHAMPWARN("readReconCube", "Error closing file: ");
240        fits_report_error(stderr, status);
241      }
242
243      // We don't want to write out the recon or resid files at the end
244      this->par.setFlagOutputRecon(false);
245      this->par.setFlagOutputResid(false);
246
247      this->convertFluxUnits(fluxunits,this->par.getNewFluxUnits(),RECON);
248
249      // The reconstruction is done -- set the appropriate flag
250      this->reconExists = true;
251
252      return SUCCESS;
253    }
254  }
255
256}
Note: See TracBrowser for help on using the repository browser.