$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; ?>