#!/bin/csh

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

rm -f ./cluster.??/log

# distribute conf files:
set Np = 12
foreach n ( `seq -f%02g 2 $Np` )
  echo "${n}of${Np}"
  sed 's/01of'${Np}'/'${n}'of'${Np}'/g' ./cluster.01/VIC.conf >! ./cluster.${n}/VIC.conf
end

# run vic in parallel:
foreach n ( `seq -f%02g 1 $Np` )
  cd ${bsdir}/cluster.${n}
  $vicNl -g VIC.conf >&! log &
end

# zip results:
wait
cd ${bsdir}/output/fluxes
set snm = `pwd | awk -F/ '{print $(NF-1);}'`
set bsn = `pwd | awk -F/ '{print $(NF-2);}'`
cd ../../..
tar czf ${bsn}/${snm}/${scenario}_fluxes.tar.gz ${bsn}/${snm}/fluxes/

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

