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