Blame view

php/classes/APISMgr.php 4.62 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
<?php
/**  
*     @class APISMgr 
*     @version $Id: APISMgr.php 2101 2014-02-19 10:12:21Z natacha $
*    
*/

b7796276   Benjamin Renard   Migration to supp...
8
9
  define('NB_ANSWER_LIMIT',2000);

16035364   Benjamin Renard   First commit
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  class APISMgr  
  {
  	
  	function __construct() 
  	{	 
  	}
  	
  	function jdayToUTC($jday)
  	{
  		 return round(($jday - 2440587.5) * 86400);
  	}

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

b7796276   Benjamin Renard   Migration to supp...
28
  	function get($planet, $startTime, $stopTime, $datasets = null)
16035364   Benjamin Renard   First commit
29
  	{
91eef29d   Benjamin Renard   Fix APIS client
30
        $planetNewName = ucfirst($planet);
16035364   Benjamin Renard   First commit
31
32
33
34
35
36
37
38
39
  		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'";
91eef29d   Benjamin Renard   Fix APIS client
40
41
  		$req .= " AND access_format='image/fits'";
        $req .= " AND time_min>".$this->jday($start);
b7796276   Benjamin Renard   Migration to supp...
42
  		$req .= " AND time_min<".$this->jday($stop);
91eef29d   Benjamin Renard   Fix APIS client
43
44

        if (isset($datasets) && !in_array("all",$datasets))
b7796276   Benjamin Renard   Migration to supp...
45
46
47
48
49
50
51
  		{
  			$req .= " AND (";
  			$firstDatasetId = true;
  			foreach ($datasets as $dataset)
  			{
  				if (!$firstDatasetId)
  					$req .= " OR ";
b26c1cbb   Myriam Bouchemit   passage epntap v2...
52
  				$req .= "granule_gid ='".$dataset."'";
b7796276   Benjamin Renard   Migration to supp...
53
54
55
56
57
  				$firstDatasetId = false;
  			}
  			$req .= ")";
  		}
  		
b26c1cbb   Myriam Bouchemit   passage epntap v2...
58
  		$req .= " ORDER BY time_min, granule_gid";
16035364   Benjamin Renard   First commit
59
60
61
62
63

  		$postfields = array(
                    'REQUEST' => 'doQuery',
                    'LANG' => 'ADQL',
                    'QUERY' => $req,
b7796276   Benjamin Renard   Migration to supp...
64
                    'TIMEOUT' => '30',
16035364   Benjamin Renard   First commit
65
66
67
68
69
70
71
72
73
74
75
                    '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);
91eef29d   Benjamin Renard   Fix APIS client
76

16035364   Benjamin Renard   First commit
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  		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");
b7796276   Benjamin Renard   Migration to supp...
94
				//fprintf($log,$req_res);
b26c1cbb   Myriam Bouchemit   passage epntap v2...
95
				//fprintf($log,$req);
b7796276   Benjamin Renard   Migration to supp...
96
97
98
				
				$limitReached = false;
				$lastDate = 0;
16035364   Benjamin Renard   First commit
99
100
101
				foreach ($tabledatas as $tabledata)
				{
					$trs = $tabledata->getElementsByTagName('TR');
b7796276   Benjamin Renard   Migration to supp...
102
103
					
					$limitReached = ($trs->length >= NB_ANSWER_LIMIT);
16035364   Benjamin Renard   First commit
104
105
106
107
108
109
110
111
112
113
114
115
116
					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);
								
b7796276   Benjamin Renard   Migration to supp...
117
118
								$lastDate = $time;
								
16035364   Benjamin Renard   First commit
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
								//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);
							 
					}
				}
  			 
b7796276   Benjamin Renard   Migration to supp...
145
146
147
148
149
				$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;
16035364   Benjamin Renard   First commit
150
151
152
  	}
  }
  
b7796276   Benjamin Renard   Migration to supp...
153
154
155
156
  /*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);*/
16035364   Benjamin Renard   First commit
157
		
b26c1cbb   Myriam Bouchemit   passage epntap v2...
158
?>