124 lines
3.3 KiB
PHP
124 lines
3.3 KiB
PHP
<?php
|
|
// Enable error reporting for debugging
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
require_once __DIR__ . '/../config.php';
|
|
|
|
$bulkFileName = 'weather_14.json.gz';
|
|
$bulkUrl = "https://bulk.openweathermap.org/snapshot/{$bulkFileName}?appid=" . OWM_API_KEY;
|
|
|
|
// Use a stream context to capture HTTP status headers
|
|
$context = stream_context_create(['http' => ['ignore_errors' => true]]);
|
|
$gzData = @file_get_contents($bulkUrl, false, $context);
|
|
|
|
// Check for errors and specific HTTP status codes
|
|
if ($gzData === false || !isset($http_response_header[0]) || strpos($http_response_header[0], '200 OK') === false) {
|
|
$error_message = 'Failed to download bulk weather data.';
|
|
if (isset($http_response_header[0])) {
|
|
if (strpos($http_response_header[0], '401 Unauthorized') !== false) {
|
|
$error_message = 'OpenWeatherMap API key is invalid for the Bulk Data service. The key may be correct for other services like weather tiles, but lacks permission for bulk downloads.';
|
|
http_response_code(401);
|
|
} else {
|
|
$error_message .= ' Server responded with: ' . $http_response_header[0];
|
|
http_response_code(500);
|
|
}
|
|
} else {
|
|
http_response_code(500);
|
|
}
|
|
echo json_encode(['error' => $error_message]);
|
|
exit;
|
|
}
|
|
|
|
$jsonData = gzdecode($gzData);
|
|
if ($jsonData === false) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to decompress weather data.']);
|
|
exit;
|
|
}
|
|
|
|
// The file contains JSON objects separated by newlines
|
|
$lines = explode("\n", trim($jsonData));
|
|
$cities = [];
|
|
foreach ($lines as $line) {
|
|
if (!empty($line)) {
|
|
$cities[] = json_decode($line, true);
|
|
}
|
|
}
|
|
|
|
if (empty($cities)) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to parse weather data JSON or file is empty.']);
|
|
exit;
|
|
}
|
|
|
|
// --- Interpolation ---
|
|
|
|
$nx = 72; // Grid points in longitude (every 5 degrees)
|
|
$ny = 37; // Grid points in latitude (every 5 degrees)
|
|
$lo1 = -180;
|
|
$la1 = 90;
|
|
$dx = 5;
|
|
$dy = 5;
|
|
|
|
$uData = [];
|
|
$vData = [];
|
|
|
|
for ($j = 0; $j < $ny; $j++) {
|
|
$lat = $la1 - $j * $dy;
|
|
for ($i = 0; $i < $nx; $i++) {
|
|
$lon = $lo1 + $i * $dx;
|
|
$wind = interpolatePoint($lat, $lon, $cities);
|
|
$uData[] = $wind['u'];
|
|
$vData[] = $wind['v'];
|
|
}
|
|
}
|
|
|
|
// --- Format Data ---
|
|
|
|
$refTime = gmdate("Y-m-d\\TH:i:s.v\\Z");
|
|
|
|
$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
|
|
]
|
|
];
|
|
|
|
// --- Caching and Output ---
|
|
$cacheDir = dirname($cacheFile);
|
|
if (!is_dir($cacheDir)) {
|
|
mkdir($cacheDir, 0755, true);
|
|
}
|
|
file_put_contents($cacheFile, json_encode($formattedData));
|
|
|
|
echo json_encode($formattedData);
|
|
|
|
?>
|