Blame view

php/classes/EpnTapMgr.php 3.8 KB
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
1
2
3
4
5
6
<?php
/**  @class EpnTapMgr
*    @brief Manager to communicates with EPN-TAP services.
*/

class EpnTapMgr {
ee761f1a   Nathanael Jourdane   Make the VOTable ...
7
8
9
10
11
12
13
14
15
16
17
18
	private $logEpnTap;

	function __construct()
    {
    	$this->logEpnTap = fopen(USERDATADIR."logEpnTap", "a");
		$this->addLog("\n---\n");
    }

	function addLog($msg)
    {
  		fwrite($this->logEpnTap, date("h:i:s A") . $msg . "\n");
    }
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

	public function call($function_name, $args) {
		switch($function_name) {
			case 'getServiceNbResults':
				if(count($args) == 6)
					return $this->getServiceNbResults($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
				break;
			case 'getGranules':
				if(count($args) == 6)
					return $this->getGranules($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
				break;
			default:
				return array('success' => false, 'message' => $function_name.' is an unknown function.');
				break;
		}
	return array('success' => false, 'message' => 'The function do not have the required number of arguments');
	}

a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
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
	private function date2JD($date) {
		list($month, $day, $year) = split('[/.-]', $date);

		if($month == 1 || $month == 2) {
			$yearp = $year - 1;
			$monthp = $month + 12;
		} else {
			$yearp = $year;
			$monthp = $month;
		}

		# this checks where we are in relation to October 15, 1582, the beginning
		# of the Gregorian calendar.
		if (($year < 1582) ||
				($year == 1582 && $month < 10) ||
				($year == 1582 && $month == 10 && $day < 15))
			$j_day = 0;
		else
			$j_day = 2 - (int)($yearp / 100.0) + (int)((int)($yearp / 100.0) / 4.0);

		$j_day += $yearp < 0 ? (int)((365.25 * $yearp) - 0.75) : (int)(365.25 * $yearp);
		$j_day += (int)(30.6001 * ($monthp + 1)) + $day + 1720994.5;

		return $j_day;
	}

	private function getServiceNbResults($table_name, $access_url, $target_name, $dataproduct_type, $time_min, $time_max) {
ee761f1a   Nathanael Jourdane   Make the VOTable ...
64
		// $this->addLog("getServiceNbResults(" . $table_name .", ". $access_url .", ". $target_name .", ". $dataproduct_type .", ". $time_min .", ". $time_max . ")");
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
65
66
67
68
69
70
71
72
73
74
		$filter = array();
		if($target_name)
			array_push($filter, "target_name = '$target_name'");
		if($dataproduct_type)
			array_push($filter, "dataproduct_type = '$dataproduct_type'");
		if($time_min)
			array_push($filter, "time_min >= " . $this->date2JD($time_min));
		if($time_max)
			array_push($filter, "time_max >= " . $this->date2JD($time_max));

ee761f1a   Nathanael Jourdane   Make the VOTable ...
75
76
77
78
		$query = "SELECT COUNT(granule_uid) AS nb_results FROM $table_name.epn_core"
				. (count($filter) > 0 ? ' WHERE '.join(' AND ', $filter) : '');

		$this->addLog("query: " . $query);
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
79
80
81
82

		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
ee761f1a   Nathanael Jourdane   Make the VOTable ...
83
		$this->addLog("URL: " . $url);
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
84
		$votMgr->load($url);
ee761f1a   Nathanael Jourdane   Make the VOTable ...
85
86
		$res = $votMgr->isValidSchema() ? $votMgr->getStream() : "-";
		return $res["nb_results"];
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
87
88
	}

ee761f1a   Nathanael Jourdane   Make the VOTable ...
89
90
	private function getGranules($table_name, $access_url, $target_name, $dataproduct_type, $time_min, $time_max) {
		$this->addLog("getGranules(" . $table_name .", ". $access_url .", ". $target_name .", ". $dataproduct_type .", ". $time_min .", ". $time_max . ")");
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
91
92
93
94
95
96
97
98
99
100
		$filter = array();
		if($target_name)
			array_push($filter, "target_name = '$target_name'");
		if($dataproduct_type)
			array_push($filter, "dataproduct_type = '$dataproduct_type'");
		if($time_min)
			array_push($filter, "time_min >= " . $this->date2JD($time_min));
		if($time_max)
			array_push($filter, "time_max >= " . $this->date2JD($time_max));

ee761f1a   Nathanael Jourdane   Make the VOTable ...
101
		$query = "SELECT dataproduct_type, target_name, time_min, time_max FROM $table_name.epn_core"
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
102
103
104
105
106
107
				. ($filter.length > 0 ? ' WHERE ' . join(' AND ', $filter) : '');

		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
		$votMgr->load($url);
ee761f1a   Nathanael Jourdane   Make the VOTable ...
108
109
		$res = $votMgr->isValidSchema() ? $votMgr->getStream() : "-";
		return $res;
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
110
111
112
	}
}
?>