118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?php
|
|
// Enable error reporting for debugging
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
$cacheFile = __DIR__ . '/wind.json';
|
|
$cacheTime = 3600; // 1 hour
|
|
|
|
// Check if a cached file exists and is recent
|
|
if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTime) {
|
|
header('Content-Type: application/json');
|
|
readfile($cacheFile);
|
|
exit;
|
|
}
|
|
|
|
// --- Grid setup ---
|
|
$nx = 36; // Grid points in longitude (every 10 degrees)
|
|
$ny = 18; // Grid points in latitude (every 10 degrees)
|
|
$lo1 = -180;
|
|
$la1 = 90;
|
|
$dx = 10;
|
|
$dy = 10;
|
|
|
|
$uData = array_fill(0, $nx * $ny, 0);
|
|
$vData = array_fill(0, $nx * $ny, 0);
|
|
|
|
// --- Build coordinate arrays for batch API call ---
|
|
$lats = [];
|
|
$lons = [];
|
|
for ($j = 0; $j < $ny; $j++) {
|
|
$lat = $la1 - $j * $dy;
|
|
for ($i = 0; $i < $nx; $i++) {
|
|
$lon = $lo1 + $i * $dx;
|
|
$lats[] = $lat;
|
|
$lons[] = $lon;
|
|
}
|
|
}
|
|
|
|
// --- Fetch data from Open-Meteo in a single call ---
|
|
$url = "https://api.open-meteo.com/v1/forecast?latitude=" . implode(',', $lats) . "&longitude=" . implode(',', $lons) . "&hourly=windspeed_10m,winddirection_10m¤t_weather=true";
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
if ($response) {
|
|
$results = json_decode($response, true);
|
|
|
|
// The API returns an array of results, one for each coordinate pair
|
|
if (is_array($results)) {
|
|
foreach ($results as $index => $data) {
|
|
if (isset($data['current_weather'])) {
|
|
$windspeed = $data['current_weather']['windspeed'];
|
|
$winddirection = $data['current_weather']['winddirection'];
|
|
|
|
// Convert to u and v components
|
|
$angle = ($winddirection + 180) * M_PI / 180;
|
|
$u = $windspeed * cos($angle);
|
|
$v = $windspeed * sin($angle);
|
|
|
|
// The index in the response corresponds to the index in our grid
|
|
$uData[$index] = $u;
|
|
$vData[$index] = $v;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Format Data ---
|
|
$refTime = gmdate("Y-m-d\\TH:i:s.vZ");
|
|
|
|
$formattedData = [
|
|
[
|
|
"header" => [
|
|
"nx" => $nx,
|
|
"ny" => $ny,
|
|
"lo1" => $lo1,
|
|
"la1" => $la1,
|
|
"dx" => $dx,
|
|
"dy" => $dy,
|
|
"parameterCategory" => 2,
|
|
"parameterNumber" => 2,
|
|
"forecastTime" => 0,
|
|
"refTime" => $refTime,
|
|
],
|
|
"data" => $uData
|
|
],
|
|
[
|
|
"header" => [
|
|
"nx" => $nx,
|
|
"ny" => $ny,
|
|
"lo1" => $lo1,
|
|
"la1" => $la1,
|
|
"dx" => $dx,
|
|
"dy" => $dy,
|
|
"parameterCategory" => 2,
|
|
"parameterNumber" => 3,
|
|
"forecastTime" => 0,
|
|
"refTime" => $refTime,
|
|
],
|
|
"data" => $vData
|
|
]
|
|
];
|
|
|
|
$json_data = json_encode($formattedData);
|
|
|
|
// Cache the result
|
|
file_put_contents($cacheFile, $json_data);
|
|
|
|
header('Content-Type: application/json');
|
|
echo $json_data;
|
|
|
|
?>
|