-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmhi_rain.html
executable file
·379 lines (312 loc) · 12.3 KB
/
smhi_rain.html
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
<html>
<head>
<!--<script type="text/javascript" src="dygraph-combined.js"></script>-->
<link rel="stylesheet" href="dygraph.css">
<link rel="icon" href="dashboard/images/favicon.png" type="image/png" />
<script type="text/javascript" src="dygraph.min.js"></script>
<title>SMHI nederbörd Kållered</title>
<style>
#my_table { border-collapse: collapse; text-align: center; width: 100%; }
#my_table th { background: #8aa3f8; font-weight: bold; font-size: 100%}
#my_table td { width: 8%; border: 1px solid #cccccc; overflow: hidden; font-size: 100%; }
</style>
</head>
<body>
<a href="https://www.smhi.se/data/meteorologi/ladda-ner-meteorologiska-observationer#param=precipitation24HourSum,stations=all,stationid=72360">SMHI OpenData Kållered stationd id 72360</a>
<br>
<h2 id="abro_title">Nederbörd Kållered</h2>
<div id="graphdiv1" style="width:600px; height:300px;"></div>
<br>
<br>
<select id="select_position_dropdown" onchange="position_changed()" style="width: 120px; height: 25px">
<option value="kallered" selected>Kållered</option>
<option value="goteborg">Göteborg</option>
</select>
<div>
<div id="graphdiv" style="width:600px; height:300px;"></div>
</div>
<br>
<br>
<div>
<select id="select_year_dropdown" onchange="year_changed()" style="width: 120px; height: 25px">
</select>
<span id="info"> </span>
<table id=my_table style="width:70%;">
</table>
</div>
<br>
<br>
<span id="debug_info"> </span>
<br>
<br>
<script type="text/javascript">
function get_storage_size(){
// From https://stackoverflow.com/questions/4391575/how-to-find-the-size-of-localstorage
var allStrings = '';
for(var key in window.localStorage){
if(window.localStorage.hasOwnProperty(key)){
allStrings += window.localStorage[key];
}
}
return allStrings ? 3 + ((allStrings.length*16)/(8*1024)) + ' KB' : 'Empty (0 KB)';
}
function format_with_two_characters(month) {
if (month<10){
return "0" + month;
}
return month;
}
function position_changed() {
elem = document.getElementById("select_position_dropdown");
var position = elem.value;
populate_local_storage_for_position(position);
year_changed();
create_rain_year_dynagraph();
}
function year_changed() {
elem = document.getElementById("select_year_dropdown");
year = elem.value;
create_rain_year_html_table(year);
}
function populate_year_dropdown() {
var rightNow = new Date();
var currYear = rightNow.getFullYear();
var opts = '';
for (var year=currYear; year>=1995; year--)
opts += `<option value=${year}>${year}`;
dropdown = document.getElementById("select_year_dropdown");
dropdown.innerHTML = opts;
dropdown.value = currYear;
}
function round(value, decimals) {
// From https://www.jacklmoore.com/notes/rounding-in-javascript/
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
function show_info(year)
{
var year_total = round(localStorage.getItem(year), 1);
info_elem = document.getElementById("info");
var location = "Kållered";
if (localStorage.getItem('position') == 'kallered') {
location = "Kållered"
} else {
location = "Göteborg"
}
info_elem.innerHTML = `<b>${location} ${year}, totalt ${year_total} mm</b>`
}
function get_html_for_sum(sum_months) {
var html = "<tr id=\"sum\">\
<th>SUM</th>";
for (var month=1; month<=12; month++) {
value = round(sum_months[month], 1);
html += "<th>" + value + "</th>";
}
html += "</tr>"
return html;
}
function create_html_for_year(year) {
var html = "";
var year_total = 0;
var month_sums = {1:0.0, 2:0.0, 3:0.0, 4:0.0, 5:0.0, 6:0.0, 7:0.0, 8:0.0, 9:0.0, 10:0.0, 11:0.0, 12:0.0};
for (var day=1; day<=31; day++) {
html += "<tr id=\"d" + day + "\">";
html += "<th>" + day +"</th>\r\n";
for (var month=1; month<=12; month++) {
key = year + "-" + format_with_two_characters(month) + "-" + format_with_two_characters(day);
var value = localStorage.getItem(key)
if (value) {
month_sums[month] += parseFloat(value);
if (value == 0) {
value = "";
}
html += "<td data-pcp=\"" + value + "\">" + value + "</td>";
} else {
html += "<td data-pcp=\"-1\">x</td>";
}
}
html += "</tr>";
}
html += get_html_for_sum(month_sums);
show_info(year);
return html;
}
// This function draws bars for a single series. See
// multiColumnBarPlotter below for a plotter which can draw multi-series
// bar charts.
function barChartPlotter(e) {
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0);
ctx.fillStyle = darkenColor(e.color);
// Find the minimum separation between x-values.
// This determines the bar width.
var min_sep = Infinity;
for (var i = 1; i < points.length; i++) {
//console.log(points[i].canvasx)
var sep = points[i].canvasx - points[i - 1].canvasx;
//fredbond also check sep != 0
if (sep < min_sep & sep != 0) min_sep = sep;
}
var bar_width = Math.floor(2.0 / 3 * min_sep);
// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
var p = points[i];
var center_x = p.canvasx;
ctx.fillRect(center_x - bar_width / 2, p.canvasy,
bar_width, y_bottom - p.canvasy);
ctx.strokeRect(center_x - bar_width / 2, p.canvasy,
bar_width, y_bottom - p.canvasy);
}
}
function darkenColor(colorStr) {
// Defined in dygraph-utils.js
var color = Dygraph.toRGB_(colorStr);
color.r = Math.floor((255 + color.r) / 2);
color.g = Math.floor((255 + color.g) / 2);
color.b = Math.floor((255 + color.b) / 2);
return 'rgb(' + color.r + ',' + color.g + ',' + color.b + ')';
}
/*function get_rain_precipitation_per_year() {
localStorage.getItem(date);
}*/
function get_rain_per_year_as_array() {
var rightNow = new Date();
var currYear = rightNow.getFullYear();
var year_precipitation = [];
year_precipitation.push([1994, 0]);
for (var year=1995; year<=currYear; year++) {
var value = localStorage.getItem(year);
year_precipitation.push([year, parseFloat(value)])
}
next_year = currYear + 1;
year_precipitation.push([next_year, 0]);
return year_precipitation;
}
function create_rain_year_dynagraph() {
clear_element("graphdiv");
var year_precipitation = get_rain_per_year_as_array();
//console.log(year_precipitation);
g = new Dygraph(document.getElementById("graphdiv"),
year_precipitation,
{
labels: [ "År", "Nederbörd" ],
showRangeSelector:true,
plotter: barChartPlotter,
includeZero: true,
colors: ["rgb(138, 163, 248)"],
width: 840,
height: 280,
title: 'Nederbörd',
xlabel: 'År',
ylabel: 'mm',
axes: {
x: {
drawGrid: true
}
}
}
);
}
function clear_element(id) {
var elem = document.getElementById(id);
elem.innerText = "";
}
function create_rain_year_html_table(year) {
clear_element('my_table');
var table_head_html = '<thead>\
<tr> <th> </th> <th>Jan</th> <th>Feb</th> <th>Mar</th> <th>Apr</th>\
<th>Maj</th> <th>Jun</th> <th>Jul</th> <th>Aug</th>\
<th>Sep</th> <th>Okt</th> <th>Nov</th> <th>Dec</th> </tr>\
</thead>'
document.getElementById('my_table').innerHTML += table_head_html;
var table_content_html = create_html_for_year(year);
document.getElementById('my_table').innerHTML += table_content_html;
}
function parse_file_and_populate_local_storage() {
var content = this.responseText.split('\r\n');
if (content.length == 1) { // Probably linux file
content = this.responseText.split('\n');
}
var index=0;
var year_total = {}
console.log("------------------------------------------")
console.log(content)
for (index = 1; index < content.length; ++index) {
var date_value = content[index].split(',')
if (date_value.length == 1) {
//date_value = content[index].split(';')
}
if (date_value != "" || date_value != null) {
var date = date_value[0];
var value = date_value[1];
var current_value = localStorage.getItem(date);
year = date.substring(0, 4);
if (year) {
if (year_total[year]) {
year_total[year] += parseFloat(value);
} else {
year_total[year] = parseFloat(value);
}
}
if (current_value == null || parseFloat(value) != current_value)
{
localStorage.setItem(date, parseFloat(value));
}
}
}
for(var year in year_total) {
var value = year_total[year];
localStorage.setItem(year, parseFloat(value));
//console.log("For year " + year + " set " + value);
}
show_debug_info();
// How did this work before?
//localStorage.setItem('position', position);
}
function show_debug_info(){
var localStorageSpace = get_storage_size();
console.log("localStorageSpace=" + localStorageSpace);
debug_elem = document.getElementById("debug_info");
debug_elem.innerHTML = `Data Size: ${localStorageSpace}`
}
function clear_local_storage_if_needed(position) {
last_position = localStorage.getItem('position')
if (last_position != position) {
localStorage.clear()
console.log("Localstorage cleared since changed from " + last_position + " to " + position);
} else {
console.log('Same position -> No Clear');
}
}
function get_file_url_for_position(position) {
file_url = "data/smhi_opendata_kallered_72360.csv";
if (position == "kallered") {
file_url = "data/smhi_opendata_kallered_72360.csv";
} else {
file_url = "data/smhi_opendata_goteborg_71420.csv";
}
return file_url;
}
function populate_local_storage_for_position(position) {
file_url = get_file_url_for_position(position);
clear_local_storage_if_needed(position);
var xhr = new XMLHttpRequest();
xhr.onload = parse_file_and_populate_local_storage;
let async = false; // Make XMLHttpRequest synchronous, TODO this is deprecated
xhr.open('GET', file_url, async);
xhr.send();
localStorage.setItem('position', position);
}
g1 = new Dygraph(document.getElementById("graphdiv1"),
"data/smhi_opendata_kallered_72360.csv",
{
showRangeSelector:true,
title: "Nederbörd",
ylabel: 'mm',
xlabel: "Tidpunkt",
});
populate_year_dropdown();
position_changed();
</script>
</body>
</html>