source: branches/pixel-map-branch/src/Cubes/readSmooth.cc @ 1441

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

Large raft of changes. Most are minor ones related to fixing up the use of std::string and std::vector (whether they are declared as using, or not...). Other changes include:

  • Moving the reconstruction filter class Filter into its own header/implementation files filter.{hh,cc}. As a result, the atrous.cc file is removed, but atrous.hh remains with just the function prototypes.
  • Incorporating a Filter object into the Param set, so that the reconstruction routines can see it without the messy "extern" call. The define() function is now called in both the Param() and Param::readParams() functions, and no longer in the main body.
  • Col functions in columns.cc moved into the Column namespace, while the template function printEntry is moved into the columns.hh file -- it would not compile on delphinus with it in the columns.cc, even though all the implementations were present.
  • Improved the introductory section of the Guide, with a bit more detail about each of the execution steps. This way the reader can read this section and have a reasonably good idea about what is happening.
  • Other minor changes to the Guide.
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    long nelements;
93    int bitpix,numAxesNew,anynul;
94
95    status = 0;
96    fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
97    if(status){
98      fits_report_error(stderr, status);
99      return FAILURE;
100    }
101
102    if(numAxesNew != this->numDim){
103      std::stringstream errmsg;
104      errmsg << "Smoothed cube has a different number of axes to original!"
105             << " (" << numAxesNew << " cf. " << this->numDim << ")\n";
106      duchampError("readSmoothCube", errmsg.str());
107      return FAILURE;
108    }
109
110    for(int i=0;i<numAxesNew;i++){
111      if(dimAxesNew[i]!=this->axisDim[i]){
112        std::stringstream errmsg;
113        errmsg << "Smoothed cube has different axis dimensions to original!"
114               << "\nAxis #" << i+1 << " has size " << dimAxesNew[i]
115               << " cf. " << this->axisDim[i] <<" in original.\n";     
116        duchampError("readSmoothCube", errmsg.str());
117        return FAILURE;
118      }
119    }
120
121    char *comment = new char[80];
122
123    if(this->par.getFlagSubsection()){
124      char *subsection = new char[80];
125      status = 0;
126      fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(),
127                    subsection, comment, &status);
128      if(status){
129        duchampError("readSmoothCube",
130                     "subsection keyword not present in smoothFile.\n");
131        return FAILURE;
132      }
133      else{
134        if(this->par.getSubsection() != subsection){
135          std::stringstream errmsg;
136          errmsg << "subsection keyword in smoothFile (" << subsection
137                 << ") does not match that requested ("
138                 << this->par.getSubsection() << ").\n";
139          duchampError("readSmoothCube", errmsg.str());
140          return FAILURE;
141        }
142      }
143      delete subsection;
144    }
145
146    int hannWidth;
147    status = 0;
148    fits_read_key(fptr, TINT, (char *)keyword_hanningwidth.c_str(),
149                  &hannWidth, comment, &status);
150    if(hannWidth != this->par.getHanningWidth()){
151      std::stringstream errmsg;
152      errmsg << "hanningWidth keyword in smoothFile (" << hannWidth
153             << ") does not match that requested ("
154             << this->par.getHanningWidth() << ").\n";
155      duchampError("readSmoothCube", errmsg.str());
156      return FAILURE;
157    }
158
159    //
160    // If we get to here, the smoothFile exists and the hanningWidth
161    //  parameter matches that requested.
162
163    status = 0;
164    fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL,
165                  this->recon, &anynul, &status);
166 
167    status = 0;
168    fits_close_file(fptr, &status);
169    if (status){
170      duchampWarning("readSmoothCube", "Error closing file: ");
171      fits_report_error(stderr, status);
172    }
173
174    // We don't want to write out the smoothed files at the end
175    this->par.setFlagOutputSmooth(false);
176
177    // The reconstruction is done -- set the appropriate flag
178    this->reconExists = true;
179
180    return SUCCESS;
181  }
182}
Note: See TracBrowser for help on using the repository browser.