"""Plot glacier-area weighted Alps temperature anomalies (vs 1961-1990). Reads ``series/tas__anom_1961-1990.csv`` (run regional.py first). Thin lines: annual values; thick lines: centered running mean, drawn only where at least min_window years are available (default: the full window, so the line stops window // 2 years before the ends of each series). Usage: python plot_tas_anomaly.py [annual|jjas] [start_year] [window] [min_window] (defaults: annual 1700 11 11) """ import os import sys import matplotlib.pyplot as plt import pandas as pd HERE = os.path.dirname(os.path.abspath(__file__)) LABELS = {'lmr_v2.1': 'LMR v2.1', 'lmr_seas': 'LMR Seasonal', 'berkeley-earth': 'Berkeley Earth', 'histalp': 'HISTALP', 'gswp3-w5e5': 'GSWP3-W5E5', 'cru': 'CRU TS 4.04'} COLORS = {'lmr_v2.1': '#2a78d6', 'lmr_seas': '#4a3aa7', 'berkeley-earth': '#eb6834', 'histalp': '#1baf7a', 'gswp3-w5e5': '#eda100', 'cru': '#e87ba4'} season = sys.argv[1] if len(sys.argv) > 1 else 'annual' start = int(sys.argv[2]) if len(sys.argv) > 2 else 1700 window = int(sys.argv[3]) if len(sys.argv) > 3 else 11 min_window = int(sys.argv[4]) if len(sys.argv) > 4 else window df = pd.read_csv(os.path.join(HERE, 'series', f'tas_{season}_anom_1961-1990.csv'), index_col=0) fig, ax = plt.subplots(figsize=(11, 6.5)) ax.axhline(0, color='#52514e', lw=0.8) ax.axvspan(1961, 1990, color='#d0cfc8', alpha=0.35, lw=0) ax.axvline(1850, color='k', lw=1) for name in df.columns: s = df[name].dropna() # reindex so that missing years (e.g. BE 1752) break the line s = s.reindex(range(s.index[0], s.index[-1] + 1)) c = COLORS[name] # LMR Seasonal has no September: its summer is JJA jja = ', JJA' if (name == 'lmr_seas' and season == 'jjas') else '' # single missing years (BE 1752) are interpolated for the running mean # only, and it is computed before cutting at the start year sm = s.interpolate(limit=1, limit_area='inside').rolling( window, center=True, min_periods=min_window).mean().loc[start:] s = s.loc[start:] ax.plot(s.index, s, color=c, lw=0.6, alpha=0.4) ax.plot(sm.index, sm, color=c, lw=2, label=f'{LABELS[name]}{jja} ({s.dropna().index[0]}–' f'{s.dropna().index[-1]})') ax.set_xlim(start, df.index[-1] + 2) ax.set_ylabel('Temperature anomaly vs 1961–1990 (K)') what = 'Annual' if season == 'annual' else season.upper() ax.set_title(f'{what} temperature, glacier-area weighted over the Alps ' f'(RGI6.2 11-01). Thin: yearly, thick: {window}-yr running mean', loc='left', fontsize=11) ax.grid(axis='y', color='#e6e5e0', lw=0.6) for sp in ['top', 'right']: ax.spines[sp].set_visible(False) ax.legend(frameon=False, loc='upper left', fontsize=9) fig.tight_layout() os.makedirs(os.path.join(HERE, 'figs'), exist_ok=True) f = os.path.join(HERE, 'figs', f'tas_{season}_anom_{start}.png') fig.savefig(f, dpi=150) print(f)