#!/bin/bash
pool_member_rank=$1
#pool_rank=$2
member=$2
FirstInitialize=$3
set -vx
#cd ..
echo ${FirstInitialize}
pwd
#if ( ${FirstInitialize} ) ; then

#     for (( i = 0 ; i < $((${pool_size})) ; i++ ))
#     do
# 	#  cd OASIS_RESTART
# 	cd MEMBER_$i
# 	whole_ensemble_size=$((${pool_size}*${pool_rank}+$i))
#         cp ../ENSEMBLE_TEMPLATE/flxat.nc flxat__${whole_ensemble_size}.nc
#         cp ../ENSEMBLE_TEMPLATE/sstoc.nc sstoc__${whole_ensemble_size}.nc
#         chmod u+w flxat__${whole_ensemble_size}.nc sstoc__${whole_ensemble_size}.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}__${i} flxat__${whole_ensemble_size}.nc
# #    ncrename -O -d .${varname}_nx,${varname}__${i}_nx flxat__${whole_ensemble_size}.nc
# #	    ncrename -O -d .${varname}_ny,${varname}__${i}_ny flxat__${whole_ensemble_size}.nc
# 	done
# 	for varname in O_SSTSST OIceFrc O_AlbIce O_TepIce O_OCurx1 O_OCury1 O_OCurz1
# 	do
# 	    ncrename -O -v .$varname,${varname}__${i} sstoc__${whole_ensemble_size}.nc
# #    ncrename -O -d .${varname}_nx,${varname}__${i}_nx sstoc__${whole_ensemble_size}.nc
# #	    ncrename -O -d .${varname}_ny,${varname}__${i}_ny sstoc__${whole_ensemble_size}.nc
# #    ncrename -O -v .loc02_${varname},loc02_${varname}__${i}_ny sstoc__${whole_ensemble_size}.nc
# #	    ncrename -O -d .loc02_${varname}_nx,loc02_${varname}__${i}_nx sstoc__${whole_ensemble_size}.nc
# #	    ncrename -O -d .loc02_${varname}_ny,loc02_${varname}__${i}_ny sstoc__${whole_ensemble_size}.nc
# 	done
# 	cd ..
#     done
    mv flxat.nc flxat__${pool_member_rank}.nc
    mv sstoc.nc sstoc__${pool_member_rank}.nc
     chmod u+w flxat__${pool_member_rank}.nc sstoc__${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
#    ncrename -O -d .${varname}__$member_nx,${varname}__${i}_nx flxat__${whole_ensemble_size}.nc
#	    ncrename -O -d .${varname}__$member_ny,${varname}__${i}_ny flxat__${whole_ensemble_size}.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

# else

#    for (( i = 0 ; i < $((${pool_size})) ; i++ ))
#    do
	#  cd OASIS_RESTART
#	cd MEMBER_$i
#	whole_ensemble_size=$((${pool_size}*${pool_rank}+$i))
#	cp ../MEMBER_$member/flxat.nc flxat__${whole_ensemble_size}.nc
#	cp ../MEMBER_$member/sstoc.nc sstoc__${whole_ensemble_size}.nc
#        chmod u+w flxat__${whole_ensemble_size}.nc sstoc__${whole_ensemble_size}.nc

#    mv flxat.nc flxat__${pool_member_rank}.nc
#    mv sstoc.nc sstoc__${pool_member_rank}.nc
#         chmod u+w flxat__${pool_member_rank}.nc sstoc__${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}__${member},${varname}__${pool_member_rank} flxat__${pool_member_rank}.nc
#    ncrename -O -d .${varname}__$member_nx,${varname}__${i}_nx flxat__${whole_ensemble_size}.nc
#	    ncrename -O -d .${varname}__$member_ny,${varname}__${i}_ny flxat__${whole_ensemble_size}.nc
#	done
#	for varname in O_SSTSST OIceFrc O_AlbIce O_TepIce O_OCurx1 O_OCury1 O_OCurz1
#	do
#	    ncrename -O -v .${varname}__${member},${varname}__${pool_member_rank} sstoc__${pool_member_rank}.nc
#    ncrename -O -d .${varname}__$member_nx,${varname}__${i}_nx sstoc__${whole_ensemble_size}.nc
#	    ncrename -O -d .${varname}__$member_ny,${varname}__${i}_ny sstoc__${whole_ensemble_size}.nc
#    ncrename -O -v .loc02_${varname}__$member,loc02_${varname}__${i}_ny sstoc__${whole_ensemble_size}.nc
#	    ncrename -O -d .loc02_${varname}__$member_nx,loc02_${varname}__${i}_nx sstoc__${whole_ensemble_size}.nc
#	    ncrename -O -d .loc02_${varname}__$member_ny,loc02_${varname}__${i}_ny sstoc__${whole_ensemble_size}.nc
#	done
#	cd ..
#    done

#fi
cd ..
set +vx
