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