EpnTapMgr.php
2.74 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
<?php
/** @class EpnTapMgr
* @brief Manager to communicates with EPN-TAP services.
*/
class EpnTapMgr {
private $logEpnTap;
private function addLog($msg)
{
// fwrite($this->logEpnTap, date("h:i:s A") . ": " . $msg . "\n");
error_log("INFO " . $msg);
// print "test";
}
private function dateToJD($gregorian_date) {
list($day, $month, $year, $hours, $minutes, $seconds) = preg_split('/[\s\/:]+/', $gregorian_date);
return GregorianToJD($month, $day, $year) + $hours/24 + $minutes/(24*60) + $seconds/(24*60*60);
}
public function JDTodate($jd) {
list($month, $day, $year) = preg_split('/[\s\/:]+/', JDToGregorian($jd));
$date = sprintf('%02d', $day) . '/' . sprintf('%02d', $month) . '/' . sprintf('%04d', $year);
return ($date == '00/00/0000') ? '' : $date;
}
public function createFilter($dataproduct_type, $target_name, $time_min, $time_max) {
$filter = array();
if($dataproduct_type)
array_push($filter, "dataproduct_type = '$dataproduct_type'");
if($target_name)
array_push($filter, "target_name = '$target_name'");
if($time_min)
array_push($filter, "time_min >= " . $this->dateToJD($time_min));
if($time_max)
array_push($filter, "time_max <= " . $this->dateToJD($time_max));
return (count($filter) > 0 ? ' WHERE ' . join(' AND ', $filter) : '');
}
public function request($access_url, $query) {
$votMgr = new VOTableMgr;
$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
$res = $votMgr->load($url);
$this->addLog("Query URL: " . $url);
$result = $votMgr->parseStream();
return $votMgr->getVotableError() ? array('error' => $votMgr->getVotableError()) : $result;
}
/* filter order: product type, target name, time min, time max */
public function getGranules($table_name, $access_url, $filter, $select, $limit, $offset) {
$query = "SELECT TOP {$limit} " . join(', ', $select) . " FROM {$table_name}.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]) . " OFFSET {$offset}";
// return $query;
$result = $this->request($access_url, $query);
if(! array_key_exists("error", $result)) {
for ($i = 0 ; $i < sizeof($result) ; $i++) {
$result[$i]['num'] = $i + $offset + 1;
$result[$i]['time_min'] = $this->JDTodate($result[$i]['time_min']);
$result[$i]['time_max'] = $this->JDTodate($result[$i]['time_max']);
}
}
return $result;
}
/* filter order: product type, target name, time min, time max */
public function getNbRows($table_name, $access_url, $filter) {
$query = "SELECT COUNT(*) AS nb_rows FROM {$table_name}.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]);
// return $query;
return $this->request($access_url, $query)[0]['nb_rows'];
}
}
?>