#!/usr/bin/bash

set -e

#CDO=$(which cdo)
#CDO="/home/jonas/usr/cdo-2.0/bin/cdo"
CDO="/home/jonas/usr/src/cdo-2.3.0/src/cdo"
FN="icon-d2-eps_germany_icosahedral_single-level_2023102600_000_2d_t_2m.grib2"
DWD_ICON_TARGET="target_grid_icon_d2_002.txt"
DWD_ICON_WEIGHTS="weights_icon_d2_002.nc"

# Calculate varsvar of unstructured grid
$CDO --pedantic -f grb2 -varsvar $FN out-varsvar.grb2
python -c "import xarray; print(xarray.open_dataset('out-varsvar.grb2').t2m.sum().item())"

# Calculate varsvar of remapped grid
$CDO --pedantic -f grb2 -remap,$DWD_ICON_TARGET,$DWD_ICON_WEIGHTS $FN out-remap.grb2
$CDO --pedantic -f grb2 -varsvar out-remap.grb2 out-remap-varsvar.grb2
python -c "import xarray; print(xarray.open_dataset('out-remap-varsvar.grb2').t2m.sum().item())"

# Calculate varsvar of bboxed and remapped grid
$CDO --pedantic -f grb2 -sellonlatbox,3,15,47,56 -remap,$DWD_ICON_TARGET,$DWD_ICON_WEIGHTS $FN out-remap-sellonlatbox.grb2
$CDO --pedantic -f grb2 -varsvar out-remap-sellonlatbox.grb2 out-remap-sellonlatbox-varsvar.grb2
python -c "import xarray; print(xarray.open_dataset('out-remap-sellonlatbox-varsvar.grb2').t2m.sum().item())"
