-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesmeralda-web-api.php
171 lines (145 loc) · 4.28 KB
/
esmeralda-web-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
$POLLUTANTS = [ 'PM10', 'PM25', 'NO2', 'O3' ] ;
$DOMAINS = [ 'bour', 'bret', 'chp3', 'idf3',
'lgr3', 'nmd', 'npc', 'pcd' ] ;
$DATADIR = '.' ;
function localDataURL ($year, $mon, $day)
{
global $DATADIR ;
return $DATADIR . sprintf ('/%d%02d%02d.dat', $year, $mon, $day) ;
}
/* Generate URL to fetch according to date, domain and pollutant. */
function remoteDataURL ($year, $mon, $day, $dom, $pol)
{
return sprintf ('http://www.esmeralda-web.fr/esmeralda/FIGS/'
.'%d/%02d/%d%02d%02d/GN15/conc_peak.%s.%s.txt',
$year, $mon, $year, $mon, $day, $dom, $pol) ;
}
/* Get content return by url using curl, and retur it as string. */
function cURL ($url)
{
$ch = curl_init ($url) ;
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
$res = curl_exec ($ch) ;
curl_close ($ch) ;
return $res ;
}
/* Get raw data from esmeralda-web for a day / domain / pollutant.
* Parse the result and extract values. */
function fetchSheet ($year, $mon, $day, $dom, $pol)
{
$raw = cURL (remoteDataURL ($year, $mon, $day, $dom, $pol)) ;
foreach (explode ("\n", $raw) as $line)
{
preg_match ('/\s*([\w\d_]+)'
.'\s+(\d+)\.\s+\d+'
.'\s+(\d+)\.\s+\d+'
.'\s+(\d+)\.\s+\d+'
.'\s+(\d+)\.\s+\d+'
.'\s*/',
$line, $matches) ;
if (count ($matches) == 6)
{
$data [$matches[1]] =
array_map (function ($x) { return intval ($x) ;},
array_slice ($matches, 2)) ;
}
}
return $data ;
}
/* Fetch data for all pollutants and all stations according a given day. */
function fetchData ($year, $mon, $day)
{
global $DOMAINS ;
global $POLLUTANTS ;
$data = [] ;
foreach ($DOMAINS as $dom)
{
foreach ($POLLUTANTS as $pol)
{
foreach (fetchSheet ($year, $mon, $day, $dom, $pol)
as $station => $conc)
{
$data [$dom.'.'.$station] [$pol] = $conc ;
}
}
}
return $data ;
}
/* Fetch data and save them on the server. */
function importData ($year, $mon, $day)
{
$data = fetchData ($year, $mon, $day) ;
$filename = localDataURL ($year, $mon, $day) ;
return file_put_contents ($filename, serialize ($data)) ;
}
function loadData ($year, $mon, $day, $stations, $pollutants)
{
$filename = localDataURL ($year, $mon, $day) ;
if (!file_exists ($filename) && !importData ($year, $mon, $day))
{
return NULL ;
}
$db = unserialize (file_get_contents ($filename));
$selected_stations = [] ;
/* Station may be a station name or a regexp. */
foreach ($stations as $station)
{
if (!empty ($db [$station]))
{
$selected_stations [] = $station ;
}
else
{
foreach ($db as $db_station => $_)
{
if (preg_match ($station, $db_station) === 1)
{
$selected_stations [] = $db_station ;
}
}
}
}
foreach ($selected_stations as $station)
{
foreach ($pollutants as $pollutant)
{
if (!empty ($db [$station] [$pollutant]))
{
$data [$station] [$pollutant] = $db [$station] [$pollutant] ;
}
}
}
return $data;
}
function processRequest ()
{
global $POLLUTANTS ;
$stations = (isset ($_GET ['s'])) ?
(explode (',', $_GET ['s'])) :
['/.*/'];
$pollutants = (isset ($_GET ['p']) ?
(explode (',', $_GET ['p'])) :
$POLLUTANTS) ;
$date = date_parse_from_format ('Y-m-d', (isset ($_GET ['d']) ?
$_GET ['d'] :
(date ('Y-m-d')))) ;
$data = loadData ($date['year'], $date['month'], $date['day'],
$stations, $pollutants) ;
return $data ;
}
function serveJSON ($data)
{
header('Content-type: application/json') ;
if ($data == null)
{
print '{}' ;
}
else
{
$json = json_encode ($data) ;
print ($json != FALSE ? $json : '{}') ;
}
}
serveJSON (processRequest ()) ;
?>