source: trunk/src/FitsIO/ReadExisting.cc @ 1123

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

Moving the code that reads from and writes to FITS files containing reconstructed, momentmap, mask etc arrays to the FitsIO directory, away from Cubes. Updating all include statements as well.

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