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

## ccipc2 env
module purge
module load cdo_gcc-7.5.0/1.9.8
## ensemble
dir=/trd-project4/NS9039K/shared/pgchiu/archive/norcpm_ana_f09_tn14/
avgfile=ens_PRECC_avg.nc
anofile=ens_PRECC_ano.nc
var=PRECC
test -f ${avgfile} && test -f ${anofile} && exit 0
## loop for every member
memdirs=$(ls -d ${dir}/*mem??)
i=0
for d in $memdirs; do
    ifiles=$(ls ${d}/atm/hist/*.cam.h0.{1980..2020}-??.nc)
    i=$(($i+1))
    o1ts=PRECC_$(printf "%2.2d" $i).nc
    test -f "$o1ts" && continue
    cdo -s           \
        -select,name=${var} \
        ${ifiles} \
        ${o1ts}
done
## ensemble variables
cdo -ensavg PRECC_??.nc ${avgfile}
cdo -ymonsub ${avgfile} -ymonmean ${avgfile} ${anofile}
## cat to 1 file
#cdo --reduce_dim -O -merge avg_ts.nc std_ts.nc min_ts.nc max_ts.nc ${outfile}


