#!/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: 06/04/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%_pd_cg0
#PBS -j oe
#PBS -S /bin/bash
#PBS -q dev
#PBS -A NWPS-DEV
#PBS -l walltime=04:45:00
#PBS -l select=1:ncpus=120:mem=3000MB
#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%_prdgen_cg0.o
#PBS -e /lfs/h2/emc/ptmp/ali.salimi/com/nwps/v1.4.6/NWPS_%WFO%_prdgen_cg0.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_prdgen_cg${N}.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 libjpeg/${libjpeg_ver}
module load grib_util/${grib_util_ver}
module load util_shared/${util_shared_ver}
module list

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="0"
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}_pd_cg0
export jobid=pnwps_${WFO}_pd_cg0.$$

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

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

#%include <tail.h>

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