GammaLib  2.0.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GVOTable.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GVOTable.cpp - VO table class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2015-2016 by Thierry Louge *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GVOTable.cpp
23  * @brief VO table class implementation
24  * @author Thierry Louge
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GVOTable.hpp"
32 #include "GXmlElement.hpp"
33 #include "GFitsCfitsio.hpp"
34 #include "GFitsTable.hpp"
35 #include "GFitsTableCol.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 
39 /* __ Macros _____________________________________________________________ */
40 
41 /* __ Coding definitions _________________________________________________ */
42 
43 /* __ Debug definitions __________________________________________________ */
44 
45 
46 /*==========================================================================
47  = =
48  = Constructors/destructors =
49  = =
50  ==========================================================================*/
51 
52 /***********************************************************************//**
53  * @brief Void constructor
54  ***************************************************************************/
56 {
57  // Initialise members
58  init_members();
59 
60  // Return
61  return;
62 }
63 
64 
65 /***********************************************************************//**
66  * @brief FITS table constructor
67  *
68  * @param[in] table FITS table
69  ***************************************************************************/
71 {
72  // Initialise members
73  init_members();
74 
75  // Read VO table from FITS table
76  read(table);
77 
78  // Return
79  return;
80 }
81 
82 
83 /***********************************************************************//**
84  * @brief Copy constructor
85  *
86  * @param[in] votable VO table
87  ***************************************************************************/
89 {
90  // Initialise members
91  init_members();
92 
93  // Copy members
94  copy_members(votable);
95 
96  // Return
97  return;
98 }
99 
100 
101 /***********************************************************************//**
102  * @brief Destructor
103  ***************************************************************************/
105 {
106  // Free members
107  free_members();
108 
109  // Return
110  return;
111 }
112 
113 
114 /*==========================================================================
115  = =
116  = Operators =
117  = =
118  ==========================================================================*/
119 
120 /***********************************************************************//**
121  * @brief Assignment operator
122  *
123  * @param[in] votable VO table.
124  * @return VO table.
125  ***************************************************************************/
127 {
128  // Execute only if object is not identical
129  if (this != &votable) {
130 
131  // Free members
132  free_members();
133 
134  // Initialise members
135  init_members();
136 
137  // Copy members
138  copy_members(votable);
139 
140  } // endif: object was not identical
141 
142  // Return
143  return *this;
144 }
145 
146 
147 /*==========================================================================
148  = =
149  = Public methods =
150  = =
151  ==========================================================================*/
152 
153 /***********************************************************************//**
154  * @brief Clear object.
155  *
156  * Reset object to a clean initial state.
157  ***************************************************************************/
158 void GVOTable::clear(void)
159 {
160  // Free members
161  free_members();
162 
163  // Initialise members
164  init_members();
165 
166  // Return
167  return;
168 }
169 
170 
171 /***********************************************************************//**
172  * @brief Clone object
173  ***************************************************************************/
175 {
176  // Clone client
177  return new GVOTable(*this);
178 }
179 
180 
181 /***********************************************************************//**
182  * @brief Read VO table from FITS table
183  ***************************************************************************/
184 void GVOTable::read(const GFitsTable& table)
185 {
186  // Clear VO table
187  m_xml.clear();
188 
189  // Set name and resource string
190  m_name = table.extname();
191  m_resource = table.classname();
192 
193  // Set VOTABLE element string
194  std::string s1("VOTABLE version=\"1.3\" xmlns:xsi=\"http://www.w3.org/"
195  "2001/XMLSchema-instance\" xmlns=\"http://www.ivoa.net/"
196  "xml/VOTable/v1.3\" xmlns:stc=\"http://www.ivoa.net/xml/"
197  "STC/v1.30\"");
198  std::string s2("RESOURCE name=\""+m_resource+"\"");
199  std::string s3("TABLE name=\""+m_name+"\"");
200 
201  // Create table element
202  GXmlElement* votable = m_xml.append(s1)->append(s2)->append(s3);
203 
204  // Append description
205  votable->append("DESCRIPTION")->append(GXmlText(m_description));
206 
207  // Append FIELD elements by extracting the information from the FITS
208  // columns
209  for (int i = 0; i < table.ncols(); ++i) {
210  votable->append(field_from_fits_column(*table[i]));
211  }
212 
213  // Append data
214  votable->append(data_from_fits_table(table));
215 
216  // Return
217  return;
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Print VO Table information
223  *
224  * @param[in] chatter Chattiness.
225  * @return String containing VO table information.
226  ***************************************************************************/
227 std::string GVOTable::print(const GChatter& chatter) const
228 {
229  // Initialise result string
230  std::string result;
231 
232  // Continue only if chatter is not silent
233  if (chatter != SILENT) {
234 
235  // Append VO table
236  result.append("=== GVOTable ===");
237  result.append(m_xml.print(chatter, 0));
238 
239  } // endif: chatter was not silent
240 
241  // Return result
242  return result;
243 }
244 
245 
246 /*==========================================================================
247  = =
248  = Private methods =
249  = =
250  ==========================================================================*/
251 
252 /***********************************************************************//**
253  * @brief Initialise class members
254  ***************************************************************************/
256 {
257  // Initialise members
258  m_xml.clear();
259  m_name.clear();
260  m_resource.clear();
261  m_description.clear();
262 
263  // Return
264  return;
265 }
266 
267 
268 /***********************************************************************//**
269  * @brief Copy class members
270  *
271  * @param[in] table VO table.
272  ***************************************************************************/
274 {
275  // Copy members
276  m_xml = table.m_xml;
277  m_name = table.m_name;
278  m_resource = table.m_resource;
280 
281  // Return
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Delete class members
288  ***************************************************************************/
290 {
291  // Return
292  return;
293 }
294 
295 
296 /***********************************************************************//**
297  * @brief Return FIELD element with column description
298  *
299  * @param[in] column FITS column
300  * @return XML FIELD element with column description
301  ***************************************************************************/
303 {
304  // Create FIELD element
305  GXmlElement field("FIELD");
306 
307  // Set datatype
308  std::string datatype;
309  switch (column.type()) {
310  case __TLOGICAL:
311  datatype = "boolean";
312  break;
313  case __TBIT:
314  datatype = "bit";
315  break;
316  case __TBYTE:
317  case __TSBYTE:
318  datatype = "unsignedByte";
319  break;
320  case __TSHORT:
321  case __TUSHORT:
322  datatype = "short";
323  break;
324  case __TINT:
325  case __TUINT:
326  datatype = "int";
327  break;
328  case __TLONG:
329  case __TULONG:
330  case __TLONGLONG:
331  datatype = "long";
332  break;
333  case __TSTRING:
334  datatype = "char";
335  break;
336  case __TFLOAT:
337  datatype = "float";
338  break;
339  case __TDOUBLE:
340  datatype = "double";
341  break;
342  case __TCOMPLEX:
343  datatype = "floatComplex";
344  break;
345  case __TDBLCOMPLEX:
346  datatype = "doubleComplex";
347  break;
348  default:
349  datatype = "unknown";
350  break;
351  }
352 
353  // Set FIELD attributes
354  field.attribute("name", column.name());
355  field.attribute("ID", "col"+gammalib::str(column.colnum()+1));
356  field.attribute("datatype", datatype);
357  field.attribute("unit", column.unit());
358 
359  // Return XML FIELD element
360  return field;
361 }
362 
363 
364 /***********************************************************************//**
365  * @brief Return DATA element with FITS table data
366  *
367  * @param[in] table FITS table
368  * @return XML DATA element with FITS data
369  ***************************************************************************/
371 {
372  // Create DATA element and append TABLEDATA element
373  GXmlElement data("DATA");
374  GXmlElement* tabledata = data.append("TABLEDATA");
375 
376  // Loop over all rows
377  for (int row = 0; row < table.nrows(); ++row) {
378 
379  // Create TR element
380  GXmlElement tr("TR");
381 
382  // Append all column data
383  for (int col = 0; col < table.ncols(); ++col) {
384 
385  // Create TD element
386  GXmlElement td("TD");
387 
388  // Set column data as text of TD element
389  td.append(GXmlText(table[col]->string(row)));
390 
391  // Append TD element to TR element
392  tr.append(td);
393 
394  } // endfor: looped over columns
395 
396  // Append TR element to TABLEDATA
397  tabledata->append(tr);
398 
399  } // endfor: looped over rows
400 
401  // Return XML DATA element
402  return data;
403 }
#define __TINT
void unit(const std::string &unit)
Set column unit.
const int & ncols(void) const
Return number of columns in table.
Definition: GFitsTable.hpp:134
#define __TUSHORT
GVOTable(void)
Void constructor.
Definition: GVOTable.cpp:55
#define __TULONG
#define __TLOGICAL
XML element node class interface definition.
std::string print(const GChatter &chatter=NORMAL) const
Print XML object.
Definition: GXml.cpp:669
XML element node class.
Definition: GXmlElement.hpp:47
GXmlElement field_from_fits_column(const GFitsTableCol &column) const
Return FIELD element with column description.
Definition: GVOTable.cpp:302
GVOTable & operator=(const GVOTable &votable)
Assignment operator.
Definition: GVOTable.cpp:126
std::string m_description
VO table description.
Definition: GVOTable.hpp:88
#define __TDOUBLE
void clear(void)
Clear XML object.
Definition: GXml.cpp:232
FITS table column abstract base class definition.
#define __TDBLCOMPLEX
#define __TBYTE
std::string m_resource
VO resource name.
Definition: GVOTable.hpp:87
virtual std::string classname(void) const =0
Return class name.
void init_members(void)
Initialise class members.
Definition: GVOTable.cpp:255
virtual ~GVOTable(void)
Destructor.
Definition: GVOTable.cpp:104
#define __TBIT
#define __TCOMPLEX
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void read(const GFitsTable &table)
Read VO table from FITS table.
Definition: GVOTable.cpp:184
#define __TSHORT
CFITSIO interface header.
Abstract interface for FITS table column.
XML text node class.
Definition: GXmlText.hpp:43
#define __TUINT
Abstract interface for FITS table.
Definition: GFitsTable.hpp:44
void type(const int &type)
Set type code.
GChatter
Definition: GTypemaps.hpp:33
void free_members(void)
Delete class members.
Definition: GVOTable.cpp:289
#define __TLONG
void copy_members(const GVOTable &table)
Copy class members.
Definition: GVOTable.cpp:273
GXml m_xml
VO table.
Definition: GVOTable.hpp:85
const std::string & extname(void) const
Return extension name.
Definition: GFitsHDU.hpp:162
#define __TFLOAT
GVOTable * clone(void) const
Clone object.
Definition: GVOTable.cpp:174
const int & nrows(void) const
Return number of rows in table.
Definition: GFitsTable.hpp:119
void colnum(const int &colnum)
Set column number.
#define __TLONGLONG
std::string m_name
VO table name.
Definition: GVOTable.hpp:86
std::string print(const GChatter &chatter=NORMAL) const
Print VO Table information.
Definition: GVOTable.cpp:227
GXmlElement data_from_fits_table(const GFitsTable &table) const
Return DATA element with FITS table data.
Definition: GVOTable.cpp:370
#define __TSBYTE
VO table class definition.
void clear(void)
Clear object.
Definition: GVOTable.cpp:158
virtual GXmlNode * append(const GXmlNode &node)
Append XML child node.
Definition: GXmlNode.cpp:284
VOTable class.
Definition: GVOTable.hpp:55
void name(const std::string &name)
Set column name.
#define __TSTRING
GXmlNode * append(const GXmlNode &node)
Append child node to XML document root.
Definition: GXml.cpp:279
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:415
FITS table abstract base class interface definition.