"""Copy the HIST strands of the states next to their permutation runs. python collect_hist.py Merges the L5 HIST runs of the state tree (/runs/hist_ens###_reg11_ chunk###.nc: each member's HIST from its 1850 state, the same restart as the permutation runs) into /perm_state###_hist_reg11.nc. """ import glob import os import re import sys sys.path.insert(0, '/home/www/fmaussion/runs/lmr_2025/round5') from merge_chunks import merge # noqa: E402 data_dir, tree = sys.argv[1], sys.argv[2] members = sorted({re.search(r'hist_ens(\d{3})_reg11', f).group(1) for f in glob.glob(f'{tree}/runs/hist_ens*_reg11_chunk*.nc')}) if not members: sys.exit(f'No L5 runs in {tree}/runs') for m in members: out = os.path.join(data_dir, f'perm_state{m}_hist_reg11.nc') merge(os.path.join(tree, 'runs'), out, verbose=False, prefix=f'hist_ens{m}_reg11') print('Written', out)