1 | // ----------------------------------------------------------------------- |
---|
2 | // readRecon.cc: Read in an existing wavelet-reconstructed FITS file. |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <sstream> |
---|
30 | #include <string> |
---|
31 | #include <wcslib/wcs.h> |
---|
32 | #include <wcslib/wcshdr.h> |
---|
33 | #include <wcslib/wcsunits.h> |
---|
34 | #define WCSLIB_GETWCSTAB // define this so that we don't try to redefine wtbarr |
---|
35 | // (this is a problem when using gcc v.4+ |
---|
36 | #include <fitsio.h> |
---|
37 | #include <duchamp/duchamp.hh> |
---|
38 | #include <duchamp/Cubes/cubes.hh> |
---|
39 | |
---|
40 | namespace duchamp |
---|
41 | { |
---|
42 | |
---|
43 | OUTCOME Cube::readReconCube() |
---|
44 | { |
---|
45 | /// @details |
---|
46 | /// A way to read in a previously reconstructed array, corresponding |
---|
47 | /// to the requested parameters, or in the filename given by reconFile. |
---|
48 | /// Performs various tests to make sure there are no clashes between |
---|
49 | /// the requested parameters and those stored in the header of the |
---|
50 | /// FITS file. Also test to make sure that the size (and subsection, |
---|
51 | /// if applicable) of the array is the same. |
---|
52 | |
---|
53 | int status = 0; |
---|
54 | |
---|
55 | if(!this->par.getFlagReconExists()){ |
---|
56 | DUCHAMPWARN("readReconCube", "reconExists flag is not set. Not reading anything in!"); |
---|
57 | return FAILURE; |
---|
58 | } |
---|
59 | else if(!this->par.getFlagATrous()){ |
---|
60 | DUCHAMPWARN("readReconCube","flagATrous is not set. Don't need to read in recon array!"); |
---|
61 | return FAILURE; |
---|
62 | } |
---|
63 | else { |
---|
64 | |
---|
65 | // Check to see whether the parameters reconFile and/or residFile are defined |
---|
66 | bool reconGood = false; |
---|
67 | int exists; |
---|
68 | if(this->par.getReconFile() != ""){ |
---|
69 | reconGood = true; |
---|
70 | fits_file_exists(this->par.getReconFile().c_str(),&exists,&status); |
---|
71 | if(exists<=0){ |
---|
72 | fits_report_error(stderr, status); |
---|
73 | DUCHAMPWARN("readReconCube", "Cannot find requested ReconFile. Trying with parameters. Bad reconFile was: "<<this->par.getReconFile()); |
---|
74 | reconGood = false; |
---|
75 | } |
---|
76 | } |
---|
77 | else{ |
---|
78 | DUCHAMPWARN("readReconCube", "ReconFile not specified. Working out name from parameters."); |
---|
79 | } |
---|
80 | |
---|
81 | if(!reconGood){ // if bad, need to look at parameters |
---|
82 | |
---|
83 | std::string reconFile = this->par.outputReconFile(); |
---|
84 | DUCHAMPWARN("readReconCube", "Trying file " << reconFile ); |
---|
85 | reconGood = true; |
---|
86 | fits_file_exists(reconFile.c_str(),&exists,&status); |
---|
87 | if(exists<=0){ |
---|
88 | fits_report_error(stderr, status); |
---|
89 | // DUCHAMPWARNING("readReconCube","ReconFile not present."); |
---|
90 | reconGood = false; |
---|
91 | } |
---|
92 | |
---|
93 | if(reconGood){ |
---|
94 | // were able to open this new file -- use this, so reset the |
---|
95 | // relevant parameter |
---|
96 | this->par.setReconFile(reconFile); |
---|
97 | } |
---|
98 | else { // if STILL bad, give error message and exit. |
---|
99 | DUCHAMPERROR("readReconCube","Cannot find reconstructed file."); |
---|
100 | return FAILURE; |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | // if we get to here, reconGood is true (ie. file is open); |
---|
106 | |
---|
107 | status=0; |
---|
108 | fitsfile *fptr; |
---|
109 | fits_open_file(&fptr,this->par.getReconFile().c_str(),READONLY,&status); |
---|
110 | short int maxdim=3; |
---|
111 | long *fpixel = new long[maxdim]; |
---|
112 | for(int i=0;i<maxdim;i++) fpixel[i]=1; |
---|
113 | long *dimAxesNew = new long[maxdim]; |
---|
114 | for(int i=0;i<maxdim;i++) dimAxesNew[i]=1; |
---|
115 | int bitpix,numAxesNew,anynul; |
---|
116 | |
---|
117 | status = 0; |
---|
118 | fits_get_img_param(fptr, maxdim, &bitpix, &numAxesNew, dimAxesNew, &status); |
---|
119 | if(status){ |
---|
120 | fits_report_error(stderr, status); |
---|
121 | return FAILURE; |
---|
122 | } |
---|
123 | |
---|
124 | if(numAxesNew != this->numDim){ |
---|
125 | DUCHAMPERROR("readReconCube", "Reconstructed cube has a different number of axes to original!" << " (" << numAxesNew << " cf. " << this->numDim << ")"); |
---|
126 | return FAILURE; |
---|
127 | } |
---|
128 | |
---|
129 | for(int i=0;i<numAxesNew;i++){ |
---|
130 | if(dimAxesNew[i]!=int(this->axisDim[i])){ |
---|
131 | DUCHAMPERROR("readReconCube", "Reconstructed cube has different axis dimensions to original! Axis #" << i+1 << " has size " << dimAxesNew[i] << " cf. " << this->axisDim[i] <<" in original."); |
---|
132 | return FAILURE; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | char *comment = new char[80]; |
---|
137 | |
---|
138 | if(this->par.getFlagSubsection()){ |
---|
139 | char *subsection = new char[80]; |
---|
140 | status = 0; |
---|
141 | fits_read_key(fptr, TSTRING, (char *)keyword_subsection.c_str(), |
---|
142 | subsection, comment, &status); |
---|
143 | if(status){ |
---|
144 | DUCHAMPERROR("readReconCube", "subsection keyword not present in reconFile."); |
---|
145 | return FAILURE; |
---|
146 | } |
---|
147 | else{ |
---|
148 | if(this->par.getSubsection() != subsection){ |
---|
149 | DUCHAMPERROR("readReconCube", "subsection keyword in reconFile (" << subsection << ") does not match that requested (" << this->par.getSubsection() << ")."); |
---|
150 | return FAILURE; |
---|
151 | } |
---|
152 | } |
---|
153 | delete subsection; |
---|
154 | } |
---|
155 | |
---|
156 | unsigned int scaleMin; |
---|
157 | int filterCode,reconDim; |
---|
158 | float snrRecon; |
---|
159 | status = 0; |
---|
160 | fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(), |
---|
161 | &reconDim, comment, &status); |
---|
162 | if(reconDim != this->par.getReconDim()){ |
---|
163 | DUCHAMPERROR("readReconCube", "reconDim keyword in reconFile (" << reconDim << ") does not match that requested (" << this->par.getReconDim() << ")."); |
---|
164 | return FAILURE; |
---|
165 | } |
---|
166 | status = 0; |
---|
167 | fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(), |
---|
168 | &filterCode, comment, &status); |
---|
169 | if(filterCode != this->par.getFilterCode()){ |
---|
170 | DUCHAMPERROR("readReconCube", "filterCode keyword in reconFile (" << filterCode << ") does not match that requested (" << this->par.getFilterCode() << ")."); |
---|
171 | return FAILURE; |
---|
172 | } |
---|
173 | status = 0; |
---|
174 | fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), |
---|
175 | &snrRecon, comment, &status); |
---|
176 | if(snrRecon != this->par.getAtrousCut()){ |
---|
177 | DUCHAMPERROR("readReconCube", "snrRecon keyword in reconFile (" << snrRecon << ") does not match that requested (" << this->par.getAtrousCut() << ")."); |
---|
178 | return FAILURE; |
---|
179 | } |
---|
180 | status = 0; |
---|
181 | fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), |
---|
182 | &scaleMin, comment, &status); |
---|
183 | if(scaleMin != this->par.getMinScale()){ |
---|
184 | DUCHAMPERROR("readReconCube", "scaleMin keyword in reconFile (" << scaleMin << ") does not match that requested (" << this->par.getMinScale() << ")."); |
---|
185 | return FAILURE; |
---|
186 | } |
---|
187 | |
---|
188 | // Read the BUNIT keyword, and translate to standard unit format if needs be |
---|
189 | std::string header("BUNIT"); |
---|
190 | char *unit = new char[FLEN_VALUE]; |
---|
191 | std::string fluxunits; |
---|
192 | fits_read_key(fptr, TSTRING, (char *)header.c_str(), unit, comment, &status); |
---|
193 | if (status){ |
---|
194 | DUCHAMPWARN("Cube Reader","Error reading BUNIT keyword: "); |
---|
195 | fits_report_error(stderr, status); |
---|
196 | return FAILURE; |
---|
197 | } |
---|
198 | else{ |
---|
199 | wcsutrn(0,unit); |
---|
200 | fluxunits = unit; |
---|
201 | } |
---|
202 | |
---|
203 | // |
---|
204 | // If we get to here, the reconFile exists and matches the atrous |
---|
205 | // parameters the user has requested. |
---|
206 | |
---|
207 | status = 0; |
---|
208 | fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, |
---|
209 | this->recon, &anynul, &status); |
---|
210 | |
---|
211 | status = 0; |
---|
212 | fits_close_file(fptr, &status); |
---|
213 | if (status){ |
---|
214 | DUCHAMPWARN("readReconCube", "Error closing file: "); |
---|
215 | fits_report_error(stderr, status); |
---|
216 | } |
---|
217 | |
---|
218 | // We don't want to write out the recon or resid files at the end |
---|
219 | this->par.setFlagOutputRecon(false); |
---|
220 | this->par.setFlagOutputResid(false); |
---|
221 | |
---|
222 | this->convertFluxUnits(fluxunits,this->par.getNewFluxUnits(),RECON); |
---|
223 | |
---|
224 | // The reconstruction is done -- set the appropriate flag |
---|
225 | this->reconExists = true; |
---|
226 | |
---|
227 | return SUCCESS; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|