34935-vm/api/wind.php
2025-10-14 18:46:14 +00:00

130 lines
3.6 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;
// Ensure longitude is within -180 to 180 range for the API
$lats[] = round($lat, 4);
$lons[] = round($lon, 4);
}
}
// --- Fetch data from Open-Meteo in a single call ---
// The API expects `hourly` and `current` parameters.
$url = "https://api.open-meteo.com/v1/forecast?latitude=" . implode(',', $lats) . "&longitude=" . implode(',', $lons) . "&current=wind_speed_10m,wind_direction_10m";
$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'])) {
$windspeed = $data['current']['wind_speed_10m'];
$winddirection = $data['current']['wind_direction_10m'];
// Convert to u and v components
// Wind direction: the direction from which the wind is blowing (0° for North, 90° for East)
// Angle for calculation needs to be where the wind is going
$angle = ($winddirection + 180) * M_PI / 180;
$u = $windspeed * cos($angle); // Eastward component
$v = $windspeed * sin($angle); // Northward component
// The index in the response corresponds to the index in our grid
if (isset($uData[$index]) && isset($vData[$index])) {
$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;
?>