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 | // Write the parameters to screen. |
---|
83 | std::cout << cube->pars() << "\n"; |
---|
84 | |
---|
85 | if(cube->pars().getFlagUsePrevious()){ |
---|
86 | std::cout << "Reading detections from existing log file... \n"; |
---|
87 | if(cube->getExistingDetections() == FAILURE){ |
---|
88 | DUCHAMPTHROW("Duchamp", "Could not read detections from log file"); |
---|
89 | return FAILURE; |
---|
90 | } |
---|
91 | else std::cout << "Done.\n"; |
---|
92 | } |
---|
93 | else { |
---|
94 | |
---|
95 | if(cube->pars().getFlagLog()){ |
---|
96 | // Prepare the log file. |
---|
97 | cube->prepareLogFile(argc, argv); |
---|
98 | } |
---|
99 | |
---|
100 | //if(cube->pars().getFlagBlankPix()){ |
---|
101 | if(cube->pars().getFlagTrim()){ |
---|
102 | // Trim any blank pixels from the edges, and report the new size |
---|
103 | // of the cube |
---|
104 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
105 | cube->trimCube(); |
---|
106 | std::cout<<"Done."<<std::endl; |
---|
107 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
108 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
109 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
110 | std::cout << std::endl; |
---|
111 | } |
---|
112 | |
---|
113 | if(cube->pars().getFlagBaseline()){ |
---|
114 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
115 | cube->removeBaseline(); |
---|
116 | std::cout<<" Done. \n"; |
---|
117 | } |
---|
118 | |
---|
119 | if(cube->pars().getFlagNegative()){ |
---|
120 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
121 | cube->invert(); |
---|
122 | std::cout<<" Done.\n"; |
---|
123 | } |
---|
124 | |
---|
125 | cube->Search(); |
---|
126 | std::cout << "Done. Intermediate list has " << cube->getNumObj(); |
---|
127 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
128 | else std::cout << " objects.\n"; |
---|
129 | |
---|
130 | if(cube->getNumObj() > 0){ |
---|
131 | if(cube->pars().getFlagGrowth()) |
---|
132 | std::cout<<"Merging, Growing and Rejecting... "<<std::flush; |
---|
133 | else |
---|
134 | std::cout<<"Merging and Rejecting... "<<std::flush; |
---|
135 | cube->ObjectMerger(); |
---|
136 | std::cout<<"Done. "<<std::endl; |
---|
137 | } |
---|
138 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
139 | |
---|
140 | if(cube->pars().getFlagNegative()){ |
---|
141 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
142 | cube->reInvert(); |
---|
143 | std::cout<<" Done."<<std::endl; |
---|
144 | } |
---|
145 | |
---|
146 | if(cube->pars().getFlagBaseline()){ |
---|
147 | std::cout<<"Replacing the baselines... "<<std::flush; |
---|
148 | cube->replaceBaseline(); |
---|
149 | std::cout<<"Done."<<std::endl; |
---|
150 | } |
---|
151 | |
---|
152 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
153 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
154 | cube->unTrimCube(); |
---|
155 | std::cout<<"Done."<<std::endl; |
---|
156 | } |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | if(cube->getNumObj()>0){ |
---|
161 | |
---|
162 | std::cout << "Calculating final parameters and setting flags... "<<std::flush; |
---|
163 | cube->calcObjectWCSparams(); |
---|
164 | |
---|
165 | cube->setObjectFlags(); |
---|
166 | |
---|
167 | cube->sortDetections(); |
---|
168 | |
---|
169 | std::cout << "Done." << std::endl; |
---|
170 | |
---|
171 | } |
---|
172 | |
---|
173 | cube->outputCatalogue(); |
---|
174 | |
---|
175 | if(USE_PGPLOT){ |
---|
176 | std::cout<<"Creating the maps... "<<std::flush; |
---|
177 | std::vector<std::string> devices; |
---|
178 | if(cube->nondegDim()>1){ |
---|
179 | if(cube->pars().getFlagXOutput()) devices.push_back("/xs"); |
---|
180 | if(cube->pars().getFlagMaps()) |
---|
181 | devices.push_back(cube->pars().getMomentMap()+"/vcps"); |
---|
182 | cube->plotMomentMap(devices); |
---|
183 | } |
---|
184 | if(cube->pars().getFlagMaps()){ |
---|
185 | if(cube->nondegDim()==1) cube->plotDetectionMap("/xs"); |
---|
186 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
187 | } |
---|
188 | std::cout << "Done.\n"; |
---|
189 | |
---|
190 | if(cube->getNumObj()>0 && cube->pars().getFlagPlotSpectra()){ |
---|
191 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
192 | cube->outputSpectra(); |
---|
193 | std::cout << "Done.\n"; |
---|
194 | } |
---|
195 | } |
---|
196 | else{ |
---|
197 | std::cout << "PGPLOT has not been enabled, so no graphical output.\n"; |
---|
198 | } |
---|
199 | |
---|
200 | if(!cube->pars().getFlagUsePrevious()){ |
---|
201 | |
---|
202 | if(cube->pars().getFlagATrous()&& |
---|
203 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
204 | std::cout << "Saving reconstructed cube to " |
---|
205 | << cube->pars().outputReconFile() << "... "<<std::flush; |
---|
206 | if(cube->saveReconstructedCube() == FAILURE) |
---|
207 | std::cout << "Failed!\n"; |
---|
208 | else |
---|
209 | std::cout << "done.\n"; |
---|
210 | } |
---|
211 | if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){ |
---|
212 | std::cout << "Saving smoothed cube to " |
---|
213 | << cube->pars().outputSmoothFile() << "... " <<std::flush; |
---|
214 | if(cube->saveSmoothedCube() == FAILURE) |
---|
215 | std::cout << "Failed!\n"; |
---|
216 | else |
---|
217 | std::cout << "done.\n"; |
---|
218 | } |
---|
219 | if(cube->pars().getFlagOutputMomentMap()){ |
---|
220 | std::cout << "Saving 0th moment map image to " |
---|
221 | << cube->pars().outputMomentMapFile() << "... " <<std::flush; |
---|
222 | if(cube->saveMomentMapImage() == FAILURE) |
---|
223 | std::cout << "Failed!\n"; |
---|
224 | else |
---|
225 | std::cout << "done.\n"; |
---|
226 | } |
---|
227 | |
---|
228 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
229 | cube->logSummary(); |
---|
230 | } |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | if(cube->pars().getFlagOutputMask()){ |
---|
235 | std::cout << "Saving mask cube to " |
---|
236 | << cube->pars().outputMaskFile() << "... " <<std::flush; |
---|
237 | if(cube->saveMaskCube() == FAILURE) |
---|
238 | std::cout << "Failed!\n"; |
---|
239 | else |
---|
240 | std::cout << "done.\n"; |
---|
241 | } |
---|
242 | |
---|
243 | if(cube->pars().getFlagVOT()){ |
---|
244 | cube->outputDetectionsVOTable(); |
---|
245 | } |
---|
246 | |
---|
247 | // write annotation files, if required. |
---|
248 | cube->outputAnnotations(); |
---|
249 | |
---|
250 | if(cube->pars().getFlagTextSpectra()){ |
---|
251 | if(cube->pars().isVerbose()) std::cout << "Saving spectra to text file ... "; |
---|
252 | cube->writeSpectralData(); |
---|
253 | if(cube->pars().isVerbose()) std::cout << "done.\n"; |
---|
254 | } |
---|
255 | |
---|
256 | if(!cube->pars().getFlagUsePrevious() && cube->pars().getFlagLog()){ |
---|
257 | // Open the logfile and write the time on the first line |
---|
258 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
259 | logfile << "Duchamp completed: "; |
---|
260 | time_t now = time(NULL); |
---|
261 | logfile << asctime( localtime(&now) ); |
---|
262 | logfile.close(); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | delete cube; |
---|
267 | |
---|
268 | } catch (const DuchampError &err) { |
---|
269 | std::cout << "\nDuchamp failed with the following error:\n" << err.what() << "\n"; |
---|
270 | exit(1); |
---|
271 | } |
---|
272 | |
---|
273 | return SUCCESS; |
---|
274 | |
---|
275 | } |
---|
276 | |
---|