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

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