source: trunk/Cubes/detectionIO.cc @ 53

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

Added code to the spectral-plotting routine that allows plotting of the spectra
of WCS-less data -- the spectra are plotted as a function of z-pixel.
The information returned is just the pixel and flux information, both as the
spectrum header and the results printed to screen and to the output file.
The main file was changed so that it does not do the spectral plotting only if
there is no z-dimension, or there are no objects found.

File size: 9.4 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    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  stream<<"      <PARAM name=\"FITS file\" datatype=\"char\" ucd=\"meta.file;meta.fits\" value=\"" << this->par.getImageFile() << "\"/>"<<endl;
65
66  // FIELD section -- names, titles and info for each column.
67  stream<<"      <FIELD name=\"ID\" ID=\"col1\" ucd=\"meta.id\" datatype=\"int\" width=\"4\"/>"<<endl;
68  stream<<"      <FIELD name=\"Name\" ID=\"col2\" ucd=\"meta.id;meta.main\" datatype=\"char\" arraysize=\"14\"/>"<<endl;
69  stream<<"      <FIELD name=\"RA\" ID=\"col3\" ucd=\"pos.eq.ra;meta.main\" ref=\"J2000\" datatype=\"float\" width=\"10\" precision=\"6\" unit=\"deg\"/>"<<endl;
70  stream<<"      <FIELD name=\"Dec\" ID=\"col4\" ucd=\"pos.eq.dec;meta.main\" ref=\"J2000\" datatype=\"float\" width=\"10\" precision=\"6\" unit=\"deg\"/>"<<endl;
71  stream<<"      <FIELD name=\"w_RA\" ID=\"col3\" ucd=\"phys.angSize;pos.eq.ra\" ref=\"J2000\" datatype=\"float\" width=\"7\" precision=\"2\" unit=\"arcmin\"/>"<<endl;
72  stream<<"      <FIELD name=\"w_Dec\" ID=\"col4\" ucd=\"phys.angSize;pos.eq.dec\" ref=\"J2000\" datatype=\"float\" width=\"7\" precision=\"2\" unit=\"arcmin\"/>"<<endl;
73  stream<<"      <FIELD name=\"Vel\" ID=\"col4\" ucd=\"phys.veloc;src.dopplerVeloc\" datatype=\"float\" width=\"9\" precision=\"3\" unit=\"km/s\"/>"<<endl;
74  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;
75  stream<<"      <FIELD name=\"Integrated_Flux\" ID=\"col4\" ucd=\"phys.flux;spect.line.intensity\" datatype=\"float\" width=\"10\" precision=\"4\" unit=\"km/s\"/>"<<endl;
76
77
78  stream<<"      <DATA>"<<endl
79        <<"        <TABLEDATA>"<<endl;
80
81  stream.setf(std::ios::fixed); 
82  for(int i=0;i<this->objectList.size();i++){
83    if(this->objectList[i].isWCS()){
84      stream<<"        <TR>"<<endl;
85      stream<<"          <TD>"<<setw(4)<<i+1<<"</TD>";
86      stream<<"<TD>" << setw(14) << this->objectList[i].getName()       <<"</TD>";
87      stream<<setprecision(6);                                     
88      stream<<"<TD>" << setw(10) << this->objectList[i].getRA()         <<"</TD>";
89      stream<<"<TD>" << setw(10) << this->objectList[i].getDec()        <<"</TD>";
90      stream<<setprecision(2);                                     
91      stream<<"<TD>" << setw(7)  << this->objectList[i].getRAWidth()    <<"</TD>";
92      stream<<"<TD>" << setw(7)  << this->objectList[i].getDecWidth()   <<"</TD>";
93      stream<<setprecision(3);                                     
94      stream<<"<TD>" << setw(9)  << this->objectList[i].getVel()        <<"</TD>";
95      stream<<"<TD>" << setw(8)  << this->objectList[i].getVelWidth()   <<"</TD>";
96      stream<<setprecision(3);
97      stream<<"<TD>" << setw(10) << this->objectList[i].getIntegFlux()  <<"</TD>";
98     
99      stream<<endl;
100      stream<<"        </TR>"<<endl;
101    }
102  }
103  stream<<"        </TABLEDATA>"<<endl;
104  stream<<"      </DATA>"<<endl;
105  stream<<"    </TABLE>"<<endl;
106  stream<<"  </RESOURCE>"<<endl;
107  stream<<"</VOTABLE>"<<endl;
108  resetiosflags(std::ios::fixed);
109 
110}
111
112
113void Cube::outputDetectionList()
114{
115  /**
116   * outputDetectionList
117   *  A front-end to writing the full list of detected objects to a results file and to cout.
118   *  Uses outputDetectionTextWCS for each objects.
119   *  Leaves the testing of whether the WCS parameters for each object have been calculated
120   *   to outputDetectionTextWCS.
121   */
122
123  int count=0;
124  int flag;
125  std::ofstream output(this->par.getOutFile().c_str());
126  output<<"Results of the Duchamp source finder: ";
127  output.close();
128  string syscall = "date >> " + this->par.getOutFile();
129  system(syscall.c_str());
130  output.open(this->par.getOutFile().c_str(),std::ios::app);
131  this->showParam(output);
132  output<<"Total number of detections = "<<this->objectList.size()<<endl;
133  output<<"--------------------"<<endl;
134  if(flag=wcsset(wcs)){
135    std::cerr<<"outputDetectionList():: WCSSET failed! Code="<<flag <<": "<<wcs_errmsg[flag]<<endl;
136  }
137  if(this->flagWCS){
138    outputDetectionTextWCSHeader(output,this->wcs);
139    outputDetectionTextWCSHeader(std::cout,this->wcs);
140  }
141  else{
142    outputDetectionTextHeader(output);
143    outputDetectionTextHeader(std::cout);
144  }
145  for(int i=0;i<this->objectList.size();i++){
146    if(this->flagWCS){
147      this->objectList[i].outputDetectionTextWCS(output);
148      this->objectList[i].outputDetectionTextWCS(std::cout);
149    }
150    else{
151      this->objectList[i].outputDetectionText(output,i+1);
152      this->objectList[i].outputDetectionText(std::cout,i+1);
153    }
154  }
155}
156
157void Cube::logDetectionList()
158{
159  /**
160   * logDetectionList
161   *  A front-end to writing a list of detected objects to the log file.
162   *  Does not assume WCS, so uses outputDetectionText.
163   *  Designed to be used by searching routines before returning their final list.
164   */
165
166  std::ofstream fout(this->par.getLogFile().c_str(),std::ios::app);
167  outputDetectionTextHeader(fout);
168  long pos;
169  bool baselineFlag = this->par.getFlagBaseline();
170  for(int objCtr=0;objCtr<this->objectList.size();objCtr++){
171    Detection *obj = new Detection;
172    *obj = objectList[objCtr];
173    if(this->par.getFlagCubeTrimmed()){
174      for(int pix=0;pix<obj->getSize();pix++){
175        // Need to correct the pixels first, as this hasn't been done yet.
176        // Corrections needed for positions (to account for trimmed region)
177        //  and for the baseline removal, if it has happened.
178        // Don't want to keep these changes, so just do it on a dummy variable.
179        pos = obj->getX(pix) + obj->getY(pix)*this->axisDim[0]
180          + obj->getZ(pix)*this->axisDim[0]*this->axisDim[1];
181        obj->setX(pix, obj->getX(pix) + this->par.getBorderLeft() );
182        obj->setY(pix, obj->getY(pix) + this->par.getBorderBottom() );
183        if(baselineFlag) obj->setF(pix, obj->getF(pix) + this->baseline[pos]);   // add in baseline
184      }
185      obj->calcParams();
186    }
187    obj->outputDetectionText(fout,objCtr);
188    delete obj;
189  }
190  fout.close();
191}
192
193void Cube::logDetection(Detection obj, int counter)
194{
195  /**
196   * logDetection
197   *  A front-end to writing a detected object to the log file.
198   *  Does not assume WCS, so uses outputDetectionText.
199   *  obj is passed by value, as we want to potentially change positions etc, but not for
200   *  object in the calling function.
201   *  Corrects for changes to positions of pixels and removal of baselines.
202   *  Designed to be used by searching routines before returning their final list.
203   */
204
205  std::ofstream fout(this->par.getLogFile().c_str(),std::ios::app);
206  bool baselineFlag = this->par.getFlagBaseline();
207  if(this->par.getFlagCubeTrimmed()){
208    for(int pix=0;pix<obj.getSize();pix++){
209      // Need to correct the pixels first, as this hasn't been done yet.
210      // Corrections needed for positions (to account for trimmed region)
211      //  and for the baseline removal, if it has happened.
212      // Don't want to keep these changes, so just do it on a dummy variable.
213      long pos = obj.getX(pix) + obj.getY(pix)*this->axisDim[0]
214        + obj.getZ(pix)*this->axisDim[0]*this->axisDim[1];
215      obj.setX(pix, obj.getX(pix) + this->par.getBorderLeft() );
216      obj.setY(pix, obj.getY(pix) + this->par.getBorderBottom() );
217      if(baselineFlag) obj.setF(pix, obj.getF(pix) + this->baseline[pos]);   // add in baseline
218    }
219    obj.calcParams();
220  }
221  obj.outputDetectionText(fout,counter);
222  fout.close();
223}
Note: See TracBrowser for help on using the repository browser.