// ----------------------------------------------------------------------- // detectionIO.cc: Screen and File output of the detected objects. // ----------------------------------------------------------------------- // Copyright (C) 2006, Matthew Whiting, ATNF // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // Duchamp is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License // along with Duchamp; if not, write to the Free Software Foundation, // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA // // Correspondence concerning Duchamp may be directed to: // Internet email: Matthew.Whiting [at] atnf.csiro.au // Postal address: Dr. Matthew Whiting // Australia Telescope National Facility, CSIRO // PO Box 76 // Epping NSW 1710 // AUSTRALIA // ----------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using std::endl; using std::setw; using std::setprecision; using namespace PixelInfo; using namespace Statistics; namespace duchamp { void Cube::outputDetectionsKarma() { KarmaAnnotationWriter writer(this->pars().getKarmaFile()); writer.setup(this); writer.openCatalogue(); writer.writeHeader(); writer.writeParameters(); writer.writeStats(); writer.writeTableHeader(); writer.writeEntries(); writer.writeFooter(); writer.closeCatalogue(); } void Cube::outputDetectionsKarma(std::ostream &stream) { /// @details /// Prints to a stream (provided) the list of detected objects in the cube /// in the format of an annotation file for the Karma suite of programs. /// Annotation file draws a box enclosing the detection, and writes the /// ID number of the detection to the right of the box. std::string fname = this->par.getImageFile(); if(this->par.getFlagSubsection()) fname+=this->par.getSubsection(); stream << "# Duchamp Source Finder v."<< VERSION << endl; stream << "# Results for FITS file: " << fname << endl; if(this->par.getFlagFDR()) stream<<"# FDR Significance = " << this->par.getAlpha() << endl; else stream<<"# Threshold = " << this->par.getCut() << endl; if(this->par.getFlagATrous()){ stream<<"# The a trous reconstruction method was used, with the following parameters." << endl; stream<<"# Dimension = " << this->par.getReconDim() << endl; stream<<"# Threshold = " << this->par.getAtrousCut() << endl; stream<<"# Minimum Scale =" << this->par.getMinScale() << endl; stream<<"# Filter = " << this->par.getFilterName() << endl; } else if(this->par.getFlagSmooth()){ stream<<"# The data was smoothed prior to searching, with the following parameters." << endl; stream<<"# Smoothing type = " << this->par.getSmoothType() << endl; if(this->par.getSmoothType()=="spectral"){ stream << "# Hanning width = " << this->par.getHanningWidth() << endl; } else{ stream << "# Kernel Major axis = " << this->par.getKernMaj() << endl; if(this->par.getKernMin()>0) stream << "# Kernel Minor axis = " << this->par.getKernMin() << endl; else stream << "# Kernel Minor axis = " << this->par.getKernMaj() << endl; stream << "# Kernel Major axis = " << this->par.getKernPA() << endl; } } stream << "#\n"; stream << "COLOR RED" << endl; if(this->head.isWCS()) stream << "COORD W" << endl; else stream << "COORD P" << endl; stream << std::setprecision(6); stream.setf(std::ios::fixed); double *pix = new double[3]; double *wld = new double[3]; std::vector::iterator obj; for(obj=this->objectList->begin();objobjectList->end();obj++){ if(this->par.getAnnotationType()=="borders"){ std::vector vertexSet = obj->getVertexSet(); for(size_t i=0;igetZcentre(); if(this->head.isWCS()){ this->head.pixToWCS(pix,wld); stream << "LINE " << wld[0] << " " << wld[1]; } else stream << "LINE " << pix[0] << " " << pix[1]; pix[0] = vertexSet[i*4+2]-0.5; pix[1] = vertexSet[i*4+3]-0.5; if(this->head.isWCS()){ this->head.pixToWCS(pix,wld); stream << " " << wld[0] << " " << wld[1] << "\n"; } else stream << " " << pix[0] << " " << pix[1] << "\n"; } } else if(this->par.getAnnotationType()=="circles"){ float radius = obj->getRAWidth()/120.; if(obj->getDecWidth()/120.>radius) radius = obj->getDecWidth()/120.; stream << "CIRCLE " << obj->getRA() << " " << obj->getDec() << " " << radius << "\n"; } stream << "TEXT " << obj->getRA() << " " << obj->getDec() << " " << obj->getID() << "\n\n"; } delete [] pix; delete [] wld; } void Cube::outputCatalogue() { this->setupColumns(); ASCIICatalogueWriter catWriter(this->par.getOutFile(),Catalogues::FILE); catWriter.setup(this); ASCIICatalogueWriter headerWriter(this->par.getHeaderFile(),Catalogues::FILE); headerWriter.setup(this); ASCIICatalogueWriter screenWriter(Catalogues::SCREEN); screenWriter.setup(this); ASCIICatalogueWriter *writer; // write the header information if(this->par.getFlagSeparateHeader()) writer = &headerWriter; else writer=&catWriter; writer->openCatalogue(); writer->writeHeader(); writer->writeParameters(); writer->writeStats(); if(this->par.getFlagSeparateHeader()){ writer->closeCatalogue(); writer = &catWriter; writer->openCatalogue(); } // write the catalogue writer->writeTableHeader(); writer->writeEntries(); writer->closeCatalogue(); screenWriter.openCatalogue(); screenWriter.writeTableHeader(); screenWriter.writeEntries(); screenWriter.closeCatalogue(); } void Cube::outputDetectionsVOTable() { VOTableCatalogueWriter writer(this->pars().getVOTFile()); writer.setup(this); writer.setTableName("Detections"); writer.setTableDescription("Detected sources and parameters from running the Duchamp source finder."); writer.openCatalogue(); writer.writeHeader(); writer.writeParameters(); writer.writeStats(); writer.writeTableHeader(); writer.writeEntries(); writer.writeFooter(); writer.closeCatalogue(); } void Cube::prepareOutputFile() { /// @details /// A function to write the paramters, time of execution, and /// statistical information to the output file. std::string outfile; if(this->par.getFlagSeparateHeader()) outfile = this->par.getHeaderFile(); else outfile = this->par.getOutFile(); std::ofstream output(outfile.c_str()); output<<"Results of the Duchamp source finder v."<showParam(output); output<<"--------------------\n"; output.close(); this->outputStats(); } void Cube::outputStats() { /// @details /// A function to write the statistical information to the output /// file. This writes the threshold, its equivalent S/N ratio, and /// the noise level and spread. /// /// If smoothing has been done, the noise level & spread for the /// original array are calculated and printed as well. std::string outfile; if(this->par.getFlagSeparateHeader()) outfile = this->par.getHeaderFile(); else outfile = this->par.getOutFile(); std::ofstream output(outfile.c_str(),std::ios::app); output<<"Summary of statistics:\n"; output<<"Detection threshold = " << this->Stats.getThreshold() <<" " << this->head.getFluxUnits(); if(this->par.getFlagFDR()) output<<" (or S/N=" << this->Stats.getThresholdSNR()<<")"; if(this->par.getFlagSmooth()){ output << " in smoothed cube."; if(!this->par.getFlagUserThreshold()) output<<"\nNoise level = " << this->Stats.getMiddle() <<", Noise spread = " << this->Stats.getSpread() <<" in smoothed cube."; // calculate the stats for the original array, so that we can // quote S/N values correctly. this->par.setFlagSmooth(false); bool verb=this->par.isVerbose(); bool fdrflag=this->par.getFlagFDR(); this->par.setVerbosity(false); this->par.setFlagFDR(false); this->setCubeStats(); this->par.setVerbosity(verb); this->par.setFlagFDR(fdrflag); this->par.setFlagSmooth(true); output << "\nNoise properties for the original cube are:"; } if(!this->par.getFlagUserThreshold()) output<<"\nNoise level = " << this->Stats.getMiddle() <<", Noise spread = " << this->Stats.getSpread(); if(this->par.getFlagGrowth()){ StatsContainer growthStats = this->Stats; if(this->par.getFlagUserGrowthThreshold()) growthStats.setThreshold(this->par.getGrowthThreshold()); else growthStats.setThresholdSNR(this->par.getGrowthCut()); growthStats.setUseFDR(false); output<<"\nDetections grown down to threshold of " << growthStats.getThreshold() << " " << this->head.getFluxUnits(); } if(!this->par.getFlagUserThreshold()) output << "\nFull stats:\n" << this->Stats; else output << "\n\nNot calculating full stats since threshold was provided directly.\n"; output<<"--------------------\n"; output.close(); } void Cube::prepareLogFile(int argc, char *argv[]) { /// @details /// Opens the log file so that it can be written to, and writes /// the parameter summary as well as the time of execution to the /// file. /// /// It also writes the command-line statement, hence the need for /// argv and argc. ASCIICatalogueWriter logwriter(this->par.getLogFile(),Catalogues::LOG); logwriter.setup(this); logwriter.openCatalogue(); logwriter.writeHeader(); logwriter.writeCommandLineEntry(argc,argv); logwriter.writeParameters(); logwriter.closeCatalogue(); } void Cube::logDetectionList(bool calcFluxes) { /// @details /// A front-end to writing a list of detected objects to the log file. /// Does not assume WCS is present. /// Designed to be used by searching routines before returning their /// final list. /// @param[in] calcFluxes If true (the default), calculate the various flux parameters for each object. if(this->objectList->size()>0){ ASCIICatalogueWriter logwriter(this->par.getLogFile(),Catalogues::LOG); logwriter.setup(this); logwriter.openCatalogue(std::ios::app); long left = this->par.getBorderLeft(); long bottom = this->par.getBorderBottom(); // std::ofstream fout(this->par.getLogFile().c_str(),std::ios::app); if(calcFluxes) this->calcObjectFluxes(); this->setupColumns(); // outputTableHeader(fout,this->fullCols,Catalogues::LOG,this->head.isWCS()); logwriter.writeTableHeader(); if(this->par.getFlagBaseline()){ for(size_t i=0;iaxisDim[0]*this->axisDim[1]*this->axisDim[2];i++) this->array[i] += this->baseline[i]; } for(size_t objCtr=0;objCtrobjectList->size();objCtr++){ Detection obj = this->objectList->at(objCtr); obj.setOffsets(par); if(this->par.getFlagCubeTrimmed()){ obj.addOffsets(left,bottom,0); } if(calcFluxes) obj.calcFluxes(this->array, this->axisDim); obj.setID(objCtr+1); // obj.printTableRow(fout,this->fullCols,Catalogues::LOG); logwriter.writeEntry(&obj); } if(this->par.getFlagBaseline()){ for(size_t i=0;iaxisDim[0]*this->axisDim[1]*this->axisDim[2];i++) this->array[i] -= this->baseline[i]; } // fout.close(); logwriter.closeCatalogue(); } } void Cube::logSummary() { ASCIICatalogueWriter logwriter(this->par.getLogFile(),Catalogues::LOG); logwriter.setup(this); logwriter.openCatalogue(std::ios::app); logwriter.writeCubeSummary(); logwriter.closeCatalogue(); } void Cube::writeSpectralData() { /// @details /// A function to write, in ascii form, the spectra of each /// detected object to a file. The file consists of a column for /// the spectral coordinates, and one column for each object /// showing the flux at that spectral position. The units are the /// same as those shown in the graphical output. The filename is /// given by the Param::spectraTextFile parameter in the Cube::par /// parameter set. const int zdim = this->axisDim[2]; const int numObj = this->objectList->size(); float *specxOut = new float[zdim]; float *spectra = new float[numObj*zdim]; for(int obj=0; objgetSpectralArrays(obj, specx, temp, recon, base); for(int z=0;zpar.getSpectraTextFile().c_str()); fspec.setf(std::ios::fixed); for(int z=0;z