""" GLAMBIE spinup-geometry (varying geometry, mass-conserving) monthly specific MB. Post-processes the compiled OGGM `run_with_hydro` regional summary output (spinup_historical_run_output_RR.nc), already produced by the oggm_prepro dynamic-spinup pipeline, into a regional + per-glacier netcdf pair with time-varying (dynamic) glacier area. No OGGM workflow/gdirs/SLURM needed here: the heavy dynamical modelling already happened upstream, and the whole computation is vectorized over all glaciers in the region at once. Environment variables (all required): OGGM_HYDRO_SUMMARY_DIR – L4/summary directory containing spinup_historical_run_output_RR.nc and glacier_statistics_RR.csv OGGM_OUTDIR – output directory (both the regional and per-glacier netcdfs land here directly) OGGM_RGI_REG – two-digit GTN-G / RGI region (01-19) """ import logging import os from oggm import utils from glambie_helper import spinup_geometry_specific_mb_region # --------------------------------------------------------------------------- # Environment # --------------------------------------------------------------------------- SUMMARY_DIR = os.environ.get("OGGM_HYDRO_SUMMARY_DIR", "") if not SUMMARY_DIR: raise RuntimeError("Set OGGM_HYDRO_SUMMARY_DIR") OUTPUT_DIR = os.environ.get("OGGM_OUTDIR", "") if not OUTPUT_DIR: raise RuntimeError("Set OGGM_OUTDIR") rgi_reg = os.environ.get("OGGM_RGI_REG", "") if rgi_reg not in [f"{r:02d}" for r in range(1, 20)]: raise RuntimeError("Set OGGM_RGI_REG to a two-digit region code (01-19)") OUTPUT_DIR = utils.mkdir(OUTPUT_DIR) log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) log.info(f"Starting GLAMBIE spinup-geometry run for RGI region {rgi_reg}") # --------------------------------------------------------------------------- # Compile per-glacier + regional monthly specific MB (1975-01 to 2025-12) # --------------------------------------------------------------------------- hydro_nc_path = os.path.join(SUMMARY_DIR, f"spinup_historical_run_output_{rgi_reg}.nc") rgi_area_csv_path = os.path.join(SUMMARY_DIR, f"glacier_statistics_{rgi_reg}.csv") reg = spinup_geometry_specific_mb_region( hydro_nc_path=hydro_nc_path, rgi_area_csv_path=rgi_area_csv_path, y0=1975, y1=2025, outdir=OUTPUT_DIR, filesuffix="_v17a_TIModel", ) log.info( f"RGI{rgi_reg}: {int(reg['n_glaciers_success'])}/{int(reg['n_glaciers_total'])} glaciers, " f"completion={float(reg['completion_rate_pct']):.2f}%" ) log.info("GLAMBIE spinup-geometry run complete.")