1 | // ----------------------------------------------------------------------- |
---|
2 | // ASCIICatalogueWriter.cc: Writing output catalogues to basic ASCII-format |
---|
3 | // files (or the screen). |
---|
4 | // ----------------------------------------------------------------------- |
---|
5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or modify it |
---|
8 | // under the terms of the GNU General Public License as published by the |
---|
9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
10 | // option) any later version. |
---|
11 | // |
---|
12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
15 | // for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License |
---|
18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
20 | // |
---|
21 | // Correspondence concerning Duchamp may be directed to: |
---|
22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
23 | // Postal address: Dr. Matthew Whiting |
---|
24 | // Australia Telescope National Facility, CSIRO |
---|
25 | // PO Box 76 |
---|
26 | // Epping NSW 1710 |
---|
27 | // AUSTRALIA |
---|
28 | // ----------------------------------------------------------------------- |
---|
29 | |
---|
30 | #include <duchamp/duchamp.hh> |
---|
31 | #include <duchamp/Outputs/ASCIICatalogueWriter.hh> |
---|
32 | #include <duchamp/Outputs/CatalogueWriter.hh> |
---|
33 | #include <duchamp/Outputs/FileCatalogueWriter.hh> |
---|
34 | #include <duchamp/param.hh> |
---|
35 | #include <duchamp/fitsHeader.hh> |
---|
36 | #include <duchamp/Detection/detection.hh> |
---|
37 | #include <duchamp/Outputs/columns.hh> |
---|
38 | #include <duchamp/Cubes/cubes.hh> |
---|
39 | #include <duchamp/Utils/Statistics.hh> |
---|
40 | #include <ios> |
---|
41 | #include <iostream> |
---|
42 | #include <fstream> |
---|
43 | #include <vector> |
---|
44 | |
---|
45 | namespace duchamp { |
---|
46 | |
---|
47 | ASCIICatalogueWriter::ASCIICatalogueWriter(): |
---|
48 | FileCatalogueWriter() |
---|
49 | { |
---|
50 | this->itsStream=0; |
---|
51 | } |
---|
52 | |
---|
53 | ASCIICatalogueWriter::ASCIICatalogueWriter(std::string name): |
---|
54 | FileCatalogueWriter(name) |
---|
55 | { |
---|
56 | this->itsStream=0; |
---|
57 | } |
---|
58 | |
---|
59 | ASCIICatalogueWriter::ASCIICatalogueWriter(Catalogues::DESTINATION dest): |
---|
60 | itsDestination(dest) |
---|
61 | { |
---|
62 | this->itsStream=0; |
---|
63 | } |
---|
64 | |
---|
65 | ASCIICatalogueWriter::ASCIICatalogueWriter(std::string name, Catalogues::DESTINATION dest): |
---|
66 | FileCatalogueWriter(name), itsDestination(dest) |
---|
67 | { |
---|
68 | this->itsStream=0; |
---|
69 | } |
---|
70 | |
---|
71 | ASCIICatalogueWriter::ASCIICatalogueWriter(const ASCIICatalogueWriter& other) |
---|
72 | { |
---|
73 | this->operator=(other); |
---|
74 | } |
---|
75 | |
---|
76 | ASCIICatalogueWriter& ASCIICatalogueWriter::operator= (const ASCIICatalogueWriter& other) |
---|
77 | { |
---|
78 | if(this==&other) return *this; |
---|
79 | ((FileCatalogueWriter &) *this) = other; |
---|
80 | this->itsStream = other.itsStream; |
---|
81 | this->itsDestination = other.itsDestination; |
---|
82 | return *this; |
---|
83 | } |
---|
84 | |
---|
85 | bool ASCIICatalogueWriter::openCatalogue(std::ios_base::openmode mode) |
---|
86 | { |
---|
87 | if(this->itsName == "" && this->itsDestination!=Catalogues::SCREEN){ |
---|
88 | DUCHAMPERROR("ASCIICatalogueWriter","No catalogue name provided"); |
---|
89 | this->itsOpenFlag = false; |
---|
90 | } |
---|
91 | else { |
---|
92 | if(this->itsDestination==Catalogues::SCREEN){ |
---|
93 | this->itsStream = &std::cout; |
---|
94 | this->itsOpenFlag = true; |
---|
95 | } |
---|
96 | else{ |
---|
97 | FileCatalogueWriter::openCatalogue(mode); |
---|
98 | this->itsStream = &this->itsFileStream; |
---|
99 | } |
---|
100 | |
---|
101 | if(!this->itsOpenFlag) |
---|
102 | DUCHAMPERROR("ASCIICatalogueWriter","Could not open file \""<<this->itsName<<"\""); |
---|
103 | } |
---|
104 | return this->itsOpenFlag; |
---|
105 | } |
---|
106 | |
---|
107 | void ASCIICatalogueWriter::writeHeader() |
---|
108 | { |
---|
109 | if(this->itsOpenFlag){ |
---|
110 | *this->itsStream <<"Results of the Duchamp source finder v."<<VERSION<<": "; |
---|
111 | time_t now = time(NULL); |
---|
112 | *this->itsStream << asctime( localtime(&now) ); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | void ASCIICatalogueWriter::writeCommandLineEntry(int argc, char *argv[]) |
---|
117 | { |
---|
118 | if(this->itsOpenFlag){ |
---|
119 | *this->itsStream << "Executing statement: "; |
---|
120 | for(int i=0;i<argc;i++) *this->itsStream << argv[i] << " "; |
---|
121 | *this->itsStream << std::endl; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | void ASCIICatalogueWriter::writeParameters() |
---|
126 | { |
---|
127 | if(this->itsOpenFlag){ |
---|
128 | *this->itsStream << *this->itsParam << "\n"; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | void ASCIICatalogueWriter::writeStats() |
---|
133 | { |
---|
134 | if(this->itsOpenFlag){ |
---|
135 | |
---|
136 | *this->itsStream<<"--------------------\n"; |
---|
137 | *this->itsStream<<"Summary of statistics:\n"; |
---|
138 | *this->itsStream<<"Detection threshold = " << this->itsStats->getThreshold() |
---|
139 | <<" " << this->itsHead->getFluxUnits(); |
---|
140 | if(this->itsParam->getFlagFDR()) |
---|
141 | *this->itsStream<<" (or S/N=" << this->itsStats->getThresholdSNR()<<")"; |
---|
142 | if(this->itsParam->getFlagSmooth()) *this->itsStream << " in smoothed cube."; |
---|
143 | if(!this->itsParam->getFlagUserThreshold()) |
---|
144 | *this->itsStream<<"\nNoise level = " << this->itsStats->getMiddle() |
---|
145 | <<", Noise spread = " << this->itsStats->getSpread(); |
---|
146 | if(this->itsParam->getFlagSmooth()) *this->itsStream <<" in smoothed cube."; |
---|
147 | |
---|
148 | // // calculate the stats for the original array, so that we can |
---|
149 | // // quote S/N values correctly. |
---|
150 | // this->itsParam->setFlagSmooth(false); |
---|
151 | // bool verb=this->itsParam->isVerbose(); |
---|
152 | // bool fdrflag=this->itsParam->getFlagFDR(); |
---|
153 | // this->itsParam->setVerbosity(false); |
---|
154 | // this->itsParam->setFlagFDR(false); |
---|
155 | // this->setCubeStats(); |
---|
156 | // this->itsParam->setVerbosity(verb); |
---|
157 | // this->itsParam->setFlagFDR(fdrflag); |
---|
158 | // this->itsParam->setFlagSmooth(true); |
---|
159 | |
---|
160 | // *this->itsStream << "\nNoise properties for the original cube are:"; |
---|
161 | // } |
---|
162 | |
---|
163 | // if(!this->itsParam->getFlagUserThreshold()) |
---|
164 | // *this->itsStream<<"\nNoise level = " << this->itsStats->getMiddle() |
---|
165 | // <<", Noise spread = " << this->itsStats->getSpread(); |
---|
166 | |
---|
167 | if(this->itsParam->getFlagGrowth()){ |
---|
168 | Statistics::StatsContainer<float> growthStats = *this->itsStats; |
---|
169 | if(this->itsParam->getFlagUserGrowthThreshold()) |
---|
170 | growthStats.setThreshold(this->itsParam->getGrowthThreshold()); |
---|
171 | else |
---|
172 | growthStats.setThresholdSNR(this->itsParam->getGrowthCut()); |
---|
173 | growthStats.setUseFDR(false); |
---|
174 | *this->itsStream<<"\nDetections grown down to threshold of " |
---|
175 | << growthStats.getThreshold() << " " |
---|
176 | << this->itsHead->getFluxUnits(); |
---|
177 | } |
---|
178 | |
---|
179 | if(!this->itsParam->getFlagUserThreshold()) |
---|
180 | *this->itsStream << "\nFull stats:\n" << *this->itsStats; |
---|
181 | else |
---|
182 | *this->itsStream << "\n\nNot calculating full stats since threshold was provided directly.\n"; |
---|
183 | |
---|
184 | *this->itsStream<<"--------------------\n"; |
---|
185 | |
---|
186 | *this->itsStream<<"Total number of detections = "<<this->itsObjectList->size()<<"\n"; |
---|
187 | *this->itsStream<<"--------------------\n"; |
---|
188 | |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | void ASCIICatalogueWriter::writeTableHeader() |
---|
193 | { |
---|
194 | if(this->itsOpenFlag){ |
---|
195 | outputTableHeader(*this->itsStream,*this->itsColumnSpecification,this->itsDestination,this->itsHead->isWCS()); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | void ASCIICatalogueWriter::writeEntry(Detection *object) |
---|
200 | { |
---|
201 | if(this->itsOpenFlag){ |
---|
202 | object->printTableRow(*this->itsStream, *this->itsColumnSpecification, this->itsDestination); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | void ASCIICatalogueWriter::writeCubeSummary() |
---|
207 | { |
---|
208 | if(this->itsOpenFlag){ |
---|
209 | |
---|
210 | *this->itsStream << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n"; |
---|
211 | |
---|
212 | *this->itsStream<<this->itsCubeDim[0]; |
---|
213 | for(int i=1;i<3;i++) *this->itsStream<<"x"<<this->itsCubeDim[i]; |
---|
214 | *this->itsStream<<std::endl; |
---|
215 | |
---|
216 | *this->itsStream<<"Threshold\tmiddle\tspread\trobust\n" << this->itsStats->getThreshold() << "\t"; |
---|
217 | if(this->itsParam->getFlagUserThreshold()) |
---|
218 | *this->itsStream << "0.0000\t" << this->itsStats->getThreshold() << "\t"; |
---|
219 | else |
---|
220 | *this->itsStream << this->itsStats->getMiddle() << " " << this->itsStats->getSpread() << "\t"; |
---|
221 | *this->itsStream << this->itsStats->getRobust()<<"\n"; |
---|
222 | |
---|
223 | *this->itsStream<<this->itsObjectList->size()<<" detections:\n--------------\n"; |
---|
224 | std::vector<Detection>::iterator obj; |
---|
225 | for(obj=this->itsObjectList->begin();obj<this->itsObjectList->end();obj++){ |
---|
226 | *this->itsStream << "Detection #" << obj->getID()<<std::endl; |
---|
227 | Detection *newobj = new Detection(*obj); |
---|
228 | newobj->addOffsets(); |
---|
229 | *this->itsStream<<*newobj; |
---|
230 | delete newobj; |
---|
231 | } |
---|
232 | *this->itsStream<<"--------------\n"; |
---|
233 | } |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | bool ASCIICatalogueWriter::closeCatalogue() |
---|
239 | { |
---|
240 | bool returnval=true; |
---|
241 | if(this->itsDestination!=Catalogues::SCREEN){ |
---|
242 | FileCatalogueWriter::closeCatalogue(); |
---|
243 | } |
---|
244 | return returnval; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | } |
---|