#!/bin/csh

date
set rundir = `dirname $PWD`
set bsdir = $PWD
set outdnm = WRF_A1B_2020s_echam5
set scenario = WUS_CSC_wrf_16d_A1B_2020s_echam5
set vicNl = /home/disk/columbia2/marketa/SRC/VIC_4.0.7_final_w_tmax_tmin_static/vicNl

set styr = 2010
set fiyr = 2039

set Np = 12
rm -rf ./cluster.*of${Np}

set Vcnf_mstr = /home/disk/picea/mauger/WUS_CSC/DATA/VIC_config/params/wrf_16d/VIC.conf.master
sed 's/OUT_DIRNAME/'${outdnm}'/g' $Vcnf_mstr >! Vcnf.tmp
sed -i 's/STYR/'${styr}'/g' Vcnf.tmp
sed -i 's/FIYR/'${fiyr}'/g' Vcnf.tmp

# ----------------------------------------------------
# distribute conf files, and kick off VIC runs:
foreach n ( `seq -f%02g 1 $Np` )
  set sfx = ${n}of${Np}
  echo "sfx = ${sfx}"

  mkdir -p ./cluster.$sfx

  if ( $n == 1 ) then
    cp -p Vcnf.tmp ./cluster.${sfx}/VIC.conf
  else
    sed 's/01of'${Np}'/'${sfx}'/g' Vcnf.tmp >! ./cluster.${sfx}/VIC.conf
  endif

  # run vic:
  cd ${bsdir}/cluster.${sfx}
  $vicNl -g VIC.conf >& log &

  cd $bsdir
end
rm -f Vcnf.tmp

# ----------------------------------------------------
# zip results:
wait
cd ${bsdir}/output/fluxes
cd ..
set dnm = `basename $PWD`
cd ..
tar czf ${dnm}/${scenario}_fluxes.tar.gz ${dnm}/fluxes/

wait
date
echo "DONE -- $rundir / $scenario"
exit

