#!/bin/bash
# ----------------------------------------------------------- 
# UNIX Shell Script File
# Tested Operating System(s): RHEL 5,6
# Tested Run Level(s): 3, 5
# Shell Used: BASH shell
# Original Author(s): Roberto Padilla-Hernandez
# File Creation Date: 02/01/2015
# Date Last Modified:
#
# Version control: 1.14
#
# Support Team:
#
# Contributors: Floyd fayton
#
# ----------------------------------------------------------- 
# ------------- Program Description and Details ------------- 
# ----------------------------------------------------------- 
#
# Job Card to set the resources required for a NWPS run
#
# ----------------------------------------------------------- 

#PBS -N %WFO%_wt_cg1
#PBS -j oe
#PBS -S /bin/bash
#PBS -q dev
#PBS -A NWPS-DEV
#PBS -l walltime=05:15:00
#PBS -l select=1:ncpus=120:mem=20GB
#PBS -l place=excl
#PBS -l debug=true
#PBS -V
#PBS -o /lfs/h2/emc/ptmp/ali.salimi/com/nwps/v1.4.6/NWPS_%WFO%_wavetrack_cg1.o
#PBS -e /lfs/h2/emc/ptmp/ali.salimi/com/nwps/v1.4.6/NWPS_%WFO%_wavetrack_cg1.o

cd $PBS_O_WORKDIR

# Setup and load modules
export HOMEnwps=${NWPSdir}
VERSION_FILE=${HOMEnwps}/versions/run.ver
if [ -f $VERSION_FILE ]; then
  . $VERSION_FILE
else
  ECFNAME="jnwps_prep.ecf"
  echo "***JOB ${ECFNAME} ERROR: Version File $VERSION_FILE does not exist ***"
  exit
fi

module purge
module load envvar/${envvar_ver}
module load prod_envir/${prod_envir_ver}
module load prod_util/${prod_util_ver}
module load PrgEnv-intel/${PrgEnv_intel_ver}
module load craype/${craype_ver}
module load intel/${inter_ver}
module load cray-pals/${cray_pals_ver}
module load wgrib2/${wgrib2_ver}
module load cfp/${cfp_ver}
module load cpe/${cpe_ver}
module load proj/${proj_ver}
module load geos/${geos_ver}
module load libjpeg/${libjpeg_ver}
module load python/${python_ver}
module load iobuf/${iobuf_ver}
module list

export IOBUF_PARAMS="*:count=4:size=32M:prefetch=1:preflush=1"

export FORT_BUFFERED=true
export NWPSplatform="DEVWCOSS"
export MP_PGMMODEL=mpmd
export MP_CSS_INTERRUPT=yes
export WFO="%WFO%"
export siteid=${WFO}
export SITEID=${WFO^^}
export N="1"
export KEEPDATA="YES"
export model="nwps"
export MODEL=${model^^}

export envir="para"

export DEVWCOSS_USER=$(whoami)
export COMROOT=/lfs/h2/emc/ptmp/${DEVWCOSS_USER}/com

#Dev environment value for $job
export job=pnwps_${WFO}_wt_cg1
export jobid=pnwps_${WFO}_wt_cg1.$$

#Dev environment jlogfile
export ver=${nwps_ver}
mkdir -p /lfs/h2/emc/ptmp/${DEVWCOSS_USER}/$NET/$ver/logs/jlogfiles
export jlogfile=/lfs/h2/emc/ptmp/${DEVWCOSS_USER}/$NET/$ver/logs/jlogfiles/jlogfile.${job}

#Set platform
export WCOSS_SYSTEM=$( echo $COMROOTp1 | cut -c7 )d1
export WCOSS_SYSTEM_COM=$( echo $COMROOTp1 | cut -c7-9 )

# Wait for upstream component job to finish
while [ -z $(grep 'COMPLETED NORMALLY' /lfs/h2/emc/ptmp/${DEVWCOSS_USER}/$NET/$ver/logs/jlogfiles/jlogfile.pnwps_%WFO%_pd_cg1 | awk '{print $1}') ]
do
   if [ ! -z $(grep 'FAILED' /lfs/h2/emc/ptmp/${DEVWCOSS_USER}/$NET/$ver/logs/jlogfiles/jlogfile.pnwps_%WFO%_pd_cg1 | awk '{print $1}') ]
   then
       msg="FATAL ERROR: Upstream job pnwps_%WFO%_pd_cg1 failed"
       postmsg "$jlogfile" "$msg"
       export err=1; err_chk
   fi
   echo 'Waiting for pnwps_%WFO%_pd_cg1 to complete, at date'
   date
   sleep 60
done

${HOMEnwps}/dev/jobs/J${MODEL}_WAVETRACK

#%include <tail.h>

#%manual
################################################################################
#TASKSCRIPT DOCUMENTATION BLOCK
#TASKSCRIPT:     jnwps_%FAMILY1%_wavetrack_cg%N%
#LAST MODIFIER:  Floyd Fayton
#ORGANIZATION:   NP11
#DATE:           SEP 2014
#FULL NAME:    /nwps_para/nwps/SR/j%WFO%
#PURPOSE:  To execute the job that creates NWPS %WFO% GRIB output files
#JOB SCRIPT CALLED: /nwprod/nwps.${nwps_ver}/jobs/JNWPS ==>
#                            /nwprod/nwps.${nwps_ver}/scripts/exnwps_wavetrack_cg%N%.sh.ecf
#################################################################################
######################################################################
# Job specific troubleshooting instructions:
#  see generic troubleshoot manual page
#
######################################################################
#%end