source: trunk/src/Outputs/ASCIICatalogueWriter.cc

Last change on this file 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
RevLine 
[1069]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
[1089]30#include <duchamp/duchamp.hh>
[1046]31#include <duchamp/Outputs/ASCIICatalogueWriter.hh>
32#include <duchamp/Outputs/CatalogueWriter.hh>
[1071]33#include <duchamp/Outputs/FileCatalogueWriter.hh>
[1046]34#include <duchamp/param.hh>
[1048]35#include <duchamp/fitsHeader.hh>
[1046]36#include <duchamp/Detection/detection.hh>
[1061]37#include <duchamp/Outputs/columns.hh>
[1048]38#include <duchamp/Cubes/cubes.hh>
39#include <duchamp/Utils/Statistics.hh>
[1049]40#include <ios>
[1046]41#include <iostream>
42#include <fstream>
[1048]43#include <vector>
[1046]44
45namespace duchamp {
46
47  ASCIICatalogueWriter::ASCIICatalogueWriter():
[1071]48    FileCatalogueWriter()
[1046]49  {
[1048]50    this->itsStream=0;
[1046]51  }
52
53  ASCIICatalogueWriter::ASCIICatalogueWriter(std::string name):
[1071]54    FileCatalogueWriter(name)
[1046]55  {
[1048]56    this->itsStream=0;
[1046]57  }
58
[1061]59  ASCIICatalogueWriter::ASCIICatalogueWriter(Catalogues::DESTINATION dest):
[1046]60    itsDestination(dest)
61  {
[1048]62    this->itsStream=0;
[1046]63  }
64
[1061]65  ASCIICatalogueWriter::ASCIICatalogueWriter(std::string name, Catalogues::DESTINATION dest):
[1071]66    FileCatalogueWriter(name), itsDestination(dest)
[1046]67  {
[1048]68    this->itsStream=0;
[1046]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;
[1071]79    ((FileCatalogueWriter &) *this) = other;
[1048]80    this->itsStream = other.itsStream;
81    this->itsDestination = other.itsDestination;
[1046]82    return *this;
83  }
84 
[1049]85  bool ASCIICatalogueWriter::openCatalogue(std::ios_base::openmode mode)
[1046]86  {
[1061]87    if(this->itsName == "" && this->itsDestination!=Catalogues::SCREEN){
[1046]88      DUCHAMPERROR("ASCIICatalogueWriter","No catalogue name provided");
89      this->itsOpenFlag = false;
90    }
91    else {
[1061]92      if(this->itsDestination==Catalogues::SCREEN){
[1048]93        this->itsStream = &std::cout;
[1046]94        this->itsOpenFlag = true;
[1137]95        this->itsParam->setCommentString("");
96        this->itsStats->setCommentString("");
97        this->itsColumnSpecification->setCommentString("");
[1046]98      }
99      else{
[1071]100        FileCatalogueWriter::openCatalogue(mode);
[1049]101        this->itsStream = &this->itsFileStream;
[1137]102        this->itsParam->setCommentString("# ");
103        this->itsStats->setCommentString("# ");
104        this->itsColumnSpecification->setCommentString("# ");
[1046]105      }
[1071]106
[1046]107      if(!this->itsOpenFlag)
108        DUCHAMPERROR("ASCIICatalogueWriter","Could not open file \""<<this->itsName<<"\"");
109    }
[1048]110    return this->itsOpenFlag;
[1046]111  }
112
113  void ASCIICatalogueWriter::writeHeader()
114  {
115    if(this->itsOpenFlag){
[1137]116      *this->itsStream <<"# Results of the Duchamp source finder v."<<VERSION<<": ";
[1046]117      time_t now = time(NULL);
[1048]118      *this->itsStream << asctime( localtime(&now) );
[1046]119    }
120  }
121
[1049]122  void ASCIICatalogueWriter::writeCommandLineEntry(int argc, char *argv[])
123  {
124    if(this->itsOpenFlag){
[1137]125      *this->itsStream << "# Executing statement: ";
[1049]126       for(int i=0;i<argc;i++) *this->itsStream << argv[i] << " ";
127       *this->itsStream << std::endl;
128    }
129  }
130
[1048]131  void ASCIICatalogueWriter::writeParameters()
[1046]132  {
133    if(this->itsOpenFlag){
[1137]134      // if(this->itsDestination != Catalogues::SCREEN) this->itsParam->setCommentString("# ");
135      *this->itsStream << *this->itsParam << this->itsParam->commentString() << "\n";
[1046]136    }
137  }
138
[1048]139  void ASCIICatalogueWriter::writeStats()
[1046]140  {
[1048]141    if(this->itsOpenFlag){
[1046]142
[1137]143      *this->itsStream<<"# --------------------\n";
144      *this->itsStream<<"# Summary of statistics:\n";
145      *this->itsStream<<"# Detection threshold = " << this->itsStats->getThreshold()
[1048]146                      <<" " << this->itsHead->getFluxUnits();
147      if(this->itsParam->getFlagFDR())
148        *this->itsStream<<" (or S/N=" << this->itsStats->getThresholdSNR()<<")";
[1049]149      if(this->itsParam->getFlagSmooth()) *this->itsStream << " in smoothed cube.";
150      if(!this->itsParam->getFlagUserThreshold())
[1137]151        *this->itsStream<<"\n# Noise level = " << this->itsStats->getMiddle()
[1049]152                        <<", Noise spread = " << this->itsStats->getSpread();
153      if(this->itsParam->getFlagSmooth()) *this->itsStream  <<" in smoothed cube.";
[1048]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:";
[1049]168      // }
[1048]169     
[1049]170      // if(!this->itsParam->getFlagUserThreshold())
171      //        *this->itsStream<<"\nNoise level = " << this->itsStats->getMiddle()
172      //                        <<", Noise spread = " << this->itsStats->getSpread();
[1048]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);
[1137]181        *this->itsStream<<"\n# Detections grown down to threshold of "
[1048]182                        << growthStats.getThreshold() << " "
183                        << this->itsHead->getFluxUnits();
184      }
185
[1137]186      if(!this->itsParam->getFlagUserThreshold()){
187        // this->itsStats->setCommentString("#");
188        *this->itsStream << "\n# Full stats:\n" << *this->itsStats;
189      }
[1048]190      else
[1137]191        *this->itsStream << "\n#\n# Not calculating full stats since threshold was provided directly.\n";
[1048]192
[1137]193      *this->itsStream<<"# --------------------\n";
[1048]194 
[1137]195      *this->itsStream<<"# Total number of detections = "<<this->itsObjectList->size()<<"\n";
196      *this->itsStream<<"# --------------------\n";
[1048]197
198    }
[1046]199  }
200
[1048]201  void ASCIICatalogueWriter::writeTableHeader()
202  {
203    if(this->itsOpenFlag){
[1137]204      this->itsColumnSpecification->outputTableHeader(*this->itsStream,this->itsDestination,this->itsHead->isWCS());
[1048]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
[1049]215  void ASCIICatalogueWriter::writeCubeSummary()
216  {
217    if(this->itsOpenFlag){
218     
[1146]219      *this->itsStream << "=-=-=-=-=-=-=-\nCube summary\n=-=-=-=-=-=-=-\n";
[1049]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
[1048]247  bool ASCIICatalogueWriter::closeCatalogue()
248  {
249    bool returnval=true;
[1061]250    if(this->itsDestination!=Catalogues::SCREEN){
[1071]251      FileCatalogueWriter::closeCatalogue();
[1048]252    }
253    return returnval;
254  }
255
256
[1046]257}
Note: See TracBrowser for help on using the repository browser.