#!/bin/bash

## Just run code in recipes, use it carefully
## example: use cdo to make cache file. Since cdo is quicker than ncl.
#;DIAG_NORCPM; RUNTHESECODES: echo '    no codes run here.'

#file list
cases='TBI_NUG_Ano TBI_NUG_FF TBI_N2_Ano_w2 TBI_N2_FF_w2'
seasons='DJF MAM JJA SON'
p1='1980-2000'
p2='2000-2020'

for s in $seasons ; do
for p in $p1 $p2 ; do
fns=''
for c in $cases ; do
    cc=$(echo $c|sed 's/TBI_//')
    fn="../../${c}/10_vp200/${cc}_UV_${s}_${p}.nc"
    dst=$(basename $fn | sed -e's/^/TBI_/')
    test -f "$dst" || cp "$fn" "$dst"
    fns="$fns $dst"
done
## regrid N2_* to NUG_*
ncremap -d "TBI_NUG_FF_UV_${s}_${p}.nc" "TBI_N2_FF_w2_UV_${s}_${p}.nc" tmp.nc && mv tmp.nc "TBI_N2_FF_w2_UV_${s}_${p}.nc"
ncremap -d "TBI_NUG_FF_UV_${s}_${p}.nc" "TBI_N2_Ano_w2_UV_${s}_${p}.nc" tmp.nc && mv tmp.nc "TBI_N2_Ano_w2_UV_${s}_${p}.nc"
test -f "superensmean_UV_${s}_${p}.nc" || ncea $fns -o "superensmean_UV_${s}_${p}.nc"
done
done
for s in $seasons ; do
    if [ ! -f "superensmean_dUV_${s}_P2-P1.nc" ] ;then
        ncbo --op_typ=- "superensmean_UV_${s}_${p2}.nc" "superensmean_UV_${s}_${p1}.nc" "superensmean_dUV_${s}_P2-P1.nc"
    fi
done


