1 | #include <iostream> |
---|
2 | #include <iomanip> |
---|
3 | #include <sstream> |
---|
4 | #include <fstream> |
---|
5 | #include <vector> |
---|
6 | #include <string> |
---|
7 | #include <cpgplot.h> |
---|
8 | #include <math.h> |
---|
9 | #include <unistd.h> |
---|
10 | #include <time.h> |
---|
11 | |
---|
12 | #include <duchamp.hh> |
---|
13 | #include <Detection/detection.hh> |
---|
14 | #include <Cubes/cubes.hh> |
---|
15 | #include <Utils/utils.hh> |
---|
16 | #include <ATrous/atrous.hh> |
---|
17 | |
---|
18 | using std::ofstream; |
---|
19 | using std::endl; |
---|
20 | |
---|
21 | Filter reconFilter; |
---|
22 | |
---|
23 | int main(int argc, char * argv[]) |
---|
24 | { |
---|
25 | |
---|
26 | string paramFile,fitsfile; |
---|
27 | Cube *cube = new Cube; |
---|
28 | Param *par = new Param; |
---|
29 | |
---|
30 | if(argc==1){ |
---|
31 | std::cout << ERR_USAGE_MSG; |
---|
32 | return 1; |
---|
33 | } |
---|
34 | else{ |
---|
35 | char c; |
---|
36 | while( ( c = getopt(argc,argv,"p:f:hv") )!=-1){ |
---|
37 | switch(c) { |
---|
38 | case 'p': |
---|
39 | paramFile = optarg; |
---|
40 | cube->readParam(paramFile); |
---|
41 | break; |
---|
42 | case 'f': |
---|
43 | fitsfile = optarg; |
---|
44 | par->setImageFile(fitsfile); |
---|
45 | cube->saveParam(*par); |
---|
46 | break; |
---|
47 | case 'v': |
---|
48 | std::cout << PROGNAME << " " << VERSION << std::endl; |
---|
49 | return 1; |
---|
50 | break; |
---|
51 | case 'h': |
---|
52 | default : |
---|
53 | std::cout << ERR_USAGE_MSG; |
---|
54 | return 1; |
---|
55 | break; |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | delete par; |
---|
61 | |
---|
62 | if(cube->pars().getImageFile().empty()){ |
---|
63 | std::cerr << "ERROR : No input image has been given!\n"; |
---|
64 | std::cerr << " Use the imageFile parameter in "<< paramFile << " to specify the FITS file.\n"; |
---|
65 | std::cerr << "Exiting...\n\n"; |
---|
66 | return 1; |
---|
67 | } |
---|
68 | |
---|
69 | // Filter needed for baseline removal and a trous reconstruction |
---|
70 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
71 | reconFilter.define(cube->pars().getFilterCode()); |
---|
72 | cube->pars().setFilterName(reconFilter.getName()); |
---|
73 | } |
---|
74 | |
---|
75 | string fname = cube->pars().getImageFile(); |
---|
76 | if(cube->pars().getFlagSubsection()){ |
---|
77 | cube->pars().parseSubsection(); |
---|
78 | fname+=cube->pars().getSubsection(); |
---|
79 | } |
---|
80 | std::cout << "Opening image: " << fname << endl; |
---|
81 | if( cube->getCube(fname) == FAILURE){ |
---|
82 | std::cerr << "ERROR : Unable to open image file : " << fname << endl; |
---|
83 | std::cerr << "Exiting...\n\n"; |
---|
84 | return 1; |
---|
85 | } |
---|
86 | else std::cout << "Opened successfully." << endl; |
---|
87 | |
---|
88 | // If the reconstructed array is to be read in from disk |
---|
89 | if( cube->pars().getFlagReconExists() && cube->pars().getFlagATrous() ){ |
---|
90 | std::cout << "Reading reconstructed array: "; |
---|
91 | if( cube->readReconCube() == FAILURE){ |
---|
92 | std::cerr << "WARNING : Could not read in existing reconstructed array.\n" |
---|
93 | << " Will perform reconstruction using assigned parameters.\n"; |
---|
94 | cube->pars().setFlagReconExists(false); |
---|
95 | } |
---|
96 | std::cout << " Done.\n"; |
---|
97 | } |
---|
98 | |
---|
99 | std::cout << cube->pars(); |
---|
100 | |
---|
101 | if(cube->pars().getFlagLog()){ |
---|
102 | ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
103 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
104 | time_t now = time(NULL); |
---|
105 | logfile << asctime( localtime(&now) ); |
---|
106 | logfile << cube->pars(); |
---|
107 | logfile.close(); |
---|
108 | } |
---|
109 | |
---|
110 | if(cube->pars().getFlagBlankPix()){ |
---|
111 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
112 | cube->trimCube(); |
---|
113 | std::cout<<"Done."<<endl; |
---|
114 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
115 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
116 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
117 | std::cout << endl; |
---|
118 | } |
---|
119 | |
---|
120 | if(cube->pars().getFlagMW()){ |
---|
121 | std::cout<<"Removing the Milky Way channels... "; |
---|
122 | cube->removeMW(); |
---|
123 | std::cout<<"Done."<<endl; |
---|
124 | } |
---|
125 | |
---|
126 | if(cube->pars().getFlagBaseline()){ |
---|
127 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
128 | cube->removeBaseline(); |
---|
129 | std::cout<<" Done. "<<std::endl; |
---|
130 | } |
---|
131 | |
---|
132 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); // special case for 2D images. |
---|
133 | |
---|
134 | if(cube->pars().getFlagNegative()){ |
---|
135 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
136 | cube->invert(); |
---|
137 | std::cout<<" Done."<<std::endl; |
---|
138 | } |
---|
139 | |
---|
140 | if(cube->pars().getFlagATrous()){ |
---|
141 | std::cout<<"Commencing search in reconstructed cube..."<<endl; |
---|
142 | cube->ReconSearch3D(); |
---|
143 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<endl; |
---|
144 | } |
---|
145 | else{ |
---|
146 | std::cout<<"Commencing search in cube..."<<endl; |
---|
147 | cube->SimpleSearch3D(); |
---|
148 | std::cout<<"Done. Found "<<cube->getNumObj()<<" objects."<<endl; |
---|
149 | } |
---|
150 | |
---|
151 | std::cout<<"Merging lists... "<<std::flush; |
---|
152 | cube->ObjectMerger(); |
---|
153 | std::cout<<"Done. "<<endl; |
---|
154 | std::cout<<"Final object count = "<<cube->getNumObj()<<endl; |
---|
155 | |
---|
156 | if(cube->pars().getFlagNegative()){ |
---|
157 | std::cout<<"Un-Inverting the Cube... "<<std::flush; |
---|
158 | cube->reInvert(); |
---|
159 | std::cout<<" Done."<<std::endl; |
---|
160 | } |
---|
161 | |
---|
162 | cube->replaceBaseline(); |
---|
163 | |
---|
164 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
165 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
166 | cube->unTrimCube(); |
---|
167 | std::cout<<"Done."<<endl; |
---|
168 | } |
---|
169 | |
---|
170 | if(cube->getNumObj()>0){ |
---|
171 | |
---|
172 | cube->calcObjectWCSparams(); |
---|
173 | |
---|
174 | cube->setObjectFlags(); |
---|
175 | |
---|
176 | cube->sortDetections(); |
---|
177 | |
---|
178 | cube->outputDetectionList(); |
---|
179 | } |
---|
180 | |
---|
181 | std::cout<<"Creating the maps... "<<std::flush; |
---|
182 | cube->plotMomentMap("/xs"); |
---|
183 | if(cube->pars().getFlagMaps()){ |
---|
184 | cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
185 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
186 | } |
---|
187 | std::cout << "done.\n"; |
---|
188 | |
---|
189 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
190 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
191 | cube->outputSpectra(); |
---|
192 | std::cout << "done.\n"; |
---|
193 | } |
---|
194 | else if(cube->getDimZ()<=1) std::cerr << "WARNING : Not plotting any spectra -- no third dimension.\n"; |
---|
195 | |
---|
196 | cpgend(); |
---|
197 | |
---|
198 | if(cube->pars().getFlagATrous()&& |
---|
199 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
200 | std::cout << "Saving reconstructed cube... "<<std::flush; |
---|
201 | cube->saveReconstructedCube(); |
---|
202 | std::cout << "done.\n"; |
---|
203 | } |
---|
204 | |
---|
205 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
206 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
207 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
208 | logfile << *cube; |
---|
209 | logfile.close(); |
---|
210 | } |
---|
211 | |
---|
212 | if(cube->pars().getFlagVOT()){ |
---|
213 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
214 | cube->outputDetectionsVOTable(votfile); |
---|
215 | votfile.close(); |
---|
216 | } |
---|
217 | |
---|
218 | if(cube->pars().getFlagKarma()){ |
---|
219 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
220 | cube->outputDetectionsKarma(karmafile); |
---|
221 | karmafile.close(); |
---|
222 | } |
---|
223 | |
---|
224 | delete cube; |
---|
225 | |
---|
226 | return 0; |
---|
227 | } |
---|
228 | |
---|