<?php
// Coordinates for Glasgow City Centre where Figure A is walking
$latitude = 55.8652;
$longitude = -4.2559;

// Open-Meteo endpoint URL
$url = "https://api.open-meteo.com/v1/forecast?latitude=$latitude&longitude=$longitude&current=temperature_2m,weathercode,windspeed_10m";

// Get JSON data
$response = file_get_contents($url);
$data = json_decode($response, true);

// Check if data is valid
if (!isset($data['current'])) {
    echo '<p style="text-align:center; color:red;">Weather data unavailable.</p>';
    exit;
}

// Extract current weather data
$temp = $data['current']['temperature_2m'];
$weatherCode = $data['current']['weathercode'];
$windSpeed = $data['current']['windspeed_10m'];

// Define weather descriptions (for reference)
$descriptions = [
    0 => 'Clear sky',
    1 => 'Mainly clear',
    2 => 'Partly cloudy',
    3 => 'Overcast',
    45 => 'Fog',
    48 => 'Depositing rime fog',
    51 => 'Light drizzle',
    61 => 'Light rain',
    71 => 'Light snow',
    80 => 'Rain showers',
    95 => 'Thunderstorm',
];

// Set flags
$sun = in_array($weatherCode, [0, 1]);
$rain = in_array($weatherCode, [51, 53, 55, 61, 63, 65, 80, 81, 82]);
$cloudy = in_array($weatherCode, [2, 3, 45, 48]);
$thunderstorm = in_array($weatherCode, [95]);
$wind = $windSpeed > 15; // Windy if wind speed over 15 km/h
$coldsun = $sun && $temp < 8;

// Get description or fallback
$desc = $descriptions[$weatherCode] ?? 'Unknown weather';

// Output
echo "Weather: $desc<br>";
echo "Temperature: " . round($temp, 1) . " °C<br>";


?>
