source: trunk/Cubes/detectionIO.cc @ 20

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

Added a function to write a Karma annotation file. Needed new parameters and
function declaration, and added comments in the Guide as well.
Guide also had VOTable and results examples updated.

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