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

Last change on this file since 1441 was 394, checked in by MatthewWhiting, 17 years ago

Fixing wcslib include calls so that it is a bit more robust.

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