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 | |
---|
12 | int Cube::readReconCube() |
---|
13 | { |
---|
14 | /** |
---|
15 | * A way to read in a previously reconstructed array, corresponding |
---|
16 | * to the requested parameters, or in the filename given by reconFile. |
---|
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.getFlagReconExists()){ |
---|
27 | duchampWarning("readReconCube", |
---|
28 | "reconExists flag is not set. Not reading anything in!\n"); |
---|
29 | return FAILURE; |
---|
30 | } |
---|
31 | else if(!this->par.getFlagATrous()){ |
---|
32 | duchampWarning("readReconCube", |
---|
33 | "flagATrous is not set. Don't need to read in recon array!\n"); |
---|
34 | return FAILURE; |
---|
35 | } |
---|
36 | else { |
---|
37 | |
---|
38 | // Check to see whether the parameters reconFile and/or residFile are defined |
---|
39 | bool reconGood = false; |
---|
40 | int exists; |
---|
41 | std::stringstream errmsg; |
---|
42 | if(this->par.getReconFile() != ""){ |
---|
43 | reconGood = true; |
---|
44 | fits_file_exists(this->par.getReconFile().c_str(),&exists,&status); |
---|
45 | if(exists<=0){ |
---|
46 | fits_report_error(stderr, status); |
---|
47 | errmsg<< "Cannot find requested ReconFile. Trying with parameters.\n" |
---|
48 | << "Bad reconFile was: "<<this->par.getReconFile() << std::endl; |
---|
49 | duchampWarning("readReconCube", errmsg.str()); |
---|
50 | reconGood = false; |
---|
51 | } |
---|
52 | } |
---|
53 | else{ |
---|
54 | errmsg<< "ReconFile not specified. Working out name from parameters.\n"; |
---|
55 | } |
---|
56 | |
---|
57 | if(!reconGood){ // if bad, need to look at parameters |
---|
58 | |
---|
59 | std::string reconFile = this->par.outputReconFile(); |
---|
60 | errmsg << "Trying file " << reconFile << std::endl; |
---|
61 | duchampWarning("readReconCube", errmsg.str() ); |
---|
62 | reconGood = true; |
---|
63 | fits_file_exists(reconFile.c_str(),&exists,&status); |
---|
64 | if(exists<=0){ |
---|
65 | fits_report_error(stderr, status); |
---|
66 | duchampWarning("readReconCube","ReconFile not present.\n"); |
---|
67 | reconGood = false; |
---|
68 | } |
---|
69 | |
---|
70 | if(reconGood){ |
---|
71 | // were able to open this new file -- use this, so reset the |
---|
72 | // relevant parameter |
---|
73 | this->par.setReconFile(reconFile); |
---|
74 | } |
---|
75 | else { // if STILL bad, give error message and exit. |
---|
76 | duchampError("readReconCube","Cannot find reconstructed file.\n"); |
---|
77 | return FAILURE; |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | // if we get to here, reconGood is true (ie. file is open); |
---|
83 | |
---|
84 | status=0; |
---|
85 | fitsfile *fptr; |
---|
86 | fits_open_file(&fptr,this->par.getReconFile().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 << "Reconstructed cube has a different number of axes to original!" |
---|
105 | << " (" << numAxesNew << " cf. " << this->numDim << ")\n"; |
---|
106 | duchampError("readReconCube", 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 << "Reconstructed 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("readReconCube", 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("readReconCube", |
---|
130 | "subsection keyword not present in reconFile.\n"); |
---|
131 | return FAILURE; |
---|
132 | } |
---|
133 | else{ |
---|
134 | if(this->par.getSubsection() != subsection){ |
---|
135 | std::stringstream errmsg; |
---|
136 | errmsg << "subsection keyword in reconFile (" << subsection |
---|
137 | << ") does not match that requested (" |
---|
138 | << this->par.getSubsection() << ").\n"; |
---|
139 | duchampError("readReconCube", errmsg.str()); |
---|
140 | return FAILURE; |
---|
141 | } |
---|
142 | } |
---|
143 | delete subsection; |
---|
144 | } |
---|
145 | |
---|
146 | int scaleMin,filterCode,reconDim; |
---|
147 | float snrRecon; |
---|
148 | status = 0; |
---|
149 | fits_read_key(fptr, TINT, (char *)keyword_reconDim.c_str(), |
---|
150 | &reconDim, comment, &status); |
---|
151 | if(reconDim != this->par.getReconDim()){ |
---|
152 | std::stringstream errmsg; |
---|
153 | errmsg << "reconDim keyword in reconFile (" << reconDim |
---|
154 | << ") does not match that requested (" |
---|
155 | << this->par.getReconDim() << ").\n"; |
---|
156 | duchampError("readReconCube", errmsg.str()); |
---|
157 | return FAILURE; |
---|
158 | } |
---|
159 | status = 0; |
---|
160 | fits_read_key(fptr, TINT, (char *)keyword_filterCode.c_str(), |
---|
161 | &filterCode, comment, &status); |
---|
162 | if(filterCode != this->par.getFilterCode()){ |
---|
163 | std::stringstream errmsg; |
---|
164 | errmsg << "filterCode keyword in reconFile (" << filterCode |
---|
165 | << ") does not match that requested (" |
---|
166 | << this->par.getFilterCode() << ").\n"; |
---|
167 | duchampError("readReconCube", errmsg.str()); |
---|
168 | return FAILURE; |
---|
169 | } |
---|
170 | status = 0; |
---|
171 | fits_read_key(fptr, TFLOAT, (char *)keyword_snrRecon.c_str(), |
---|
172 | &snrRecon, comment, &status); |
---|
173 | if(snrRecon != this->par.getAtrousCut()){ |
---|
174 | std::stringstream errmsg; |
---|
175 | errmsg << "snrRecon keyword in reconFile (" << snrRecon |
---|
176 | << ") does not match that requested (" |
---|
177 | << this->par.getAtrousCut() << ").\n"; |
---|
178 | duchampError("readReconCube", errmsg.str()); |
---|
179 | return FAILURE; |
---|
180 | } |
---|
181 | status = 0; |
---|
182 | fits_read_key(fptr, TINT, (char *)keyword_scaleMin.c_str(), |
---|
183 | &scaleMin, comment, &status); |
---|
184 | if(scaleMin != this->par.getMinScale()){ |
---|
185 | std::stringstream errmsg; |
---|
186 | errmsg << "scaleMin keyword in reconFile (" << scaleMin |
---|
187 | << ") does not match that requested (" |
---|
188 | << this->par.getMinScale() << ").\n"; |
---|
189 | duchampError("readReconCube", errmsg.str()); |
---|
190 | return FAILURE; |
---|
191 | } |
---|
192 | |
---|
193 | // |
---|
194 | // If we get to here, the reconFile exists and matches the atrous |
---|
195 | // parameters the user has requested. |
---|
196 | |
---|
197 | status = 0; |
---|
198 | fits_read_pix(fptr, TFLOAT, fpixel, this->numPixels, NULL, |
---|
199 | this->recon, &anynul, &status); |
---|
200 | |
---|
201 | status = 0; |
---|
202 | fits_close_file(fptr, &status); |
---|
203 | if (status){ |
---|
204 | duchampWarning("readReconCube", "Error closing file: "); |
---|
205 | fits_report_error(stderr, status); |
---|
206 | } |
---|
207 | |
---|
208 | // We don't want to write out the recon or resid files at the end |
---|
209 | this->par.setFlagOutputRecon(false); |
---|
210 | this->par.setFlagOutputResid(false); |
---|
211 | |
---|
212 | // The reconstruction is done -- set the appropriate flag |
---|
213 | this->reconExists = true; |
---|
214 | |
---|
215 | return SUCCESS; |
---|
216 | } |
---|
217 | } |
---|