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

Last change on this file since 912 was 912, checked in by MatthewWhiting, 12 years ago

First lot of changes aimed at addressing #135 - moving VOParam to a separate file, and changing the interface so that we define it using the constructor.

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