EpnTapMgr.php 2.08 KB
<?php
/**  @class EpnTapMgr
*	@brief Manager to communicates with EPN-TAP services.
*/

class EpnTapMgr {
	private $logEpnTap;

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

	private function addLog($msg)
	{
		fwrite($this->logEpnTap, date("h:i:s A") . ": " . $msg . "\n");
	}

	private function dateToJD($gregorian_date) {
		list($month, $day, $year) = split('[/.-]', $gregorian_date);
		return GregorianToJD($month, $day, $year);
	}

	public function JDTodate($jd) {
		list($month, $day, $year) = split('/', JDToGregorian($jd));
		return "$day/$month/$year";
	}

	public function createFilter($target_name, $dataproduct_type, $time_min, $time_max) {
		$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->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);

		$this->addLog("Query URL: " . $url);
		$votMgr->load($url);
		$result = $votMgr->isValidSchema() ? $votMgr->parseStream() : NULL;
		for ($i = 0 ; $i < sizeof($result) ; $i++) {
			$result[$i]['time_min'] = JDToGregorian($result[$i]['time_min']);//$this->JDTodate($result[$i]['time_min']);
			$result[$i]['time_max'] = $this->JDTodate($result[$i]['time_max']);
		}
		return $result;
	}

	/* filtre order: $target_name, $time_min, $time_max, $table_name, $access_url */
	public function getGranules($table_name, $access_url, $filter, $select) {
		$query = "SELECT " . join(', ', $select) . " FROM $table_name.epn_core" . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]);
		// return $query;
		return $this->request($access_url, $query);
	}
}
?>