source: trunk/src/Cubes/ReadExisting.cc @ 1095

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

Classes to address #166, that solve (I think) #164 in the process. Need a bit of testing

File size: 5.2 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,anynul;
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      return result;
102
103    }
104  }
105
106  OUTCOME ReadExisting::readFromFile()
107  {
108    OUTCOME result=SUCCESS;
109    if(this->itsFptr==0){
110      DUCHAMPERROR("Reading Cube","FITS file not open");
111      result = FAILURE;
112    }
113    else{
114
115      short int maxdim=3;
116      long *fpixel = new long[maxdim];
117      for(int i=0;i<maxdim;i++) fpixel[i]=1;
118      long *lpixel = new long[maxdim];
119      for(int i=0;i<maxdim;i++) lpixel[i]=this->itsCube->getDimArray()[i];
120      long *inc = new long[maxdim];
121      for(int i=0;i<maxdim;i++) inc[i]=1;
122      long *dimAxesNew = new long[maxdim];
123      for(int i=0;i<maxdim;i++) dimAxesNew[i]=this->itsCube->getDimArray()[i];
124      int bitpix,numAxesNew,anynul;
125      int colnum = 0;  // want the first dataset in the FITS file
126      int status = 0;
127
128      if(fits_read_subset_flt(this->itsFptr, colnum, 3, dimAxesNew, fpixel, lpixel, inc,
129                              this->itsCube->pars().getBlankPixVal(), this->itsArray, &anynul, &status)){
130        DUCHAMPERROR("Recon Reader", "There was an error reading in the data array:");
131        fits_report_error(stderr, status);
132        result = FAILURE;
133      }
134      else{
135        this->itsCube->setReconFlag(true);
136
137        std::string header("BUNIT");
138        char *unit = new char[FLEN_VALUE];
139        char *comment = new char[80];
140        std::string fluxunits;
141        fits_read_key(this->itsFptr, TSTRING, (char *)header.c_str(), unit, comment, &status);
142        if (status){
143          DUCHAMPWARN("Cube Reader","Error reading BUNIT keyword: ");
144          fits_report_error(stderr, status);
145          return FAILURE;
146        }
147        else{
148          wcsutrn(0,unit);
149          fluxunits = unit;
150        }
151        this->itsCube->convertFluxUnits(fluxunits,this->itsCube->pars().getNewFluxUnits(),RECON);
152      }
153
154    }
155    return result;
156  }
157
158  OUTCOME ReadExisting::read()
159  {
160    OUTCOME result = this->checkPars();
161    if(result==SUCCESS) result=this->checkFile();
162    if(result==SUCCESS) result=this->openFile();
163    if(result==SUCCESS) result=this->checkDim();
164    if(result==SUCCESS) result=this->checkHeaders();
165    if(result==SUCCESS) result=this->readFromFile();
166    if(result==SUCCESS) result=this->closeFile();
167    return result;
168  }
169
170  OUTCOME ReadExisting::closeFile()
171  {
172    OUTCOME result=SUCCESS;
173    int status = 0;
174    if(this->itsFptr==0){
175      fits_close_file(this->itsFptr, &status);
176      if (status){
177        DUCHAMPWARN("Reading Cube", "Error closing file " << this->itsFilename);
178        fits_report_error(stderr, status);
179        result=FAILURE;
180      }
181    }
182    return result;
183  }
184
185}
Note: See TracBrowser for help on using the repository browser.