#!/bin/sh
# this runs the model to access the file system local to SMD_HOME, which
# is currently on /vol/hy_ext/ (hyades).  

# It checks for the existance of another run with the same runName,
# executes the "Check" script to view runtime parms, 
# forces the user to write some notes on the run,
# makes duplicate copies of the input data that are about to be used (for later archiving),
# executes the "go" script to run the model,
# then executes the "ArchiveRun" script to archive the results etc.

if test $# = 0
  then
    echo Error, you must supply a ProjectName argument 
    echo and give a descriptive suffix to designate the run. 
    exit
fi
if test $# = 1
  then
    echo Error, you must supply a ProjectName argument 
    echo and give a descriptive suffix to designate the run. 
    exit
fi


ProjName=$1
export ProjName

ModelPath=$SMD_HOME/Projects/
export ModelPath

RunName=$2
export RunName

# output hardwired for using filesystem local to SMD_HOME on hyades
OutputPath=$ModelPath
export OutputPath
ArchivePath=/b_erd/bb_elmmod/arc_out/
export ArchivePath

if test -f $ArchivePath$ProjName/$RunName/Input/Driver.parm.$RunName
  then
    echo That run designation exists - use another name.
    exit
fi

# run the shell script to see the Runtime parms and what is to be output
Check $ProjName

# open a file to provide notes on the run
vi $OutputPath$ProjName/Output/Notes.$RunName
cat $OutputPath$ProjName/Output/Notes_* $OutputPath$ProjName/Output/Notes.$RunName > $OutputPath$ProjName/Output/tempfile
rm $OutputPath$ProjName/Output/Notes_* 
mv $OutputPath$ProjName/Output/tempfile $OutputPath$ProjName/Output/Notes_$RunName

# make copies of data files that are shared by other projects, and may be changed during the course of
# a particular run (so that we will later archive the data file that was actually used in this run)
cp -p $ModelPath$ProjName/Data/CanalData.chan $ModelPath$ProjName/Data/CanalData.chan.$RunName
cp -p $ModelPath$ProjName/Data/CanalData.struct $ModelPath$ProjName/Data/CanalData.struct.$RunName
cp -p $ModelPath$ProjName/Data/HabParms $ModelPath$ProjName/Data/HabParms.$RunName
cp -p $ModelPath$ProjName/Data/GlobalParms $ModelPath$ProjName/Data/GlobalParms.$RunName
cp -p $ModelPath$ProjName/Data/HabSwData $ModelPath$ProjName/Data/HabSwData.$RunName
cp -p $ModelPath$ProjName/RunParms/Driver.parm $ModelPath$ProjName/RunParms/Driver.parm.$RunName

echo ***Simulation notes:
cat $OutputPath$ProjName/Output/Notes.$RunName

# run the code
go $ProjName

echo Archiving the run now...
# hardwired for using filesystem local to SMD_HOME on hyades
ArchiveRun local $ProjName $RunName

echo Done with running and archiving the $ProjName project sim designated as $RunName
