source: tags/release-1.2.2/src/Cubes/ReadExisting.cc

Last change on this file was 1111, checked in by MatthewWhiting, 12 years ago

Fixing some compilation warnings.

File size: 5.1 KB
Line 
1#include <duchamp/Cubes/ReadExisting.hh>
2#include <duchamp/duchamp.hh>
3#include <duchamp/Cubes/cubes.hh>
4#include <fitsio.h>
5#include <wcslib/wcsunits.h>
6
7namespace duchamp {
8
9  ReadExisting::ReadExisting()
10  {
11    itsCube = 0;
12    itsArray = 0;
13    itsFptr = 0;
14  }
15
16  ReadExisting::ReadExisting(Cube *cube):
17    itsCube(cube)
18  {
19    itsArray = this->itsCube->getRecon();
20    itsFptr = 0;
21  }
22
23  ReadExisting::ReadExisting(const ReadExisting &other)
24  {
25    this->operator=(other);
26  }
27
28  ReadExisting& ReadExisting::operator= (const ReadExisting& other)
29  {
30    if(this==&other) return *this;
31    this->itsCube = other.itsCube;
32    this->itsArray = other.itsArray;
33    return *this;
34  }
35
36  OUTCOME ReadExisting::openFile()
37  {
38    int status=0;
39    fits_open_file(&this->itsFptr,this->itsFilename.c_str(),READONLY,&status);
40    if(status){
41      DUCHAMPWARN("Reading Cube", "Error opening file " << this->itsFilename);
42      fits_report_error(stderr, status);
43      return FAILURE;
44    }
45    return SUCCESS;
46  }
47
48  OUTCOME ReadExisting::checkDim()
49  {
50    OUTCOME result=SUCCESS;
51    if(this->itsFptr==0){
52      DUCHAMPERROR("Reading Cube","FITS file not open");
53      result = FAILURE;
54    }
55    else{
56     
57      int status = 0, maxdim = 3;
58      int bitpix,numAxesNew;
59      long *dimAxesNew = new long[maxdim];
60      for(int i=0;i<maxdim;i++) dimAxesNew[i]=1;
61      fits_get_img_param(this->itsFptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status);
62      if(status){
63        fits_report_error(stderr, status);
64        result = FAILURE;
65      }
66
67      if(numAxesNew != this->itsCube->getNumDim()){
68        DUCHAMPERROR("readReconCube", "Input cube " << this->itsFilename << " has a different number of axes to original!"
69                     << " (" << numAxesNew << " cf. " << this->itsCube->getNumDim() << ")");
70        result = FAILURE;
71      }
72
73      for(int i=0;i<numAxesNew;i++){
74        if(dimAxesNew[i]!=int(this->itsCube->getDimArray()[i])){
75          DUCHAMPERROR("readReconCube", "Input cube " << this->itsFilename << " has different axis dimensions to original! Axis #"
76                       << i+1 << " has size " << dimAxesNew[i] << " cf. " << this->itsCube->getDimArray()[i] <<" in original.");
77          result = FAILURE;
78        }
79      }
80
81
82      char *comment = new char[80];
83      if(this->itsCube->pars().getFlagSubsection()){
84        char *subsection = new char[80];
85        status = 0;
86        fits_read_key(this->itsFptr, TSTRING, (char *)keyword_subsection.c_str(), subsection, comment, &status);
87        if(status){
88          DUCHAMPERROR("readReconCube", "subsection keyword not present in reconFile.");
89          result = FAILURE;
90        }
91        else{
92          if(this->itsCube->pars().getSubsection() != subsection){
93            DUCHAMPERROR("readReconCube", "subsection keyword in reconFile (" << subsection
94                         << ") does not match that requested (" << this->itsCube->pars().getSubsection() << ").");
95            result = FAILURE;
96          }
97        }
98        delete subsection;
99      }
100
101    }
102
103    return result;
104
105  }
106
107  OUTCOME ReadExisting::readFromFile()
108  {
109    OUTCOME result=SUCCESS;
110    if(this->itsFptr==0){
111      DUCHAMPERROR("Reading Cube","FITS file not open");
112      result = FAILURE;
113    }
114    else{
115
116      short int maxdim=3;
117      long *fpixel = new long[maxdim];
118      for(int i=0;i<maxdim;i++) fpixel[i]=1;
119      long *lpixel = new long[maxdim];
120      for(int i=0;i<maxdim;i++) lpixel[i]=this->itsCube->getDimArray()[i];
121      long *inc = new long[maxdim];
122      for(int i=0;i<maxdim;i++) inc[i]=1;
123      long *dimAxesNew = new long[maxdim];
124      for(int i=0;i<maxdim;i++) dimAxesNew[i]=this->itsCube->getDimArray()[i];
125      int anynul;
126      int colnum = 0;  // want the first dataset in the FITS file
127      int status = 0;
128
129      if(fits_read_subset_flt(this->itsFptr, colnum, 3, dimAxesNew, fpixel, lpixel, inc,
130                              this->itsCube->pars().getBlankPixVal(), this->itsArray, &anynul, &status)){
131        DUCHAMPERROR("Recon Reader", "There was an error reading in the data array:");
132        fits_report_error(stderr, status);
133        result = FAILURE;
134      }
135      else{
136        this->itsCube->setReconFlag(true);
137
138        std::string header("BUNIT");
139        char *unit = new char[FLEN_VALUE];
140        char *comment = new char[80];
141        std::string fluxunits;
142        fits_read_key(this->itsFptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
143        if (status){
144          DUCHAMPWARN("Cube Reader","Error reading BUNIT keyword: ");
145          fits_report_error(stderr, status);
146          return FAILURE;
147        }
148        else{
149          wcsutrn(0,unit);
150          fluxunits = unit;
151        }
152        this->itsCube->convertFluxUnits(fluxunits,this->itsCube->pars().getNewFluxUnits(),RECON);
153      }
154
155    }
156    return result;
157  }
158
159  OUTCOME ReadExisting::read()
160  {
161    OUTCOME result = this->checkPars();
162    if(result==SUCCESS) result=this->checkFile();
163    if(result==SUCCESS) result=this->openFile();
164    if(result==SUCCESS) result=this->checkDim();
165    if(result==SUCCESS) result=this->checkHeaders();
166    if(result==SUCCESS) result=this->readFromFile();
167    if(result==SUCCESS) result=this->closeFile();
168    return result;
169  }
170
171  OUTCOME ReadExisting::closeFile()
172  {
173    OUTCOME result=SUCCESS;
174    int status = 0;
175    if(this->itsFptr==0){
176      fits_close_file(this->itsFptr, &status);
177      if (status){
178        DUCHAMPWARN("Reading Cube", "Error closing file " << this->itsFilename);
179        fits_report_error(stderr, status);
180        result=FAILURE;
181      }
182    }
183    return result;
184  }
185
186}
Note: See TracBrowser for help on using the repository browser.