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