VOTableMgr.php
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
/**
* @class VOTableMgr
* @version $Id: VOTableMgr.php 2916 2015-05-19 13:08:33Z elena $
*/
//set DEBUG_MODE to TRUE to have some log information in user data dir
define("DEBUG_MODE",FALSE);
class VOTableMgr
{
public $xml = null;
private $log, $xp;
function __construct()
{
if (DEBUG_MODE)
$this->log = fopen(USERDATADIR."logVOTable","w");
}
function addLog($msg)
{
if (DEBUG_MODE)
fprintf($this->log,$msg);
}
function load($fileName)
{
$this->xml = new DomDocument("1.0");
if (!$this->xml->load($fileName))
{
$this->addLog("Cannot load file ".$fileName."\n");
return FALSE;
}
$this->checkIDAttribute();
/*if ($this->xml->namespaceURI == '')
{
$this->addLog("File don't have a namespace defined\n");
if (!$this->xml->createAttributeNS('http://www.ivoa.net/xml/VOTable/v1.1','xmlns'))
$this->addLog("Cannot create namespace attribute\n");
}
$this->addLog($this->xml->namespaceURI."\n");*/
$rootNamespace = $this->xml->lookupNamespaceUri($this->xml->namespaceURI);
$this->xp = new domxpath($this->xml);
$this->xp->registerNameSpace('x', $rootNamespace);
return TRUE;
}
function isValidSchema()
{
if (!$this->xml)
return FALSE;
//ToDo - BRE - add validation!!
return TRUE;
if (DEBUG_MODE)
libxml_use_internal_errors(true);
$vers = $this->getVersion();
$this->addLog("VOTable version : ".$vers."\n");
$result = FALSE;
switch ($vers)
{
case '1.2' :
$result = $this->xml->schemaValidate(XMLPATH.'VOTable-1.2.xsd');
case '1.0' :
$result = $this->xml->schemaValidate(XMLPATH.'VOTable-1.0.xsd');
default :
$result = $this->xml->schemaValidate(XMLPATH.'VOTable-1.1.xsd');
}
if (DEBUG_MODE)
{
$errors = libxml_get_errors();
foreach ($errors as $error)
{
$msg = '';
switch ($error->level)
{
case LIBXML_ERR_WARNING:
$msg .= ("WARNING ".$error->code.": ");
break;
case LIBXML_ERR_ERROR:
$msg .= ("ERROR ".$error->code.": ");
break;
case LIBXML_ERR_FATAL:
$msg .= ("FATAL ".$error->code.": ");
break;
}
$msg .= ($error->message." - In line : ".$error->line." - Of file : ".$error->file."\n");
$this->addLog($msg);
}
libxml_use_internal_errors(false);
}
return $result;
}
protected function queryResource()
{
return "//x:RESOURCE";
}
protected function queryTable()
{
return $this->queryResource()."/x:TABLE";
}
protected function queryDescription()
{
return $this->queryTable()."/x:DESCRIPTION";
}
protected function queryFields()
{
return $this->queryTable()."/x:FIELD";
}
protected function queryField($field_id)
{
return $this->queryFields()."[@ID='".$field_id."']";
}
protected function queryFieldByName($field_id)
{
return $this->queryFields()."[@name='".$field_id."']";
}
protected function queryFieldDescription($field_id)
{
return $this->queryField($field_id)."/x:DESCRIPTION";
}
protected function queryData()
{
return $this->queryTable()."/x:DATA";
}
protected function queryTableData()
{
return $this->queryData()."/x:TABLEDATA";
}
protected function queryTR()
{
return $this->queryTableData()."/x:TR";
}
//
public function getVersion()
{
if (!$this->xml)
return '';
$root = $this->xml->documentElement;
return $root->getAttribute('version');
}
public function getDescription()
{
if (!$this->xp)
return '';
$desc = $this->xp->query($this->queryDescription());
if ($desc->length < 1)
return '';
return $desc->item(0)->nodeValue;
}
public function isData()
{
$trs = $this->xml->getElementsByTagName('TR');
if ($trs->length > 0) return true;
return false;
}
public function getFirstTR()
{
if (!$this->xp)
return NULL;
/*$trs = $this->xp->query($this->queryTR());
if ($trs->length < 1)
return NULL;
return $trs->item(0);*/
$tabledatas = $this->xp->query($this->queryTableData());
if ($tabledatas->length < 1)
return NULL;
$tabledata = $tabledatas->item(0);
$node = $tabledata->firstChild;
while($node && ($node->nodeType != 1) && ($node->nodeName != "TR"))
$node = $node->nextSibling;
return $node;
}
public function getNextTR($tr)
{
if (!$this->xp)
return NULL;
while($tr->nextSibling && ($tr->nextSibling->nodeType != 1) && ($node->nodeName != "TR"))
$tr = $tr->nextSibling;
return $tr->nextSibling;
}
public function getTDValueByFieldIndex($tr,$field_index)
{
if (!$this->xp)
return NULL;
$tds = $tr->getElementsByTagName("TD");
if (($tds->length < 1) || ($field_index >= $tds->length))
return NULL;
return $tds->item($field_index)->nodeValue;
}
protected function isTimeField($field)
{
if (!$this->xp)
return FALSE;
return (($field->getAttribute("ucd") == "time.epoch") && ($field->getAttribute("xtype") == "dateTime"));
}
public function getTimeFieldIndex()
{
if (!$this->xp)
return -1;
$fields = $this->xp->query($this->queryFields());
if ($fields->length < 1)
return -1;
for ($i = 0; $i < $fields->length; $i++)
if ($this->isTimeField($fields->item($i)))
return $i;
return -1;
}
protected function getFieldByID($field_id)
{
if (!$this->xp)
return NULL;
$fields = $this->xp->query($this->queryFields());
if ($fields->length < 1)
return NULL;
foreach ($fields as $field)
if ($field->getAttribute("ID") == $field_id)
return $field;
return NULL;
}
protected function getFieldByName($field_id)
{
if (!$this->xp)
return NULL;
$fields = $this->xp->query($this->queryFieldByName($field_id));
if ($fields->length < 1)
return NULL;
foreach ($fields as $field)
if ($field->getAttribute("name") == $field_id)
return $field;
return NULL;
}
protected function checkIDAttribute()
{
$fields = $this->xml->getElementsByTagName('FIELD');
$i = 0;
foreach ($fields as $field)
{
$i++;
if (!$field->hasAttribute("ID"))
{
$field->setAttribute("ID", "col".$i);
}
}
$this->xml->saveXML();
}
public function getFieldIndexByID($field_id)
{
if (!$this->xp)
return -1;
$fields = $this->xp->query($this->queryFields());
if ($fields->length < 1)
return -1;
for ($i = 0; $i < $fields->length; $i++)
if ($fields->item($i)->getAttribute("ID") == $field_id)
return $i;
return -1;
}
public function getStartStop()
{
if (!$this->xp)
return '0 0';
$timeIndex = $this->getTimeFieldIndex();
if ($timeIndex < 0)
return '0 0';
$tr = $this->getFirstTR();
if (!$tr)
return '0 0';
$start = $this->getTDValueByFieldIndex($tr,$timeIndex);
$stop = $start;
while ($tr)
{
$stop = $this->getTDValueByFieldIndex($tr,$timeIndex);
$tr = $this->getNextTR($tr);
}
if (!$start)
$start = 0;
else
$start = strtotime($start);
if (!$stop)
$stop = 0;
else
$stop = strtotime($stop);
return $start." ".$stop;
}
public function getFieldInfoByID($field_id)
{
if (!$this->xp)
return array("id" => $field_id, "error" => "No file loaded");
$field = $this->getFieldByID($field_id);
if (!$field)
$field = $this->getFieldByName($field_id);
if (!$field)
return array("id" => $field_id, "error" => "This field doesn't exist");
return $this->getFieldInfo($field);
}
public function getFieldInfo($field)
{
if (!$this->xp)
return array("id" => $field_id, "error" => "No file loaded");
$description = '';
$desc = $field->getElementsByTagName("DESCRIPTION");
if ($desc->length >= 1)
$description = $desc->item(0)->nodeValue;
$size = $field->getAttribute("arraysize");
if ($size == '')
$size = 1;
else
$size = intval($size);
switch ($field->getAttribute("datatype"))
{
case "short" :
$type = "SHORT";
break;
case "int" :
$type = "INTEGER";
break;
case "long" :
case "double" :
$type = "DOUBLE";
break;
default :
$type = "FLOAT";
}
if (!$field->getAttribute("ID"))
$id = "col".$n;
else
$id = $field->getAttribute("ID");
return array("id" => $field->getAttribute("ID"),
"type" => $type,
"name" => $field->getAttribute("name"),
"ucd" => $field->getAttribute("ucd"),
"unit" => $field->getAttribute("unit"),
"size" => $size,
"description" => $description
);
}
public function getFieldsInfo()
{
if (!$this->xp)
return array("error" => "No file loaded");
$fields_info = array();
$fields = $this->xp->query($this->queryFields());
if ($fields->length < 1)
return $fields_info;
foreach ($fields as $field)
{
if ($this->isTimeField($field))
continue;
array_push($fields_info,$this->getFieldInfo($field));
}
return $fields_info;
}
public function getSamplings()
{
if (!$this->xp)
return array("minSampling" => 0,
"maxSampling" => 0);
$timeIndex = $this->getTimeFieldIndex();
if ($timeIndex < 0)
return array("minSampling" => 0,
"maxSampling" => 0);
$tr = $this->getFirstTR();
if (!$tr)
return array("minSampling" => 0,
"maxSampling" => 0);
$prevTime = 0;
while ($tr)
{
$time = $this->getTDValueByFieldIndex($tr,$timeIndex);
if ($time)
{
$time = strtotime($time);
if (($prevTime > 0) && ($time-$prevTime > 0))
$deltaT[$time-$prevTime]++;
$prevTime = $time;
}
$tr = $this->getNextTR($tr);
}
$minSampling = +1.e31;
$maxSampling = 0.0;
foreach ($deltaT as $key => $value)
{
if ($value/count($deltaT) < 0.10)
continue;
if ($key < $minSampling) $minSampling = $key;
if ($key > $maxSampling) $maxSampling = $key;
}
return array("minSampling" => $minSampling,
"maxSampling" => $maxSampling);
}
/*
* Add vector data made from components to IMPEX VOT
*/
public function addVectorToVot($paramID, $fileName)
{
$argsArr = explode(',', $paramID);
$fields = $this->xml->getElementsByTagName('FIELD');
$table = $this->xml->getElementsByTagName('TABLE')->item(0);
$data = $this->xml->getElementsByTagName('DATA')->item(0);
$i=0;
$find = false;
foreach ($fields as $field)
{
if ($field->getAttribute('name') == $argsArr[0])
{
$unit = $field->getAttribute('unit');
$ucd = $field->getAttribute('ucd');
$datatype = $field->getAttribute('datatype');
$firstTD = $i;
$find = true;
break;
}
$i++;
}
if ($find)
{
$new_field = $this->xml->createElement('FIELD');
$new_field->setAttribute('ID', $paramID);
$new_field->setAttribute('name', $paramID);
$new_field->setAttribute('datatype', $datatype);
$new_field->setAttribute('arraysize', '3');
$new_field->setAttribute('unit', $unit);
$new_field->setAttribute('ucd', $ucd);
$colN = $fields->length + 1;
$new_field->setAttribute('ID', 'col'.$colN);
$table->insertBefore($new_field,$data);
$trs = $this->xml->getElementsByTagName('TR');
foreach($trs as $tr)
{
$tds = $tr->getElementsByTagName('TD');
$value = trim($tds->item($firstTD)->nodeValue).' '.trim($tds->item($firstTD + 1)->nodeValue).' '.trim($tds->item($firstTD + 2)->nodeValue);
$td = $this->xml->createElement('TD', $value);
$tr->appendChild($td);
}
$this->xml->save($fileName);
}
}
}
?>