source: tags/release-1.1.11/src/Cubes/readSmooth.cc @ 1441

Last change on this file since 1441 was 698, checked in by MatthewWhiting, 14 years ago

A bunch of changes aimed at improving the use of OUTCOME to report SUCCESS/FAILURE. When such a value is returned by a function, the returned type is duchamp::OUTCOME.

Also improved the error reporting in saveImage

File size: 8.4 KB
Line 
1// -----------------------------------------------------------------------
2// readSmooth.cc: Read in an existing smoothed 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#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/duchamp.hh>
37#include <duchamp/Cubes/cubes.hh>
38
39namespace duchamp
40{
41
42  OUTCOME Cube::readSmoothCube()
43  {
44    ///  @details
45    ///   A way to read in a previously smoothed array, corresponding
46    ///    to the requested parameters, or in the filename given by smoothFile.
47    ///   Performs various tests to make sure there are no clashes between
48    ///    the requested parameters and those stored in the header of the
49    ///    FITS file. Also test to make sure that the size (and subsection,
50    ///    if applicable) of the array is the same.
51
52
53    int status = 0;
54
55    if(!this->par.getFlagSmoothExists()){
56      duchampWarning("readSmoothCube",
57                     "flagSmoothExists is not set. Not reading anything in!\n");
58      return FAILURE;
59    }
60    else if(!this->par.getFlagSmooth()){
61      duchampWarning("readSmoothCube",
62                     "flagSmooth is not set. Don't need to read in smoothed array!\n");
63      return FAILURE;
64    }
65    else {
66
67      // if kernMin is negative (not defined), make it equal to kernMaj
68      if(this->par.getKernMin() < 0)
69        this->par.setKernMin(this->par.getKernMaj());
70
71      // Check to see whether the parameter smoothFile is defined
72      bool smoothGood = false;
73      int exists;
74      std::stringstream errmsg;
75      if(this->par.getSmoothFile() != ""){
76        smoothGood = true;
77        fits_file_exists(this->par.getSmoothFile().c_str(),&exists,&status);
78        if(exists<=0){
79          fits_report_error(stderr, status);
80          errmsg<< "Cannot find requested SmoothFile. Trying with parameters.\n"
81                << "Bad smoothFile was: "<<this->par.getSmoothFile() <<std::endl;
82          duchampWarning("readSmoothCube", errmsg.str());
83          smoothGood = false;
84        }
85      }
86      else{
87        errmsg<< "SmoothFile not specified. Working out name from parameters.\n";
88      }
89 
90      if(!smoothGood){ // if bad, need to look at parameters
91
92        std::string smoothFile = this->par.outputSmoothFile();
93        errmsg << "Trying file " << smoothFile << std::endl;
94        duchampWarning("readSmoothCube", errmsg.str() );
95        smoothGood = true;
96        fits_file_exists(smoothFile.c_str(),&exists,&status);
97        if(exists<=0){
98          fits_report_error(stderr, status);
99          //    duchampWarning("readSmoothCube","SmoothFile not present.\n");
100          smoothGood = false;
101        }
102
103        if(smoothGood){
104          // were able to open this new file -- use this, so reset the
105          //  relevant parameter
106          this->par.setSmoothFile(smoothFile);
107        }
108        else { // if STILL bad, give error message and exit.
109          duchampError("readSmoothCube","Cannot find Smoothed file.\n");
110          return FAILURE;
111        }
112
113      }
114
115      // if we get to here, smoothGood is true (ie. file is open);
116
117      status=0;
118      fitsfile *fptr;
119      fits_open_file(&fptr,this->par.getSmoothFile().c_str(),READONLY,&status);
120      short int maxdim=3;
121      long *fpixel = new long[maxdim];
122      for(int i=0;i<maxdim;i++) fpixel[i]=1;
123      long *dimAxesNew = new long[maxdim];
124      for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
125      int bitpix,numAxesNew,anynul;
126
127      status = 0;
128      fits_get_img_param(fptr,maxdim,&bitpix,&numAxesNew,dimAxesNew,&status);
129      if(status){
130        fits_report_error(stderr, status);
131        return FAILURE;
132      }
133
134      if(numAxesNew != this->numDim){
135        std::stringstream errmsg;
136        errmsg << "Smoothed cube has a different number of axes to original!"
137               << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
138        duchampError("readSmoothCube", errmsg.str());
139        return FAILURE;
140      }
141
142      for(int i=0;i<numAxesNew;i++){
143        if(dimAxesNew[i]!=this->axisDim[i]){
144          std::stringstream errmsg;
145          errmsg << "Smoothed cube has different axis dimensions to original!"
146                 << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
147                 << " cf. " << this->axisDim[i] <<" in original.\n";   
148          duchampError("readSmoothCube", errmsg.str());
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("readSmoothCube",
162                       "subsection keyword not present in smoothFile.\n");
163          return FAILURE;
164        }
165        else{
166          if(this->par.getSubsection() != subsection){
167            std::stringstream errmsg;
168            errmsg << "subsection keyword in smoothFile (" << subsection
169                   << ") does not match that requested ("
170                   << this->par.getSubsection() << ").\n";
171            duchampError("readSmoothCube", errmsg.str());
172            return FAILURE;
173          }
174        }
175        delete subsection;
176      }
177
178      if(this->par.getSmoothType()=="spectral"){
179
180        int hannWidth;
181        status = 0;
182        fits_read_key(fptr, TINT, (char *)keyword_hanningwidth.c_str(),
183                      &hannWidth, comment, &status);
184        if(hannWidth != this->par.getHanningWidth()){
185          std::stringstream errmsg;
186          errmsg << keyword_hanningwidth << " keyword in smoothFile ("
187                 << hannWidth << ") does not match the hanningWidth parameter ("
188                 << this->par.getHanningWidth() << ").\n";
189          duchampError("readSmoothCube", errmsg.str());
190          return FAILURE;
191        }
192
193      }
194      else if(this->par.getSmoothType()=="spatial"){
195
196        float maj,min,pa;
197        status = 0;
198        fits_read_key(fptr, TFLOAT, (char *)keyword_kernmaj.c_str(),
199                      &maj, comment, &status);
200        if(maj != this->par.getKernMaj()){
201          std::stringstream errmsg;
202          errmsg << keyword_kernmaj << " keyword in smoothFile ("
203                 << maj << ") does not match the kernMaj parameter ("
204                 << this->par.getKernMaj() << ").\n";
205          duchampError("readSmoothCube", errmsg.str());
206          return FAILURE;
207        }
208        status = 0;
209        fits_read_key(fptr, TFLOAT, (char *)keyword_kernmin.c_str(),
210                      &min, comment, &status);
211        if(min != this->par.getKernMin()){
212          std::stringstream errmsg;
213          errmsg << keyword_kernmin << " keyword in smoothFile ("
214                 << maj << ") does not match the kernMin parameter ("
215                 << this->par.getKernMin() << ").\n";
216          duchampError("readSmoothCube", errmsg.str());
217          return FAILURE;
218        }
219        status = 0;
220        fits_read_key(fptr, TFLOAT, (char *)keyword_kernpa.c_str(),
221                      &pa, comment, &status);
222        if(pa != this->par.getKernPA()){
223          std::stringstream errmsg;
224          errmsg << keyword_kernpa << " keyword in smoothFile ("
225                 << maj << ") does not match the kernPA parameter ("
226                 << this->par.getKernPA() << ").\n";
227          duchampError("readSmoothCube", errmsg.str());
228          return FAILURE;
229        }
230
231      }
232
233      //
234      // If we get to here, the smoothFile exists and the smoothing
235      // parameters match those requested.
236
237      status = 0;
238      fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
239                    this->recon, &anynul, &status);
240 
241      status = 0;
242      fits_close_file(fptr, &status);
243      if (status){
244        duchampWarning("readSmoothCube", "Error closing file: ");
245        fits_report_error(stderr, status);
246      }
247
248      // We don't want to write out the smoothed files at the end
249      this->par.setFlagOutputSmooth(false);
250
251      // The reconstruction is done -- set the appropriate flag
252      this->reconExists = true;
253
254      return SUCCESS;
255    }
256  }
257
258}
Note: See TracBrowser for help on using the repository browser.