This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.php
314 lines (251 loc) · 13.8 KB
/
forecast.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
//Create Data Directory if it does not exist
if (!file_exists('data')) {
mkdir('data', 0777, true);
}
function getYRDailyForecast($forecasturl) {
// This function gets forecast pr. day from the selected YR.NO location, and returns them in an Array
$forecast_url = $forecasturl."/forecast.xml";
// Check if we have a cached Forecast File - If not, download it from yr.no
$downloadfile = True;
$forecast_file = "data/dailyforecast.xml";
if (file_exists($forecast_file)) {
//We already have a cached file, now check if it is still valid
$xml = simplexml_load_file($forecast_file); // where $xml_string is the XML data you'd like to use (a well-formatted XML string). If retrieving from an external source, you can use file_get_contents to retrieve the data and populate this variable.
$nextupdate=date_create($xml->meta->nextupdate);
$currentTime = new DateTime();
if ($currentTime > $nextupdate) {$downloadfile = True;} else {$downloadfile = False;}
}
if ($downloadfile) {
file_put_contents($forecast_file, fopen($forecast_url, 'r'));
}
// Now we have a valid forecast file. Start processing it
$xml = simplexml_load_file($forecast_file);
// Get the Copyright Text
$copyrighttext = ''.$xml->credit->link['text'].'';
$copyrighturl = ''.$xml->credit->link['url'].'';
//Initialize Variables
$forecastarray = array();
//Loop through the XML file and retrieve data
foreach($xml->forecast->tabular->time as $fcst) {
$datestamp = date_format(date_create($fcst['from']),'Y-m-d');
$timefrom = date_format(date_create($fcst['from']),'H:i');
$timeto = date_format(date_create($fcst['to']),'H:i');
$period = round($fcst['period']);
$imagename = ''.$fcst->symbol['var'].'';
$description = ''.$fcst->symbol['name'].'';
$temperature = round($fcst->temperature['value']);
$precipitation = floatval($fcst->precipitation['value']);
$windSpeed = round($fcst->windSpeed['mps']);
$windDescription = ''.$fcst->windSpeed['name'].'';
$windDirection = ''.$fcst->windDirection['code'].'';
$avgbearing = round($fcst->windDirection['deg']);
$pressure = floatval($fcst->pressure['value']);
// Store data in Array
$data=array("datestamp"=>$datestamp,"period"=>$period,"timefrom"=>$timefrom,"timeto"=>$timeto,"description"=>$description,"imagename"=>$imagename, "temperature"=>$temperature,"precipitation"=>$precipitation,"windSpeed"=>$windSpeed,"windDescription"=>$windDescription,"windDirection"=>$windDirection,"avgbearing"=>$avgbearing,"pressure"=>$pressure,"copyrighttext"=>$copyrighttext,"copyrighturl"=>$copyrighturl);
$forecastarray[]=$data;
}
return json_encode($forecastarray);
}
function getYRHourlyForecast($forecasturl) {
// This function gets forecast pr. hour from the selected YR.NO location, and returns them in an Array
$forecast_url = $forecasturl."/forecast_hour_by_hour.xml";
// Check if we have a cached Forecast File - If not, download it from yr.no
$downloadfile = True;
$forecast_file = "data/hourlyforecast.xml";
if (file_exists($forecast_file)) {
//We already have a cached file, now check if it is still valid
$xml = simplexml_load_file($forecast_file); // where $xml_string is the XML data you'd like to use (a well-formatted XML string). If retrieving from an external source, you can use file_get_contents to retrieve the data and populate this variable.
$nextupdate=date_create($xml->meta->nextupdate);
$currentTime = new DateTime();
if ($currentTime > $nextupdate) {$downloadfile = True;} else {$downloadfile = False;}
}
if ($downloadfile) {
file_put_contents($forecast_file, fopen($forecast_url, 'r'));
}
// Now we have a valid forecast file. Start processing it
$xml = simplexml_load_file($forecast_file);
// Get the Copyright Text
$copyrighttext = ''.$xml->credit->link['text'].'';
$copyrighturl = ''.$xml->credit->link['url'].'';
//Create empty Array to hold data
$forecastarray = array();
//Loop through the XML and select all data in the future
$currentTime = new DateTime();
foreach($xml->forecast->tabular->time as $fcst) {
$hour = date_create($fcst['from']);
if ($currentTime < $hour) {
$datestamp = date_format(date_create($fcst['from']),'Y-m-d H:i');
$description = ''.$fcst->symbol['name'].'';
$imagename = ''.$fcst->symbol['var'].'';
$name = $fcst->symbol['name'];
$temperature = ''.$fcst->temperature['value'].'';
$precipitation = floatval($fcst->precipitation['value']);
$windSpeed = round($fcst->windSpeed['mps']);
$windDescription = ''.$fcst->windSpeed['name'].'';
$windDirection = ''.$fcst->windDirection['code'].'';
$avgbearing = round($fcst->windDirection['deg']);
$pressure = floatval($fcst->pressure['value']);
$data=array("datestamp"=>$datestamp,"description"=>$description,"imagename"=>$imagename, "temperature"=>$temperature,"precipitation"=>$precipitation,"windSpeed"=>$windSpeed,"windDescription"=>$windDescription,"windDirection"=>$windDirection,"avgbearing"=>$avgbearing,"pressure"=>$pressure,"copyrighttext"=>$copyrighttext,"copyrighturl"=>$copyrighturl);
$forecastarray[]=$data;
}
}
return json_encode($forecastarray);
}
function getDarkSkyDailyForecast($apiKey, $lat, $lon, $language, $unit, $TZ) {
$forecast_url = "https://api.darksky.net/forecast/".$apiKey."/".$lat.",".$lon."?exclude=minutely&lang=".$language."&units=".$unit;
$downloadfile = True;
$forecast_file = "data/dsforecast.json";
if (file_exists($forecast_file)) {
$lastmodified = date("Y-m-d H:i:s",filectime($forecast_file));
if (strtotime($lastmodified) < time() - (60*60*2)) {$downloadfile = True;} else {$downloadfile = False;}
}
if ($downloadfile) {
file_put_contents($forecast_file, fopen($forecast_url, 'r'));
}
//We now have the latest forecast as a file, load it.
$json = file_get_contents($forecast_file);
$rawdata = json_decode($json, true);
//Create empty Array to hold data
$forecastarray = array();
// Create the Day-by-Day forecast and Push to New JSON Array
$fcstarray = $rawdata['daily']['data'];
foreach($fcstarray as $i => $row) {
$datestamp = convertEpochDate($row['time'],$TZ);
$summary = $row['summary'];
$icon = $row['icon'];
$tempmax = $row['temperatureMax'];
$tempmin = $row['temperatureMin'];
$precipitation = $row['precipIntensity']*24;
$precipProbability = $row['precipProbability']*100;
$windSpeed = $row['windSpeed'];
$avgbearing = $row['windBearing'];
$windDirection = getWindDirString($row['windBearing']);
$pressure = $row['pressure'];
$uvindex = $row['uvIndex'];
$copyrighttext = "Dark Sky Company";
$copyrighturl = "https://darksky.net/forecast/".$lat.",".$lon."/si12/en";
$data=array("datestamp"=>$datestamp,"description"=>$summary,"imagename"=>$icon, "tempmax"=>$tempmax, "tempmin"=>$tempmin,"precipitation"=>$precipitation,"precipProbability"=>$precipProbability,"windSpeed"=>$windSpeed,"windDirection"=>$windDirection,"avgbearing"=>$avgbearing,"pressure"=>$pressure,"uvindex"=>$uvindex,"copyrighttext"=>$copyrighttext,"copyrighturl"=>$copyrighturl);
$forecastarray[]=$data;
}
return json_encode($forecastarray);
}
function getDarkSkyHourlyForecast($apiKey, $lat, $lon, $language, $unit, $TZ) {
$forecast_url = "https://api.darksky.net/forecast/".$apiKey."/".$lat.",".$lon."?exclude=currently,minutely&lang=".$language."&units=".$unit;
$downloadfile = True;
$forecast_file = "data/dsforecast.json";
$updatetime = date("1970-01-01");
if (file_exists($forecast_file)) {
$lastmodified = date("Y-m-d H:i:s",filectime($forecast_file));
if (strtotime($lastmodified) < time() - (60*60*2)) {$downloadfile = True;} else {$downloadfile = False;}
}
if ($downloadfile) {
file_put_contents($forecast_file, fopen($forecast_url, 'r'));
$updatetime = date("Y-m-d H:i");
}
//We now have the latest forecast as a file, load it.
$json = file_get_contents($forecast_file);
$rawdata = json_decode($json, true);
//Create empty Array to hold data
$forecastarray = array();
// Create the Hourly forecast and Push to New JSON Array
$fcstarray = $rawdata['hourly']['data'];
foreach($fcstarray as $i => $row) {
$datestamp = convertEpoch($row['time'],$TZ);
$summary = $row['summary'];
$icon = $row['icon'];
$temperature = $row['temperature'];
$precipitation = $row['precipIntensity'];
$precipProbability = $row['precipProbability']*100;
$windSpeed = $row['windSpeed'];
$avgbearing = $row['windBearing'];
$windDirection = getWindDirString($row['windBearing']);
$pressure = $row['pressure'];
$uvindex = $row['uvIndex'];
$copyrighttext = "Dark Sky Company";
$copyrighturl = "https://darksky.net/forecast/".$lat.",".$lon."/si12/en";
$data=array("datestamp"=>$datestamp,"description"=>$summary,"imagename"=>$icon, "temperature"=>$temperature,"precipitation"=>$precipitation,"precipProbability"=>$precipProbability,"windSpeed"=>$windSpeed,"windDirection"=>$windDirection,"avgbearing"=>$avgbearing,"pressure"=>$pressure,"uvindex"=>$uvindex,"copyrighttext"=>$copyrighttext,"copyrighturl"=>$copyrighturl);
$forecastarray[]=$data;
}
return json_encode($forecastarray);
}
function getWUDailyForecast($apiKey, $lat, $lon, $language, $TZ) {
$forecast_url = "http://api.wunderground.com/api/".$apiKey."/forecast10day/lang:".$language."/q/".$lat.",".$lon.".json";
$downloadfile = True;
$forecast_file = "data/wuforecastday.json";
if (file_exists($forecast_file)) {
$lastmodified = date("Y-m-d H:i:s",filectime($forecast_file));
$updatetime = $lastmodified;
if (strtotime($lastmodified) < time() - (60*60*2)) {$downloadfile = True;} else {$downloadfile = False;}
}
if ($downloadfile) {
file_put_contents($forecast_file, fopen($forecast_url, 'r'));
$updatetime = date("Y-m-d H:i");
}
//We now have the latest forecast as a file, load it.
$json = file_get_contents($forecast_file);
$rawdata = json_decode($json, true);
//Create empty Array to hold data
$forecastarray = array();
// Create the Day-by-Day forecast and Push to New JSON Array
$fcstarray = $rawdata['forecast']['simpleforecast']['forecastday'];
foreach($fcstarray as $i => $row) {
$datestamp = convertEpochDate($row['date']['epoch'],$TZ);
$summary = $row['conditions'];
$icon = $row['icon'];
$tempmax = $row['high']['celsius'];
$tempmin = $row['low']['celsius'];
$precipitation = $row['qpf_allday']['mm'];
$precipProbability = $row['pop'];
$windSpeed = getMS($row['avewind']['kph']);
$avgbearing = $row['avewind']['degrees'];
$windDirection = getWindDirString($avgbearing);
$pressure = 0;
$uvindex = 0;
$copyrighttext = "Weather Underground";
$copyrighturl = "https://www.wunderground.com/personal-weather-station/dashboard?ID=I84SOHOL2";
$data=array("datestamp"=>$datestamp,"description"=>$summary,"imagename"=>$icon, "tempmax"=>$tempmax, "tempmin"=>$tempmin,"precipitation"=>$precipitation,"precipProbability"=>$precipProbability,"windSpeed"=>$windSpeed,"windDirection"=>$windDirection,"avgbearing"=>$avgbearing,"pressure"=>$pressure,"uvindex"=>$uvindex,"copyrighttext"=>$copyrighttext,"copyrighturl"=>$copyrighturl,"updatetime"=>$updatetime);
$forecastarray[]=$data;
}
return json_encode($forecastarray);
}
function getWUHourlyForecast($apiKey, $lat, $lon, $language, $TZ) {
$forecast_url = "http://api.wunderground.com/api/".$apiKey."/hourly/lang:".$language."/q/".$lat.",".$lon.".json";
$downloadfile = True;
$forecast_file = "data/wuforecasthour.json";
if (file_exists($forecast_file)) {
$lastmodified = date("Y-m-d H:i:s",filectime($forecast_file));
$updatetime = $lastmodified;
if (strtotime($lastmodified) < time() - (60*60*2)) {$downloadfile = True;} else {$downloadfile = False;}
}
if ($downloadfile) {
file_put_contents($forecast_file, fopen($forecast_url, 'r'));
$updatetime = date("Y-m-d H:i");
}
//We now have the latest forecast as a file, load it.
$json = file_get_contents($forecast_file);
$rawdata = json_decode($json, true);
//Create empty Array to hold data
$forecastarray = array();
// Create the Hourly forecast and Push to New JSON Array
$fcstarray = $rawdata['hourly_forecast'];
foreach($fcstarray as $i => $row) {
$datestamp = convertEpoch($row['FCTTIME']['epoch'],$TZ);
$summary = $row['condition'];
$icon = $row['icon'];
$temperature = $row['temp']['metric'];
$precipitation = $row['qpf']['metric'];
$precipProbability = $row['pop'];
$windSpeed = getMS($row['wspd']['metric']);
$avgbearing = $row['wdir']['degrees'];
$windDirection = getWindDirString($avgbearing);
$pressure = $row['mslp']['metric'];
$uvindex = $row['uvi'];
$copyrighttext = "Weather Underground";
$copyrighturl = "https://www.wunderground.com/personal-weather-station/dashboard?ID=I84SOHOL2";
$data=array("datestamp"=>$datestamp,"description"=>$summary,"imagename"=>$icon, "temperature"=>$temperature,"precipitation"=>$precipitation,"precipProbability"=>$precipProbability,"windSpeed"=>$windSpeed,"windDirection"=>$windDirection,"avgbearing"=>$avgbearing,"pressure"=>$pressure,"uvindex"=>$uvindex,"copyrighttext"=>$copyrighttext,"copyrighturl"=>$copyrighturl,"updatetime"=>$updatetime);
$forecastarray[]=$data;
}
return json_encode($forecastarray);
}
?>