From df996bda501bb4cdac95ec6cb3443b772e24a0c9 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 14 Apr 2024 23:58:52 +0200 Subject: meteogram: refactor --- plotter/meteogram.py | 105 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 44 deletions(-) (limited to 'plotter/meteogram.py') diff --git a/plotter/meteogram.py b/plotter/meteogram.py index a04fe34..7fca360 100755 --- a/plotter/meteogram.py +++ b/plotter/meteogram.py @@ -24,22 +24,12 @@ def run(data, plots, output='.'): return index -def _plot(data, output, name, lat, lon): - index = [] - - data = data.sel(latitude=lat, longitude = lon, method='nearest') - - 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=(12, 10), layout="constrained") - - - # start figure and set axis - ax = fig.add_subplot(HEIGHT,1,(1,4)) +def _get_next_subplot(size, counter=0): + ret = (counter + 1, counter + size) + counter += size + return counter, ret +def _add_cloudcov(ax, data): ax.set_ylabel('Pressure level [hPa]') clc = ax.contourf(data.valid_time, data.isobaricInhPa, data.ccl.transpose(), cmap='clcov', vmin=0, vmax=100, levels=9) @@ -47,63 +37,89 @@ def _plot(data, output, name, lat, lon): # 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.valid_time, data.isobaricInhPa, data.t.metpy.convert_units('degC').transpose()) ax.clabel(cf, inline=True, fontsize=10) + # TODO convert to knots? barbs = ax.barbs(data.valid_time, data.isobaricInhPa, data.u.transpose(), data.v.transpose()) ax.invert_yaxis() +def _add_temp_dewpoint(ax, data): ### Temp + Dewpoint - ax2 = fig.add_subplot(HEIGHT,1,(5,6),sharex=ax) - ax2.plot(data.valid_time, data.t2m.metpy.convert_units('degC').transpose(), color='red', label='Temperature (2m)') - ax2.plot(data.valid_time, mpcalc.dewpoint_from_relative_humidity(data.t2m, data.r2).transpose(), color='blue', label='Dewpoint (2m)') - ax2.plot(data.valid_time, data.sel(isobaricInhPa=850.0).t.metpy.convert_units('degC').transpose(), color='grey', label='Tempreature (850hPa)') - ax2.set_ylabel('Temperature [degC]') - ax2.legend(loc='lower right') - - - ## MSLP - ax3 = fig.add_subplot(HEIGHT,1,(7,8),sharex=ax) - ax3.plot(data.valid_time, data.prmsl.metpy.convert_units('hPa').transpose(), color='black', label='Temperature (2m)') - ax3.set_ylabel('Mean Sea Level Pressure [hPa]') - #ax3.legend(loc='lower right') + ax.plot(data.valid_time, data.t2m.metpy.convert_units('degC').transpose(), color='red', label='Temperature (2m)') + ax.plot(data.valid_time, mpcalc.dewpoint_from_relative_humidity(data.t2m, data.r2).transpose(), color='blue', label='Dewpoint (2m)') + ax.plot(data.valid_time, data.sel(isobaricInhPa=850.0).t.metpy.convert_units('degC').transpose(), color='grey', label='Tempreature (850hPa)') + ax.set_ylabel('Temperature [degC]') + ax.legend(loc='lower right') + +def _add_mslp(ax, data): + ax.plot(data.valid_time, data.prmsl.metpy.convert_units('hPa').transpose(), color='black', label='Temperature (2m)') + ax.set_ylabel('Mean Sea Level Pressure [hPa]') + +def _add_convective_clouds(ax, data): # TODO: ADD HBAS_CON, HTOP_CON # If none: -500m - - ax4 = ax3.twinx() - ax4.set_ylim(0, 14) - ax4.set_ylabel('Convective Clouds Height [km]') - ax4.bar(data.valid_time, alpha=0.5, + ax.set_ylim(0, 14) + ax.set_ylabel('Convective Clouds Height [km]') + ax.bar(data.valid_time, alpha=0.5, bottom=data.HBAS_CON.metpy.convert_units('km').transpose(), height=(data.hcct.metpy.convert_units('km')-data.HBAS_CON.metpy.convert_units('km')).transpose(), align='edge', width=np.timedelta64(3, 'h')) - # Precip - ax5 = fig.add_subplot(HEIGHT,1,(9,10),sharex=ax) - ax5.set_ylabel('Total precipitation [mm]') - ax5.set_ylim(0, 30) - ax5.bar(data.valid_time[:-1], data.tp.diff('step').transpose(), width=np.timedelta64(3, 'h'), +def _add_precip(ax, data): + ax.set_ylabel('Total precipitation [mm]') + ax.set_ylim(0, 30) + ax.bar(data.valid_time[:-1], data.tp.diff('step').transpose(), width=np.timedelta64(3, 'h'), align='edge', alpha=0.7, color='green') - ax6 = ax5.twinx() - ax6.set_ylabel('Snow depth [m]') - ax6.set_ylim(bottom=0) - ax6.plot(data.valid_time, data.sde.transpose(), color='blue') + ax_p = ax.twinx() + ax_p.set_ylabel('Snow depth [m]') + ax_p.set_ylim(bottom=0) + ax_p.plot(data.valid_time, data.sde.transpose(), color='blue') + + +def _plot(data, output, name, lat, lon): + data = data.sel(latitude=lat, longitude = lon, method='nearest') + + fig = plt.figure(figsize=(12, 10), layout="constrained") + + sp_cnt, spec = _get_next_subplot(4) + ax = fig.add_subplot(HEIGHT,1,spec) + _add_cloudcov(ax, data) + + sp_cnt, spec2 = _get_next_subplot(2,sp_cnt) + ax2 = fig.add_subplot(HEIGHT,1,spec2,sharex=ax) + _add_temp_dewpoint(ax2, data) + + sp_cnt, spec3 = _get_next_subplot(2,sp_cnt) + ax3 = fig.add_subplot(HEIGHT,1,spec3,sharex=ax) + #ax3.legend(loc='lower right') + _add_mslp(ax3, data) + + ax4 = ax3.twinx() + _add_convective_clouds(ax4, data) + + sp_cnt, spec4 = _get_next_subplot(2,sp_cnt) + ax5 = fig.add_subplot(HEIGHT,1,spec4,sharex=ax) + _add_precip(ax5, data) ### Info Lines + sp_cnt, spec5 = _get_next_subplot(1,sp_cnt) + ax_text = fig.add_subplot(HEIGHT, 1, spec5) + info_lines = [] 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') info_lines.append(f'{name}') info_lines.append(f"INIT : {init_str}") info_lines.append(f"LAT {lat} LON {lon}") + if '_description' in data.attrs: info_lines.append(data.attrs['_description']) - ax_text = fig.add_subplot(HEIGHT, 1, 11) ax_text.text(0, 0, '\n'.join(info_lines), ha='left', va='center', size=10, fontfamily='monospace') ax_text.axis("off") @@ -114,6 +130,7 @@ def _plot(data, output, name, lat, lon): plt.savefig(os.path.join(output, outname)) plt.close('all') + index = [] index.append( { 'file': outname, -- cgit v1.2.3