#!/bin/bash
pool_member_rank=$1
member=$2

mv flxat.nc flxat__${pool_member_rank}.nc
mv sstoc.nc sstoc__${pool_member_rank}.nc
mv icbrg.nc icbrg__${pool_member_rank}.nc
mv icshf.nc icshf__${pool_member_rank}.nc

chmod u+w flxat__${pool_member_rank}.nc sstoc__${pool_member_rank}.nc icbrg__${pool_member_rank}.nc icshf__${pool_member_rank}.nc 

for varname in COTAUXXU COTAUYYU COTAUZZU COTAUXXV COTAUYYV COTAUZZV COTOTRAI COTOTSNO COTOTEVA COICEVAP COQSRMIX COQNSMIX COSHFICE CONSFICE CODFLXDT COCALVIN COLIQRUN COWINDSP COTAUMOD
do
    ncrename -O -v .${varname},${varname}__${pool_member_rank} flxat__${pool_member_rank}.nc
done

for varname in O_SSTSST OIceFrc O_AlbIce O_TepIce O_OCurx1 O_OCury1 O_OCurz1
do
    ncrename -O -v .${varname},${varname}__${pool_member_rank} sstoc__${pool_member_rank}.nc
done
for varname in COCALVIN
do
    ncrename -O -v .${varname},${varname}__${pool_member_rank} icbrg__${pool_member_rank}.nc
done
for varname in COCALVIN
do
    ncrename -O -v .${varname},${varname}__${pool_member_rank} icshf__${pool_member_rank}.nc
done

Detectloc=$( ncdump -h flxat__${pool_member_rank}.nc | grep loc000000_COLIQRUN_cnt_ncnt | wc -l )

if [ ${Detectloc} -ge 1 ] ; then

    NumberCOLIQRUNflxat=$((27*${pool_member_rank}+27))
    NumberCOLIQRUNflxat_4dgts=$(printf "%04i\n" ${NumberCOLIQRUNflxat})

    ncrename -O -d loc000000_COLIQRUN_cnt_ncnt,loc00${NumberCOLIQRUNflxat_4dgts}_cnt_ncnt flxat__${pool_member_rank}.nc
    ncrename -O -d loc000000_COLIQRUN_nx,loc00${NumberCOLIQRUNflxat_4dgts}_COLIQRUN__${pool_member_rank}_nx flxat__${pool_member_rank}.nc
    ncrename -O -d loc000000_COLIQRUN_ny,loc00${NumberCOLIQRUNflxat_4dgts}_COLIQRUN__${pool_member_rank}_ny flxat__${pool_member_rank}.nc
    ncrename -O -v loc000000_COLIQRUN_cnt,loc00${NumberCOLIQRUNflxat_4dgts}_cnt flxat__${pool_member_rank}.nc
    ncrename -O -v loc000000_COLIQRUN,loc00${NumberCOLIQRUNflxat_4dgts}_COLIQRUN__${pool_member_rank} flxat__${pool_member_rank}.nc

    NumberCOCALVINflxat=$((27*${pool_member_rank}+24))
    NumberCOCALVINflxat_4dgts=$(printf "%04i\n" ${NumberCOCALVINflxat})

    ncrename -O -d loc000000_COCALVIN_cnt_ncnt,loc00${NumberCOCALVINflxat_4dgts}_cnt_ncnt flxat__${pool_member_rank}.nc
    ncrename -O -d loc000000_COCALVIN_nx,loc00${NumberCOCALVINflxat_4dgts}_COCALVIN__${pool_member_rank}_nx flxat__${pool_member_rank}.nc
    ncrename -O -d loc000000_COCALVIN_ny,loc00${NumberCOCALVINflxat_4dgts}_COCALVIN__${pool_member_rank}_ny flxat__${pool_member_rank}.nc
    ncrename -O -v loc000000_COCALVIN_cnt,loc00${NumberCOCALVINflxat_4dgts}_cnt flxat__${pool_member_rank}.nc
    ncrename -O -v loc000000_COCALVIN,loc00${NumberCOCALVINflxat_4dgts}_COCALVIN__${pool_member_rank} flxat__${pool_member_rank}.nc 

    NumberCOCALVINicbrg=$((27*${pool_member_rank}+25))
    NumberCOCALVINicbrg_4dgts=$(printf "%04i\n" ${NumberCOCALVINicbrg})

    ncrename -O -d loc000000_cnt_ncnt,loc00${NumberCOCALVINicbrg_4dgts}_cnt_ncnt icbrg__${pool_member_rank}.nc
    ncrename -O -d loc000000_COCALVIN_nx,loc00${NumberCOCALVINicbrg_4dgts}_COCALVIN__${pool_member_rank}_nx icbrg__${pool_member_rank}.nc
    ncrename -O -d loc000000_COCALVIN_ny,loc00${NumberCOCALVINicbrg_4dgts}_COCALVIN__${pool_member_rank}_ny icbrg__${pool_member_rank}.nc
    ncrename -O -v loc000000_cnt,loc00${NumberCOCALVINicbrg_4dgts}_cnt icbrg__${pool_member_rank}.nc
    ncrename -O -v loc000000_COCALVIN,loc00${NumberCOCALVINicbrg_4dgts}_COCALVIN__${pool_member_rank} icbrg__${pool_member_rank}.nc

    NumberCOCALVINicshf=$((27*${pool_member_rank}+26))
    NumberCOCALVINicshf_4dgts=$(printf "%04i\n" ${NumberCOCALVINicshf})
    ncrename -O -d loc000000_cnt_ncnt,loc00${NumberCOCALVINicshf_4dgts}_cnt_ncnt icshf__${pool_member_rank}.nc
    ncrename -O -d loc000000_COCALVIN_nx,loc00${NumberCOCALVINicshf_4dgts}_COCALVIN__${pool_member_rank}_nx icshf__${pool_member_rank}.nc
    ncrename -O -d loc000000_COCALVIN_ny,loc00${NumberCOCALVINicshf_4dgts}_COCALVIN__${pool_member_rank}_ny icshf__${pool_member_rank}.nc
    ncrename -O -v loc000000_cnt,loc00${NumberCOCALVINicshf_4dgts}_cnt icshf__${pool_member_rank}.nc 
    ncrename -O -v loc000000_COCALVIN,loc00${NumberCOCALVINicshf_4dgts}_COCALVIN__${pool_member_rank} icshf__${pool_member_rank}.nc

fi
cd ..
