#!/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.'

#!/bin/bash
dir=/trd-project4/NS9039K/shared/pgchiu/archive/norcpm_ana_f09_tn14/
var=sst
memdirs=$(ls -d ${dir}/*mem??)
i=0
for d in $memdirs; do
ifiles=$(ls ${d}/ocn/hist/*.hm.{1980..1982}-??.nc)
i=$(($i+1))
o1=model_${var}_mem$(printf "%2.2d" $i).nc
o1ano=model_${var}_ano_mem$(printf "%2.2d" $i).nc
test -f "$o1ano" && continue
## NorESM (BLOM/MICOM)
cdo -s -b f32          \
    -setgrid,griddes.txt \
    -select,name=${var} \
    ${ifiles} \
    ${o1}
cdo -s  \
    -ymonsub ${o1} -ymonmean ${o1}  \
    ${o1ano}
done
cdo -s \
    -select,name=${var} \
    -ensmean model_${var}_mem*.nc \
     model_${var}_ens.nc
cdo -s \
    -select,name=${var} \
    -ensmean model_${var}_ano_mem*.nc \
     model_${var}_ano_ens.nc


