#!/bin/bash 
# #### 05/05/99 ###############################
# Bias Correct NCEP Global Ensemble Forecast              
# #############################################

######################################################
# The following two variable could be defined in the
# loadleveler submission script (the sms script), if
# not they will take the default values which is set
# for the NCO running enviroment
#######################################################

set -xa

####################################
# Specify NET and RUN Name and model
####################################
export NET=${NET:-naefs}
export RUN=${RUN:-gefs}

###########################################
# Run config file to get input parameters
###########################################

export PS4='${PMI_FORK_RANK}: $SECONDS + '
date

#########################################################
# obtain unique process id (pid) and make temp directory
#########################################################

export pid=$$
export DATAROOT=${DATAROOT:-/lfs/f1/ops/${envir}/tmp}
export DATA=${DATA:-${DATAROOT:?}/${jobid}}

mkdir -p $DATA
cd $DATA

export cycle=t${cyc}z

##################
# File To Log Msgs
##################

export jlogfile=${jlogfile:-${DATA}/jlogfile.${job}.${pid}}

####################################
# Determine Job Output Name on System
####################################
export outid="LL$job"
export jobid="${outid}.o${pid}"
#export pgmout="OUTPUT.${pid}"
export pgmout="OUTPUT"

##############################################
# SENDECF  - Flag Events on ECF
# SENDCOM  - Copy Files From TMPDIR to $COMOUT
# SENDDBN  - Issue DBNet Client Calls
##############################################

export SENDCOM=${SENDCOM:-YES}
#export SENDECF=${SENDECF:-YES}
export SENDDBN=${SENDDBN:-YES}

########################################################################
# RUNJOB   - Judge the Job for Regular Run or Extended 00 Cycle Run
########################################################################
export RUNJOB=${RUNJOB:-REGULAR}

###################################################################
# IFENSBC1D   - Judge if Generate 1 Degree Bias Corrected Forecasts
###################################################################
export IFENSBC1D=YES                      

####################################
# Specify Execution Areas
####################################
export HOMEgefs=${HOMEgefs:-$PACKAGEROOT/naefs.${naefs_ver}}
export EXECgefs=${EXECgefs:-$HOMEgefs/exec}
export FIXgefs=${FIXgefs:-$HOMEgefs/fix}
export USHgefs=${USHgefs:-$HOMEgefs/ush}

export ERRSCRIPT=err_chk
export LOGSCRIPT=startmsg
export REDOUT='1>>'
export REDERR='2>'

##############################
# Run setpdy and initialize PDY variables
##############################
setpdy.sh
. ./PDY

##############################################
# Define COM directories
##############################################

export COMINbias=${COMINbias:-$(compath.py $envir/$NET/${naefs_ver})}
export COMINgefs=${COMINgefs:-$(compath.py $envir/com/gefs/${gefs_ver}/${RUN}.${PDY})/${cyc}/atmos/pgrb2ap5}

export COM_OUT=${COM_OUT:-$(compath.py -o $NET/${naefs_ver})}

export COMOUTBC_p5=${COM_OUT}/${RUN}.${PDY}/${cyc}/pgrb2ap5_bc
export COMOUTAN_p5=${COM_OUT}/${RUN}.${PDY}/${cyc}/pgrb2ap5_an
export COMOUTWT_p5=${COM_OUT}/${RUN}.${PDY}/${cyc}/pgrb2ap5_wt

export COMOUTBC=${COM_OUT}/${RUN}.${PDY}/${cyc}/pgrb2a_bc
export COMOUTAN=${COM_OUT}/${RUN}.${PDY}/${cyc}/pgrb2a_an
export COMOUTWT=${COM_OUT}/${RUN}.${PDY}/${cyc}/pgrb2a_wt

mkdir -m 775 -p ${COMOUTBC_p5}
mkdir -m 775 -p ${COMOUTAN_p5}
mkdir -m 775 -p ${COMOUTWT_p5}

if [ "$IFENSBC1D" = "YES" ]; then
  mkdir -m 775 -p ${COMOUTBC}
  mkdir -m 775 -p ${COMOUTAN}
# mkdir -m 775 -p ${COMOUTWT}
fi

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

env

cd $DATA

#####################
# Execute the script
#####################

runscript=$HOMEgefs/scripts/exnaefs_gefs_debias.sh

export memberlist="p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 \
                   p11 p12 p13 p14 p15 p16 p17 p18 p19 p20 \
                   p21 p22 p23 p24 p25 p26 p27 p28 p29 p30 \
                   gfs c00"

if [ -s $DATA/poescript ]; then
  rm -f $DATA/poescript
fi

for mem in $memberlist; do
  mkdir -p $DATA/group.$mem
  echo "$runscript \"$mem\" $DATA/group.$mem " >>$DATA/poescript
done

export MP_LABELIO=${MP_LABELIO:-YES}
export MP_STDOUTMODE=${MP_STDOUTMODE:-ordered}

chmod +x $DATA/poescript
startmsg
$APRUN $DATA/poescript
export err=$?; err_chk
wait

cat $DATA/group.c00/$pgmout.024.c00
cat $DATA/group.p20/$pgmout.360.p20
cat $DATA/group.c00/$pgmout.024.c00_an
cat $DATA/group.c00/$pgmout.024.c00_wt
cat $DATA/group.p20/$pgmout.360.p20_an
cat $DATA/group.p20/$pgmout.360.p20_wt

msg="JOB COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

##############################
# Remove the Temporary working directory
##############################
if [ $KEEPDATA != YES ]; then
  cd $DATAROOT
  rm -rf $DATA
fi

date