Blame view

php/classes/VOTableMgr.php 16.4 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php
/**
 * @class VOTableMgr
f534c4a8   Nathanael Jourdane   Make the VOTable ...
4
 * @version $Id: VOTableMgr.php 2916 2015-05-19 13:08:33Z elena $
16035364   Benjamin Renard   First commit
5
6
 */

16035364   Benjamin Renard   First commit
7
//set DEBUG_MODE to TRUE to have some log information in user data dir
f534c4a8   Nathanael Jourdane   Make the VOTable ...
8
define("DEBUG_MODE", TRUE);
16035364   Benjamin Renard   First commit
9
10
11


class VOTableMgr {
5de62950   Nathanael Jourdane   Improve the VOTab...
12
13
14
15
16
17
18
19
20
21
	private $xml, $xp;
	private $logVotable; // The log file.
	private $stream; // The stream in the VOTable
	private $c; // Current character position on the stream

	function __construct()
	{
		if (DEBUG_MODE)
			$this->logVotable = fopen(LOG_DIR . "VOTable.log", "a");
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
22

5de62950   Nathanael Jourdane   Improve the VOTab...
23
24
25
26
27
	function addLog($msg)
	{
		 if (DEBUG_MODE)
		fwrite($this->logVotable, date("h:i:s A") . ": " . $msg . "\n");
	}
16035364   Benjamin Renard   First commit
28

f534c4a8   Nathanael Jourdane   Make the VOTable ...
29
30
31
32
33
34
35
36
37
38
39
	function load($fileName) {
		$this->addLog("File name" . $fileName);

		// see http://php.net/manual/en/domdocument.load.php#91384
		$opts = array(
			'http' => array('user_agent' => 'PHP libxml agent')
		);
		$context = stream_context_create($opts);
		libxml_set_streams_context($context);

		$this->xml = new DomDocument();
5de62950   Nathanael Jourdane   Improve the VOTab...
40
41
42
43
44
		$res = $this->xml->load($fileName);

		if(!$res) {
			$this->addLog("Can not load VOTAble.");
			return false;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		}

		$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);

		$this->addLog($fileName . " loaded.");
		return true;
	}

5de62950   Nathanael Jourdane   Improve the VOTab...
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
	function isValidSchema()
	{
		if (!$this->xml)
			return FALSE;

		$infos = $this->xp->query($this->queryResourceInfo());
		foreach($infos as $info) {
			if($info->getAttribute('value') == 'ERROR') {
				$this->addLog("There is an error on the VOTable: " . $info->textContent);
				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 queryResourceInfo()
	{
		return $this->queryResource()."/x:INFO";
	}

	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";
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
183
184
185
186
187
188
189
190
191
192

	protected function queryBinaryData() {
		return $this->queryData()."/x:BINARY";
	}

	protected function queryStream() {
		return $this->queryBinaryData()."/x:STREAM";
	}


5de62950   Nathanael Jourdane   Improve the VOTab...
193
194
195
196
197
	public function getVersion()
	{
		 if (!$this->xml)
		 		return '';
		 $root = $this->xml->documentElement;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
198

5de62950   Nathanael Jourdane   Improve the VOTab...
199
200
		 return $root->getAttribute('version');
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
201

5de62950   Nathanael Jourdane   Improve the VOTab...
202
203
204
205
	public function getDescription()
	{
		 if (!$this->xp)
			 return '';
f534c4a8   Nathanael Jourdane   Make the VOTable ...
206

5de62950   Nathanael Jourdane   Improve the VOTab...
207
		 $desc = $this->xp->query($this->queryDescription());
f534c4a8   Nathanael Jourdane   Make the VOTable ...
208

5de62950   Nathanael Jourdane   Improve the VOTab...
209
210
		 if ($desc->length < 1)
			 return '';
f534c4a8   Nathanael Jourdane   Make the VOTable ...
211

5de62950   Nathanael Jourdane   Improve the VOTab...
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
		 return $desc->item(0)->nodeValue;
	}

	private function get_block_size($data_type) {
		switch($data_type) {
			case 'boolean':
			case 'unsignedByte':
			case 'char':
				$block_size = 1;
				break;
			case 'unicodeChar':
			case 'short':
				$block_size = 2;
				break;
			case 'int':
			case 'float':
				$block_size = 4;
				break;
			case 'long':
			case 'double':
			case 'float_complex':
				$block_size = 8;
				break;
			case 'double_complex':
				$block_size = 8;
				break;
			default:
				$block_size = 0;
				break;
		}
		return $block_size;
	}

	private function get_row_size($field_node) {
		$datatype = $field_node->getAttribute("datatype");
		$block_size = $this->get_block_size($datatype);
		if($field_node->getAttribute("arraysize") == NULL) {
			$array_size = $block_size;
		} else if("*" == $field_node->getAttribute("arraysize")) {
			// echo "substr: " . json_encode(substr($this->stream, $this->c, 4)) . "\n";
			$array_size = unpack("Ns", substr($this->stream, $this->c, 4))["s"] * $block_size;
			// echo "array_size: $array_size\n";
			$this->c+=4;
		} else {
			$array_size = (int)($field_node->getAttribute("arraysize")) * $block_size;
		}
		return $array_size;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
260
261

	/** Get the VOTable stream content.*/
5de62950   Nathanael Jourdane   Improve the VOTab...
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
	public function parseStream() {
		$data = Array();
		$fields = $this->xp->query($this->queryFields());
		$nb_columns = $fields->length;
		$row = Array();
		$n_value = 0; // index of current value
		$this->c = 0; // initialize cursor position.
		$query_stream = $this->xp->query($this->queryStream())->item(0);
		if($query_stream == NULL)
			return NULL;
		$this->stream = base64_decode($query_stream->textContent);
		// echo "stream: " . json_encode($this->stream) . "\n";
		$stream_len = strlen($this->stream);
		if($stream_len == 0)
			return NULL;
		while($this->c < strlen($this->stream)) {
			$col_id = $n_value % $nb_columns;
			$field_node = $fields[$col_id];

			// echo "c=$this->c, n_value=$n_value, nb_columns=$nb_columns, col_id=$col_id, column_name=" . $field_node->getAttribute("ID") . "\n";

			if($col_id == 0) {
				$row = Array();
			}
			$row[$field_node->getAttribute("ID")] = $this->process_datablock($field_node);
			if($col_id == $nb_columns-1) {
				array_push($data, $row);
f534c4a8   Nathanael Jourdane   Make the VOTable ...
289
			}
5de62950   Nathanael Jourdane   Improve the VOTab...
290
291
			$n_value+=1;
			// echo "Char pos: $this->c\n";
f534c4a8   Nathanael Jourdane   Make the VOTable ...
292
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
293
294
		return $data;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
295

5de62950   Nathanael Jourdane   Improve the VOTab...
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
	private function process_datablock($field_node) {
		$data_type = $field_node->getAttribute("datatype");
		$row_size = $this->get_row_size($field_node);
		// echo "datatype: " . $field_node->getAttribute("datatype") . "\n";
		// echo "Row size: $row_size\n";
		switch ($data_type) {
			case 'boolean':
			case 'unsignedByte':
				$res = $this->stream[$this->c] == "T" || $this->stream[$this->c] == "t" || $this->stream[$this->c] == "1";
				break;
			case 'char':
			case 'unicodeChar':
				$res = $row_size !=0 ? substr($this->stream, $this->c, $row_size) : NULL;
				break;
			case 'short':
				$res = unpack("ss", substr($this->stream, $this->c, 2))["s"];
				break;
			case 'int':
				$res = unpack("Ns", substr($this->stream, $this->c, 4))["s"];
				break;
			case 'long':
				$res = unpack("Js", substr($this->stream, $this->c, 8))["s"];  // /!\ J -> PHP 5.6 only
				break;
			case 'float':
				$res = unpack("fs", substr($this->stream, $this->c, 4))["s"];
				break;
			case 'double':
				$res = unpack("ds", substr($this->stream, $this->c, 8))["s"];
				break;
			default:
				$res = NULL;
				$this->addLog("Unknown character: $data_type");
				break;
		}
		$this->c+=$row_size;
		// echo $field_node->getAttribute("ID") . "($data_type $row_size) = " . json_encode($res) . "\n";
		return $res;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
333
334
	}

f534c4a8   Nathanael Jourdane   Make the VOTable ...
335

5de62950   Nathanael Jourdane   Improve the VOTab...
336
337
338
339
	public function getFirstTR()
	{
		 if (!$this->xp)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
340

5de62950   Nathanael Jourdane   Improve the VOTab...
341
		 /*$trs = $this->xp->query($this->queryTR());
f534c4a8   Nathanael Jourdane   Make the VOTable ...
342

5de62950   Nathanael Jourdane   Improve the VOTab...
343
344
		 if ($trs->length < 1)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
345

5de62950   Nathanael Jourdane   Improve the VOTab...
346
		 return $trs->item(0);*/
f534c4a8   Nathanael Jourdane   Make the VOTable ...
347

5de62950   Nathanael Jourdane   Improve the VOTab...
348
349
350
		$tabledatas = $this->xp->query($this->queryTableData());
		 if ($tabledatas->length < 1)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
351

5de62950   Nathanael Jourdane   Improve the VOTab...
352
		 $tabledata = $tabledatas->item(0);
f534c4a8   Nathanael Jourdane   Make the VOTable ...
353

5de62950   Nathanael Jourdane   Improve the VOTab...
354
		 $node = $tabledata->firstChild;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
355

5de62950   Nathanael Jourdane   Improve the VOTab...
356
357
		 while($node && ($node->nodeType != 1) && ($node->nodeName != "TR"))
			$node = $node->nextSibling;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
358

5de62950   Nathanael Jourdane   Improve the VOTab...
359
360
361
362
363
364
365
		return $node;
	}

	public function getNextTR($tr)
	{
		 if (!$this->xp)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
366

5de62950   Nathanael Jourdane   Improve the VOTab...
367
368
		while($tr->nextSibling && ($tr->nextSibling->nodeType != 1) && ($node->nodeName != "TR"))
			$tr = $tr->nextSibling;
16035364   Benjamin Renard   First commit
369
				return $tr->nextSibling;
5de62950   Nathanael Jourdane   Improve the VOTab...
370
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
371

5de62950   Nathanael Jourdane   Improve the VOTab...
372
373
374
375
	public function getTDValueByFieldIndex($tr,$field_index)
	{
		 if (!$this->xp)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
376

5de62950   Nathanael Jourdane   Improve the VOTab...
377
		 $tds = $tr->getElementsByTagName("TD");
f534c4a8   Nathanael Jourdane   Make the VOTable ...
378

5de62950   Nathanael Jourdane   Improve the VOTab...
379
380
		 if (($tds->length < 1) || ($field_index >= $tds->length))
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
381

5de62950   Nathanael Jourdane   Improve the VOTab...
382
383
		 return $tds->item($field_index)->nodeValue;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
384

5de62950   Nathanael Jourdane   Improve the VOTab...
385
386
387
388
	protected function isTimeField($field)
	{
		if (!$this->xp)
			 return FALSE;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
389

5de62950   Nathanael Jourdane   Improve the VOTab...
390
391
		 return (($field->getAttribute("ucd") == "time.epoch") && ($field->getAttribute("xtype") == "dateTime"));
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
392

5de62950   Nathanael Jourdane   Improve the VOTab...
393
394
395
396
	public function getTimeFieldIndex()
	{
		 if (!$this->xp)
			 return -1;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
397

5de62950   Nathanael Jourdane   Improve the VOTab...
398
		 $fields = $this->xp->query($this->queryFields());
f534c4a8   Nathanael Jourdane   Make the VOTable ...
399

5de62950   Nathanael Jourdane   Improve the VOTab...
400
401
		 if ($fields->length < 1)
			 return -1;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
402

5de62950   Nathanael Jourdane   Improve the VOTab...
403
404
405
		 for ($i = 0; $i < $fields->length; $i++)
			 if ($this->isTimeField($fields->item($i)))
				 return $i;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
406

5de62950   Nathanael Jourdane   Improve the VOTab...
407
408
		 return -1;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
409

5de62950   Nathanael Jourdane   Improve the VOTab...
410
411
412
413
	protected function getFieldByID($field_id)
	{
		 if (!$this->xp)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
414

5de62950   Nathanael Jourdane   Improve the VOTab...
415
		 $fields = $this->xp->query($this->queryFields());
f534c4a8   Nathanael Jourdane   Make the VOTable ...
416

5de62950   Nathanael Jourdane   Improve the VOTab...
417
418
		 if ($fields->length < 1)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
419

5de62950   Nathanael Jourdane   Improve the VOTab...
420
421
422
		 foreach ($fields as $field)
			 if ($field->getAttribute("ID") == $field_id)
				 return $field;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
423

5de62950   Nathanael Jourdane   Improve the VOTab...
424
425
		 return NULL;
	}
16035364   Benjamin Renard   First commit
426

5de62950   Nathanael Jourdane   Improve the VOTab...
427
428
429
430
		protected function getFieldByName($field_id)
	{
		 if (!$this->xp)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
431

5de62950   Nathanael Jourdane   Improve the VOTab...
432
		 $fields = $this->xp->query($this->queryFieldByName($field_id));
f534c4a8   Nathanael Jourdane   Make the VOTable ...
433

5de62950   Nathanael Jourdane   Improve the VOTab...
434
435
		 if ($fields->length < 1)
			 return NULL;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
436

5de62950   Nathanael Jourdane   Improve the VOTab...
437
438
439
		 foreach ($fields as $field)
			 if ($field->getAttribute("name") == $field_id)
				 return $field;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
440

5de62950   Nathanael Jourdane   Improve the VOTab...
441
442
		 return NULL;
	}
16035364   Benjamin Renard   First commit
443

5de62950   Nathanael Jourdane   Improve the VOTab...
444
	protected function checkIDAttribute(){
16035364   Benjamin Renard   First commit
445

5de62950   Nathanael Jourdane   Improve the VOTab...
446
447
448
			$fields = $this->xml->getElementsByTagName('FIELD');
			$i = 0;
			foreach ($fields as $field){
16035364   Benjamin Renard   First commit
449
450
	$i++;
	if (!$field->hasAttribute("ID")){
5de62950   Nathanael Jourdane   Improve the VOTab...
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
			$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";
		}
16035364   Benjamin Renard   First commit
560
	if (!$field->getAttribute("ID"))
5de62950   Nathanael Jourdane   Improve the VOTab...
561
		$id = "col".$n;
16035364   Benjamin Renard   First commit
562
	else $id = $field->getAttribute("ID");
f534c4a8   Nathanael Jourdane   Make the VOTable ...
563

5de62950   Nathanael Jourdane   Improve the VOTab...
564
565
566
567
568
569
570
571
572
		 return array("id"		 => $field->getAttribute("ID"),
								 "type"				=> $type,
								 "name"				=> $field->getAttribute("name"),
								 "ucd"				 => $field->getAttribute("ucd"),
								 "unit"				=> $field->getAttribute("unit"),
								 "size"				=> $size,
								 "description" => $description
								);
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
573

5de62950   Nathanael Jourdane   Improve the VOTab...
574
575
576
577
	public function getFieldsInfo()
	{
	 	 if (!$this->xp)
			 return array("error" => "No file loaded");
f534c4a8   Nathanael Jourdane   Make the VOTable ...
578

5de62950   Nathanael Jourdane   Improve the VOTab...
579
		$fields_info = array();
f534c4a8   Nathanael Jourdane   Make the VOTable ...
580

5de62950   Nathanael Jourdane   Improve the VOTab...
581
		 $fields = $this->xp->query($this->queryFields());
f534c4a8   Nathanael Jourdane   Make the VOTable ...
582

5de62950   Nathanael Jourdane   Improve the VOTab...
583
584
		 if ($fields->length < 1)
			 return $fields_info;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
585

5de62950   Nathanael Jourdane   Improve the VOTab...
586
587
588
589
		foreach ($fields as $field)
		{
		 	if ($this->isTimeField($field))
				continue;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
590

5de62950   Nathanael Jourdane   Improve the VOTab...
591
592
			array_push($fields_info,$this->getFieldInfo($field));
		}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
593

5de62950   Nathanael Jourdane   Improve the VOTab...
594
595
		return $fields_info;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
596

5de62950   Nathanael Jourdane   Improve the VOTab...
597
598
599
600
601
	public function getSamplings()
	{
		 if (!$this->xp)
			 return array("minSampling" => 0,
									 "maxSampling" => 0);
f534c4a8   Nathanael Jourdane   Make the VOTable ...
602

5de62950   Nathanael Jourdane   Improve the VOTab...
603
604
605
606
		 $timeIndex = $this->getTimeFieldIndex();
		if ($timeIndex < 0)
			return array("minSampling" => 0,
									 "maxSampling" => 0);
16035364   Benjamin Renard   First commit
607

5de62950   Nathanael Jourdane   Improve the VOTab...
608
		$tr = $this->getFirstTR();
16035364   Benjamin Renard   First commit
609

5de62950   Nathanael Jourdane   Improve the VOTab...
610
611
612
		if (!$tr)
			return array("minSampling" => 0,
									 "maxSampling" => 0);
f534c4a8   Nathanael Jourdane   Make the VOTable ...
613

5de62950   Nathanael Jourdane   Improve the VOTab...
614
615
616
617
618
619
620
621
622
623
624
625
		$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;
			}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
626

5de62950   Nathanael Jourdane   Improve the VOTab...
627
628
			$tr = $this->getNextTR($tr);
		}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
629

5de62950   Nathanael Jourdane   Improve the VOTab...
630
631
		$minSampling = +1.e31;
		$maxSampling = 0.0;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
632

5de62950   Nathanael Jourdane   Improve the VOTab...
633
634
		foreach ($deltaT as $key => $value)
		{
f534c4a8   Nathanael Jourdane   Make the VOTable ...
635

5de62950   Nathanael Jourdane   Improve the VOTab...
636
637
		 if ($value/count($deltaT) < 0.10)
				continue;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
638

5de62950   Nathanael Jourdane   Improve the VOTab...
639
640
641
			if ($key < $minSampling) $minSampling = $key;
			if ($key > $maxSampling) $maxSampling = $key;
		}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
642

5de62950   Nathanael Jourdane   Improve the VOTab...
643
644
645
		return array("minSampling" => $minSampling,
								 "maxSampling" => $maxSampling);
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
646

5de62950   Nathanael Jourdane   Improve the VOTab...
647
	public function args2vector($file, $paramID){
16035364   Benjamin Renard   First commit
648

5de62950   Nathanael Jourdane   Improve the VOTab...
649
	$argsArr = explode('_', $paramID);
16035364   Benjamin Renard   First commit
650

5de62950   Nathanael Jourdane   Improve the VOTab...
651
652
653
654
655
656
657
	$dom = new DOMDocument();
	$dom->load($file);
	$fields = $dom->getElementsByTagName('FIELD');
	$table = $dom->getElementsByTagName('TABLE')->item(0);
	$i=0;
	foreach ($fields as $field){
			if ($field->getAttribute('name') == $argsArr[0]){
16035364   Benjamin Renard   First commit
658
	$unit = $field->getAttribute('unit');
5de62950   Nathanael Jourdane   Improve the VOTab...
659
660
	$ucd	= $field->getAttribute('ucd');
	$datatype	= $field->getAttribute('datatype');
16035364   Benjamin Renard   First commit
661
	$firstTD = $i;
5de62950   Nathanael Jourdane   Improve the VOTab...
662
663
664
665
666
667
668
669
670
671
		}
		$i++;
	}
	if ($firstTD > 0){
		$table->removeChild($fields->item($firstTD + 2));
		$table->removeChild($fields->item($firstTD + 1));
		$table->removeChild($fields->item($firstTD));

		$i = 0;
		foreach ($fields as $field){
16035364   Benjamin Renard   First commit
672
	$i++;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
673
	if (strpos($field->getAttribute('ID'),'col') !== FALSE){
5de62950   Nathanael Jourdane   Improve the VOTab...
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
			$field->setAttribute('ID', 'col'.$i);
			$dom->saveXML();
	}
		}

		$group = $dom->createElement('GROUP');
		$group->appendChild(new DOMAttr('ID', 'info_'.$paramID));
		$table->appendChild($group);

		$param = $dom->createElement('PARAM');
		$param->appendChild(new DOMAttr('ID', 'components_'.$paramID));
		$param->appendChild(new DOMAttr('name', 'components_'.$paramID));
		$param->appendChild(new DOMAttr('datatype', 'char'));
		$param->appendChild(new DOMAttr('arraysize', '*'));
		$param->appendChild(new DOMAttr('value', $argsArr[0].' '.$argsArr[1].' '.$argsArr[2] ));
		$group->appendChild($param);

		$new_field = $dom->createElement('FIELD');
		$new_field->appendChild(new DOMAttr('ID', $paramID));
		$new_field->appendChild(new DOMAttr('name', $paramID));
		$new_field->appendChild(new DOMAttr('datatype', $datatype));
		$new_field->appendChild(new DOMAttr('arraysize', '3'));
		$new_field->appendChild(new DOMAttr('unit', $unit));
		$new_field->appendChild(new DOMAttr('ucd', $ucd));
		$new_field->appendChild(new DOMAttr('ref', 'info_'.$paramID));
		$table->appendChild($new_field);

		$trs = $dom->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);
			$toRemote	= $tds->item($firstTD);
			$tr->removeChild($toRemote);
			 $toRemote = $tds->item($firstTD);
			$tr->removeChild($toRemote);
			 $toRemote = $tds->item($firstTD);
			$tr->removeChild($toRemote);

			$td = $dom->createElement('TD', $value);
			$tr->appendChild($td);
		}

		$dom->save($file);
		}
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
719

16035364   Benjamin Renard   First commit
720
721
722
723
}



f534c4a8   Nathanael Jourdane   Make the VOTable ...
724
?>