#!/bin/bash
#
################################################################################
#
# Script:	start_wrf_long.sh
# Author: 	Brian Vanderwende
# Date:		08/13/13
# Description:	This script submits all job segments required for long WRF runs.
#		Note that before running this script, namelist files for each segment in 
#		the job must be placed into long_nl folder with the extension type being
#		the job number (01,02,...N)
#
# Usage:	./start_wrf_long.sh fseg lseg proj_name run_name nodes hours 
#					queue [alloc]
#
# Args	
#		fseg		- number of the first namelist file (job segment ID)	
#		lseg 		- number of the last namelist file (job segment ID)
#		proj_name	- name of the WRF project that contains the run
#		run_name	- name of the current run within the project
#		nodes		- number of Janus nodes to use for job (12 cores per node) 
#		hours		- number of hours to requestfor each job segment
#		queue		- the Janus queue to use
#		alloc		- requested allocation ID (optional)
#
# Example:	./start_wrf_long.sh 1 20 PBL_TESTS MYJ_TEST 5 2 janus-small
#
################################################################################

# Create directory structure for job
export JSPATH=`pwd`

if [ -d "${3}/settings/${4}" ]; then
	mkdir ${3}/settings/${4}
	mkdir ${3}/output/${4}
	mkdir ${3}/restart/${4}
fi

cp long_nl/namelist* ${3}/settings/${4}

# Initialize environment variables for use in job scripts
export FJOB=$1
export LJOB=$2
export WRFPATH=${JSPATH}/../WRFV3/run/
export NLPATH=${JSPATH}/${3}/settings/${4}

wrf_build=`readlink ${WRFPATH}namelist.input | sed -e 's/..\///'`

export NLDEST=${WRFPATH}/../${wrf_build}
export OUTPATH=${JSPATH}/${3}/output/${4}
export RSTPATH=${JSPATH}/${3}/restart/${4}

# Set the output path to be used in the job script
opath=${JSPATH}/${3}/jout/${4}.out

# Calculate number of processors for MPI to use (12 per node on Janus)
nproc=$[$5 * 12]

# Set allocation replacement string if one is specified
if [ $8 ]; then
	alloc="PBS -A ${8}"
else
	alloc=" No allocation requested"
fi

# Create the custom job script to be used for all submitted jobs
awk -v run=$4 -v opath=$opath -v node=$5 -v hour=$6 							\
		-v nproc=$nproc -v alloc="$alloc" '{ \
			gsub(/run_replace/,run,$0); \
			gsub(/path_replace/,opath,$0); \
			gsub(/node_replace/,node,$0); \
			gsub(/hour_replace/,hour,$0); \
			gsub(/proc_replace/,nproc,$0); \
			gsub(/alloc_replace/,alloc,$0); \
			print }' run_wrf_long.temp > run_wrf_long.job

printf "Job log output path set to file: ${opath}\n"

# Create job segment tracking file
snum=$1
printf $snum > njob.dat

# Initialize job output file
printf "WRF JOB "${4}" INITIALIZED IN PROJECT ${3}\n" > $opath
printf "Job submit time: " >> $opath
date >> $opath

# Submit the first job segment in the sequence
prevID=`qsub -q $7 run_wrf_long.job`
printf "Job ${prevID} submitted...\n"

# Submit dependent segments to scheduler
while [ $snum -lt $LJOB ]; do
	prevID=`qsub -q $7 -W depend=afterok:$prevID run_wrf_long.job`
	printf "Job ${prevID} submitted...\n"
	
	((snum++))
done

rm run_wrf_long.job
printf "Job submission complete!\n"

### END SCRIPT