
## Aggregate/extract variable from model for each member
## output files: variable_memnum.nc

dir=/cluster/projects/nn9039k/people/pgchiu/diag_norcpm/norcpm2a_tro10Atl5m_ng2l1d_test/testdata
var=sst
ifilelist='ocn/hist/*.blom.hm.{1980..2000}-??.nc'
extractdir=extract

## if var is end with number: perhaps level
lev=$(grep -Eo '[0-9]+$' <<< "$var" || true)
if [ ! -z "$lev" ]; then
    pvar=${var%$lev}
fi

## function for is var in file?
function is_var_in_file () {
    varname=$1
    fn="$2"
    ## slow, need a better method
    ncdump -v "$varname" "$fn" >/dev/null 2>/dev/null && exist=true || exist=false
    echo -n $exist
    return 
}

mkdir -p $extractdir

memdirs=$(ls -d ${dir}/*mem?? | sort)
for d in $memdirs; do
    mem=$(echo $d | sed -e's/.*mem//')
    o1=${var}_$(printf "%2.2d" $mem).nc
    test -f "$o1" && continue
    t0=$(date +%s)
    echo "making ${o1}..."
    ifiles=$(ls ${d}/ocn/hist/*.blom.hm.{1980..2000}-??.nc)
    iifiles=''
    ## unpacking, can be parallelize but maybe later
    for ifile in $ifiles ; do
        ofn="${extractdir}/${var}_$(basename $ifile)"
        iifiles="$iifiles $ofn"
        test -f "$ofn" && continue

        ## check if variable is in file, run once only
        test -z "$exist" && exist=$(is_var_in_file $var $ifile) || true
        if [ "$exist" == 'true' ] ; then
            ncpdq -O -U -v${var} $ifile -o $ofn
        else ## if not, maybe var is tailed with plev, TBD
            if [ "$(is_var_in_file $pvar $ifile)" == 'true' ] && [ ! -z "$lev" ] ; then
                ## make lev file
                test -f vrt_prs_${lev}.nc || ncap2 -O -v -s 'defdim("plev",1);plev[$plev]={'${lev}00'};' vrt_prs_${lev}.nc
                ## get var at specific lev
                ncremap --vrt_fl=vrt_prs_${lev}.nc -v $pvar $ifile ${ofn}_$$_tmp.nc
                ncwa -v $pvar -a plev ${ofn}_$$_tmp.nc $ofn
                rm -f ${ofn}_$$_tmp.nc
                ## rename to var
                ncrename -v ${pvar},${var} $ofn
            elif [ "$var" == 'PRECT' ] ;then
                ## PRECT = PRECC + PRECL
                ncap2 -v -s 'PRECT=PRECL+PRECC' $ifile $ofn
            else
                echo "$var is not in $ifile"
                exit 1
            fi
        fi
    done
    ## NorESM (BLOM/MICOM)
    ncrcat -O -h -v ${var} ${iifiles} ${o1}_tmp1.nc
    ncatted  \
        -a coordinates,${var},d,, \
        -a valid_range,${var},d,, \
        ${o1}_tmp1.nc ${o1} && rm ${o1}_tmp1.nc
done
rm -rf $extractdir

