source: tags/release-0.9.2/Cubes/detectionIO.cc @ 1453

Last change on this file since 1453 was 86, checked in by Matthew Whiting, 18 years ago

Large commit, mostly removing dud commented code, and adding comments and
documentation to the existing code. No new features.

File size: 10.3 KB
Line 
1#include <iostream>
2#include <sstream>
3#include <fstream>
4#include <iomanip>
5#include <string>
6#include <time.h>
7#include <Cubes/cubes.hh>
8#include <Utils/utils.hh>
9
10using std::endl;
11using std::setw;
12using std::setprecision;
13
14void Cube::outputDetectionsKarma(std::ostream &stream)
15{
16  /**
17   * outputDetectionsKarma
18   *  Prints to a stream (provided) the list of detected objects in the cube
19   *   in the format of an annotation file for the Karma suite of programs.
20   *  Annotation file draws a box enclosing the detection, and writes the ID number
21   *   of the detection to the right of the box.
22   */
23
24  stream << "# Duchamp Source Finder results for cube " << this->par.getImageFile() << endl;
25  stream << "COLOR RED" << endl;
26  stream << "COORD W" << endl;
27  for(int i=0;i<this->objectList.size();i++){
28    float radius = this->objectList[i].getRAWidth()/120.;
29    if(this->objectList[i].getDecWidth()/120.>radius)
30      radius = this->objectList[i].getDecWidth()/120.;
31    stream << "CIRCLE "
32           << this->objectList[i].getRA() << " "
33           << this->objectList[i].getDec() << " "
34           << radius << endl;
35    stream << "TEXT "
36           << this->objectList[i].getRA() << " "
37           << this->objectList[i].getDec() << " "
38           << this->objectList[i].getID() << endl;
39  }
40
41}
42
43void Cube::outputDetectionsVOTable(std::ostream &stream)
44{
45  /**
46   * outputDetectionsVOTable
47   *  Prints to a stream (provided) the list of detected objects in the cube
48   *   in a VOTable format.
49   *  Uses WCS information and assumes WCS parameters have been calculated for each
50   *   detected object.
51   *  If they have not (given by the isWCS() function), then those objects are not written...
52   */
53
54  stream<<"<?xml version=\"1.0\"?>"<<endl;
55  stream<<"<VOTABLE version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""<<endl;
56  stream<<" xsi:noNamespaceSchemaLocation=\"http://www.ivoa.net/xml/VOTable/VOTable/v1.1\">"<<endl;
57
58  stream<<"  <COOSYS ID=\"J2000\" equinox=\"J2000.\" epoch=\"J2000.\" system=\"eq_FK5\"/>"<<endl;
59  stream<<"  <RESOURCE name=\"Duchamp Output\">"<<endl;
60  stream<<"    <TABLE name=\"Detections\">"<<endl;
61  stream<<"      <DESCRIPTION>Detected sources and parameters from running the Duchamp source finder.</DESCRIPTION>"<<endl;
62
63  // PARAM section -- parts that are not entry-specific ie. apply to whole dataset
64  string fname = this->pars().getImageFile();
65  if(this->pars().getFlagSubsection()) fname+=this->pars().getSubsection();
66  stream<<"      <PARAM name=\"FITS file\" datatype=\"char\" ucd=\"meta.file;meta.fits\" value=\"" << fname << "\"/>"<<endl;
67  if(this->pars().getFlagFDR())
68    stream<<"      <PARAM name=\"FDR Significance\" datatype=\"float\" ucd=\"stat.param\" value=\"" << this->pars().getAlpha() << "\">" << endl;
69  else
70    stream<<"      <PARAM name=\"Threshold\" datatype=\"float\" ucd=\"stat.snr\" value=\"" << this->pars().getCut() << "\">" << endl;
71  if(this->pars().getFlagATrous()){
72    stream<<"      <PARAM name=\"ATrous note\" datatype=\"char\" ucd=\"meta.note\" value=\"The a trous reconstruction method was used, with the following parameters.\">" << endl;
73    stream<<"      <PARAM name=\"ATrous Cut\" datatype=\"float\" ucd=\"stat.snr\" value=\"" << this->pars().getAtrousCut() << "\">" << endl;
74    stream<<"      <PARAM name=\"ATrous Minimum Scale\" datatype=\"int\" ucd=\"stat.param\" value=\"" << this->pars().getMinScale() << "\">" << endl;
75    stream<<"      <PARAM name=\"ATrous Filter\" datatype=\"char\" ucd=\"meta.code;stat\" value=\"" << this->pars().getFilterName() << "\">" << endl;
76  }
77  // FIELD section -- names, titles and info for each column.
78  stream<<"      <FIELD name=\"ID\" ID=\"col1\" ucd=\"meta.id\" datatype=\"int\" width=\"4\"/>"<<endl;
79  stream<<"      <FIELD name=\"Name\" ID=\"col2\" ucd=\"meta.id;meta.main\" datatype=\"char\" arraysize=\"14\"/>"<<endl;
80  stream<<"      <FIELD name=\"RA\" ID=\"col3\" ucd=\"pos.eq.ra;meta.main\" ref=\"J2000\" datatype=\"float\" width=\"10\" precision=\"6\" unit=\"deg\"/>"<<endl;
81  stream<<"      <FIELD name=\"Dec\" ID=\"col4\" ucd=\"pos.eq.dec;meta.main\" ref=\"J2000\" datatype=\"float\" width=\"10\" precision=\"6\" unit=\"deg\"/>"<<endl;
82  stream<<"      <FIELD name=\"w_RA\" ID=\"col3\" ucd=\"phys.angSize;pos.eq.ra\" ref=\"J2000\" datatype=\"float\" width=\"7\" precision=\"2\" unit=\"arcmin\"/>"<<endl;
83  stream<<"      <FIELD name=\"w_Dec\" ID=\"col4\" ucd=\"phys.angSize;pos.eq.dec\" ref=\"J2000\" datatype=\"float\" width=\"7\" precision=\"2\" unit=\"arcmin\"/>"<<endl;
84  stream<<"      <FIELD name=\"Vel\" ID=\"col4\" ucd=\"phys.veloc;src.dopplerVeloc\" datatype=\"float\" width=\"9\" precision=\"3\" unit=\"km/s\"/>"<<endl;
85  stream<<"      <FIELD name=\"w_Vel\" ID=\"col4\" ucd=\"phys.veloc;src.dopplerVeloc;spect.line.width\" datatype=\"float\" width=\"8\" precision=\"3\" unit=\"km/s\"/>"<<endl;
86  stream<<"      <FIELD name=\"Integrated_Flux\" ID=\"col4\" ucd=\"phys.flux;spect.line.intensity\" datatype=\"float\" width=\"10\" precision=\"4\" unit=\"km/s\"/>"<<endl;
87
88
89  stream<<"      <DATA>"<<endl
90        <<"        <TABLEDATA>"<<endl;
91
92  stream.setf(std::ios::fixed); 
93  for(int i=0;i<this->objectList.size();i++){
94    if(this->objectList[i].isWCS()){
95      stream<<"        <TR>"<<endl;
96      stream<<"          <TD>"<<setw(4)<<i+1<<"</TD>";
97      stream<<"<TD>" << setw(14) << this->objectList[i].getName()       <<"</TD>";
98      stream<<setprecision(6);                                     
99      stream<<"<TD>" << setw(10) << this->objectList[i].getRA()         <<"</TD>";
100      stream<<"<TD>" << setw(10) << this->objectList[i].getDec()        <<"</TD>";
101      stream<<setprecision(2);                                     
102      stream<<"<TD>" << setw(7)  << this->objectList[i].getRAWidth()    <<"</TD>";
103      stream<<"<TD>" << setw(7)  << this->objectList[i].getDecWidth()   <<"</TD>";
104      stream<<setprecision(3);                                     
105      stream<<"<TD>" << setw(9)  << this->objectList[i].getVel()        <<"</TD>";
106      stream<<"<TD>" << setw(8)  << this->objectList[i].getVelWidth()   <<"</TD>";
107      stream<<setprecision(3);
108      stream<<"<TD>" << setw(10) << this->objectList[i].getIntegFlux()  <<"</TD>";
109     
110      stream<<endl;
111      stream<<"        </TR>"<<endl;
112    }
113  }
114  stream<<"        </TABLEDATA>"<<endl;
115  stream<<"      </DATA>"<<endl;
116  stream<<"    </TABLE>"<<endl;
117  stream<<"  </RESOURCE>"<<endl;
118  stream<<"</VOTABLE>"<<endl;
119  resetiosflags(std::ios::fixed);
120 
121}
122
123
124void Cube::outputDetectionList()
125{
126  /**
127   * outputDetectionList
128   *  A front-end to writing the full list of detected objects to a results file and to cout.
129   *  Uses outputDetectionTextWCS for each objects.
130   *  Leaves the testing of whether the WCS parameters for each object have been calculated
131   *   to outputDetectionTextWCS.
132   */
133
134  int count=0;
135  int flag;
136  std::ofstream output(this->par.getOutFile().c_str());
137  output<<"Results of the Duchamp source finder: ";
138  time_t now = time(NULL);
139  output << asctime( localtime(&now) );
140  this->showParam(output);
141  output<<"Total number of detections = "<<this->objectList.size()<<endl;
142  output<<"--------------------"<<endl;
143  if(flag=wcsset(wcs)){
144    std::cerr<<"outputDetectionList():: WCSSET failed! Code="<<flag <<": "<<wcs_errmsg[flag]<<endl;
145  }
146  if(this->flagWCS){
147    outputDetectionTextWCSHeader(output,this->wcs);
148    outputDetectionTextWCSHeader(std::cout,this->wcs);
149  }
150  else{
151    outputDetectionTextHeader(output);
152    outputDetectionTextHeader(std::cout);
153  }
154  for(int i=0;i<this->objectList.size();i++){
155    if(this->flagWCS){
156      this->objectList[i].outputDetectionTextWCS(output);
157      this->objectList[i].outputDetectionTextWCS(std::cout);
158    }
159    else{
160      this->objectList[i].outputDetectionText(output,i+1);
161      this->objectList[i].outputDetectionText(std::cout,i+1);
162    }
163  }
164}
165
166void Cube::logDetectionList()
167{
168  /**
169   * logDetectionList
170   *  A front-end to writing a list of detected objects to the log file.
171   *  Does not assume WCS, so uses outputDetectionText.
172   *  Designed to be used by searching routines before returning their final list.
173   */
174
175  std::ofstream fout(this->par.getLogFile().c_str(),std::ios::app);
176  outputDetectionTextHeader(fout);
177  long pos;
178  bool baselineFlag = this->par.getFlagBaseline();
179  for(int objCtr=0;objCtr<this->objectList.size();objCtr++){
180    Detection *obj = new Detection;
181    *obj = objectList[objCtr];
182    if(this->par.getFlagCubeTrimmed()){
183      for(int pix=0;pix<obj->getSize();pix++){
184        // Need to correct the pixels first, as this hasn't been done yet.
185        // Corrections needed for positions (to account for trimmed region)
186        //  and for the baseline removal, if it has happened.
187        // Don't want to keep these changes, so just do it on a dummy variable.
188        pos = obj->getX(pix) + obj->getY(pix)*this->axisDim[0]
189          + obj->getZ(pix)*this->axisDim[0]*this->axisDim[1];
190        obj->setX(pix, obj->getX(pix) + this->par.getBorderLeft() );
191        obj->setY(pix, obj->getY(pix) + this->par.getBorderBottom() );
192        if(baselineFlag) obj->setF(pix, obj->getF(pix) + this->baseline[pos]);   // add in baseline
193      }
194      obj->calcParams();
195    }
196    obj->outputDetectionText(fout,objCtr);
197    delete obj;
198  }
199  fout.close();
200}
201
202void Cube::logDetection(Detection obj, int counter)
203{
204  /**
205   * logDetection
206   *  A front-end to writing a detected object to the log file.
207   *  Does not assume WCS, so uses outputDetectionText.
208   *  obj is passed by value, as we want to potentially change positions etc, but not for
209   *  object in the calling function.
210   *  Corrects for changes to positions of pixels and removal of baselines.
211   *  Designed to be used by searching routines before returning their final list.
212   */
213
214  std::ofstream fout(this->par.getLogFile().c_str(),std::ios::app);
215  bool baselineFlag = this->par.getFlagBaseline();
216  if(this->par.getFlagCubeTrimmed()){
217    for(int pix=0;pix<obj.getSize();pix++){
218      // Need to correct the pixels first, as this hasn't been done yet.
219      // Corrections needed for positions (to account for trimmed region)
220      //  and for the baseline removal, if it has happened.
221      // Don't want to keep these changes, so just do it on a dummy variable.
222      long pos = obj.getX(pix) + obj.getY(pix)*this->axisDim[0]
223        + obj.getZ(pix)*this->axisDim[0]*this->axisDim[1];
224      obj.setX(pix, obj.getX(pix) + this->par.getBorderLeft() );
225      obj.setY(pix, obj.getY(pix) + this->par.getBorderBottom() );
226      if(baselineFlag) obj.setF(pix, obj.getF(pix) + this->baseline[pos]);   // add in baseline
227    }
228    obj.calcParams();
229  }
230  obj.outputDetectionText(fout,counter);
231  fout.close();
232}
Note: See TracBrowser for help on using the repository browser.