1 | #include <iostream> |
---|
2 | #include <fstream> |
---|
3 | #include <string> |
---|
4 | #include <cpgplot.h> |
---|
5 | #include <math.h> |
---|
6 | #include <unistd.h> |
---|
7 | #include <time.h> |
---|
8 | |
---|
9 | #include <duchamp.hh> |
---|
10 | #include <param.hh> |
---|
11 | #include <Detection/detection.hh> |
---|
12 | #include <Cubes/cubes.hh> |
---|
13 | #include <Utils/utils.hh> |
---|
14 | #include <ATrous/atrous.hh> |
---|
15 | |
---|
16 | int main(int argc, char * argv[]) |
---|
17 | { |
---|
18 | |
---|
19 | std::string paramFile,fitsfile; |
---|
20 | Cube *cube = new Cube; |
---|
21 | |
---|
22 | if(cube->getopts(argc,argv)==FAILURE) return FAILURE; |
---|
23 | |
---|
24 | if(cube->pars().getImageFile().empty()){ |
---|
25 | std::stringstream errmsg; |
---|
26 | errmsg << "No input image has been given!\n" |
---|
27 | << "Use the imageFile parameter in " |
---|
28 | << paramFile << " to specify the FITS file.\nExiting...\n"; |
---|
29 | duchampError("Duchamp", errmsg.str()); |
---|
30 | return FAILURE; |
---|
31 | } |
---|
32 | |
---|
33 | if(cube->pars().getFlagSubsection() || cube->pars().getFlagStatSec()){ |
---|
34 | // make sure the subsection is OK. |
---|
35 | if(cube->pars().verifySubsection() == FAILURE){ |
---|
36 | duchampError("Duchamp", |
---|
37 | "Unable to use the subsection provided.\nExiting...\n"); |
---|
38 | return FAILURE; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | std::cout << "Opening image: " |
---|
43 | << cube->pars().getFullImageFile() << std::endl; |
---|
44 | |
---|
45 | if( cube->getCube() == FAILURE){ |
---|
46 | std::stringstream errmsg; |
---|
47 | errmsg << "Unable to open image file " |
---|
48 | << cube->pars().getFullImageFile() |
---|
49 | << "\nExiting...\n"; |
---|
50 | duchampError("Duchamp", errmsg.str()); |
---|
51 | return FAILURE; |
---|
52 | } |
---|
53 | else std::cout << "Opened successfully." << std::endl; |
---|
54 | |
---|
55 | // Read in any saved arrays that are in FITS files on disk. |
---|
56 | cube->readSavedArrays(); |
---|
57 | |
---|
58 | // special case for 2D images -- ignore the minChannels requirement |
---|
59 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); |
---|
60 | |
---|
61 | // Write the parameters to screen. |
---|
62 | std::cout << cube->pars(); |
---|
63 | |
---|
64 | if(cube->pars().getFlagLog()){ |
---|
65 | // Open the logfile and write the time on the first line |
---|
66 | std::ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
67 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
68 | time_t now = time(NULL); |
---|
69 | logfile << asctime( localtime(&now) ); |
---|
70 | // Write out the command-line statement |
---|
71 | logfile << "Executing statement : "; |
---|
72 | for(int i=0;i<argc;i++) logfile << argv[i] << " "; |
---|
73 | logfile << std::endl; |
---|
74 | logfile << cube->pars(); |
---|
75 | logfile.close(); |
---|
76 | } |
---|
77 | |
---|
78 | if(cube->pars().getFlagBlankPix()){ |
---|
79 | // Trim any blank pixels from the edges, |
---|
80 | // and report the new size of the cube |
---|
81 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
82 | cube->trimCube(); |
---|
83 | std::cout<<"Done."<<std::endl; |
---|
84 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
85 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
86 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
87 | std::cout << std::endl; |
---|
88 | } |
---|
89 | |
---|
90 | if(cube->pars().getFlagBaseline()){ |
---|
91 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
92 | cube->removeBaseline(); |
---|
93 | std::cout<<" Done. "<<std::endl; |
---|
94 | } |
---|
95 | |
---|
96 | if(cube->pars().getFlagNegative()){ |
---|
97 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
98 | cube->invert(); |
---|
99 | std::cout<<" Done."<<std::endl; |
---|
100 | } |
---|
101 | |
---|
102 | if(cube->pars().getFlagATrous()){ |
---|
103 | std::cout<<"Commencing search in reconstructed cube..."<<std::endl; |
---|
104 | cube->ReconSearch(); |
---|
105 | } |
---|
106 | else if(cube->pars().getFlagSmooth()){ |
---|
107 | std::cout<<"Commencing search in hanning smoothed cube..."<<std::endl; |
---|
108 | cube->SmoothSearch(); |
---|
109 | } |
---|
110 | else{ |
---|
111 | std::cout<<"Commencing search in cube..."<<std::endl; |
---|
112 | cube->CubicSearch(); |
---|
113 | } |
---|
114 | std::cout << "Done. Intermediate list has " << cube->getNumObj(); |
---|
115 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
116 | else std::cout << " objects.\n"; |
---|
117 | |
---|
118 | if(cube->getNumObj() > 1){ |
---|
119 | std::cout<<"Merging intermediate detections... "<<std::flush; |
---|
120 | cube->ObjectMerger(); |
---|
121 | std::cout<<"Done. "<<std::endl; |
---|
122 | } |
---|
123 | std::cout<<"Final object count = "<<cube->getNumObj()<<std::endl; |
---|
124 | |
---|
125 | if(cube->pars().getFlagNegative()){ |
---|
126 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
127 | cube->reInvert(); |
---|
128 | std::cout<<" Done."<<std::endl; |
---|
129 | } |
---|
130 | |
---|
131 | if(cube->pars().getFlagBaseline()){ |
---|
132 | std::cout<<"Replacing the baselines... "<<std::flush; |
---|
133 | cube->replaceBaseline(); |
---|
134 | std::cout<<"Done."<<std::endl; |
---|
135 | } |
---|
136 | |
---|
137 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
138 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
139 | cube->unTrimCube(); |
---|
140 | std::cout<<"Done."<<std::endl; |
---|
141 | } |
---|
142 | |
---|
143 | if(cube->getNumObj()>0){ |
---|
144 | |
---|
145 | cube->calcObjectWCSparams(); |
---|
146 | |
---|
147 | cube->setObjectFlags(); |
---|
148 | |
---|
149 | cube->sortDetections(); |
---|
150 | |
---|
151 | cube->outputDetectionList(); |
---|
152 | } |
---|
153 | |
---|
154 | std::cout<<"Creating the maps... "<<std::flush; |
---|
155 | std::vector<std::string> devices; |
---|
156 | if(cube->pars().getFlagXOutput()) devices.push_back("/xs"); |
---|
157 | if(cube->pars().getFlagMaps()) |
---|
158 | devices.push_back(cube->pars().getMomentMap()+"/vcps"); |
---|
159 | cube->plotMomentMap(devices); |
---|
160 | if(cube->pars().getFlagMaps()) |
---|
161 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
162 | std::cout << "Done.\n"; |
---|
163 | |
---|
164 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
165 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
166 | cube->outputSpectra(); |
---|
167 | std::cout << "Done.\n"; |
---|
168 | } |
---|
169 | else if(cube->getDimZ()<=1) |
---|
170 | duchampWarning("Duchamp", |
---|
171 | "Not plotting any spectra : no third dimension.\n"); |
---|
172 | |
---|
173 | cpgend(); |
---|
174 | |
---|
175 | if(cube->pars().getFlagATrous()&& |
---|
176 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
177 | std::cout << "Saving reconstructed cube to " |
---|
178 | << cube->pars().outputReconFile() << "... "<<std::flush; |
---|
179 | cube->saveReconstructedCube(); |
---|
180 | std::cout << "done.\n"; |
---|
181 | } |
---|
182 | if(cube->pars().getFlagSmooth()&& cube->pars().getFlagOutputSmooth()){ |
---|
183 | std::cout << "Saving Hanning-smoothed cube to " |
---|
184 | << cube->pars().outputSmoothFile() << "... " <<std::flush; |
---|
185 | cube->saveSmoothedCube(); |
---|
186 | std::cout << "done.\n"; |
---|
187 | } |
---|
188 | |
---|
189 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
190 | std::ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
191 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
192 | logfile << *cube; |
---|
193 | logfile.close(); |
---|
194 | } |
---|
195 | |
---|
196 | if(cube->pars().getFlagVOT()){ |
---|
197 | std::ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
198 | cube->outputDetectionsVOTable(votfile); |
---|
199 | votfile.close(); |
---|
200 | } |
---|
201 | |
---|
202 | if(cube->pars().getFlagKarma()){ |
---|
203 | std::ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
204 | cube->outputDetectionsKarma(karmafile); |
---|
205 | karmafile.close(); |
---|
206 | } |
---|
207 | |
---|
208 | delete cube; |
---|
209 | |
---|
210 | return SUCCESS; |
---|
211 | } |
---|
212 | |
---|