source: trunk/src/Cubes/VOTable.cc @ 500

Last change on this file since 500 was 484, checked in by MatthewWhiting, 16 years ago

Some documentation additions

File size: 14.5 KB
Line 
1// -----------------------------------------------------------------------
2// VOTable.cc: Output of the detected objects to a VOTable
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28
29#include <iostream>
30#include <sstream>
31#include <fstream>
32#include <iomanip>
33#include <string>
34#include <vector>
35#include <time.h>
36#include <duchamp/Cubes/VOTable.hh>
37#include <duchamp/param.hh>
38#include <duchamp/fitsHeader.hh>
39#include <duchamp/Cubes/cubes.hh>
40#include <duchamp/Detection/detection.hh>
41#include <duchamp/Detection/columns.hh>
42 
43namespace duchamp
44{
45
46  using namespace Column;
47  std::string fixUnitsVOT(std::string oldstring)
48  {
49    /**
50     * Fix a string containing units to make it acceptable for a VOTable.
51     *
52     * This function makes the provided units string acceptable
53     * according to the standard found at
54     * http://vizier.u-strasbg.fr/doc/catstd-3.2.htx
55     * This should then be able to convert the units used in the text
56     * table to units suitable for putting in a VOTable.
57     *
58     * Specifically, it removes any square brackets [] from the
59     * start/end of the string, and replaces blank spaces (representing
60     * multiplication) with a '.' (full stop).
61     *
62     * \param oldstring Input unit string to be fixed
63     * \return String with fixed units
64     */
65
66    std::string newstring;
67    for(int i=0;i<oldstring.size();i++){
68      if((oldstring[i]!='[')&&(oldstring[i]!=']')){
69        if(oldstring[i]==' ') newstring += '.';
70        else newstring += oldstring[i];
71      }
72    }
73    return newstring; 
74  }
75 
76
77  void VOField::define(std::string i, std::string n, std::string U, std::string u, std::string d, std::string r, int w, int p)
78  {
79    /**
80     * A basic definition function, defining each parameter individually.
81     * \param i The ID parameter
82     * \param n The name parameter
83     * \param U The UCD
84     * \param u The units (fixed by fixUnits())
85     * \param d The datatype
86     * \param r The ref
87     * \param w The width
88     * \param p The precision
89     */
90    this->ID = i;
91    this->name = n;
92    this->UCD = U;
93    this->unit = fixUnitsVOT(u);
94    this->datatype = d;
95    this->ref = r;
96    this->width = w;
97    this->precision = p;
98  }
99
100  void VOField::define(Column::Col column, std::string i, std::string U, std::string d, std::string r)
101  {
102    /**
103     * Another definition function, using the information in a Column::Col object for some parameters.
104     * \param column A Column::Col object, used for name, unit, width & precision
105     * \param i The ID parameter
106     * \param U The UCD
107     * \param d The datatype
108     * \param r The ref
109     */
110    this->ID = i;
111    this->name = column.getName();
112    this->UCD = U;
113    this->unit = fixUnitsVOT(column.getUnits());
114    this->datatype = d;
115    this->ref = r;
116    this->width = column.getWidth();
117    this->precision = column.getPrecision();
118  }
119
120  void VOField::define(Column::Col column)
121  {
122    /**
123     * A more useful definition function, using the Column::COLNAME
124     * key to specify particular values for each of the parameters for
125     * different columns.
126     * \param column A Column::Col object of a particular type. The
127     * column.getType() function is used to decide which call to
128     * VOField::define(Column::Col column, std::string i, std::string
129     * U, std::string d, std::string r) to use
130     */
131    switch(column.getType())
132      {
133      case NUM:
134        this->define(column,"col01","meta.id","int","");
135        break;
136      case NAME:
137        this->define(column,"col02","meta.id;meta.main","char","");
138        break;
139      case RAJD:
140        this->define(column,"col03","","float","J2000");
141        break;
142      case DECJD:
143        this->define(column,"col04","","float","J2000");
144        break;
145      case VEL:
146        this->define(column,"col05","phys.veloc;src.dopplerVeloc","float","");
147        break;
148      case WRA:
149        this->define(column,"col06","","float","J2000");
150        break;
151      case WDEC:
152        this->define(column,"col07","","float","J2000");
153        break;
154      case WVEL:
155        this->define(column,"col08","phys.veloc;src.dopplerVeloc;spect.line.width","float","");
156        break;
157      case FINT:
158        this->define(column,"col09","phot.flux;spect.line.intensity","float","");
159        this->name = "Integrated_Flux";
160        break;
161      case FTOT:
162        this->define(column,"col09","phot.flux;spect.line.intensity","float","");
163        this->name = "Total_Flux";
164        break;
165      case FPEAK:
166        this->define(column,"col10","phot.flux;spect.line.intensity","float","");
167        this->name = "Peak_Flux";
168        break;
169      case SNRPEAK:
170        this->define(column,"col11","phot.flux;stat.snr","float","");
171        break;
172      case FLAG:
173        this->define(column,"col12","meta.code.qual","char","");
174        break;
175      case XAV:
176        this->define(column,"col13","pos.cartesian.x","float","");
177        break;
178      case YAV:
179        this->define(column,"col14","pos.cartesian.y","float","");
180        break;
181      case ZAV:
182        this->define(column,"col15","pos.cartesian.z","float","");
183        break;
184      case XCENT:
185        this->define(column,"col16","pos.cartesian.x","float","");
186        this->name = "X_Centroid";
187        break;
188      case YCENT:
189        this->define(column,"col17","pos.cartesian.y","float","");
190        this->name = "Y_Centroid";
191        break;
192      case ZCENT:
193        this->define(column,"col18","pos.cartesian.z","float","");
194        this->name = "Z_Centroid";
195        break;
196      case XPEAK:
197        this->define(column,"col19","pos.cartesian.x","int","");
198        break;
199      case YPEAK:
200        this->define(column,"col20","pos.cartesian.y","int","");
201        break;
202      case ZPEAK:
203        this->define(column,"col21","pos.cartesian.z","int","");
204        break;
205      default:
206        break;
207      };
208  }
209
210  void VOField::printField(std::ostream &stream)
211  {
212    /**
213     * Print the Field entry with appropriate formatting.
214     * \param stream The output stream to send the text to.
215     */
216    stream << "<FIELD name=\"" <<this->name
217           << "\" ID=\"" << this->ID
218           << "\" ucd=\"" << this->UCD;
219    if(this->ref!="") stream << "\" ref=\"" << this->ref;
220    stream << "\" datatype=\"" << this->datatype;
221    stream << "\" unit=\"" << this->unit;
222    if(datatype=="char")
223      stream << "\" arraysize=\"" << this->width;
224    else{
225      stream << "\" width=\"" << this->width;
226      if(datatype=="float" || datatype=="double")
227        stream << "\" precision=\"" << this->precision;
228    }
229    stream << "\"/>\n";
230
231  }
232
233
234
235  //------------------------------------------------
236
237
238  template <class T> void VOParam::define(std::string n, std::string U, std::string d, T v, int w)
239  {
240    /**
241     * A basic definition function, defining each parameter
242     * individually. The value (v) is written to a stringstream, and
243     * from there stored as a string.
244     * \param n The name
245     * \param U The UCD
246     * \param d The datatype
247     * \param v The value
248     * \param w The width
249     */
250    this->name = n;
251    this->UCD = U;
252    this->datatype = d;
253    this->width = w;
254    std::stringstream ss;
255    ss << v;
256    this->value = ss.str();
257  }
258  template void VOParam::define<int>(std::string n, std::string U, std::string d, int v, int w);
259  template void VOParam::define<long>(std::string n, std::string U, std::string d, long v, int w);
260  template void VOParam::define<float>(std::string n, std::string U, std::string d, float v, int w);
261  template void VOParam::define<double>(std::string n, std::string U, std::string d, double v, int w);
262  template void VOParam::define<std::string>(std::string n, std::string U, std::string d, std::string v, int w);
263
264  void VOParam::printParam(std::ostream &stream)
265  {
266    /**
267     * Print the Param entry with appropriate formatting.
268     * \param stream The output stream to send the text to.
269     */
270    stream << "<PARAM name=\"" <<this->name
271           << "\" ucd=\"" << this->UCD
272           << "\" datatype=\"" << this->datatype;
273    if(this->width!=0){
274      if(datatype=="char")
275        stream << "\" arraysize=\"" << this->width;
276      else
277        stream << "\" width=\"" << this->width;
278    }
279    stream << "\" value=\"" << this->value
280           << "\"/>\n";
281  }
282
283  //------------------------------------------------
284
285
286 
287  void Cube::outputDetectionsVOTable(std::ostream &stream)
288  {
289    /**
290     *  Prints to a stream (provided) the list of detected objects in the cube
291     *   in a VOTable format.
292     *  Uses WCS information and assumes WCS parameters have been calculated for each
293     *   detected object.
294     * \param stream The output stream to send the text to.
295     */
296   
297    stream<<"<?xml version=\"1.0\"?>\n";
298    stream<<"<VOTABLE version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
299    stream<<" xsi:noNamespaceSchemaLocation=\"http://www.ivoa.net/xml/VOTable/VOTable/v1.1\">\n";
300
301    stream<<"  <COOSYS ID=\"J2000\" equinox=\"J2000.\" epoch=\"J2000.\" system=\"eq_FK5\"/>\n";
302    stream<<"  <RESOURCE name=\"Duchamp Output\">\n";
303    stream<<"    <TABLE name=\"Detections\">\n";
304    stream<<"      <DESCRIPTION>Detected sources and parameters from running the Duchamp source finder.</DESCRIPTION>\n";
305
306    // PARAM section -- parts that are not entry-specific ie. apply to whole dataset
307    std::vector<VOParam> paramList;
308    std::vector<VOParam>::iterator param;
309    VOParam singleParam;
310
311    std::string fname = this->par.getImageFile();
312    if(this->par.getFlagSubsection()) fname+=this->par.getSubsection();
313   
314    singleParam.define("FITS file","meta.file;meta.fits","char",fname,fname.size());
315    paramList.push_back(singleParam);
316    if(this->par.getFlagFDR())
317      singleParam.define("FDR Significance","stat.param","float",this->par.getAlpha(),0);
318    else
319      singleParam.define("Threshold","stat.snr","float",this->par.getCut(),0);
320    paramList.push_back(singleParam);
321   
322    if(this->par.getFlagATrous()){
323      std::string note = "The a trous reconstruction method was used, with the following parameters.";
324      singleParam.define("ATrous note","meta.note","char",note,note.size());
325      paramList.push_back(singleParam);
326      singleParam.define("ATrous Dimension","meta.code;stat","int",this->par.getReconDim(),0);
327      paramList.push_back(singleParam);
328      singleParam.define("ATrous Threshold","stat.snr","float",this->par.getAtrousCut(),0);
329      paramList.push_back(singleParam);
330      singleParam.define("ATrous Minimum Scale","stat.param","int",this->par.getMinScale(),0);
331      paramList.push_back(singleParam);
332      singleParam.define("ATrous Filter","meta.code;stat","char",this->par.getFilterName(),this->par.getFilterName().size());
333      paramList.push_back(singleParam);
334    }
335    if(this->par.getFlagSmooth()){
336      if(this->par.getSmoothType()=="spectral"){
337        std::string note = "The cube was smoothed spectrally with a Hanning filter, with the following parameters.";
338        singleParam.define("Smoothing note","meta.note","char",note,note.size());
339        paramList.push_back(singleParam);
340        singleParam.define("Hanning filter width","meta.code;stat","int",this->par.getHanningWidth(),0);
341        paramList.push_back(singleParam);
342      }
343      else if(this->par.getSmoothType()=="spatial"){
344        std::string note = "The cube was smoothed spatially with a Gaussian kernel, with the following parameters.";
345        singleParam.define("Smoothing note","meta.note","char",note,note.size());
346        paramList.push_back(singleParam);
347        singleParam.define("Gaussian kernel major-axis FWHM","meta.code;stat","int",this->par.getKernMaj(),0);
348        paramList.push_back(singleParam);
349        singleParam.define("Gaussian kernel minor-axis FWHM","meta.code;stat","int",this->par.getKernMin(),0);
350        paramList.push_back(singleParam);
351        singleParam.define("Gaussian kernel position angle","meta.code;stat","int",this->par.getKernPA(),0);
352        paramList.push_back(singleParam);
353      }   
354    }
355
356    for(param=paramList.begin();param<paramList.end();param++){
357      stream << "      ";
358      param->printParam(stream);
359    }   
360
361
362    if(this->fullCols.size()==0) this->setupColumns();
363    // in case cols haven't been set -- need the column names
364
365    std::string posUCD[4];
366    if(makelower(this->fullCols[Column::RAJD].getName())=="ra"){
367      posUCD[0] = "pos.eq.ra;meta.main";
368      posUCD[2] = "phys.angSize;pos.eq.ra";
369    }
370    else{
371      posUCD[0] = "pos.galactic.lat;meta.main";
372      posUCD[2] = "phys.angSize;pos.galactic.lat";
373    }
374    if(makelower(this->fullCols[DECJD].getName())=="dec"){
375      posUCD[1] = "pos.eq.dec;meta.main";
376      posUCD[3] = "phys.angSize;pos.eq.dec";
377    }
378    else{
379      posUCD[1] = "pos.galactic.lon;meta.main";
380      posUCD[3] = "phys.angSize;pos.galactic.lon";
381    }
382
383    std::vector<Column::Col>::iterator col;
384    for(col=this->fullCols.begin();col<this->fullCols.end();col++){
385
386      if(col->doCol("votable",this->head.isSpecOK())){
387        VOField field;
388        field.define(*col);
389        if(col->getType()==RAJD)  field.setUCD(posUCD[0]);
390        if(col->getType()==WRA)   field.setUCD(posUCD[1]);
391        if(col->getType()==DECJD) field.setUCD(posUCD[2]);
392        if(col->getType()==WDEC)  field.setUCD(posUCD[3]);     
393        stream << "      ";
394        field.printField(stream);
395      }
396
397    }
398
399    stream<<"      <DATA>\n"
400          <<"        <TABLEDATA>\n";
401
402    stream.setf(std::ios::fixed); 
403
404    std::vector<Detection>::iterator obj;
405    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
406
407      stream<<"        <TR>\n";
408      stream<<"          ";
409      std::vector<Column::Col>::iterator col;
410      for(col=this->fullCols.begin();col<this->fullCols.end();col++){
411        if(col->doCol("votable",this->head.isSpecOK())){
412          stream<<"<TD>";
413          obj->printTableEntry(stream, *col);
414          stream<<"</TD>";
415        }
416      }
417      stream<<"\n";
418      stream<<"        </TR>\n";
419
420    }
421
422    stream<<"        </TABLEDATA>\n";
423    stream<<"      </DATA>\n";
424    stream<<"    </TABLE>\n";
425    stream<<"  </RESOURCE>\n";
426    stream<<"</VOTABLE>\n";
427    resetiosflags(std::ios::fixed);
428 
429  }
430
431
432
433
434}
Note: See TracBrowser for help on using the repository browser.