# lmr_2025

## Python environment

All the data-processing code here (xarray, cftime, oggm, ...) runs in the
`oggm_env` conda/mamba environment, not the system python. The user
normally enters it interactively with a shell function called `goconda`,
but that function lives in `~/.bashrc` behind an interactive-shell guard
(`if [[ $- != *i* ]] ; then return; fi`), so it never loads in a
non-interactive tool shell.

Don't bother with `mamba shell hook` / `mamba activate` either — just call
the env's python binary directly:

```
/home/users/fmaussion/.miniforge3/envs/oggm_env/bin/python script.py ...
```

## data/

- `data/orig/<round_name>/` — original netCDF files as received (one
  subfolder per delivery round, e.g. `Round1_w_mismatch`,
  `ForFabien_Pilot_nc_Round2`). Files use a `(lon, lat, month, year)` grid
  instead of a real time axis, inconsistent variable names/units, and
  ensemble members as separate `<base>_ens###.nc` files.
- `data/unstacked/<round_name>/` — converted output: proper
  `(lon, lat, time)` netCDFs with fixed units, produced by
  [data/unstack.py](data/unstack.py). Ensemble members go under an
  `ensemble/` subfolder.
- `data/unstack.py` — the conversion script (see below). Superseded
  `data/unstack.ipynb`, which was a one-off, hardcoded-per-file notebook
  and is being kept around only for reference on the old approach.

### data/unstack.py

Usage:

```
/home/users/fmaussion/.miniforge3/envs/oggm_env/bin/python data/unstack.py \
    data/orig/<round_name> data/unstacked/<round_name>
```

Converts every `.nc` file in the input directory:
- Files with `year`/`month` dims are stacked into a `time` axis (calendar
  year + month, `noleap` cftime calendar, year 0 is valid/kept — the
  `year` coordinate is documented in the source files as "Calendar year").
  Unlike the old notebook, there is no hardcoded `year=slice(...)` per
  file; each file's own year range is used as-is. The old per-round manual
  slicing (`slice(1, 2023)` for one file, `slice(1, 2012)` for another) is
  believed to be the cause of the mismatch in `Round1_w_mismatch`.
- Files without those dims (e.g. elevation) are treated as static 2D
  grids and just get `longitude`/`latitude` renamed to `lon`/`lat`.
- `<base>_ens###.nc` files are auto-detected via filename and routed to an
  `ensemble/` output subfolder.
- Which variable maps to which output name/units/transform is config-only:
  see the `VARIABLES` and `STATIC_VARIABLES` dicts at the top of the
  script. To support a new file/round with a new variable name, add an
  entry there — no other code changes needed.

Known variable mappings (input var -> output var, transform):
- `P_monthly` (m/month) -> `pr` (kg m-2 s-1)
- `Tp_monthly` (°C anomaly) -> `tas` (K)
- `TpNAT_monthly` (°C anomaly) -> `tas` (K)
- `elevation` -> `elevation` (unchanged, just dim rename)
