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