aboutsummaryrefslogtreecommitdiff
path: root/horizontal.py
blob: d421cd19d325f5fd4350ed695cd956053c1f95b2 (plain)
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
#!/usr/bin/env python3
import xarray as xr

from metpy.plots import MapPanel, PanelContainer, RasterPlot, ContourPlot

config = {
    'source': 'dwd_icon-d2/combined.grib2',
    'plots': [
        {
            'name':'',
            'area': None,
            'layers': [
                {
                    'layertype': 'raster',
                    'field': 'r',
                    'level': 750,
                },
                {
                    'layertype': 'contour',
                    'field': 't',
                    'level': 750,
                    'contours': 5,
                    'clabels': True
                },
            ]
        },
    ]
}

def run(config):
    data = xr.load_dataset(config['source'], engine='cfgrib')

    for plot in config['plots']:
        _plot(data, **plot)

def _plot(data, name, area, layers):

    for step in data.coords['step']:
        this_step = data.sel(step=step)

        map_layers = []

        for layer in layers:
            map_layers.append(_layer(this_step, **layer))

        panel = MapPanel()
        #panel.area = 'de'
        panel.projection = 'mer'
        panel.layers = ['coastline', 'borders']
        panel.plots = map_layers

        pc = PanelContainer()
        pc.size = (8, 8)
        pc.panels = [panel]
        pc.draw()
        pc.show()

def _layer(data, layertype, **kwargs):
    layertypes={
        'raster': {
            'obj': RasterPlot,
            'defaults': {
                'colorbar': 'vertical',
            }
        },
        'contour': {
            'obj': ContourPlot,
            'defaults': {}
        }
    }

    args = layertypes[layertype]['defaults'] | kwargs

    ret = layertypes[layertype]['obj'](**args)
    ret.data = data

    return ret

if __name__ == '__main__':
    run(config)