APISMgr.php 4.55 KB
<?php
/**  
*     @class APISMgr 
*     @version $Id: APISMgr.php 2101 2014-02-19 10:12:21Z natacha $
*    
*/

  define('NB_ANSWER_LIMIT',2000);

  class APISMgr  
  {
  	
  	function __construct() 
  	{	 
  	}
  	
  	function jdayToUTC($jday)
  	{
  		 return round(($jday - 2440587.5) * 86400);
  	}

  	function jday($t) {
             
            return $t / 86400 + 2440587.5;
           
        }

  	function get($planet, $startTime, $stopTime, $datasets = null)
  	{
  		$planetNewName = ucfirst($planet);
  		list($y, $mo, $d, $h, $mi, $s) = sscanf($startTime,"%4d-%02d-%02dT%02d:%02d:%02d");
  		$start = mktime($h,$mi,$s,$mo,$d,$y);
  		
  		list($y, $mo, $d, $h, $mi, $s) = sscanf($stopTime,"%4d-%02d-%02dT%02d:%02d:%02d");
  		$stop = mktime($h,$mi,$s,$mo,$d,$y);
  		
  		$req  = "SELECT * FROM apis.epn_core";
  		$req .= " WHERE target_name='".$planetNewName."'";
  		$req .= " AND dataproduct_type='im'";
  		$req .= " AND time_min>".$this->jday($start);
  		$req .= " AND time_min<".$this->jday($stop);
  		
  		if (isset($datasets) && !in_array("all",$datasets))
  		{
  			$req .= " AND (";
  			$firstDatasetId = true;
  			foreach ($datasets as $dataset)
  			{
  				if (!$firstDatasetId)
  					$req .= " OR ";
  				$req .= "dataset_id ='".$dataset."'";
  				$firstDatasetId = false;
  			}
  			$req .= ")";
  		}
  		
  		$req .= " ORDER BY time_min, dataset_id";

  		$postfields = array(
                    'REQUEST' => 'doQuery',
                    'LANG' => 'ADQL',
                    'QUERY' => $req,
                    'TIMEOUT' => '30',
                    'FORMAT' => 'VOTable/td'
                );
  		
  		$curl = curl_init();
  		curl_setopt($curl, CURLOPT_URL, EPNTAP_APIS);
  		curl_setopt($curl, CURLOPT_COOKIESESSION, true);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
                $req_res = curl_exec($curl);
  		curl_close($curl);
  		
  		if (!$req_res)
  			 return array('success' => false, 'message' => 'Error during TAP request');
  		
  		$xml = new DOMDocument();
				$xml->preserveWhiteSpace = false;
				$xml->formatOutput = true;
				$xml->loadXML($req_res);
				
				$fields = $xml->getElementsByTagName('FIELD');
				
				$tabledatas = $xml->getElementsByTagName('TABLEDATA');
				
				$result = array();
				$crtDate = 0;
				$folderIndex = -1;
				
				//$log = fopen(USERWSDIR."log","w");
				
				//fprintf($log,$req_res);
				
				$limitReached = false;
				$lastDate = 0;
				foreach ($tabledatas as $tabledata)
				{
					$trs = $tabledata->getElementsByTagName('TR');
					
					$limitReached = ($trs->length >= NB_ANSWER_LIMIT);
					foreach($trs as $tr)
					{
						$data = array();
						$tds = $tr->getElementsByTagName('TD');
						//fprintf($log,"=> ---------\n");
						for ($i = 0; $i < $tds->length; $i++)
						{
							if ($fields->item($i)->getAttribute('ID') == 'time_min')
							{
								$time = $this->jdayToUTC($tds->item($i)->nodeValue);
								
								$time = $time - ($time%86400);
								
								$lastDate = $time;
								
								//fprintf($log,"=> %s\n",$time);
								
							 if ($time != $crtDate)
							 {
							 	$crtFolder = array('date' => date("Y-m-d H:i:s", $time), 'images' => array());
							 	$crtDate = $time;
							 	array_push($result,$crtFolder);
							 	$folderIndex++;
							 }
							}	
							if ($fields->item($i)->getAttribute('unit') == 'd')
							{
								$time = $this->jdayToUTC($tds->item($i)->nodeValue);
								$data[$fields->item($i)->getAttribute('ID')] = date("Y-m-d H:i:s", $time);
								//fprintf($log,"%s %s %s\n",$fields->item($i)->getAttribute('ID'),$tds->item($i)->nodeValue,$data[$fields->item($i)->getAttribute('ID')]);
							}
							else
							  $data[$fields->item($i)->getAttribute('ID')] = $tds->item($i)->nodeValue;
							//fprintf($log,"%s = %s\n",$fields->item($i)->getAttribute('ID'),$data[$fields->item($i)->getAttribute('ID')]);
						}
						$data['name'] = basename($data['access_url']);
						array_push($result[$folderIndex]['images'],$data);
							 
					}
				}
  			 
				$res=  array('success' => true, 'result' => $result);
				if ($limitReached)
					$res['warning'] = 'Result limit reached ('.NB_ANSWER_LIMIT.'). The last result date is '.date("Y-m-d",$lastDate);
				
				return $res;
  	}
  }
  
  /*define('EPNTAP_APIS','http://voparis-tap.obspm.fr/__system__/tap/run/tap/sync');
  $tapMgr = new APISMgr();
  $res = $tapMgr->get("saturn","1990-01-01T00:00:00","2015-01-01T00:00:00",array("all"));
  //var_dump($res);*/
		
?>