"""Slide figure: model agreement with the observation-based annual series before and after 2000. Reference: WGMS-DUS combined (annual, hydrological years), the only observation-based annual series covering 1976-2025. Models are summed over exactly the same hydrological years. Usage: python plot_correlation_periods.py Output: plots/correlation_models_vs_dus_periods.png and .csv """ import itertools import os import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy as np import pandas as pd from plot_overview import DATA, DATASETS, OUT, REGIONS, INK, MUTED, style_axis MODELS = ['oggm_fix', 'fujita', 'rounce', 'huss'] PERIODS = {'early': (1976, 1999), 'late': (2000, 2023)} # by the year in which the hydrological year ends C_MM, C_WORSE, C_BETTER = '#2a78d6', '#eb6834', '#8f8e86' def monthly(rid, key): d = pd.read_csv(os.path.join(DATA, f'{REGIONS[rid][0]}_{DATASETS[key][0]}.csv'), parse_dates=['start_date']) return pd.Series(d.glacier_change_observed.values, index=d.start_date) def hydro_table(rid): """WGMS-DUS combined and the four models on the same hydrological years (index: end year).""" dus = pd.read_csv(os.path.join(DATA, f'{REGIONS[rid][0]}_combined_wgms-dus_combined.csv'), parse_dates=['start_date', 'end_date']) ms = {k: monthly(rid, k) for k in MODELS} rec = {} for x in dus.itertuples(): v = {k: s[(s.index >= x.start_date) & (s.index < x.end_date)] for k, s in ms.items()} if all(len(a) == 12 for a in v.values()): rec[x.end_date.year] = dict(dus=x.glacier_change_observed, **{k: a.sum() for k, a in v.items()}) return pd.DataFrame(rec).T def main(): rows = [] for rid in REGIONS: t = hydro_table(rid) r = dict(rid=rid, region=REGIONS[rid][1]) for p, (a, b) in PERIODS.items(): s = t.loc[a:b] r[f'n_{p}'] = len(s) r[f'mm_{p}'] = np.mean([s[x].corr(s[y]) for x, y in itertools.combinations(MODELS, 2)]) r[f'obs_{p}'] = np.mean([s[x].corr(s.dus) for x in MODELS]) rows.append(r) t = pd.DataFrame(rows) t['change'] = t.obs_early - t.obs_late t = t.sort_values('obs_late', ascending=False).reset_index(drop=True) t.to_csv(os.path.join(OUT, 'correlation_models_vs_dus_periods.csv'), index=False) n_worse = int((t.change < 0).sum()) fig, ax = plt.subplots(figsize=(12, 7.6)) y = np.arange(len(t)) for yi, r in t.iterrows(): c = C_WORSE if r.change < 0 else C_BETTER ax.annotate('', xy=(r.obs_early, yi), xytext=(r.obs_late, yi), arrowprops=dict(arrowstyle='-|>', color=c, lw=2.2, mutation_scale=16, shrinkA=6, shrinkB=2), zorder=2) ax.plot(t.obs_late, y, 'o', ms=9, mfc='white', mec='#52514c', mew=1.8, zorder=3) ax.plot(t.mm_early, y, 'o', ms=10, color=C_MM, mec='white', mew=1.5, zorder=3) style_axis(ax) ax.lines[-1].remove() ax.axvline(0, color=MUTED, lw=0.8, zorder=1) ax.set_yticks(y) ax.set_yticklabels([f'{r} {n}' for r, n in zip(t.rid, t.region)], fontsize=11.5, color=INK) ax.invert_yaxis() ax.set_xlim(-0.45, 1.0) ax.tick_params(axis='x', labelsize=11) ax.set_xlabel('Correlation of annual mass balance (hydrological years)', fontsize=11.5, color=MUTED) ax.plot([], [], 'o', ms=9, mfc='white', mec='#52514c', mew=1.8, ls='', label='models with WGMS-DUS, 2000-2023') ax.plot([], [], marker=r'$\rightarrow$', ms=16, color=C_WORSE, ls='', label='... and 1976-1999, lower') ax.plot([], [], marker=r'$\rightarrow$', ms=16, color=C_BETTER, ls='', label='... and 1976-1999, higher or equal') ax.plot([], [], 'o', ms=9, color=C_MM, ls='', label='models with each other, 1976-1999') ax.legend(loc='upper left', frameon=False, fontsize=11, handletextpad=0.4) fig.suptitle(f'Before 2000, model-observation agreement drops in {n_worse} of 19 regions', x=0.012, y=0.985, ha='left', fontsize=15, color=INK, fontweight='bold') fig.text(0.012, 0.925, 'Mean correlation of four models (OGGM, Fujita, Rounce, Huss) with WGMS-DUS combined, the observation-based ' 'annual series (in-situ anomalies\ncalibrated to geodetic trends), on its own hydrological years. ' f'Median over regions: {t.obs_late.median():.2f} for 2000-2023, {t.obs_early.median():.2f} for 1976-1999. ' 'All models share ERA5 forcing.', fontsize=10.5, color=MUTED, ha='left', va='top', linespacing=1.4) fig.tight_layout(rect=(0, 0, 1, 0.86)) fig.savefig(os.path.join(OUT, 'correlation_models_vs_dus_periods.png'), dpi=150, facecolor='white') plt.close(fig) print(t.round(2).to_string(index=False)) if __name__ == '__main__': main()