var degrees_f = 0; var degrees_c = 0; document.addEventListener("DOMContentLoaded", function () { fetch( "https://forecast.weather.gov/MapClick.php?&lat=37.35&lon=-121.94&FcstType=json", ) .then((response) => response.json()) .then((data) => { degrees_f = parseInt(data.currentobservation.Temp); document.getElementById("footer-temperature-f").textContent = degrees_f; degrees_c = Math.round(((degrees_f - 32) * 5) / 9); document.getElementById("footer-temperature-c").textContent = degrees_c; var weather = data.currentobservation.Weather; setWeatherIcon(weather); }) .catch(function (error) { console.error("Error fetching weather data:", error); }); }); const weather_icons = { fog: "fog.svg", hail: "hail.svg", haze: "haze.svg", thunderstorm: "cloud-storm.svg", "partly cloudy": "cloud-moon.svg", rain: "cloud-rain.svg", snow: "cloud-snow.svg", "partly sunny": "cloud-sun.svg", cloudy: "cloudy.svg", clear: "moon.svg", sunny: "sun.svg", windy: "wind.svg", frost: "frost.svg", }; function setWeatherIcon(input) { let formatted_weather = input.toLowerCase(); let result = ""; const isNight = isNightTime(); if (formatted_weather.includes("then")) { formatted_weather = formatted_weather.slice( 0, formatted_weather.indexOf("then"), ); } formatted_weather = formatted_weather.trim(); if (formatted_weather.includes("hail")) { result = "hail"; } else if ( formatted_weather.includes("snow") || formatted_weather.includes("flurries") || formatted_weather.includes("flurry") || formatted_weather.includes("wintry") ) { result = "snow"; } else if ( formatted_weather.includes("rain") || formatted_weather.includes("shower") || formatted_weather.includes("drizzle") ) { result = "rain"; } else if (formatted_weather.includes("frost")) { result = "frost"; } else if ( formatted_weather.includes("haze") || formatted_weather.includes("hazy") ) { result = "haze"; } else if (formatted_weather.includes("fog")) { result = "fog"; } else if (formatted_weather.includes("wind")) { result = "windy"; } else if (formatted_weather.includes("storm")) { result = "thunderstorm"; } else if (formatted_weather.includes("partly sunny")) { result = "partly sunny"; } else if ( !isNight && (formatted_weather.includes("partly cloudy") || formatted_weather.includes("clearing") || formatted_weather.includes("decreasing clouds")) ) { result = "partly sunny"; } else if ( !isNight && (formatted_weather.includes("clear") || formatted_weather.includes("sunny")) ) { result = "sunny"; } else if ( isNight && (formatted_weather.includes("partly cloudy") || formatted_weather.includes("clearing") || formatted_weather.includes("decreasing clouds")) ) { result = "partly cloudy"; } else if (isNight && formatted_weather.includes("clear")) { result = "clear"; } else if (formatted_weather.includes("breez")) { result = "windy"; } else { result = "cloudy"; } let output = document.getElementById("weather-icon"); output.src = "https://assets.scu.edu/assets/web-icons/" + weather_icons[result]; output.alt = "current weather in Santa Clara is " + result; let home_icon = document.getElementById("home-weather-icon"); if (home_icon) { home_icon.src = "https://scu.edu/media/scuedu/content-assets/icons/home-weather/" + weather_icons[result]; } let home_temperature_f = document.getElementById("home-temperature-f"); if (home_temperature_f) { home_temperature_f.textContent = degrees_f; } let home_temperature_c = document.getElementById("home-temperature-c"); if (home_temperature_c) { home_temperature_c.textContent = degrees_c; } } function getApproxSunTimes(month) { // Approximate sunrise and sunset times in 24-hour format const sunTimes = [ { sunrise: 7.5, sunset: 17 }, // Jan { sunrise: 7, sunset: 17.5 }, // Feb { sunrise: 6.5, sunset: 18.5 }, // Mar { sunrise: 6, sunset: 19.5 }, // Apr { sunrise: 5.5, sunset: 20 }, // May { sunrise: 5.5, sunset: 20.5 }, // Jun { sunrise: 5.5, sunset: 20.5 }, // Jul { sunrise: 6, sunset: 20 }, // Aug { sunrise: 6.5, sunset: 19.5 }, // Sep { sunrise: 7, sunset: 18.5 }, // Oct { sunrise: 7, sunset: 17.5 }, // Nov { sunrise: 7.5, sunset: 17 }, // Dec ]; if (month < 1 || month > 12) return null; return sunTimes[month - 1]; } function isNightTime() { const now = new Date(); const month = now.getMonth() + 1; // JS months: 0-11 const sunTimes = getApproxSunTimes(month); if (!sunTimes) return false; const hour = now.getHours() + now.getMinutes() / 60; // Night is before sunrise or after sunset return hour < sunTimes.sunrise || hour >= sunTimes.sunset; }