#!/bin/bash
pool_size=$1
pool_rank=$2

cd ..

for (( i = 0 ; i < $((${pool_size})) ; i++ ))
do
  cd OASIS_RESTART
  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 
  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
