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

class EpnTapMgr {

	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');
	}

	// dc.zah.uni-heidelberg.de/tap/sync?FORMAT=votable&LANG=ADQL&QUERY=SELECT DISTINCT table_name WHERE standard_id='ivo://ivoa.net/std/tap' AND intf_type='vs:paramhttp' AND detail_xpath='/capability/dataModel/@ivo-id' FROM&REQUEST=doQuery
	// select distinct target_name from xxx.epn_core

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

		$query = "SELECT COUNT(granule_uid) AS nb_results FROM $table_name"
				. ($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);
		return $votMgr->isValidSchema() ? $votMgr->getSimpleInteger() : 0;
	}

	static function getGranules($table_name, $access_url) {
		$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));

		$query = "SELECT dataproduct_type, target_name, time_min, time_max FROM $table_name"
				. ($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);
		if($votMgr->isValidSchema()) {
			return false;
		} else {
			return true;
		}
	}
}
?>