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
| // Configuration
const API_BASE = 'https://archive-api.open-meteo.com/v1/archive';
const FORECAST_API = 'https://api.open-meteo.com/v1/forecast';
// Variable configuration
const VARIABLES = {
temperature_2m: { name: 'Temperature', unit: '°C', color: '#ff6b6b' },
relative_humidity_2m: { name: 'Humidity', unit: '%', color: '#4ecdc4' },
precipitation: { name: 'Precipitation', unit: 'mm', color: '#45b7d1' },
wind_speed_10m: { name: 'Wind Speed', unit: 'm/s', color: '#96ceb4' }
};
// Fetch weather data from Open-Meteo
async function fetchWeatherData() {
const latitude = document.getElementById('latitude').value;
const longitude = document.getElementById('longitude').value;
const variable = document.getElementById('variable').value;
const errorDiv = document.getElementById('error');
const loadingDiv = document.getElementById('loading');
const chartDiv = document.getElementById('chart');
const metadataDiv = document.getElementById('metadata');
// Validate inputs
if (!latitude || !longitude) {
errorDiv.textContent = 'Please enter valid latitude and longitude';
errorDiv.style.display = 'block';
return;
}
errorDiv.style.display = 'none';
loadingDiv.style.display = 'block';
chartDiv.innerHTML = '';
metadataDiv.innerHTML = '';
try {
// Fetch 7-day forecast data
const now = new Date();
const endDate = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
const params = new URLSearchParams({
latitude: latitude,
longitude: longitude,
hourly: variable,
timezone: 'auto'
});
const response = await fetch(`${FORECAST_API}?${params}`);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
loadingDiv.style.display = 'none';
// Process and display data
displayChart(data, variable);
displayMetadata(data, variable);
} catch (error) {
loadingDiv.style.display = 'none';
errorDiv.textContent = `Error: ${error.message}`;
errorDiv.style.display = 'block';
console.error(error);
}
}
// Display chart using Observable Plot
function displayChart(data, variable) {
const chartDiv = document.getElementById('chart');
const hourly = data.hourly;
const times = hourly.time;
const values = hourly[variable];
// Transform data for plotting
const chartData = times.map((time, index) => ({
time: new Date(time),
value: values[index],
hour: new Date(time).toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
})
}));
const variableConfig = VARIABLES[variable];
// Create Observable Plot
const plot = Plot.plot({
title: `${variableConfig.name} - 7 Day Forecast`,
width: chartDiv.offsetWidth,
height: 400,
margin: { left: 60, right: 20, top: 40, bottom: 50 },
x: {
type: 'time',
label: 'Time'
},
y: {
label: `${variableConfig.name} (${variableConfig.unit})`
},
marks: [
Plot.line(chartData, {
x: 'time',
y: 'value',
stroke: variableConfig.color,
strokeWidth: 2
}),
Plot.dot(chartData, {
x: 'time',
y: 'value',
fill: variableConfig.color,
r: 3,
opacity: 0.6
})
],
style: {
fontSize: '12px',
fontFamily: 'system-ui'
}
});
chartDiv.appendChild(plot);
}
// Display metadata
function displayMetadata(data, variable) {
const metadataDiv = document.getElementById('metadata');
const hourly = data.hourly;
const values = hourly[variable];
// Calculate statistics
const min = Math.min(...values);
const max = Math.max(...values);
const avg = (values.reduce((a, b) => a + b, 0) / values.length).toFixed(2);
const variableConfig = VARIABLES[variable];
// Get location info
const location = `${data.latitude.toFixed(2)}°, ${data.longitude.toFixed(2)}°`;
const timezone = data.timezone;
const lastUpdate = new Date().toLocaleString();
const metadata = [
{ label: 'Location', value: location },
{ label: 'Timezone', value: timezone },
{ label: 'Minimum', value: min.toFixed(2), unit: variableConfig.unit },
{ label: 'Maximum', value: max.toFixed(2), unit: variableConfig.unit },
{ label: 'Average', value: avg, unit: variableConfig.unit },
{ label: 'Last Updated', value: lastUpdate }
];
metadataDiv.innerHTML = metadata.map(item => `
<div class="metadata-item">
<div class="metadata-label">${item.label}</div>
<div class="metadata-value">
${item.value}
${item.unit ? `<span class="metadata-unit">${item.unit}</span>` : ''}
</div>
</div>
`).join('');
}
// Fetch data on page load with default values
window.addEventListener('load', () => {
fetchWeatherData();
});
|