1 | #include <duchamp/Outputs/ASCIICatalogueWriter.hh> |
---|
2 | #include <duchamp/Outputs/CatalogueWriter.hh> |
---|
3 | #include <duchamp/duchamp.hh> |
---|
4 | #include <duchamp/param.hh> |
---|
5 | #include <duchamp/fitsHeader.hh> |
---|
6 | #include <duchamp/Detection/detection.hh> |
---|
7 | #include <duchamp/Outputs/columns.hh> |
---|
8 | #include <duchamp/Cubes/cubes.hh> |
---|
9 | #include <duchamp/Utils/Statistics.hh> |
---|
10 | #include <ios> |
---|
11 | #include <iostream> |
---|
12 | #include <fstream> |
---|
13 | #include <vector> |
---|
14 | |
---|
15 | namespace duchamp { |
---|
16 | |
---|
17 | ASCIICatalogueWriter::ASCIICatalogueWriter(): |
---|
18 | CatalogueWriter() |
---|
19 | { |
---|
20 | this->itsStream=0; |
---|
21 | // this->itsFileStream=0; |
---|
22 | } |
---|
23 | |
---|
24 | ASCIICatalogueWriter::ASCIICatalogueWriter(std::string name): |
---|
25 | CatalogueWriter(name) |
---|
26 | { |
---|
27 | this->itsStream=0; |
---|
28 | // this->itsFileStream=0; |
---|
29 | } |
---|
30 | |
---|
31 | ASCIICatalogueWriter::ASCIICatalogueWriter(Catalogues::DESTINATION dest): |
---|
32 | itsDestination(dest) |
---|
33 | { |
---|
34 | this->itsStream=0; |
---|
35 | // this->itsFileStream=0; |
---|
36 | } |
---|
37 | |
---|
38 | ASCIICatalogueWriter::ASCIICatalogueWriter(std::string name, Catalogues::DESTINATION dest): |
---|
39 | CatalogueWriter(name), itsDestination(dest) |
---|
40 | { |
---|
41 | this->itsStream=0; |
---|
42 | // this->itsFileStream=0; |
---|
43 | } |
---|
44 | |
---|
45 | ASCIICatalogueWriter::ASCIICatalogueWriter(const ASCIICatalogueWriter& other) |
---|
46 | { |
---|
47 | this->operator=(other); |
---|
48 | } |
---|
49 | |
---|
50 | ASCIICatalogueWriter& ASCIICatalogueWriter::operator= (const ASCIICatalogueWriter& other) |
---|
51 | { |
---|
52 | if(this==&other) return *this; |
---|
53 | ((CatalogueWriter &) *this) = other; |
---|
54 | this->itsStream = other.itsStream; |
---|
55 | // this->itsFileStream = other.itsFileStream; |
---|
56 | this->itsOpenFlag = false; |
---|
57 | this->itsDestination = other.itsDestination; |
---|
58 | return *this; |
---|
59 | } |
---|
60 | |
---|
61 | bool ASCIICatalogueWriter::openCatalogue(std::ios_base::openmode mode) |
---|
62 | { |
---|
63 | if(this->itsName == "" && this->itsDestination!=Catalogues::SCREEN){ |
---|
64 | DUCHAMPERROR("ASCIICatalogueWriter","No catalogue name provided"); |
---|
65 | this->itsOpenFlag = false; |
---|
66 | } |
---|
67 | else { |
---|
68 | if(this->itsDestination==Catalogues::SCREEN){ |
---|
69 | this->itsStream = &std::cout; |
---|
70 | this->itsOpenFlag = true; |
---|
71 | } |
---|
72 | else{ |
---|
73 | this->itsFileStream.open(this->itsName.c_str(),mode); |
---|
74 | this->itsStream = &this->itsFileStream; |
---|
75 | this->itsOpenFlag = this->itsFileStream.is_open(); |
---|
76 | } |
---|
77 | if(!this->itsOpenFlag) |
---|
78 | DUCHAMPERROR("ASCIICatalogueWriter","Could not open file \""<<this->itsName<<"\""); |
---|
79 | } |
---|
80 | return this->itsOpenFlag; |
---|
81 | } |
---|
82 | |
---|
83 | void ASCIICatalogueWriter::writeHeader() |
---|
84 | { |
---|
85 | if(this->itsOpenFlag){ |
---|
86 | *this->itsStream <<"Results of the Duchamp source finder v."<<VERSION<<": "; |
---|
87 | time_t now = time(NULL); |
---|
88 | *this->itsStream << asctime( localtime(&now) ); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | void ASCIICatalogueWriter::writeCommandLineEntry(int argc, char *argv[]) |
---|
93 | { |
---|
94 | if(this->itsOpenFlag){ |
---|
95 | *this->itsStream << "Executing statement: "; |
---|
96 | for(int i=0;i<argc;i++) *this->itsStream << argv[i] << " "; |
---|
97 | *this->itsStream << std::endl; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void ASCIICatalogueWriter::writeParameters() |
---|
102 | { |
---|
103 | if(this->itsOpenFlag){ |
---|
104 | *this->itsStream << *this->itsParam << "\n"; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void ASCIICatalogueWriter::writeStats() |
---|
109 | { |
---|
110 | if(this->itsOpenFlag){ |
---|
111 | |
---|
112 | *this->itsStream<<"--------------------\n"; |
---|
113 | *this->itsStream<<"Summary of statistics:\n"; |
---|
114 | *this->itsStream<<"Detection threshold = " << this->itsStats->getThreshold() |
---|
115 | <<" " << this->itsHead->getFluxUnits(); |
---|
116 | if(this->itsParam->getFlagFDR()) |
---|
117 | *this->itsStream<<" (or S/N=" << this->itsStats->getThresholdSNR()<<")"; |
---|
118 | if(this->itsParam->getFlagSmooth()) *this->itsStream << " in smoothed cube."; |
---|
119 | if(!this->itsParam->getFlagUserThreshold()) |
---|
120 | *this->itsStream<<"\nNoise level = " << this->itsStats->getMiddle() |
---|
121 | <<", Noise spread = " << this->itsStats->getSpread(); |
---|
122 | if(this->itsParam->getFlagSmooth()) *this->itsStream <<" in smoothed cube."; |
---|
123 | |
---|
124 | // // calculate the stats for the original array, so that we can |
---|
125 | // // quote S/N values correctly. |
---|
126 | // this->itsParam->setFlagSmooth(false); |
---|
127 | // bool verb=this->itsParam->isVerbose(); |
---|
128 | // bool fdrflag=this->itsParam->getFlagFDR(); |
---|
129 | // this->itsParam->setVerbosity(false); |
---|
130 | // this->itsParam->setFlagFDR(false); |
---|
131 | // this->setCubeStats(); |
---|
132 | // this->itsParam->setVerbosity(verb); |
---|
133 | // this->itsParam->setFlagFDR(fdrflag); |
---|
134 | // this->itsParam->setFlagSmooth(true); |
---|
135 | |
---|
136 | // *this->itsStream << "\nNoise properties for the original cube are:"; |
---|
137 | // } |
---|
138 | |
---|
139 | // if(!this->itsParam->getFlagUserThreshold()) |
---|
140 | // *this->itsStream<<"\nNoise level = " << this->itsStats->getMiddle() |
---|
141 | // <<", Noise spread = " << this->itsStats->getSpread(); |
---|
142 | |
---|
143 | if(this->itsParam->getFlagGrowth()){ |
---|
144 | Statistics::StatsContainer<float> growthStats = *this->itsStats; |
---|
145 | if(this->itsParam->getFlagUserGrowthThreshold()) |
---|
146 | growthStats.setThreshold(this->itsParam->getGrowthThreshold()); |
---|
147 | else |
---|
148 | growthStats.setThresholdSNR(this->itsParam->getGrowthCut()); |
---|
149 | growthStats.setUseFDR(false); |
---|
150 | *this->itsStream<<"\nDetections grown down to threshold of " |
---|
151 | << growthStats.getThreshold() << " " |
---|
152 | << this->itsHead->getFluxUnits(); |
---|
153 | } |
---|
154 | |
---|
155 | if(!this->itsParam->getFlagUserThreshold()) |
---|
156 | *this->itsStream << "\nFull stats:\n" << *this->itsStats; |
---|
157 | else |
---|
158 | *this->itsStream << "\n\nNot calculating full stats since threshold was provided directly.\n"; |
---|
159 | |
---|
160 | *this->itsStream<<"--------------------\n"; |
---|
161 | |
---|
162 | *this->itsStream<<"Total number of detections = "<<this->itsObjectList->size()<<"\n"; |
---|
163 | *this->itsStream<<"--------------------\n"; |
---|
164 | |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void ASCIICatalogueWriter::writeTableHeader() |
---|
169 | { |
---|
170 | if(this->itsOpenFlag){ |
---|
171 | outputTableHeader(*this->itsStream,*this->itsColumnSpecification,this->itsDestination,this->itsHead->isWCS()); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | void ASCIICatalogueWriter::writeEntries() |
---|
176 | { |
---|
177 | if(this->itsOpenFlag){ |
---|
178 | for(std::vector<Detection>::iterator obj=this->itsObjectList->begin();obj<this->itsObjectList->end();obj++) |
---|
179 | this->writeEntry(&*obj); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | void ASCIICatalogueWriter::writeEntry(Detection *object) |
---|
184 | { |
---|
185 | if(this->itsOpenFlag){ |
---|
186 | object->printTableRow(*this->itsStream, *this->itsColumnSpecification, this->itsDestination); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void ASCIICatalogueWriter::writeCubeSummary() |
---|
191 | { |
---|
192 | if(this->itsOpenFlag){ |
---|
193 | |
---|
194 | *this->itsStream << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
195 | |
---|
196 | *this->itsStream<<this->itsCubeDim[0]; |
---|
197 | for(int i=1;i<3;i++) *this->itsStream<<"x"<<this->itsCubeDim[i]; |
---|
198 | *this->itsStream<<std::endl; |
---|
199 | |
---|
200 | *this->itsStream<<"Threshold\tmiddle\tspread\trobust\n" << this->itsStats->getThreshold() << "\t"; |
---|
201 | if(this->itsParam->getFlagUserThreshold()) |
---|
202 | *this->itsStream << "0.0000\t" << this->itsStats->getThreshold() << "\t"; |
---|
203 | else |
---|
204 | *this->itsStream << this->itsStats->getMiddle() << " " << this->itsStats->getSpread() << "\t"; |
---|
205 | *this->itsStream << this->itsStats->getRobust()<<"\n"; |
---|
206 | |
---|
207 | *this->itsStream<<this->itsObjectList->size()<<" detections:\n--------------\n"; |
---|
208 | std::vector<Detection>::iterator obj; |
---|
209 | for(obj=this->itsObjectList->begin();obj<this->itsObjectList->end();obj++){ |
---|
210 | *this->itsStream << "Detection #" << obj->getID()<<std::endl; |
---|
211 | Detection *newobj = new Detection(*obj); |
---|
212 | newobj->addOffsets(); |
---|
213 | *this->itsStream<<*newobj; |
---|
214 | delete newobj; |
---|
215 | } |
---|
216 | *this->itsStream<<"--------------\n"; |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | bool ASCIICatalogueWriter::closeCatalogue() |
---|
223 | { |
---|
224 | bool returnval=true; |
---|
225 | if(this->itsDestination!=Catalogues::SCREEN){ |
---|
226 | this->itsFileStream.close(); |
---|
227 | returnval=!this->itsFileStream.fail(); |
---|
228 | } |
---|
229 | return returnval; |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | } |
---|