1 | // ----------------------------------------------------------------------- |
---|
2 | // mainDuchamp.cc: Main program for Duchamp source finder. |
---|
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 <fstream> |
---|
30 | #include <string> |
---|
31 | #include <math.h> |
---|
32 | #include <unistd.h> |
---|
33 | #include <time.h> |
---|
34 | |
---|
35 | #include <duchamp/duchamp.hh> |
---|
36 | #include <duchamp/param.hh> |
---|
37 | #include <duchamp/pgheader.hh> |
---|
38 | #include <duchamp/Detection/detection.hh> |
---|
39 | #include <duchamp/Cubes/cubes.hh> |
---|
40 | #include <duchamp/Utils/utils.hh> |
---|
41 | #include <duchamp/ATrous/atrous.hh> |
---|
42 | |
---|
43 | using namespace duchamp; |
---|
44 | |
---|
45 | int main(int argc, char * argv[]) |
---|
46 | { |
---|
47 | |
---|
48 | try { |
---|
49 | |
---|
50 | |
---|
51 | std::string paramFile,fitsfile; |
---|
52 | Cube *cube = new Cube; |
---|
53 | |
---|
54 | if(cube->getopts(argc,argv)==FAILURE) return FAILURE; |
---|
55 | |
---|
56 | if(cube->pars().getImageFile().empty()){ |
---|
57 | DUCHAMPTHROW("Duchamp", "No input image has been given!. Use the imageFile parameter in the parameter file to specify the FITS file."); |
---|
58 | return FAILURE; |
---|
59 | } |
---|
60 | |
---|
61 | if(cube->pars().getFlagSubsection() || cube->pars().getFlagStatSec()){ |
---|
62 | // make sure the subsection is OK. |
---|
63 | if(cube->pars().verifySubsection() == FAILURE){ |
---|
64 | DUCHAMPTHROW("Duchamp", "Unable to use the subsection provided."); |
---|
65 | return FAILURE; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | std::cout << "Opening image: " |
---|
70 | << cube->pars().getFullImageFile() << std::endl; |
---|
71 | |
---|
72 | if( cube->getCube() == FAILURE){ |
---|
73 | DUCHAMPTHROW("Duchamp", "Unable to open image file "<< cube->pars().getFullImageFile()); |
---|
74 | return FAILURE; |
---|
75 | } |
---|
76 | else std::cout << "Opened successfully." << std::endl; |
---|
77 | |
---|
78 | // Read in any saved arrays that are in FITS files on disk. |
---|
79 | if(!cube->pars().getFlagUsePrevious()) |
---|
80 | cube->readSavedArrays(); |
---|
81 | |
---|
82 | // special case for 2D images -- ignore the minChannels requirement |
---|
83 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); |
---|
84 | |
---|
85 | // Write the parameters to screen. |
---|
86 | std::cout << cube->pars(); |
---|
87 | |
---|
88 | if(cube->pars().getFlagUsePrevious()){ |
---|
89 | std::cout << "Reading detections from existing log file... \n"; |
---|
90 | if(cube->getExistingDetections() == FAILURE){ |
---|
91 | DUCHAMPTHROW("Duchamp", "Could not read detections from log file"); |
---|
92 | return FAILURE; |
---|
93 | } |
---|
94 | else std::cout << "Done.\n"; |
---|
95 | } |
---|
96 | else { |
---|
97 | |
---|
98 | if(cube->pars().getFlagLog()){ |
---|
99 | // Prepare the log file. |
---|
100 | cube->prepareLogFile(argc, argv); |
---|
101 | } |
---|
102 | |
---|
103 | //if(cube->pars().getFlagBlankPix()){ |
---|
104 | if(cube->pars().getFlagTrim()){ |
---|
105 | // Trim any blank pixels from the edges, and report the new size |
---|
106 | // of the cube |
---|
107 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
108 | cube->trimCube(); |
---|
109 | std::cout<<"Done."<<std::endl; |
---|
110 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
111 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
112 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
113 | std::cout << std::endl; |
---|
114 | } |
---|
115 | |
---|
116 | if(cube->pars().getFlagBaseline()){ |
---|
117 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
118 | cube->removeBaseline(); |
---|
119 | std::cout<<" Done. \n"; |
---|
120 | } |
---|
121 | |
---|
122 | if(cube->pars().getFlagNegative()){ |
---|
123 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
124 | cube->invert(); |
---|
125 | std::cout<<" Done.\n"; |
---|
126 | } |
---|
127 | |
---|
128 | cube->Search(); |
---|
129 | std::cout << "Done. Intermediate list has " << cube->getNumObj(); |
---|
130 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
131 | else std::cout << " objects.\n"; |
---|
132 | |
---|
133 | if(cube->getNumObj() > 0){ |
---|
134 | if(cube->pars().getFlagGrowth()) |
---|
135 | std::cout<<"Merging, Growing and Rejecting... "<<std::flush; |
---|
136 | else |
---|
137 | std::cout<<"Merging and Rejecting... "<<std::flush; |
---|
138 | cube->ObjectMerger(); |
---|
139 | std::cout<<"Done. "<<std::endl; |
---|
140 | } |
---|
141 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
142 | |
---|
143 | if(cube->pars().getFlagNegative()){ |
---|
144 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
145 | cube->reInvert(); |
---|
146 | std::cout<<" Done."<<std::endl; |
---|
147 | } |
---|
148 | |
---|
149 | if(cube->pars().getFlagBaseline()){ |
---|
150 | std::cout<<"Replacing the baselines... "<<std::flush; |
---|
151 | cube->replaceBaseline(); |
---|
152 | std::cout<<"Done."<<std::endl; |
---|
153 | } |
---|
154 | |
---|
155 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
156 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
157 | cube->unTrimCube(); |
---|
158 | std::cout<<"Done."<<std::endl; |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | cube->prepareOutputFile(); |
---|
164 | |
---|
165 | if(cube->getNumObj()>0){ |
---|
166 | |
---|
167 | std::cout << "Calculating final parameters and setting flags... "<<std::flush; |
---|
168 | cube->calcObjectWCSparams(); |
---|
169 | |
---|
170 | cube->setObjectFlags(); |
---|
171 | |
---|
172 | cube->sortDetections(); |
---|
173 | |
---|
174 | std::cout << "Done." << std::endl; |
---|
175 | |
---|
176 | } |
---|
177 | |
---|
178 | cube->outputDetectionList(); |
---|
179 | |
---|
180 | if(USE_PGPLOT){ |
---|
181 | std::cout<<"Creating the maps... "<<std::flush; |
---|
182 | std::vector<std::string> devices; |
---|
183 | if(cube->nondegDim()>1){ |
---|
184 | if(cube->pars().getFlagXOutput()) devices.push_back("/xs"); |
---|
185 | if(cube->pars().getFlagMaps()) |
---|
186 | devices.push_back(cube->pars().getMomentMap()+"/vcps"); |
---|
187 | cube->plotMomentMap(devices); |
---|
188 | } |
---|
189 | if(cube->pars().getFlagMaps()){ |
---|
190 | if(cube->nondegDim()==1) cube->plotDetectionMap("/xs"); |
---|
191 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
192 | } |
---|
193 | std::cout << "Done.\n"; |
---|
194 | |
---|
195 | if(cube->getNumObj()>0 && cube->pars().getFlagPlotSpectra()){ |
---|
196 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
197 | cube->outputSpectra(); |
---|
198 | std::cout << "Done.\n"; |
---|
199 | } |
---|
200 | } |
---|
201 | else{ |
---|
202 | std::cout << "PGPLOT has not been enabled, so no graphical output.\n"; |
---|
203 | } |
---|
204 | |
---|
205 | if(!cube->pars().getFlagUsePrevious()){ |
---|
206 | |
---|
207 | if(cube->pars().getFlagATrous()&& |
---|
208 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
209 | std::cout << "Saving reconstructed cube to " |
---|
210 | << cube->pars().outputReconFile() << "... "<<std::flush; |
---|
211 | if(cube->saveReconstructedCube() == FAILURE) |
---|
212 | std::cout << "Failed!\n"; |
---|
213 | else |
---|
214 | std::cout << "done.\n"; |
---|
215 | } |
---|
216 | if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){ |
---|
217 | std::cout << "Saving smoothed cube to " |
---|
218 | << cube->pars().outputSmoothFile() << "... " <<std::flush; |
---|
219 | if(cube->saveSmoothedCube() == FAILURE) |
---|
220 | std::cout << "Failed!\n"; |
---|
221 | else |
---|
222 | std::cout << "done.\n"; |
---|
223 | } |
---|
224 | if(cube->pars().getFlagOutputMomentMap()){ |
---|
225 | std::cout << "Saving 0th moment map image to " |
---|
226 | << cube->pars().outputMomentMapFile() << "... " <<std::flush; |
---|
227 | if(cube->saveMomentMapImage() == FAILURE) |
---|
228 | std::cout << "Failed!\n"; |
---|
229 | else |
---|
230 | std::cout << "done.\n"; |
---|
231 | } |
---|
232 | |
---|
233 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
234 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
235 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
236 | logfile << *cube; |
---|
237 | logfile.close(); |
---|
238 | } |
---|
239 | |
---|
240 | } |
---|
241 | |
---|
242 | if(cube->pars().getFlagOutputMask()){ |
---|
243 | std::cout << "Saving mask cube to " |
---|
244 | << cube->pars().outputMaskFile() << "... " <<std::flush; |
---|
245 | if(cube->saveMaskCube() == FAILURE) |
---|
246 | std::cout << "Failed!\n"; |
---|
247 | else |
---|
248 | std::cout << "done.\n"; |
---|
249 | } |
---|
250 | |
---|
251 | if(cube->pars().getFlagVOT()){ |
---|
252 | std::ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
253 | cube->outputDetectionsVOTable(votfile); |
---|
254 | votfile.close(); |
---|
255 | } |
---|
256 | |
---|
257 | if(cube->pars().getFlagKarma()){ |
---|
258 | std::ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
259 | cube->outputDetectionsKarma(karmafile); |
---|
260 | karmafile.close(); |
---|
261 | } |
---|
262 | |
---|
263 | if(cube->pars().getFlagTextSpectra()){ |
---|
264 | if(cube->pars().isVerbose()) std::cout << "Saving spectra to text file ... "; |
---|
265 | cube->writeSpectralData(); |
---|
266 | if(cube->pars().isVerbose()) std::cout << "done.\n"; |
---|
267 | } |
---|
268 | |
---|
269 | if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){ |
---|
270 | // Open the logfile and write the time on the first line |
---|
271 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
272 | logfile << "Duchamp completed: "; |
---|
273 | time_t now = time(NULL); |
---|
274 | logfile << asctime( localtime(&now) ); |
---|
275 | logfile.close(); |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | delete cube; |
---|
280 | |
---|
281 | } catch (const DuchampError &err) { |
---|
282 | std::cout << "\nDuchamp failed with the following error:\n" << err.what() << "\n"; |
---|
283 | exit(1); |
---|
284 | } |
---|
285 | |
---|
286 | return SUCCESS; |
---|
287 | |
---|
288 | } |
---|
289 | |
---|