"""Compile the IceBoost v2.0 per-glacier tables used by OGGM. One row per glacier (RGI62, RGI70G) or glacier complex (RGI70C), values taken from the attributes of the Zenodo tif files (not recomputed from the grids): - RGI62/, RGI70G/ : https://zenodo.org/records/17724512 - RGI70C/ : https://zenodo.org/records/21220985 Columns: vol_km3, vol_err_km3, vol_bsl_km3, vol_bsl_err_km3, area_km2 (index: RGI id). For RGI70C, the errors are the linear sum of the errors of the constituent glaciers (as in the Zenodo files). Usage (from the iceboost_v2 folder): python scripts/compile_tables.py RGI62 RGI70G RGI70C --date 20261116 Writes iceboostv2_compiled_{rgi62,rgi70G,rgi70C}_v{date}.parquet """ import argparse import glob import os from concurrent.futures import ProcessPoolExecutor import pandas as pd import rasterio COLS = {'volume': 'vol_km3', 'volume_error': 'vol_err_km3', 'volume_bsl': 'vol_bsl_km3', 'volume_bsl_error': 'vol_bsl_err_km3', 'area': 'area_km2'} def read_attrs(path): with rasterio.open(path) as ds: tags = ds.tags() rid = os.path.basename(path)[:-len('.tif')] # sanity check: file name and id attribute must agree tid = tags.get('id', tags.get('complex_id')) if tid != rid: raise RuntimeError(f'{path}: id attribute is {tid}') out = {'rgi_id': rid} out.update({v: float(tags[k]) for k, v in COLS.items()}) return out def compile_product(product, nproc=8): files = sorted(glob.glob(os.path.join(product, 'rgi*', '*.tif'))) print(f'{product}: {len(files)} files', flush=True) with ProcessPoolExecutor(nproc) as ex: rows = list(ex.map(read_attrs, files, chunksize=200)) df = pd.DataFrame(rows).set_index('rgi_id').sort_index() if not df.index.is_unique: raise RuntimeError(f'{product}: duplicate ids') return df def main(): p = argparse.ArgumentParser() p.add_argument('products', nargs='+', choices=['RGI62', 'RGI70G', 'RGI70C']) p.add_argument('--date', required=True, help='YYYYMMDD version tag') p.add_argument('--nproc', type=int, default=8) args = p.parse_args() for product in args.products: df = compile_product(product, nproc=args.nproc) name = 'rgi62' if product == 'RGI62' else 'rgi' + product[3:] fname = f'iceboostv2_compiled_{name}_v{args.date}.parquet' df.to_parquet(fname, engine='pyarrow', compression='zstd') print(f' -> {fname}: {len(df)} rows, vol={df.vol_km3.sum():.1f} ' f'km3, area={df.area_km2.sum():.1f} km2', flush=True) if __name__ == '__main__': main()