#!/bin/ksh -x

if [ $# -ne 1 ]; then

    echo "Must specify an input argument to set runtime environment variables!"
    echo "argument can be any one of the following:"
    echo "anal fcst post vrfy"
    echo "eobs eupd ecen efcs epos"
    echo "postsnd awips gempak"
    exit 1

fi

step=$1

# Theia information
export npe_node_max=24
export launcher="mpirun -np"

# Configure MPI environment
export MPI_BUFS_PER_PROC=2048
export MPI_BUFS_PER_HOST=2048
export MPI_GROUP_MAX=256
export MPI_MEMMAP_OFF=1
export MP_STDOUTMODE="ORDERED"
export OMP_STACKSIZE=2048000
export NTHSTACK=1024000000
#export LD_BIND_NOW=1
export job=${PBS_JOBNAME:-$step}
export jobid=${job}.${PBS_JOBID:-$$}

if [ $step = "prep" -o $step = "prepbufr" ]; then

    nth_max=$(($npe_node_max / $npe_node_prep))

    export POE="NO"
    export BACK=${BACK:-"YES"}
    export sys_tp="Cray-CS400"

elif [ $step = "anal" ]; then

    nth_max=$(($npe_node_max / $npe_node_anal))

    export NTHREADS_GSI=${nth_gsi:-$nth_max}
    [[ $NTHREADS_GSI -gt $nth_max ]] && export NTHREADS_GSI=$nth_max
    export APRUN_GSI="$launcher ${npe_gsi:-${npe_anal:-$PBS_NP}}"

    export NTHREADS_CALCINC=${nth_calcinc:-1}
    [[ $NTHREADS_CALCINC -gt $nth_max ]] && export NTHREADS_CALCINC=$nth_max
    export APRUN_CALCINC="$launcher \$ncmd"

    export NTHREADS_CYCLE=${nth_cycle:-12}
    [[ $NTHREADS_CYCLE -gt $npe_node_max ]] && export NTHREADS_CYCLE=$npe_node_max
    npe_cycle=${ntiles:-6}
    export APRUN_CYCLE="$launcher $npe_cycle"

    export NTHREADS_GAUSFCANL=1
    npe_gausfcanl=${npe_gausfcanl:-1}
    export APRUN_GAUSFCANL="$launcher $npe_gausfcanl"

elif [ $step = "eobs" ]; then

    nth_max=$(($npe_node_max / $npe_node_eobs))

    export NTHREADS_GSI=${nth_gsi:-$nth_max}
    [[ $NTHREADS_GSI -gt $nth_max ]] && export NTHREADS_GSI=$nth_max
    export APRUN_GSI="$launcher ${npe_gsi:-${npe_eobs:-$PBS_NP}}"

elif [ $step = "eupd" ]; then

    nth_max=$(($npe_node_max / $npe_node_eupd))

    export NTHREADS_ENKF=${nth_enkf:-$nth_max}
    [[ $NTHREADS_ENKF -gt $nth_max ]] && export NTHREADS_ENKF=$nth_max
    export APRUN_ENKF="$launcher ${npe_enkf:-${npe_eupd:-$PBS_NP}}"

elif [ $step = "fcst" ]; then

    nth_max=$(($npe_node_max / $npe_node_fcst))

    export NTHREADS_FV3=${nth_fv3:-$nth_max}
    [[ $NTHREADS_FV3 -gt $nth_max ]] && export NTHREADS_FV3=$nth_max
    export cores_per_node=$npe_node_max
    export APRUN_FV3="$launcher ${npe_fv3:-${npe_fcst:-$PBS_NP}}"

    export NTHREADS_REGRID_NEMSIO=${nth_regrid_nemsio:-1}
    [[ $NTHREADS_REGRID_NEMSIO -gt $nth_max ]] && export NTHREADS_REGRID_NEMSIO=$nth_max
    export APRUN_REGRID_NEMSIO="$launcher $LEVS"

    export NTHREADS_REMAP=${nth_remap:-2}
    [[ $NTHREADS_REMAP -gt $nth_max ]] && export NTHREADS_REMAP=$nth_max
    export APRUN_REMAP="$launcher ${npe_remap:-${npe_fcst:-$PBS_NP}}"

elif [ $step = "efcs" ]; then

    nth_max=$(($npe_node_max / $npe_node_efcs))

    export NTHREADS_FV3=${nth_fv3:-$nth_max}
    [[ $NTHREADS_FV3 -gt $nth_max ]] && export NTHREADS_FV3=$nth_max
    export cores_per_node=$npe_node_max
    export APRUN_FV3="$launcher ${npe_fv3:-${npe_efcs:-$PBS_NP}}"

    export NTHREADS_REGRID_NEMSIO=${nth_regrid_nemsio:-1}
    [[ $NTHREADS_REGRID_NEMSIO -gt $nth_max ]] && export NTHREADS_REGRID_NEMSIO=$nth_max
    export APRUN_REGRID_NEMSIO="$launcher $LEVS"

elif [ $step = "post" ]; then

    nth_max=$(($npe_node_max / $npe_node_post))

    export NTHREADS_NP=${nth_np:-1}
    [[ $NTHREADS_NP -gt $nth_max ]] && export NTHREADS_NP=$nth_max
    export APRUN_NP="$launcher ${npe_np:-${npe_post:-$PBS_NP}}"

    export NTHREADS_DWN=${nth_dwn:-1}
    [[ $NTHREADS_DWN -gt $nth_max ]] && export NTHREADS_DWN=$nth_max
    export APRUN_DWN="$launcher ${npe_dwn:-$PBS_NP}"

elif [ $step = "ecen" ]; then

    nth_max=$(($npe_node_max / $npe_node_ecen))

    export NTHREADS_ECEN=${nth_ecen:-$nth_max}
    [[ $NTHREADS_ECEN -gt $nth_max ]] && export NTHREADS_ECEN=$nth_max
    export APRUN_ECEN="$launcher ${npe_ecen:-$PBS_NP}"

    export NTHREADS_CHGRES=${nth_chgres:-12}
    [[ $NTHREADS_CHGRES -gt $npe_node_max ]] && export NTHREADS_CHGRES=$npe_node_max
    export APRUN_CHGRES="time"

    export NTHREADS_CALCINC=${nth_calcinc:-1}
    [[ $NTHREADS_CALCINC -gt $nth_max ]] && export NTHREADS_CALCINC=$nth_max
    export APRUN_CALCINC="$launcher ${npe_ecen:-$PBS_NP}"

elif [ $step = "epos" ]; then

    nth_max=$(($npe_node_max / $npe_node_epos))

    export NTHREADS_EPOS=${nth_epos:-$nth_max}
    [[ $NTHREADS_EPOS -gt $nth_max ]] && export NTHREADS_EPOS=$nth_max
    export APRUN_EPOS="$launcher ${npe_epos:-$PBS_NP}"

elif [ $step = "fv3ic" ]; then

    export NTHREADS_CHGRES=${nth_chgres:-$npe_node_max}
    [[ $NTHREADS_CHGRES -gt $npe_node_max ]] && export NTHREADS_CHGRES=$npe_node_max
    export APRUN_CHGRES="time"

elif [ $step = "postsnd" ]; then

    nth_max=$(($npe_node_max / $npe_node_postsnd))

    export NTHREADS_POSTSND=${nth_postsnd:-1}
    [[ $NTHREADS_POSTSND -gt $nth_max ]] && export NTHREADS_POSTSND=$nth_max
    export APRUN_POSTSND="$launcher $npe_postsnd"

    export NTHREADS_POSTSNDCFP=${nth_postsndcfp:-1}
    [[ $NTHREADS_POSTSNDCFP -gt $nth_max ]] && export NTHREADS_POSTSNDCFP=$nth_max
    export APRUN_POSTSNDCFP="$launcher $npe_postsndcfp"

elif [ $step = "awips" ]; then

     echo "WARNING: $step is not enabled on $machine!"

elif [ $step = "gempak" ]; then

     echo "WARNING: $step is not enabled on $machine!"

fi