-
Notifications
You must be signed in to change notification settings - Fork 4
/
meteogram-dev.html
749 lines (695 loc) · 23.2 KB
/
meteogram-dev.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
<!DOCTYPE script PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<script src="libraries/jquery/3.2.1/jquery.min.js"></script>
<script src="libraries/custom/sun.js"></script>
<script src="libraries/custom/meteogram.js"></script>
<script src="highstock/code/highstock-patched.js"></script>
<script src="highstock/code/modules/windbarb.js"></script>
<div id="beta_dwd_container" style="min-height: 300px; margin: 0 auto">
<div style="margin-top: 100px; text-align: center" id="loading">
<i class="fa fa-spinner fa-spin"></i> Lade Wetterdaten...
</div>
</div>
<script type="text/javascript">
/**
* ============================================================================
*
* Meteogram for DWD MOSMIX forecast stations. Dervied by Highcharts meteogram
* demo.
*
* ============================================================================
*
* Author: Michael Buchfink
*
* ============================================================================
*
* Last updated: 17.03.2018
*
* ============================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
**/
var hours_short = 24;
var hours_long = 60;
var mosmix_url = 'https://buwx.de/charts/meteogram/mosmix.php?station=10389&title=Berlin';
// var mosmix_url = 'data/10738.json';
/**
* Statistically adjust forecast parameters, based on historical data from private Station Scharnhausen.
*
* Call: glm(formula = diff1 ~ sun_pos + dwd_n + diff_td + dwd_ff, data = dftt)
*
* Coefficients:
* (Intercept) sun_pos dwd_n diff_td dwd_ff
* -1.3731 0.0137 0.0942 0.0183 0.0445
*
* Degrees of Freedom: 26099 Total (i.e. Null); 26095 Residual
* Null Deviance: 37800
* Residual Deviance: 25000 AIC: 72900
*
*
* Call: glm(formula = diff2 ~ diff_td + dwd_n + dwd_ff, data = dttd)
*
* Coefficients:
* (Intercept) diff_td dwd_n dwd_ff
* -0.5052 0.1119 0.0831 0.0119
*
* Degrees of Freedom: 26099 Total (i.e. Null); 26096 Residual
* Null Deviance: 23100
* Residual Deviance: 16900 AIC: 62700
*/
function adjust(meteogram) {
meteogram.dwd.station.title = 'Scharnhausen';
meteogram.dwd.station.titleShort = 'Scharnhausen';
var lon = meteogram.dwd.station.coordinates[0];
var lat = meteogram.dwd.station.coordinates[1];
for (i=0; i < hours_long; i++) {
var time = new Date(meteogram.temperature[i].x);
var year = time.getUTCFullYear();
var month = time.getUTCMonth();
var day = time.getUTCDate();
var hour = time.getUTCHours();
var min = time.getUTCMinutes();
var offset = hour + min/60 - 1.5;
var sun_pos = calcZenith(calcJD(year, month + 1, day), offset, lat, -lon);
var dwd_n = parseFloat(meteogram.wmo_n[i].n);
var diff_td = parseFloat(meteogram.dwd.station.TTT[i]) - parseFloat(meteogram.dwd.station.Td[i]);
var dwd_ff = parseFloat(meteogram.dwd.station.FF[i]) * 3.6;
var diff1 = -1.3731 + 0.0137*sun_pos + 0.0942*dwd_n + 0.0183*diff_td + 0.04454*dwd_ff;
var diff2 = -0.5052 + 0.1119*diff_td + 0.0831*dwd_n + 0.0119*dwd_ff;
var tt = Math.round((parseFloat(meteogram.dwd.station.TTT[i])-273.15+diff1) * 10)/10;
var td = Math.round((parseFloat(meteogram.dwd.station.Td[i])-273.15+diff2) * 10)/10;
if (td > tt) {
td = tt;
}
meteogram.temperature[i].y = tt;
meteogram.dewpoint[i].y = td;
}
}
/**
* Meteogram derived by Highcharts meteogram demo for using DWD MESOMIX data.
*/
function Meteogram(dwd, container) {
this.temperature = [];
this.dewpoint = [];
this.pressure = [];
this.precipitation = [];
this.wind = [];
this.sunshine = [];
this.extrema = [];
this.temperature_short = [];
this.dewpoint_short = [];
this.pressure_short = [];
this.precipitation_short = [];
this.wind_short = [];
this.sunshine_short = [];
this.extrema_short = [];
this.wmo_n = [];
this.wmo_ww = [];
this.plotbands = [];
// Initialize
this.dwd = dwd;
this.container = container;
// Run
this.parseDwdData();
}
/**
* Draw the weather symbols.
*/
Meteogram.prototype.drawWmoSymbols = function (chart) {
var meteogram = this;
jQuery.each(meteogram.wmo_n, function (i, wmo) {
if (wmo.image) {
wmo.image.destroy();
wmo.image = null;
}
});
jQuery.each(meteogram.wmo_ww, function (i, wmo) {
if (wmo.image) {
wmo.image.destroy();
wmo.image = null;
}
});
var n_size = Math.min(10, chart.plotWidth / chart.series[0].data.length);
var ww_size = Math.min(16, chart.plotWidth / chart.series[0].data.length);
jQuery.each(chart.series[0].data, function (i, point) {
if (!meteogram.wmo_n[i].image) {
var image = chart.renderer.image(
'wmo/n/' + meteogram.wmo_n[i].n + '.svg',
point.plotX + chart.plotLeft - n_size/2,
chart.plotTop + chart.plotHeight + n_size/2,
n_size, n_size
)
.attr({
zIndex: 2
})
.add();
meteogram.wmo_n[i].image = image;
}
if (!meteogram.wmo_ww[i].image && meteogram.wmo_ww[i].ww) {
var image = chart.renderer.image(
'wmo/ww/' + meteogram.wmo_ww[i].ww + '.svg',
point.plotX + chart.plotLeft - ww_size/2,
point.plotY + chart.plotTop - 15,
ww_size, ww_size
)
.attr({
zIndex: 2
})
.add();
meteogram.wmo_ww[i].image = image;
}
});
}
Meteogram.prototype.getChartOptions = function () {
var meteogram = this;
return {
chart: {
renderTo: this.container,
marginBottom: 70,
marginRight: 40,
marginTop: 50,
height: 380,
plotBorderWidth: 1,
alignTicks: false,
plotBackgroundColor: '#fffdf0',
events: {
redraw: function () {
meteogram.drawWmoSymbols(this);
}
}
},
time: {
useUTC: false
},
title: {
text: 'Vorhersage für ' + this.dwd.station.title,
align: 'left'
},
credits: {
text: 'Quelle: Deutscher Wetterdienst (' + this.dwd.productId + ', angepasst)',
href: 'https://www.dwd.de/opendata',
position: {
x: -40,
y: -2
}
},
tooltip: {
shared: true,
useHTML: true,
headerFormat: '<small>{point.x:%A, %e. %B, %H}-{point.to:%H} Uhr</small><br>'
},
xAxis: [{ // Bottom X axis
type: 'datetime',
tickInterval: 2 * 36e5, // two hours
minorTickInterval: 36e5, // one hour
tickLength: 0,
gridLineWidth: 1,
gridLineColor: '#F0F0F0',
startOnTick: false,
endOnTick: false,
minPadding: 0,
maxPadding: 0,
offset: 40,
showLastLabel: true,
labels: {
format: '{value:%H}'
},
crosshair: true,
plotBands: this.plotbands
}, { // Top X axis
linkedTo: 0,
type: 'datetime',
tickInterval: 24 * 3600 * 1000,
labels: {
format: '{value:%a %e. %b}',
align: 'left',
x: 3,
y: -5,
style: {
whiteSpace: 'nowrap'
}
},
opposite: true,
tickLength: 20,
gridLineWidth: 1
}],
yAxis: [{ // temperature axis
title: {
text: null
},
labels: {
format: '{value}°',
style: {
fontSize: '10px'
},
x: -3
},
plotLines: [{ // zero plane
value: 0,
color: '#BBBBBB',
width: 1,
zIndex: 2
}],
maxPadding: 0.3,
minRange: 8,
tickInterval: 1,
gridLineColor: '#F0F0F0'
}, { // precipitation axis
title: {
text: null
},
labels: {
enabled: false
},
gridLineWidth: 0,
tickLength: 0,
minRange: 1.5,
min: 0
}, { // sunshine axis
title: {
text: null
},
labels: {
enabled: false
},
gridLineWidth: 0,
tickLength: 0,
max: 240,
min: 0
}, { // Air pressure
allowDecimals: false,
title: { // Title on top of axis
text: 'hPa',
offset: 0,
align: 'high',
rotation: 0,
style: {
fontSize: '10px',
color: 'darkgreen'
},
textAlign: 'left',
x: 3
},
labels: {
style: {
fontSize: '8px',
color: 'darkgreen'
},
y: 2,
x: 3
},
gridLineWidth: 0,
opposite: true,
showLastLabel: false,
showInLegend: false
}],
plotOptions: {
series: {
pointPlacement: 'between'
}
},
series: [{
id: 'Temperatur',
name: 'Temperatur',
type: 'spline',
data: this.temperature,
marker: {
enabled: false,
states: {
hover: {
enabled: true
}
}
},
tooltip: {
pointFormat: '<span style="color:{point.color}">\u25CF</span> ' +
'{series.name}: <b>{point.y}°C</b><br/>'
},
zIndex: 2,
color: '#FF3333',
negativeColor: '#48AFE8',
showInLegend: false
}, {
id: 'Taupunkt',
name: 'Taupunkt',
type: 'spline',
data: this.dewpoint,
marker: {
enabled: false,
},
tooltip: {
pointFormat: '<span style="color:{point.color}">\u25CF</span> ' +
'{series.name}: <b>{point.y}°C</b><br/>'
},
dashStyle: 'dash',
zIndex: 2,
color: '#819FF7',
showInLegend: false
}, {
id: 'Niederschlag',
name: 'Niederschlag',
type: 'column',
data: this.precipitation,
color: '#68CFE8',
yAxis: 1,
groupPadding: 0,
pointPadding: 0,
grouping: false,
dataLabels: {
enabled: true,
formatter: function () {
if (this.y > 0) {
return this.y;
}
},
style: {
fontSize: '8px',
color: 'gray'
}
},
tooltip: {
valueSuffix: ' mm'
},
showInLegend: true
}, {
id: 'Sonnenschein',
name: 'Sonnenschein',
type: 'column',
data: this.sunshine,
color: 'gold',
yAxis: 2,
groupPadding: 0,
pointPadding: 0.2,
grouping: false,
dataLabels: {
enabled: false
},
tooltip: {
valueSuffix: ' min'
},
showInLegend: false
}, {
id: 'Luftdruck',
name: 'Luftdruck',
type: 'spline',
data: this.pressure,
color: 'lightgreen',
marker: {
enabled: false
},
shadow: false,
tooltip: {
valueSuffix: ' hPa'
},
dashStyle: 'shortdot',
zIndex: 1,
yAxis: 3,
showInLegend: false
}, {
id: 'Wind',
name: 'Wind',
type: 'windbarb',
data: this.wind,
color: '#434348',
lineWidth: 1.5,
vectorLength: 12,
yOffset: -10,
tooltip: {
pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.value_kmh} km/h {point.direction_text}</b><br/>{point.n_text}<br/>{point.ww_text}'
},
showInLegend: false
}, {
id: 'Extrema',
name: 'Extrema',
type: 'flags',
data: this.extrema,
onSeries : 'Temperatur',
shape: 'squarepin',
zIndex: 5,
style: {
fontSize: '9px',
fontWeight: 'normal'
},
showInLegend: true
}],
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
chart: {
height: 400
},
title: {
text: 'Vorhersage für ' + this.dwd.station.titleShort,
align: 'left'
},
series: [{
id: 'Temperatur',
data: this.temperature_short
}, {
id: 'Taupunkt',
data: this.dewpoint_short
}, {
id: 'Niederschlag',
data: this.precipitation_short
}, {
id: 'Sonnenschein',
data: this.sunshine_short
}, {
id: 'Luftdruck',
data: this.pressure_short
}, {
id: 'Wind',
data: this.wind_short
}, {
id: 'Extrema',
data: this.extrema_short
}]
}
}, {
condition: {
minWidth: 501
},
chartOptions: {
series: [{
id: 'Temperatur',
data: this.temperature
}, {
id: 'Taupunkt',
data: this.dewpoint
}, {
id: 'Niederschlag',
data: this.precipitation
}, {
id: 'Sonnenschein',
data: this.sunshine
}, {
id: 'Luftdruck',
data: this.pressure
}, {
id: 'Wind',
data: this.wind
}, {
id: 'Extrema',
data: this.extrema
}]
}
}]
}
}
}
/**
* Create the chart. This function is called async when the data file is loaded and parsed.
*/
Meteogram.prototype.createChart = function () {
Highcharts.setOptions({
lang: highcharts_de()
});
var meteogram = this;
this.chart = new Highcharts.Chart(this.getChartOptions());
}
Meteogram.prototype.error = function () {
jQuery('#loading').html('<i class="fa fa-frown-o"></i> Daten konnten nicht geladen werden');
}
/**
* Handle the data.
*/
Meteogram.prototype.parseDwdData = function () {
var meteogram = this,
fromTotal, toTotal,
pointStart, i;
if (!this.dwd || !this.dwd.timeSteps || !this.dwd.station) {
return this.error();
}
for (i=0; i < hours_long; i++) {
var from = this.dwd.timeSteps[i];
from = from.replace(/-/g, '/').replace('T', ' ').replace('.000Z', ' UTC');
from = Date.parse(from);
var to = from + 36e5;
// If it is more than an hour between points, show all symbols
if (i === 0) {
fromTotal = from;
pointStart = (from + to) / 2;
}
if (i === hours_long - 1) {
toTotal = to;
}
meteogram.temperature.push({
x: from,
y: Math.round((parseFloat(this.dwd.station.TTT[i])-273.15) * 10)/10,
to: to
});
meteogram.dewpoint.push({
x: from,
y: Math.round((parseFloat(this.dwd.station.Td[i])-273.15) * 10)/10
});
meteogram.pressure.push({
x: from,
y: parseFloat(this.dwd.station.PPPP[i])/100.0
});
meteogram.precipitation.push({
x: from,
y: parseFloat(this.dwd.station.RR1c[i+1])
});
var wind_speed = parseFloat(this.dwd.station.FF[i]);
var wind_direction = parseFloat(this.dwd.station.DD[i]);
meteogram.wind.push({
x: from,
value: wind_speed,
direction: wind_direction,
value_kmh: Math.round(wind_speed * 3.6),
direction_text: directionText(wind_speed, wind_direction)
});
meteogram.sunshine.push({
x: from,
y: Math.round(parseFloat(this.dwd.station.SunD1[i+1])/60)
});
var n = 'Slash';
var n_text = '';
if (this.dwd.station.Neff[i] && this.dwd.station.Neff[i] !== '-') {
var n_value = parseInt(this.dwd.station.Neff[i]);
if (n_value == 0) {
n = '0';
}
else if (n_value <= 10) {
n = '1';
}
else if (n_value <= 30) {
n = '2';
}
else if (n_value <= 40) {
n = '3';
}
else if (n_value <= 50) {
n = '4';
}
else if (n_value <= 60) {
n = '5';
}
else if (n_value <= 80) {
n = '6';
}
else if (n_value < 99) {
n = '7';
}
else {
n = '8';
n_value = 100;
}
n_text = 'Bedeckungsgrad: ' + n_value + '%';
}
meteogram.wmo_n.push({
x: from,
n: n,
text: n_text,
image: null
});
meteogram.wind[i].n_text = n_text;
var ww = null;
var ww_text = '';
if (this.dwd.station.ww[i+1] && this.dwd.station.ww[i+1] !== '-') {
var ww_value = parseInt(this.dwd.station.ww[i+1]);
if (ww_value >= 10) {
ww = ww_value.toString();
ww_text = ww_messages[ww];
}
}
meteogram.wmo_ww.push({
x: from,
ww: ww,
text: ww_text,
image: null
});
meteogram.wind[i].ww_text = ww_text;
}
// adjust(meteogram);
meteogram.temperature_short = meteogram.temperature.slice(0, hours_short);
meteogram.dewpoint_short = meteogram.dewpoint.slice(0, hours_short);
meteogram.pressure_short = meteogram.pressure.slice(0, hours_short);
meteogram.precipitation_short = meteogram.precipitation.slice(0, hours_short);
meteogram.wind_short = meteogram.wind.slice(0, hours_short);
meteogram.sunshine_short = meteogram.sunshine.slice(0, hours_short);
// Initialize plotbands
var lon = this.dwd.station.coordinates[0];
var lat = this.dwd.station.coordinates[1];
var time = new Date(fromTotal);
time.setUTCHours(0, 0, 0, 0);
var sunSet = time.getTime();
do {
var year = time.getUTCFullYear();
var month = time.getUTCMonth() + 1;
var day = time.getUTCDate();
var sunRiseOffset = calcSunriseUTC(calcJD(year, month, day), lat, -lon) - 20;
var sunSetOffset = calcSunsetUTC(calcJD(year, month, day), lat, -lon) + 20;
meteogram.plotbands.push({
color: '#f0fffe',
from: sunSet,
to: time.getTime() + sunRiseOffset * 6e4
});
sunSet = time.getTime() + sunSetOffset * 6e4;
time = new Date(time.getTime() + 24*36e5);
} while (time.getTime() < to);
meteogram.plotbands.push({
color: '#f0fffe',
from: sunSet,
to: toTotal
});
// find extremas
findExtremas(meteogram.temperature, meteogram.extrema);
findExtremas(meteogram.temperature_short, meteogram.extrema_short);
// Create the chart when the data is loaded
this.createChart();
}
// End of the Meteogram protype
// On DOM ready...
jQuery.getJSON(
mosmix_url,
function (dwd) {
window.meteogram = new Meteogram(dwd, 'beta_dwd_container');
}
);
</script>
</body>
</html>