source: trunk/Cubes/detectionIO.cc @ 19

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

Tweaking dashed lines in output so things line up.

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