source: tags/release-1.6.1/src/Outputs/ASCIICatalogueWriter.cc @ 1441

Last change on this file since 1441 was 1146, checked in by MatthewWhiting, 11 years ago

Ticket #181 - Implementing the new catalogue writing, with new functions in Cube to handle the overall reading & writing. Briefly tested and seems to work OK. A couple of minor bug fixes as well, one in particular with the baseline-writing.

File size: 8.7 KB
Line 
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
45namespace 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        this->itsParam->setCommentString("");
96        this->itsStats->setCommentString("");
97        this->itsColumnSpecification->setCommentString("");
98      }
99      else{
100        FileCatalogueWriter::openCatalogue(mode);
101        this->itsStream = &this->itsFileStream;
102        this->itsParam->setCommentString("# ");
103        this->itsStats->setCommentString("# ");
104        this->itsColumnSpecification->setCommentString("# ");
105      }
106
107      if(!this->itsOpenFlag)
108        DUCHAMPERROR("ASCIICatalogueWriter","Could not open file \""<<this->itsName<<"\"");
109    }
110    return this->itsOpenFlag;
111  }
112
113  void ASCIICatalogueWriter::writeHeader()
114  {
115    if(this->itsOpenFlag){
116      *this->itsStream <<"# Results of the Duchamp source finder v."<<VERSION<<": ";
117      time_t now = time(NULL);
118      *this->itsStream << asctime( localtime(&now) );
119    }
120  }
121
122  void ASCIICatalogueWriter::writeCommandLineEntry(int argc, char *argv[])
123  {
124    if(this->itsOpenFlag){
125      *this->itsStream << "# Executing statement: ";
126       for(int i=0;i<argc;i++) *this->itsStream << argv[i] << " ";
127       *this->itsStream << std::endl;
128    }
129  }
130
131  void ASCIICatalogueWriter::writeParameters()
132  {
133    if(this->itsOpenFlag){
134      // if(this->itsDestination != Catalogues::SCREEN) this->itsParam->setCommentString("# ");
135      *this->itsStream << *this->itsParam << this->itsParam->commentString() << "\n";
136    }
137  }
138
139  void ASCIICatalogueWriter::writeStats()
140  {
141    if(this->itsOpenFlag){
142
143      *this->itsStream<<"# --------------------\n";
144      *this->itsStream<<"# Summary of statistics:\n";
145      *this->itsStream<<"# Detection threshold = " << this->itsStats->getThreshold()
146                      <<" " << this->itsHead->getFluxUnits();
147      if(this->itsParam->getFlagFDR())
148        *this->itsStream<<" (or S/N=" << this->itsStats->getThresholdSNR()<<")";
149      if(this->itsParam->getFlagSmooth()) *this->itsStream << " in smoothed cube.";
150      if(!this->itsParam->getFlagUserThreshold())
151        *this->itsStream<<"\n# Noise level = " << this->itsStats->getMiddle()
152                        <<", Noise spread = " << this->itsStats->getSpread();
153      if(this->itsParam->getFlagSmooth()) *this->itsStream  <<" in smoothed cube.";
154     
155        // // calculate the stats for the original array, so that we can
156        // // quote S/N values correctly.
157        // this->itsParam->setFlagSmooth(false);
158        // bool verb=this->itsParam->isVerbose();
159        // bool fdrflag=this->itsParam->getFlagFDR();
160        // this->itsParam->setVerbosity(false);
161        // this->itsParam->setFlagFDR(false);
162        // this->setCubeStats();
163        // this->itsParam->setVerbosity(verb);
164        // this->itsParam->setFlagFDR(fdrflag);
165        // this->itsParam->setFlagSmooth(true);
166     
167        // *this->itsStream << "\nNoise properties for the original cube are:";
168      // }
169     
170      // if(!this->itsParam->getFlagUserThreshold())
171      //        *this->itsStream<<"\nNoise level = " << this->itsStats->getMiddle()
172      //                        <<", Noise spread = " << this->itsStats->getSpread();
173
174      if(this->itsParam->getFlagGrowth()){
175        Statistics::StatsContainer<float> growthStats = *this->itsStats;
176        if(this->itsParam->getFlagUserGrowthThreshold())
177          growthStats.setThreshold(this->itsParam->getGrowthThreshold());
178        else
179          growthStats.setThresholdSNR(this->itsParam->getGrowthCut());
180        growthStats.setUseFDR(false);
181        *this->itsStream<<"\n# Detections grown down to threshold of "
182                        << growthStats.getThreshold() << " "
183                        << this->itsHead->getFluxUnits();
184      }
185
186      if(!this->itsParam->getFlagUserThreshold()){
187        // this->itsStats->setCommentString("#");
188        *this->itsStream << "\n# Full stats:\n" << *this->itsStats;
189      }
190      else
191        *this->itsStream << "\n#\n# Not calculating full stats since threshold was provided directly.\n";
192
193      *this->itsStream<<"# --------------------\n";
194 
195      *this->itsStream<<"# Total number of detections = "<<this->itsObjectList->size()<<"\n";
196      *this->itsStream<<"# --------------------\n";
197
198    }
199  }
200
201  void ASCIICatalogueWriter::writeTableHeader()
202  {
203    if(this->itsOpenFlag){
204      this->itsColumnSpecification->outputTableHeader(*this->itsStream,this->itsDestination,this->itsHead->isWCS());
205    }
206  }
207
208  void ASCIICatalogueWriter::writeEntry(Detection *object)
209  {
210    if(this->itsOpenFlag){
211      object->printTableRow(*this->itsStream, *this->itsColumnSpecification, this->itsDestination);
212    }
213  }
214
215  void ASCIICatalogueWriter::writeCubeSummary()
216  {
217    if(this->itsOpenFlag){
218     
219      *this->itsStream << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
220
221      *this->itsStream<<this->itsCubeDim[0];
222      for(int i=1;i<3;i++) *this->itsStream<<"x"<<this->itsCubeDim[i];
223      *this->itsStream<<std::endl;
224
225      *this->itsStream<<"Threshold\tmiddle\tspread\trobust\n" << this->itsStats->getThreshold() << "\t";
226      if(this->itsParam->getFlagUserThreshold())
227        *this->itsStream << "0.0000\t" << this->itsStats->getThreshold() << "\t";
228      else
229        *this->itsStream << this->itsStats->getMiddle() << " " << this->itsStats->getSpread() << "\t";
230      *this->itsStream << this->itsStats->getRobust()<<"\n";
231
232      *this->itsStream<<this->itsObjectList->size()<<" detections:\n--------------\n";
233      std::vector<Detection>::iterator obj;
234      for(obj=this->itsObjectList->begin();obj<this->itsObjectList->end();obj++){
235        *this->itsStream << "Detection #" << obj->getID()<<std::endl;
236        Detection *newobj = new Detection(*obj);
237        newobj->addOffsets();
238        *this->itsStream<<*newobj;
239        delete newobj;
240      }
241      *this->itsStream<<"--------------\n";
242    }
243
244  }
245
246
247  bool ASCIICatalogueWriter::closeCatalogue()
248  {
249    bool returnval=true;
250    if(this->itsDestination!=Catalogues::SCREEN){
251      FileCatalogueWriter::closeCatalogue();
252    }
253    return returnval;
254  }
255
256
257}
Note: See TracBrowser for help on using the repository browser.