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

Last change on this file since 208 was 208, checked in by Matthew Whiting, 18 years ago
  • Enabled saving and reading in of a smoothed array, in manner directly analogous to that for the recon array.
    • New file : src/Cubes/readSmooth.cc
    • The other new functions go in existing files eg. saveImage.cc
    • Renamed some functions (like writeHeader...) to be more obvious what they do.
    • The reading in is taken care of by new function Cube::readSavedArrays() -- handles both smoothed and recon'd arrays.
    • Associated parameters in Param class
    • Clarified names of FITS header strings in duchamp.hh.
  • Updated the documentation to describe the ability to smooth a cube.
  • Added description of feedback mechanisms in the Install appendix.
  • Also, Hanning class improved to guard against memory leaks.


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