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 | using std::ofstream; |
---|
17 | using std::string; |
---|
18 | |
---|
19 | Filter reconFilter; |
---|
20 | |
---|
21 | int main(int argc, char * argv[]) |
---|
22 | { |
---|
23 | |
---|
24 | string paramFile,fitsfile; |
---|
25 | Cube *cube = new Cube; |
---|
26 | |
---|
27 | if(cube->getopts(argc,argv)==FAILURE) return 1; |
---|
28 | |
---|
29 | if(cube->pars().getImageFile().empty()){ |
---|
30 | std::stringstream errmsg; |
---|
31 | errmsg << "No input image has been given!\n" |
---|
32 | << "Use the imageFile parameter in " |
---|
33 | << paramFile << " to specify the FITS file.\nExiting...\n"; |
---|
34 | duchampError("mainDuchamp", errmsg.str()); |
---|
35 | return 1; |
---|
36 | } |
---|
37 | |
---|
38 | if(cube->pars().getFlagSubsection()) cube->pars().verifySubsection(); |
---|
39 | |
---|
40 | std::cout << "Opening image: " |
---|
41 | << cube->pars().getFullImageFile() << std::endl; |
---|
42 | |
---|
43 | if( cube->getCube() == FAILURE){ |
---|
44 | std::stringstream errmsg; |
---|
45 | errmsg << "Unable to open image file " |
---|
46 | << cube->pars().getFullImageFile() |
---|
47 | << "\nExiting...\n"; |
---|
48 | duchampError("mainDuchamp", errmsg.str()); |
---|
49 | return 1; |
---|
50 | } |
---|
51 | else std::cout << "Opened successfully." << std::endl; |
---|
52 | |
---|
53 | // If the reconstructed array is to be read in from disk |
---|
54 | if( cube->pars().getFlagReconExists() && cube->pars().getFlagATrous() ){ |
---|
55 | std::cout << "Reading reconstructed array: "<<std::endl; |
---|
56 | if( cube->readReconCube() == FAILURE){ |
---|
57 | std::stringstream errmsg; |
---|
58 | errmsg <<"Could not read in existing reconstructed array.\n" |
---|
59 | <<"Will perform reconstruction using assigned parameters.\n"; |
---|
60 | duchampWarning("mainDuchamp", errmsg.str()); |
---|
61 | cube->pars().setFlagReconExists(false); |
---|
62 | } |
---|
63 | else std::cout << "Reconstructed array available.\n"; |
---|
64 | } |
---|
65 | |
---|
66 | // Filter needed for baseline removal and a trous reconstruction |
---|
67 | if(cube->pars().getFlagATrous() || cube->pars().getFlagBaseline()){ |
---|
68 | reconFilter.define(cube->pars().getFilterCode()); |
---|
69 | cube->pars().setFilterName(reconFilter.getName()); |
---|
70 | } |
---|
71 | |
---|
72 | // special case for 2D images -- ignore the minChannels requirement |
---|
73 | if(cube->getDimZ()==1) cube->pars().setMinChannels(0); |
---|
74 | |
---|
75 | // Write the parameters to screen. |
---|
76 | std::cout << cube->pars(); |
---|
77 | |
---|
78 | if(cube->pars().getFlagLog()){ |
---|
79 | // Open the logfile and write the time on the first line |
---|
80 | ofstream logfile(cube->pars().getLogFile().c_str()); |
---|
81 | logfile << "New run of the Duchamp sourcefinder: "; |
---|
82 | time_t now = time(NULL); |
---|
83 | logfile << asctime( localtime(&now) ); |
---|
84 | // Write out the command-line statement |
---|
85 | logfile << "Executing statement : "; |
---|
86 | for(int i=0;i<argc;i++) logfile << argv[i] << " "; |
---|
87 | logfile << std::endl; |
---|
88 | logfile << cube->pars(); |
---|
89 | logfile.close(); |
---|
90 | } |
---|
91 | |
---|
92 | if(cube->pars().getFlagBlankPix()){ |
---|
93 | // Trim any blank pixels from the edges, |
---|
94 | // and report the new size of the cube |
---|
95 | std::cout<<"Trimming the Blank Pixels from the edges... "<<std::flush; |
---|
96 | cube->trimCube(); |
---|
97 | std::cout<<"Done."<<std::endl; |
---|
98 | std::cout << "New dimensions: " << cube->getDimX(); |
---|
99 | if(cube->getNumDim()>1) std::cout << "x" << cube->getDimY(); |
---|
100 | if(cube->getNumDim()>2) std::cout << "x" << cube->getDimZ(); |
---|
101 | std::cout << std::endl; |
---|
102 | } |
---|
103 | |
---|
104 | if(cube->pars().getFlagBaseline()){ |
---|
105 | std::cout<<"Removing the baselines... "<<std::flush; |
---|
106 | cube->removeBaseline(); |
---|
107 | std::cout<<" Done. "<<std::endl; |
---|
108 | } |
---|
109 | |
---|
110 | if(cube->pars().getFlagNegative()){ |
---|
111 | std::cout<<"Inverting the Cube... "<<std::flush; |
---|
112 | cube->invert(); |
---|
113 | std::cout<<" Done."<<std::endl; |
---|
114 | } |
---|
115 | |
---|
116 | if(cube->pars().getFlagATrous()){ |
---|
117 | std::cout<<"Commencing search in reconstructed cube..."<<std::endl; |
---|
118 | cube->ReconSearch(); |
---|
119 | std::cout << "Done. Intermediate list has " |
---|
120 | << cube->getNumObj(); |
---|
121 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
122 | else std::cout << " objects.\n"; |
---|
123 | } |
---|
124 | else{ |
---|
125 | std::cout<<"Commencing search in cube..."<<std::endl; |
---|
126 | cube->CubicSearch(); |
---|
127 | std::cout << "Done. Intermediate list has " |
---|
128 | << cube->getNumObj(); |
---|
129 | if(cube->getNumObj()==1) std::cout << " object.\n"; |
---|
130 | else std::cout << " objects.\n"; |
---|
131 | } |
---|
132 | |
---|
133 | if(cube->getNumObj() > 1){ |
---|
134 | std::cout<<"Merging lists... "<<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 | cube->replaceBaseline(); |
---|
147 | |
---|
148 | if(cube->pars().getFlagCubeTrimmed()){ |
---|
149 | std::cout<<"Replacing the trimmed pixels on the edges... "<<std::flush; |
---|
150 | cube->unTrimCube(); |
---|
151 | std::cout<<"Done."<<std::endl; |
---|
152 | } |
---|
153 | |
---|
154 | if(cube->getNumObj()>0){ |
---|
155 | |
---|
156 | cube->calcObjectWCSparams(); |
---|
157 | |
---|
158 | cube->setObjectFlags(); |
---|
159 | |
---|
160 | cube->sortDetections(); |
---|
161 | |
---|
162 | cube->outputDetectionList(); |
---|
163 | } |
---|
164 | |
---|
165 | std::cout<<"Creating the maps... "<<std::flush; |
---|
166 | cube->plotMomentMap("/xs"); |
---|
167 | if(cube->pars().getFlagMaps()){ |
---|
168 | cube->plotMomentMap(cube->pars().getMomentMap()+"/vcps"); |
---|
169 | cube->plotDetectionMap(cube->pars().getDetectionMap()+"/vcps"); |
---|
170 | } |
---|
171 | std::cout << "done.\n"; |
---|
172 | |
---|
173 | if((cube->getDimZ()>1) && (cube->getNumObj()>0)){ |
---|
174 | std::cout << "Plotting the individual spectra... " << std::flush; |
---|
175 | cube->outputSpectra(); |
---|
176 | std::cout << "done.\n"; |
---|
177 | } |
---|
178 | else if(cube->getDimZ()<=1) |
---|
179 | duchampWarning("mainDuchamp","Not plotting any spectra -- no third dimension.\n"); |
---|
180 | |
---|
181 | cpgend(); |
---|
182 | |
---|
183 | if(cube->pars().getFlagATrous()&& |
---|
184 | (cube->pars().getFlagOutputRecon()||cube->pars().getFlagOutputResid()) ){ |
---|
185 | std::cout << "Saving reconstructed cube... "<<std::flush; |
---|
186 | cube->saveReconstructedCube(); |
---|
187 | std::cout << "done.\n"; |
---|
188 | } |
---|
189 | |
---|
190 | if(cube->pars().getFlagLog() && (cube->getNumObj()>0)){ |
---|
191 | ofstream logfile(cube->pars().getLogFile().c_str(),std::ios::app); |
---|
192 | logfile << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
193 | logfile << *cube; |
---|
194 | logfile.close(); |
---|
195 | } |
---|
196 | |
---|
197 | if(cube->pars().getFlagVOT()){ |
---|
198 | ofstream votfile(cube->pars().getVOTFile().c_str()); |
---|
199 | cube->outputDetectionsVOTable(votfile); |
---|
200 | votfile.close(); |
---|
201 | } |
---|
202 | |
---|
203 | if(cube->pars().getFlagKarma()){ |
---|
204 | ofstream karmafile(cube->pars().getKarmaFile().c_str()); |
---|
205 | cube->outputDetectionsKarma(karmafile); |
---|
206 | karmafile.close(); |
---|
207 | } |
---|
208 | |
---|
209 | delete cube; |
---|
210 | |
---|
211 | return 0; |
---|
212 | } |
---|
213 | |
---|