source: trunk/src/Cubes/readSmooth.cc @ 272

Last change on this file since 272 was 270, checked in by Matthew Whiting, 17 years ago

A large set of changes, each of which small ones from compiling with the -Wall flag (plus the -Wno-sign-compare flag -- as we don't care about warnings about comparing ints and unsigned ints).

File size: 5.6 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <string>
4#include <wcs.h>
5#include <wcshdr.h>
6#define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr
7                         // (this is a problem when using gcc v.4+
8#include <fitsio.h>
9#include <duchamp.hh>
10#include <Cubes/cubes.hh>
11
12int Cube::readSmoothCube()
13{
14  /**
15   *   A way to read in a previously Hanning-smoothed array, corresponding
16   *    to the requested parameters, or in the filename given by smoothFile.
17   *   Performs various tests to make sure there are no clashes between
18   *    the requested parameters and those stored in the header of the
19   *    FITS file. Also test to make sure that the size (and subsection,
20   *    if applicable) of the array is the same.
21   */
22
23
24  int status = 0;
25
26  if(!this->par.getFlagSmoothExists()){
27    duchampWarning("readSmoothCube",
28                   "flagSmoothExists is not set. Not reading anything in!\n");
29    return FAILURE;
30  }
31  else if(!this->par.getFlagSmooth()){
32    duchampWarning("readSmoothCube",
33                   "flagSmooth is not set. Don't need to read in smoothed array!\n");
34    return FAILURE;
35  }
36  else {
37
38    // Check to see whether the parameter smoothFile is defined
39    bool smoothGood = false;
40    int exists;
41    std::stringstream errmsg;
42    if(this->par.getSmoothFile() != ""){
43      smoothGood = true;
44      fits_file_exists(this->par.getSmoothFile().c_str(),&exists,&status);
45      if(exists<=0){
46        fits_report_error(stderr, status);
47        errmsg<< "Cannot find requested SmoothFile. Trying with parameters.\n"
48              << "Bad smoothFile was: "<<this->par.getSmoothFile() <<std::endl;
49        duchampWarning("readSmoothCube", errmsg.str());
50        smoothGood = false;
51      }
52    }
53    else{
54      errmsg<< "SmoothFile not specified. Working out name from parameters.\n";
55    }
56 
57    if(!smoothGood){ // if bad, need to look at parameters
58
59      std::string smoothFile = this->par.outputSmoothFile();
60      errmsg << "Trying file " << smoothFile << std::endl;
61      duchampWarning("readSmoothCube", errmsg.str() );
62      smoothGood = true;
63      fits_file_exists(smoothFile.c_str(),&exists,&status);
64      if(exists<=0){
65        fits_report_error(stderr, status);
66        duchampWarning("readSmoothCube","SmoothFile not present.\n");
67        smoothGood = false;
68      }
69
70      if(smoothGood){
71        // were able to open this new file -- use this, so reset the
72        //  relevant parameter
73        this->par.setSmoothFile(smoothFile);
74      }
75      else { // if STILL bad, give error message and exit.
76        duchampError("readSmoothCube","Cannot find Hanning-smoothed file.\n");
77        return FAILURE;
78      }
79
80    }
81
82    // if we get to here, smoothGood is true (ie. file is open);
83
84    status=0;
85    fitsfile *fptr;
86    fits_open_file(&fptr,this->par.getSmoothFile().c_str(),READONLY,&status);
87    short int maxdim=3;
88    long *fpixel = new long[maxdim];
89    for(int i=0;i<maxdim;i++) fpixel[i]=1;
90    long *dimAxesNew = new long[maxdim];
91    for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
92    int bitpix,numAxesNew,anynul;
93
94    status = 0;
95    fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
96    if(status){
97      fits_report_error(stderr, status);
98      return FAILURE;
99    }
100
101    if(numAxesNew != this->numDim){
102      std::stringstream errmsg;
103      errmsg << "Smoothed cube has a different number of axes to original!"
104             << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
105      duchampError("readSmoothCube", errmsg.str());
106      return FAILURE;
107    }
108
109    for(int i=0;i<numAxesNew;i++){
110      if(dimAxesNew[i]!=this->axisDim[i]){
111        std::stringstream errmsg;
112        errmsg << "Smoothed cube has different axis dimensions to original!"
113               << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
114               << " cf. " << this->axisDim[i] <<" in original.\n";     
115        duchampError("readSmoothCube", errmsg.str());
116        return FAILURE;
117      }
118    }
119
120    char *comment = new char[80];
121
122    if(this->par.getFlagSubsection()){
123      char *subsection = new char[80];
124      status = 0;
125      fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
126                    subsection, comment, &status);
127      if(status){
128        duchampError("readSmoothCube",
129                     "subsection keyword not present in smoothFile.\n");
130        return FAILURE;
131      }
132      else{
133        if(this->par.getSubsection() != subsection){
134          std::stringstream errmsg;
135          errmsg << "subsection keyword in smoothFile (" << subsection
136                 << ") does not match that requested ("
137                 << this->par.getSubsection() << ").\n";
138          duchampError("readSmoothCube", errmsg.str());
139          return FAILURE;
140        }
141      }
142      delete subsection;
143    }
144
145    int hannWidth;
146    status = 0;
147    fits_read_key(fptr, TINT, (char *)keyword_hanningwidth.c_str(),
148                  &hannWidth, comment, &status);
149    if(hannWidth != this->par.getHanningWidth()){
150      std::stringstream errmsg;
151      errmsg << "hanningWidth keyword in smoothFile (" << hannWidth
152             << ") does not match that requested ("
153             << this->par.getHanningWidth() << ").\n";
154      duchampError("readSmoothCube", errmsg.str());
155      return FAILURE;
156    }
157
158    //
159    // If we get to here, the smoothFile exists and the hanningWidth
160    //  parameter matches that requested.
161
162    status = 0;
163    fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
164                  this->recon, &anynul, &status);
165 
166    status = 0;
167    fits_close_file(fptr, &status);
168    if (status){
169      duchampWarning("readSmoothCube", "Error closing file: ");
170      fits_report_error(stderr, status);
171    }
172
173    // We don't want to write out the smoothed files at the end
174    this->par.setFlagOutputSmooth(false);
175
176    // The reconstruction is done -- set the appropriate flag
177    this->reconExists = true;
178
179    return SUCCESS;
180  }
181}
Note: See TracBrowser for help on using the repository browser.