source: trunk/src/Outputs/ASCIICatalogueWriter.cc @ 1066

Last change on this file since 1066 was 1066, checked in by MatthewWhiting, 12 years ago

Cleaning up - removing all the old commented out code. We can also remove entirely the file src/Cubes/VOTable.cc, by incorporating the outputDetectionsVOTable() function to detectionIO.cc

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