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

class EpnTapMgr {
	private $logEpnTap;

	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) {
		if(!is_numeric($jd)) {
			return '';
		}
		list($month, $day, $year) = preg_split('/[\s\/:]+/', JDToGregorian(intval($jd)));
		$date = sprintf('%04d', $year) . '/' . sprintf('%02d', $month) . '/' . sprintf('%02d', $day);
		return ($date == '0000/00/00') ? '' : $date;
	}

	public function createFilter($targetName, $productTypes, $timeMin, $timeMax) {
		$filter = array();
		if($targetName) {
			array_push($filter, "target_name = '$targetName'");
		}
		if($productTypes) {
			array_push($filter, "dataproduct_type IN ('" . join("', '", $productTypes) . "')");
		}
		if($timeMin) {
			array_push($filter, "time_min >= " . $this->dateToJD($timeMin));
		}
		if($timeMax) {
			array_push($filter, "time_max <= " . $this->dateToJD($timeMax));
		}
		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(preg_replace('/\s+/', ' ', $query)); // remove also multiple whitespaces

		$votMgr->load($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, $limit, $offset) {

		$query = "SELECT TOP {$limit} * FROM {$table_name} " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]) . " OFFSET {$offset}";
		$result = $this->request($access_url, $query);

		$result_path = EpnTapDataPath . "test_result.json";
		$servicesJsonFile = fopen($result_path, "w");

		if(array_key_exists("error", $result)) {
			return $result;
		}

		$non_empty_values = Array();
		for ($i = 0 ; $i < count($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']);

			foreach($result[$i] as $key => $value) {
				$non_empty_values[$key] = (array_key_exists($key, $non_empty_values) ? $non_empty_values[$key] : False) || $value === '' || $value === 'NAN';
			}
		}

		$non_null_cols = array_keys(array_filter($non_empty_values));
		for ($i = 0 ; $i < count($result) ; $i++) {
			$result[$i] = array_diff_key($result[$i], array_flip($non_null_cols));
		}
		// error_log('Query result: ' . json_encode($result));
		return $result;
	}

	public function getNbResults($url, $tableName, $targetName, $productTypes, $timeMin, $timeMax) {
		$query = "SELECT COUNT(*) AS nb_rows FROM $tableName" . $this->createFilter($targetName, $productTypes, $timeMin, $timeMax);
		// error_log('Query: ' . $query);
		$result = $this->request($url, $query)[0]['nb_rows'];
		// error_log('Query result: ' . json_encode($result));
		return $result;
	}

}
?>