######################################################################################
#
# run_qgam &	Script to run the quasi-geostrophic annulus model.
#
#		FLAGS:
#		pu retains previous data files (necessary for pick-up run)
#		scr dumps system messages to screen rather than output file
#
# twnh Aug01 & pdw Aug02 & pdw Jan04
#
######################################################################################

# if they exist, delete the executable file (qgam), parameter files (qgam.dat and qgam_data.m) and output file (qgam.out)
if ( -e ./qgam ) then
	/bin/rm -f ./qgam
endif
if ( -e ./qgam.dat ) then
	/bin/rm -f ./qgam.dat
endif
if ( -e ./qgam_data.m ) then
	/bin/rm -f ./qgam_data.m
endif
if ( -e ./qgam.out ) then
	/bin/rm -f ./qgam.out
endif

# decide whether to delete data files
if ($1 == "pu" || $2 == "pu") then
	# don't delete any data files
else
	# delete all data files
	touch ./PSI_temp ./QGPV_temp	# so that the following two lines don't complain if there aren't actually any data files
	/bin/rm -f ./PSI_*
	/bin/rm -f ./QGPV*
endif

# copy current version of executable file from source directory
/bin/cp ~/QG_annulus/src/qgam ./

# create temporary parameter file with comments stripped out (namelist for qgam to read in)
grep -v "#" ./qgam.data > ./qgam.dat

# create parameter file with everything except the parameter values stripped out (for Matlab diagnostic scripts)
grep -v "[#&/]" ./qgam.data > ./qgam_data.m

# run the model
set START = `date`
if ($1 == "scr" || $2 == "scr") then
	./qgam
else
	./qgam > ./qgam.out
endif
set END = `date`

# delete executable file
/bin/rm -f ./qgam

# delete temporary parameter file
/bin/rm -f ./qgam.dat

# delete output file (as it can take up more disk space than the data itself)
/bin/rm -f ./qgam.out

# alert user (by message to screen and sending of e-mail and beeping of local machine) that the run has finished
set DIR = `pwd`
set DISK = `du -sh`
set RED = '\033[31m'
set BLACK = '\033[30m'
set MESSAGE = " \n run:           $DIR \n time started:  $START \n time finished: $END \n disk space:    $DISK \n user :         $USER"
echo "$RED$MESSAGE$BLACK"
echo "$MESSAGE" | mail -s "automatic message: QUAGMIRE run finished" $USER
echo "\a"
