-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull-forcast.html
More file actions
396 lines (349 loc) · 24.5 KB
/
Copy pathfull-forcast.html
File metadata and controls
396 lines (349 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Full Forecast - TalviFox Weather</title>
<style>
:root {
--bg-color: #f4f4f9;
--text-color: #333;
--card-bg: #fff;
--border-color: #ddd;
--accent-color: #3498db;
}
:root[data-theme="dark"] {
--bg-color: #1a1a24;
--text-color: #e0e0e0;
--card-bg: #2d2d3f;
--border-color: #444;
}
body { margin: 0; padding: 2rem; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(--bg-color); color: var(--text-color); }
.container { max-width: 1000px; margin: 0 auto; }
header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem; border-bottom: 2px solid var(--border-color); padding-bottom: 1rem; }
h1 { margin: 0; font-size: 2rem; display: flex; align-items: center; gap: 10px; }
.current-weather { display: flex; gap: 2rem; margin-bottom: 2rem; flex-wrap: wrap; }
.card { background: var(--card-bg); padding: 1.5rem; border-radius: 12px; border: 1px solid var(--border-color); box-shadow: 0 4px 6px rgba(0,0,0,0.05); flex: 1; min-width: 250px; }
.temp-huge { font-size: 4rem; font-weight: bold; margin: 0; line-height: 1; }
.desc-text { font-size: 1.2rem; color: #888; margin-top: 0.5rem; margin-bottom: 1rem; }
.grid-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.stat-item { display: flex; flex-direction: column; }
.stat-label { font-size: 0.8rem; color: #888; text-transform: uppercase; letter-spacing: 1px; }
.stat-value { font-size: 1.1rem; font-weight: bold; }
.daily-forecast { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 1rem; }
.day-card { background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 12px; padding: 1rem; text-align: center; transition: transform 0.2s ease, box-shadow 0.2s ease; cursor: pointer; }
.day-card:hover { transform: scale(1.05); box-shadow: 0 8px 15px rgba(0,0,0,0.1); }
.day-name { font-weight: bold; margin-bottom: 0.5rem; }
.day-icon { font-size: 2.5rem; margin: 0.5rem 0; display: flex; justify-content: center; align-items: center; min-height: 50px; }
.day-temps { display: flex; justify-content: center; gap: 0.5rem; font-size: 1.1rem; }
.day-high { font-weight: bold; }
.day-low { color: #888; }
.nasa-moon { height: 2.5rem; width: 2.5rem; object-fit: cover; border-radius: 50%; filter: drop-shadow(0 0 4px rgba(255,255,255,0.2)); pointer-events: none; user-select: none; }
.loading { text-align: center; font-size: 1.5rem; margin-top: 4rem; color: #888; }
.error { color: #e74c3c; text-align: center; padding: 2rem; border: 1px solid #e74c3c; border-radius: 8px; margin-top: 2rem; display: none; }
a.back-link { color: var(--accent-color); text-decoration: none; font-weight: bold; }
a.back-link:hover { text-decoration: underline; }
.modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); backdrop-filter: blur(4px); justify-content: center; align-items: center; }
.modal-content { background-color: var(--card-bg); padding: 2rem; border-radius: 12px; max-width: 500px; width: 90%; box-shadow: 0 10px 25px rgba(0,0,0,0.2); position: relative; color: var(--text-color); }
.close-modal { position: absolute; top: 15px; right: 20px; font-size: 1.5rem; cursor: pointer; color: #888; }
.close-modal:hover { color: var(--text-color); }
.close-modal:hover { color: var(--text-color); }
</style>
<script>
(function() {
var theme = localStorage.getItem('weatherTheme');
if (theme === 'dark' || (!theme && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.setAttribute('data-theme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
}
})();
</script>
</head>
<body>
<div class="container">
<header>
<h1 id="location-title">Loading Forecast...</h1>
<div style="display: flex; gap: 1rem; align-items: center;">
<button id="theme-toggle" style="background:none; border:none; font-size: 1.5rem; cursor: pointer; padding: 0;" title="Toggle Dark/Light Mode">🌓</button>
<a href="javascript:window.close();" class="back-link">Close Tab</a>
</div>
</header>
<div id="error-box" class="error">Failed to load forecast data. Please check the coordinates and try again.</div>
<div id="content" style="display: none;">
<div class="current-weather">
<div class="card" style="display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center;">
<div id="current-icon" style="font-size: 4rem; margin-bottom: 1rem; display: flex; align-items: center; justify-content: center;"></div>
<div id="current-temp" class="temp-huge">--°</div>
<div id="current-desc" class="desc-text">--</div>
</div>
<div class="card">
<h3 style="margin-top: 0; margin-bottom: 1.5rem; color: #888; font-size: 1rem; text-transform: uppercase;">Current Conditions</h3>
<div class="grid-stats">
<div class="stat-item"><span class="stat-label">Feels Like</span><span id="feels-like" class="stat-value">--°</span></div>
<div class="stat-item"><span class="stat-label">Humidity</span><span id="humidity" class="stat-value">--%</span></div>
<div class="stat-item"><span class="stat-label">Wind</span><span id="wind" class="stat-value">-- mph</span></div>
<div class="stat-item"><span class="stat-label">Precipitation</span><span id="precip" class="stat-value">-- in</span></div>
<div class="stat-item"><span class="stat-label">UV Index</span><span id="uv-index" class="stat-value">--</span></div>
<div class="stat-item"><span class="stat-label">Pressure</span><span id="pressure" class="stat-value">-- mb</span></div>
</div>
</div>
</div>
<div id="text-forecast-box" class="card" style="display: none; margin-bottom: 2rem;">
<h3 style="margin-top: 0; margin-bottom: 1rem; color: #888; font-size: 1rem; text-transform: uppercase;">Detailed Forecast</h3>
<div id="text-forecast-content" style="font-size: 1.1rem; line-height: 1.5;"></div>
</div>
<h3 style="margin-bottom: 1rem;">7-Day Extended Forecast</h3>
<div id="daily-grid" class="daily-forecast">
<!-- Daily cards injected here -->
</div>
<div style="margin-top: 3rem; text-align: center; color: #888; font-size: 0.9rem;">
Data powered by <a href="https://open-meteo.com/" target="_blank" style="color: var(--accent-color); text-decoration: none;">Open-Meteo</a><br>
Moon imagery provided by <a href="https://svs.gsfc.nasa.gov/" target="_blank" style="color: var(--accent-color); text-decoration: none;">NASA SVS</a><br>
© 2026 TalviFox
</div>
</div>
<div id="loading" class="loading">Fetching live atmospheric data...</div>
</div>
<div id="forecast-modal" class="modal">
<div class="modal-content">
<span class="close-modal" id="close-modal">×</span>
<h2 id="modal-title" style="margin-top: 0;">Forecast</h2>
<div id="modal-text" style="font-size: 1.1rem; line-height: 1.5;"></div>
</div>
</div>
<script>
// --- Helper Math Functions (Ported from the widget) ---
function getWMODescription(code) {
const map = {
0: 'Clear', 1: 'Mostly Clear', 2: 'Partly Cloudy', 3: 'Cloudy',
45: 'Fog', 48: 'Freezing Fog',
51: 'Light Drizzle', 53: 'Drizzle', 55: 'Heavy Drizzle',
56: 'Light Freezing Drizzle', 57: 'Freezing Drizzle',
61: 'Light Rain', 63: 'Rain', 65: 'Heavy Rain',
66: 'Light Freezing Rain', 67: 'Freezing Rain',
71: 'Light Snow', 73: 'Snow', 75: 'Heavy Snow',
77: 'Snow Grains',
80: 'Light Rain Showers', 81: 'Rain Showers', 82: 'Heavy Rain Showers',
85: 'Light Snow Showers', 86: 'Snow Showers',
95: 'Thunderstorm', 96: 'Thunderstorm with Light Hail', 99: 'Thunderstorm with Heavy Hail'
};
return map[code] || 'Unknown';
}
function isNighttime(lat, lon) {
const now = new Date();
const start = new Date(now.getFullYear(), 0, 0);
const diff = now - start;
const oneDay = 1000 * 60 * 60 * 24;
const dayOfYear = Math.floor(diff / oneDay);
const latRad = lat * Math.PI / 180;
const declination = 23.45 * Math.sin((360 / 365) * (dayOfYear - 81) * Math.PI / 180);
const declinationRad = declination * Math.PI / 180;
const zenith = 90.833 * Math.PI / 180;
let cosH = (Math.cos(zenith) - Math.sin(latRad) * Math.sin(declinationRad)) / (Math.cos(latRad) * Math.cos(declinationRad));
if (cosH > 1) return true; // Sun never rises
if (cosH < -1) return false; // Sun never sets
const hourAngle = Math.acos(cosH) * 180 / Math.PI;
const sunHours = hourAngle / 15;
const solarNoonUTC = 12 - (lon / 15);
const sunriseUTC = solarNoonUTC - sunHours;
const sunsetUTC = solarNoonUTC + sunHours;
const currentUTC = now.getUTCHours() + now.getUTCMinutes() / 60;
let sunrise = (sunriseUTC + 24) % 24;
let sunset = (sunsetUTC + 24) % 24;
if (sunrise < sunset) {
return currentUTC < sunrise || currentUTC > sunset;
} else {
return currentUTC > sunset && currentUTC < sunrise;
}
}
function getNASAMoonImageURL(targetDate = new Date()) {
if (window.DEBUG_WEATHER && window.DEBUG_WEATHER.forceMoonFrame !== null) {
return `https://svs.gsfc.nasa.gov/vis/a000000/a005100/a005187/frames/730x730_1x1_30p/moon.${String(window.DEBUG_WEATHER.forceMoonFrame).padStart(4, '0')}.jpg`;
}
const year = targetDate.getUTCFullYear();
const svsMap = { 2024: '5187', 2025: '5415', 2026: '5587' };
let svsId = svsMap[year];
if (svsId) {
const startOfYear = Date.UTC(year, 0, 1, 0, 0, 0);
const msSinceStart = targetDate.getTime() - startOfYear;
const hoursSinceStart = Math.floor(msSinceStart / (1000 * 60 * 60));
let frame = hoursSinceStart + 1;
const isLeap = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
const maxFrames = isLeap ? 8784 : 8760;
if (frame > maxFrames) frame = maxFrames;
const folderPrefix = svsId.substring(0, 2);
return `https://svs.gsfc.nasa.gov/vis/a000000/a00${folderPrefix}00/a00${svsId}/frames/730x730_1x1_30p/moon.${String(frame).padStart(4, '0')}.jpg`;
} else {
const knownNewMoon = Date.UTC(2000, 0, 6, 18, 14);
const daysSinceKnownNew = (targetDate.getTime() - knownNewMoon) / (1000 * 60 * 60 * 24);
const synodicMonth = 29.53058867;
const phase = ((daysSinceKnownNew % synodicMonth) + synodicMonth) % synodicMonth / synodicMonth;
const newMoonFrame2024 = 252;
let frame = Math.round(newMoonFrame2024 + (phase * synodicMonth * 24));
if (frame >= 8784) frame = frame % 8784;
if (frame === 0) frame = 8784;
return `https://svs.gsfc.nasa.gov/vis/a000000/a005100/a005187/frames/730x730_1x1_30p/moon.${String(frame).padStart(4, '0')}.jpg`;
}
}
function getMoonPhaseEmoji(targetDate = new Date()) {
const knownNewMoon = Date.UTC(2000, 0, 6, 18, 14);
const daysSinceKnownNew = (targetDate.getTime() - knownNewMoon) / (1000 * 60 * 60 * 24);
const synodicMonth = 29.53058867;
const phase = ((daysSinceKnownNew % synodicMonth) + synodicMonth) % synodicMonth / synodicMonth;
const moonPhases = ['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'];
return moonPhases[Math.floor(phase * 8) % 8];
}
function getWeatherIcon(description, lat, lon, overrideNight = null, targetDate = new Date()) {
const desc = description.toLowerCase();
let isNight = overrideNight !== null ? overrideNight : isNighttime(lat, lon);
let weatherIcon = '';
if (isNight) {
if (desc.includes('thunderstorm') || desc.includes('t-storm') || (desc.includes('rain') && desc.includes('thunder')) || desc.includes('hail')) weatherIcon = '⛈️';
else if (desc.includes('snow') || desc.includes('blizzard') || desc.includes('sleet') || desc.includes('freezing') || desc.includes('ice')) weatherIcon = '🌨️';
else if (desc.includes('rain') || desc.includes('showers') || desc.includes('drizzle') || desc.includes('sprinkle')) weatherIcon = '🌧️';
else if (desc.includes('fog') || desc.includes('mist') || desc.includes('haze') || desc.includes('smoke') || desc.includes('dust') || desc.includes('sand')) weatherIcon = '🌫️';
else if (desc.includes('overcast') || desc.includes('cloudy')) weatherIcon = '☁️';
else if (desc.includes('wind') || desc.includes('breezy') || desc.includes('gust')) weatherIcon = '💨';
else if (desc.includes('clear') || desc.includes('fair') || desc === 'na' || desc === '') weatherIcon = '';
else weatherIcon = '☁️';
const nasaUrl = getNASAMoonImageURL(targetDate);
const emojiMoon = getMoonPhaseEmoji(targetDate);
return { text: weatherIcon, isNight: true, moonUrl: nasaUrl, moonEmoji: emojiMoon };
} else {
if (desc.includes('thunderstorm') || desc.includes('t-storm') || (desc.includes('rain') && desc.includes('thunder')) || desc.includes('hail')) weatherIcon = '⛈️';
else if (desc.includes('snow') || desc.includes('blizzard') || desc.includes('sleet') || desc.includes('freezing') || desc.includes('ice')) weatherIcon = '🌨️';
else if (desc.includes('drizzle') || desc.includes('light rain') || desc.includes('sprinkle')) weatherIcon = '🌦️';
else if (desc.includes('rain') || desc.includes('showers')) weatherIcon = '🌧️';
else if (desc.includes('fog') || desc.includes('mist') || desc.includes('haze') || desc.includes('smoke') || desc.includes('dust') || desc.includes('sand')) weatherIcon = '🌫️';
else if (desc.includes('overcast')) weatherIcon = '☁️';
else if (desc.includes('mostly cloudy')) weatherIcon = '🌥️';
else if (desc.includes('partly cloudy') || desc.includes('partly sunny') || desc.includes('scattered clouds')) weatherIcon = '⛅';
else if (desc.includes('mostly clear') || desc.includes('mostly sunny')) weatherIcon = '🌤️';
else if (desc.includes('cloudy')) weatherIcon = '☁️';
else if (desc.includes('wind') || desc.includes('breezy') || desc.includes('gust')) weatherIcon = '💨';
else weatherIcon = '☀️';
return { text: weatherIcon, isNight: false };
}
}
// --- Main Execution ---
document.addEventListener('DOMContentLoaded', async () => {
document.getElementById('theme-toggle').addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('weatherTheme', newTheme);
});
document.getElementById('close-modal').addEventListener('click', () => {
document.getElementById('forecast-modal').style.display = 'none';
});
window.addEventListener('click', (e) => {
const modal = document.getElementById('forecast-modal');
if (e.target === modal) {
modal.style.display = 'none';
}
});
const urlParams = new URLSearchParams(window.location.search);
const lat = parseFloat(urlParams.get('lat')) || 51.5074;
const lon = parseFloat(urlParams.get('lon')) || -0.1278;
let locationName = urlParams.get('name');
if (!locationName) locationName = `${lat.toFixed(2)}, ${lon.toFixed(2)}`;
document.getElementById('location-title').textContent = locationName + ' Forecast';
try {
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weather_code,surface_pressure,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min,uv_index_max&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timezone=auto`;
const response = await fetch(url);
if (!response.ok) throw new Error('API request failed');
const data = await response.json();
// Populate Current
const current = data.current;
document.getElementById('current-temp').textContent = `${Math.round(current.temperature_2m)}°`;
document.getElementById('feels-like').textContent = `${Math.round(current.apparent_temperature)}°F`;
document.getElementById('humidity').textContent = `${current.relative_humidity_2m}%`;
document.getElementById('wind').textContent = `${Math.round(current.wind_speed_10m)} mph`;
document.getElementById('precip').textContent = `${current.precipitation} in`;
document.getElementById('pressure').textContent = `${Math.round(current.surface_pressure)} mb`;
const desc = getWMODescription(current.weather_code);
document.getElementById('current-desc').textContent = desc;
const iconData = getWeatherIcon(desc, lat, lon, null);
const currentIconDiv = document.getElementById('current-icon');
if (iconData.isNight && iconData.moonUrl) {
currentIconDiv.innerHTML = iconData.text + `
<svg viewBox="0 0 100 100" style="height: 1.2em; width: 1.2em; margin-left: 10px; filter: drop-shadow(0 0 4px rgba(255,255,255,0.2));">
<defs><clipPath id="moon-clip-main"><circle cx="50" cy="50" r="48" /></clipPath></defs>
<image href="${iconData.moonUrl}" x="-15" y="-15" width="130" height="130" clip-path="url(#moon-clip-main)" />
</svg>`;
} else {
currentIconDiv.textContent = iconData.text || '☀️';
}
// Populate Daily Grid
const dailyGrid = document.getElementById('daily-grid');
dailyGrid.innerHTML = '';
if (data.daily && data.daily.time) {
for(let i=0; i < data.daily.time.length; i++) {
const dateObj = new Date(data.daily.time[i] + 'T12:00:00');
const dayName = i === 0 ? 'Today' : dateObj.toLocaleDateString('en-US', { weekday: 'short' });
const fullDayName = i === 0 ? 'Today' : dateObj.toLocaleDateString('en-US', { weekday: 'long' });
const shortDesc = getWMODescription(data.daily.weather_code[i]);
const dIconData = getWeatherIcon(shortDesc, lat, lon, false, dateObj); // force day icon for daily forecast
let iconHtml = dIconData.text || '☀️';
const dayCard = document.createElement('div');
dayCard.className = 'day-card';
dayCard.innerHTML = `
<div class="day-name">${dayName}</div>
<div class="day-icon" title="${shortDesc}">${iconHtml}</div>
<div class="day-temps">
<span class="day-high">H: ${Math.round(data.daily.temperature_2m_max[i])}°</span>
<span class="day-low">L: ${Math.round(data.daily.temperature_2m_min[i])}°</span>
</div>
`;
dayCard.addEventListener('click', () => {
document.getElementById('modal-title').textContent = fullDayName + ' Forecast';
let detailedText = shortDesc; // Fallback
if (window.nwsPeriods && window.nwsPeriods.length > 0) {
const match = window.nwsPeriods.find(p => p.name === fullDayName || (fullDayName === 'Today' && (p.name === 'Today' || p.name === 'This Afternoon')));
if (match) {
detailedText = match.detailedForecast;
} else if (i * 2 < window.nwsPeriods.length) {
detailedText = window.nwsPeriods[i*2].detailedForecast;
}
}
document.getElementById('modal-text').textContent = detailedText;
document.getElementById('forecast-modal').style.display = 'flex';
});
dailyGrid.appendChild(dayCard);
}
}
if (data.daily && data.daily.uv_index_max && data.daily.uv_index_max.length > 0) {
document.getElementById('uv-index').textContent = Math.round(data.daily.uv_index_max[0]);
}
document.getElementById('loading').style.display = 'none';
document.getElementById('content').style.display = 'block';
// Attempt to fetch detailed text forecast from NWS (if available for these coordinates)
try {
const pointResponse = await fetch(`https://api.weather.gov/points/${lat},${lon}`);
if (pointResponse.ok) {
const pointData = await pointResponse.json();
const forecastUrl = pointData.properties.forecast;
const forecastResponse = await fetch(forecastUrl);
if (forecastResponse.ok) {
const forecastData = await forecastResponse.json();
window.nwsPeriods = forecastData.properties.periods;
const currentPeriod = window.nwsPeriods[0];
if (currentPeriod && currentPeriod.detailedForecast) {
document.getElementById('text-forecast-content').textContent = currentPeriod.detailedForecast;
document.getElementById('text-forecast-box').style.display = 'block';
}
}
}
} catch(e) {
console.log("NWS detailed forecast unavailable for this location.");
}
} catch (error) {
console.error(error);
document.getElementById('loading').style.display = 'none';
document.getElementById('error-box').style.display = 'block';
}
});
</script>
</body>
</html>