From f94fbf99dc0bf9fd8d5d8b2181636d6fd2d007e4 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Thu, 19 Oct 2023 00:32:28 +0200 Subject: first wip meteogram --- config.yaml | 10 +++++- plotter/meteogram.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ run.py | 23 ++++++++++++++ test/cross.py | 81 ----------------------------------------------- 4 files changed, 120 insertions(+), 82 deletions(-) create mode 100755 plotter/meteogram.py delete mode 100755 test/cross.py diff --git a/config.yaml b/config.yaml index d849625..5ba023a 100644 --- a/config.yaml +++ b/config.yaml @@ -21,13 +21,21 @@ aggregator: pressure_levels: [1000, 950, 925, 900, 875, 850, 825, 800, 775, 700, 600, 500, 400, 300, 250, 200, 150, 100] steps: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48] plotter: + - module: plotter.meteogram + aggregator: icon_eu + output: web/data + plots: + - name: meteogram_antersberg + lat: 47.96 + lon: 11.99 + - module: 'plotter.vertical_from_grib' aggregator: icon_eu output: web/data plots: - lat: 47.96 lon: 11.99 - name: Antersberg + name: skewt_antersberg analysis: lcl - module: 'plotter.horizontal' aggregator: icon_eu diff --git a/plotter/meteogram.py b/plotter/meteogram.py new file mode 100755 index 0000000..c1dabee --- /dev/null +++ b/plotter/meteogram.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +import os +import json + +import matplotlib.pyplot as plt + +import xarray as xr +from metpy.interpolate import cross_section +import metpy.calc as mpcalc +from metpy.units import units + +import misc + +def run(data, plots, output='.'): + misc.create_output_dir(output) + index = [] + + for plot in plots: + index.append(_plot(data, output, **plot)) + + return index + +def _plot(data, output, name, lat, lon): + index = [] + + data = data.sel(latitude=lat, longitude = lon, method='nearest') + + #data = data.assign_coords(step=(data.step / (10**9 * 3600))) + data = data.assign_coords(step=(data.step.values.astype(float) * units('ns')).to('hour')) + + init = misc.np_time_convert(data.time.values) + + init_str = init.strftime('%d %b %Y - %HUTC') + init_for_filename = init.strftime('%Y-%m-%d-%HUTC') + + fig = plt.figure(figsize=(10, 10), layout="constrained") + + + # start figure and set axis + ax = fig.add_subplot(5,1,(1,2)) + + ax.set_ylabel('Pressure level [hPa]') + + #clc = ax.plot(data.step.values.astype('float64'), data.isobaricInhPa, data.ccl.transpose()) + #clc = ax.imshow(data.ccl.transpose(), extent=(data.step.values.astype(float).min(), data.step.values.astype(float).max(), data.isobaricInhPa.min(), data.isobaricInhPa.max()), aspect='auto', cmap='Blues_r', vmin=0, vmax=100) + #plt.colorbar(clc, label='clcov') + # Blues_r + clc = ax.contourf(data.step, data.isobaricInhPa, data.ccl.transpose(), cmap='clcov', vmin=0, vmax=100, levels=9) + + # use Format parameter for n/8 + plt.colorbar(clc, label='cloudcov', extendfrac=None, ticks=[100*n/8 for n in range(9)], format=lambda x,_: f'{int(x/12.5)}/8', pad=0.0, fraction=0.015) + + + cf = ax.contour(data.step, data.isobaricInhPa, data.t.metpy.convert_units('degC').transpose()) + ax.clabel(cf, inline=True, fontsize=10) + #plt.colorbar(cf, pad=0, aspect=50) + #plt.colorbar(cf) + + barbs = ax.barbs(data.step, data.isobaricInhPa, data.u.transpose(), data.v.transpose()) + #ax.barbs(data.u, data.v, color='black', length=5, alpha=0.5) + + ax.invert_yaxis() + + ### Second plot + + ax2 = fig.add_subplot(5,1,3,sharex=ax) + ax2.plot(data.step, data.t2m.metpy.convert_units('degC').transpose(), color='red', label='Temperature (2m)') + ax2.plot(data.step, mpcalc.dewpoint_from_relative_humidity(data.t2m, data.r2).transpose(), color='blue', label='Dewpoint (2m)') + ax2.set_ylabel('Temperature [degC]') + ax2.legend(loc='lower right') + + outname = f'{name}_{init_for_filename}.png' + plt.savefig(os.path.join(output, outname)) + plt.close('all') + + index.append( + { + 'file': outname, + 'init': init_str, + 'valid': init_str, + 'valid_offset': '00' + } + ) + + with open(os.path.join(output, f'{name}.index.json'), 'w') as f: + f.write(json.dumps(index, indent=4)) + + return { 'name': name, 'indexfile': f'{name}.index.json' } diff --git a/run.py b/run.py index fa0f598..d0a004a 100755 --- a/run.py +++ b/run.py @@ -4,6 +4,8 @@ import sys import yaml import json import matplotlib.pyplot as plt +import matplotlib as mpl +from matplotlib.colors import LinearSegmentedColormap from metpy.units import units @@ -12,6 +14,27 @@ from metpy.units import units units.define('_gpm = 9.80665 * J/kg') units.define('_gpdm = 10 * _gpm') +# Define custom colormap +clcov_cmap = { + 'red': ( + (0.0, 0.0, 0.0), + (0.1, 0.9, 0.9), + (1.0, 0.3, 0.3), + ), + 'green': ( + (0.0, 0.5, 0.5), + (0.1, 0.9, 0.9), + (1.0, 0.3, 0.3), + ), + 'blue': ( + (0.0, 0.9, 0.9), + (0.1, 0.9, 0.9), + (1.0, 0.3, 0.3), + ), +} + +mpl.colormaps.register(LinearSegmentedColormap('clcov', clcov_cmap)) + FILE = 'config.yaml' if len(sys.argv) > 1: FILE = sys.argv[1] diff --git a/test/cross.py b/test/cross.py deleted file mode 100755 index 5f79a39..0000000 --- a/test/cross.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python3 - -import matplotlib.pyplot as plt -import matplotlib as mpl -from matplotlib.colors import LinearSegmentedColormap - -import xarray as xr -from metpy.interpolate import cross_section -import metpy.calc as mpcalc -from metpy.units import units - -clcov_cmap = { - 'red': ( - (0.0, 0.0, 0.0), - (0.1, 0.9, 0.9), - (1.0, 0.3, 0.3), - ), - 'green': ( - (0.0, 0.5, 0.5), - (0.1, 0.9, 0.9), - (1.0, 0.3, 0.3), - ), - 'blue': ( - (0.0, 0.9, 0.9), - (0.1, 0.9, 0.9), - (1.0, 0.3, 0.3), - ), -} - -mpl.colormaps.register(LinearSegmentedColormap('clcov', clcov_cmap)) - -# backend_kwargs={'filter_by_keys':{'typeOfLevel': 'heightAboveGround','level':2}} -data = xr.load_dataset('dwd_icon-eu/combined.grib2', engine='cfgrib') - - -lat, lon = (47.96, 11.99) -data = data.sel(latitude=lat, longitude = lon, method='nearest') - -#data = data.assign_coords(step=(data.step / (10**9 * 3600))) -data = data.assign_coords(step=(data.step.values.astype(float) * units('ns')).to('hour')) - -print(data) - -fig = plt.figure(figsize=(5, 5), layout="constrained") - - -# start figure and set axis -ax = fig.add_subplot(5,1,(1,2)) - -ax.set_ylabel('Pressure level [hPa]') - -#clc = ax.plot(data.step.values.astype('float64'), data.isobaricInhPa, data.ccl.transpose()) -#clc = ax.imshow(data.ccl.transpose(), extent=(data.step.values.astype(float).min(), data.step.values.astype(float).max(), data.isobaricInhPa.min(), data.isobaricInhPa.max()), aspect='auto', cmap='Blues_r', vmin=0, vmax=100) -#plt.colorbar(clc, label='clcov') -# Blues_r -clc = ax.contourf(data.step, data.isobaricInhPa, data.ccl.transpose(), cmap='clcov', vmin=0, vmax=100, levels=9) - -# use Format parameter for n/8 -plt.colorbar(clc, label='cloudcov', extendfrac=None, ticks=[100*n/8 for n in range(9)], format=lambda x,_: f'{int(x/12.5)}/8', pad=0.0, fraction=0.015) - - -cf = ax.contour(data.step, data.isobaricInhPa, data.t.metpy.convert_units('degC').transpose()) -ax.clabel(cf, inline=True, fontsize=10) -#plt.colorbar(cf, pad=0, aspect=50) -#plt.colorbar(cf) - -barbs = ax.barbs(data.step, data.isobaricInhPa, data.u.transpose(), data.v.transpose()) -#ax.barbs(data.u, data.v, color='black', length=5, alpha=0.5) - -ax.invert_yaxis() - -### Second plot - -ax2 = fig.add_subplot(5,1,3,sharex=ax) -ax2.plot(data.step, data.t2m.metpy.convert_units('degC').transpose(), color='red', label='Temperature (2m)') -ax2.plot(data.step, mpcalc.dewpoint_from_relative_humidity(data.t2m, data.r2).transpose(), color='blue', label='Dewpoint (2m)') -ax2.set_ylabel('Temperature [degC]') -ax2.legend(loc='lower right') - -plt.show() - -- cgit v1.2.3