#!/bin/ksh -x

########## config.resources ##########
# Set resource information for job tasks
# e.g. walltime, node, cores per node, memory etc.

if [ $# -ne 1 ]; then

    echo "Must specify an input task argument to set resource variables!"
    echo "argument can be any one of the following:"
    echo "sfcprep prep anal analcalc analdiag gldas fcst post vrfy metp arch echgres"
    echo "eobs ediag eomg eupd ecen esfc efcs epos earc"
    echo "waveinit waveprep wavepostsbs wavepostbndpnt wavepostbndpntbll wavepostpnt"
    echo "wavegempak waveawipsbulls waveawipsgridded"
    echo "postsnd awips gempak"
    exit 1

fi

step=$1

echo "BEGIN: config.resources"

if [[ "$machine" = "WCOSS2" ]]; then
   export npe_node_max=128
elif [[ "$machine" = "WCOSS_DELL_P3" ]]; then
   export npe_node_max=28
   if [ "$QUEUE" = "dev2" -o "$QUEUE" = "devonprod2" -o "$QUEUE" = "devmax2" ]; then # WCOSS Dell 3.5
     export npe_node_max=40
   fi
elif [[ "$machine" = "JET" ]]; then
   export npe_node_max=24
elif [[ "$machine" = "HERA" ]]; then
   export npe_node_max=40
elif [[ "$machine" = "ORION" ]]; then
   export npe_node_max=40
fi

if [ $step = "sfcprep" ]; then

    export wtime_sfcprep="00:08:00"
    export npe_sfcprep=1
    export nth_sfcprep=1
    export npe_node_sfcprep=1
    export NTASKS=$npe_sfcprep
    export memory_sfcprep="10GB"

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

    eval "export wtime_$step='00:45:00'"
    eval "export npe_$step=4"
    eval "export npe_node_$step=2"
    eval "export nth_$step=1"
    if [[ "$machine" != "WCOSS2" ]]; then
      eval "export memory_$step=40G"
    fi

elif [ $step = "waveinit" ]; then

    export wtime_waveinit="00:10:00"
    export npe_waveinit=11
    export nth_waveinit=1
    export npe_node_waveinit=$npe_waveinit
    export NTASKS=$npe_waveinit
    export memory_waveinit="2GB"

elif [ $step = "waveprep" ]; then

    export wtime_waveprep="00:10:00"
    export npe_waveprep=5
    export npe_waveprep_gfs=65
    export nth_waveprep=1
    export npe_node_waveprep=$npe_waveprep
    export npe_node_waveprep_gfs=$npe_waveprep_gfs
    export memory_waveprep="100GB"
    export memory_waveprep_gfs="150GB"
    export NTASKS=$npe_waveprep
    export NTASKS_gfs=$npe_waveprep_gfs

elif [ $step = "wavepostsbs" ]; then

    export wtime_wavepostsbs="00:20:00"
    export wtime_wavepostsbs_gfs="03:00:00"
    export npe_wavepostsbs=8
    export nth_wavepostsbs=1
    export npe_node_wavepostsbs=$npe_wavepostsbs
    export memory_wavepostsbs="10GB"
    export memory_wavepostsbs_gfs="100GB"
    export NTASKS=$npe_wavepostsbs

elif [ $step = "wavepostbndpnt" ]; then

    export wtime_wavepostbndpnt="00:30:00"
    export npe_wavepostbndpnt=1
    export nth_wavepostbndpnt=1
    export npe_node_wavepostbndpnt=${npe_wavepostbndpnt}
    export NTASKS=$npe_wavepostbndpnt

elif [ $step = "wavepostbndpntbll" ]; then

    export wtime_wavepostbndpntbll="00:10:00"
    export npe_wavepostbndpntbll=2
    export nth_wavepostbndpntbll=1
    export npe_node_wavepostbndpntbll=${npe_wavepostbndpntbll}
    export NTASKS=$npe_wavepostbndpntbll

elif [ $step = "wavepostpnt" ]; then

    export wtime_wavepostpnt="00:35:00"
    export npe_wavepostpnt=3
    export nth_wavepostpnt=1
    export npe_node_wavepostpnt=${npe_wavepostpnt}
    export NTASKS=$npe_wavepostpnt

elif [ $step = "wavegempak" ]; then

    export wtime_wavegempak="02:00:00"
    export npe_wavegempak=1
    export nth_wavegempak=1
    export npe_node_wavegempak=$npe_wavegempak
    export NTASKS=$npe_wavegempak
    export memory_wavegempak="100GB"

elif [ $step = "waveawipsbulls" ]; then

    export wtime_waveawipsbulls="00:20:00"
    export npe_waveawipsbulls=1
    export nth_waveawipsbulls=1
    export npe_node_waveawipsbulls=$(echo "$npe_node_max / $nth_waveawipsbulls" | bc)
    export NTASKS=$npe_waveawipsbulls

elif [ $step = "waveawipsgridded" ]; then

    export wtime_waveawipsgridded="02:00:00"
    export npe_waveawipsgridded=1
    export nth_waveawipsgridded=1
    export npe_node_waveawipsgridded=$(echo "$npe_node_max / $nth_waveawipsgridded" | bc)
    export NTASKS=$npe_waveawipsgridded
    export memory_waveawipsgridded_gfs="20GB"

elif [ $step = "anal" ]; then

    export wtime_anal="00:50:00"
    export wtime_anal_gfs="00:40:00"
    export npe_anal=780
    export nth_anal=8
    export npe_anal_gfs=825
    export nth_anal_gfs=8
    if [ $CASE = "C384" ]; then
      export npe_anal=160
      export npe_anal_gfs=160
      export nth_anal=10
      export nth_anal_gfs=10
    fi
    if [ $CASE = "C192" -o $CASE = "C96" -o $CASE = "C48" ]; then
      export npe_anal=84
      export npe_anal_gfs=84
    fi
    if [[ "$machine" = "WCOSS_DELL_P3" ]]; then export nth_anal=7; fi
    export npe_node_anal=$(echo "$npe_node_max / $nth_anal" | bc)
    if [[ "$machine" = "WCOSS2" && "$CASE" = "C768" ]]; then export npe_node_anal=15; fi
    export nth_cycle=$npe_node_max
    export npe_node_cycle=$(echo "$npe_node_max / $nth_cycle" | bc)

elif [ $step = "analcalc" ]; then

    if [[ "$machine" = "WCOSS2" ]]; then export MPICH_MPIIO_HINTS="*:romio_cb_write=disable" ; fi

    export wtime_analcalc="00:10:00"
    export npe_analcalc=127
    export ntasks=$npe_analcalc
    export nth_analcalc=1
    export nth_echgres=4
    export nth_echgres_gfs=12
    export npe_node_analcalc=$npe_node_max
    if [[ "$machine" = "WCOSS_DELL_P3" ]]; then export npe_analcalc=127 ; fi

elif [ $step = "analdiag" ]; then

    export wtime_analdiag="00:10:00"
    export npe_analdiag=96		# Should be at least twice npe_ediag
    export nth_analdiag=1
    export npe_node_analdiag=$npe_analdiag
    if [[ "$npe_node_analdiag" -gt "$npe_node_max" ]]; then export npe_node_analdiag=$npe_node_max ; fi
    export memory_analdiag="48GB"

elif [ $step = "wdqms" ]; then

    export wtime_wdqms="00:20:00"
    export npe_wdqms=1
    export nth_wdqms=1
    export npe_node_wdqms=1
    export memory_wdqms="48GB"

elif [ $step = "gldas" ]; then

    export wtime_gldas="00:10:00"
    export npe_gldas=112
    export nth_gldas=1
    export npe_node_gldas=$npe_gldas
    if [[ "$npe_node_gldas" -gt "$npe_node_max" ]]; then export npe_node_gldas=$npe_node_max ; fi
    export npe_gaussian=96  
    export nth_gaussian=1 
    export npe_node_gaussian=$(echo "$npe_node_max / $nth_gaussian" | bc)

elif [ $step = "fcst" ]; then

    export wtime_fcst="01:30:00"
    export wtime_fcst_gfs="06:00:00"
    export npe_fcst=$(echo "$layout_x * $layout_y * 6" | bc)
    export npe_fcst_gfs=$(echo "$layout_x_gfs * $layout_y_gfs * 6" | bc)
    export nth_fcst=${nth_fv3:-2}
    export nth_fcst_gfs=${nth_fv3_gfs:-2}
    export npe_node_fcst=$(echo "$npe_node_max / $nth_fcst" | bc)
    export npe_node_fcst_gfs=$(echo "$npe_node_max / $nth_fcst_gfs" | bc)
    if [[ "$machine" == "WCOSS2" ]] && [[ "$CASE" = "C768" || "$CASE" = "C384" ]]; then
       export npe_node_fcst=32
       export npe_node_fcst_gfs=24
    fi

elif [ $step = "post" ]; then

    export wtime_post="00:12:00"
    export wtime_post_gfs="01:00:00"
    export npe_post=126
    export nth_post=1
    export npe_node_post=$npe_post
    export npe_node_post_gfs=$npe_post
    export npe_node_dwn=$npe_node_max
    if [[ "$machine" = "WCOSS_DELL_P3" ]]; then export npe_node_post=28 ; fi
    if [[ "$npe_node_post" -gt "$npe_node_max" ]]; then export npe_node_post=$npe_node_max ; fi
    if [[ "$npe_node_post_gfs" -gt "$npe_node_max" ]]; then export npe_node_post_gfs=$npe_node_max ; fi

elif [ $step = "vrfy" ]; then

    export wtime_vrfy="03:00:00"
    export wtime_vrfy_gfs="06:00:00"
    export npe_vrfy=3
    export nth_vrfy=1
    export npe_node_vrfy=1
    export npe_vrfy_gfs=1
    export npe_node_vrfy_gfs=1
    if [[ "$machine" == "HERA" ]]; then
	    export memory_vrfy="16384M"
    fi

elif [ $step = "metp" ]; then
    
    export nth_metp=1
    export wtime_metp="03:00:00"
    export npe_metp=4
    export npe_node_metp=4
    export wtime_metp_gfs="06:00:00"
    export npe_metp_gfs=4
    export npe_node_metp_gfs=4

elif [ $step = "echgres" ]; then

    export wtime_echgres="00:10:00"
    export npe_echgres=3
    export nth_echgres=$npe_node_max
    export npe_node_echgres=1
    if [[ "$machine" == "WCOSS2" ]]; then
      export nth_echgres=1
      export npe_node_echgres=3
      export memory_echgres="200GB"
    fi

elif [ $step = "arch" -o $step = "earc" -o $step = "getic" ]; then

    eval "export wtime_$step='06:00:00'"
    eval "export npe_$step=1"
    eval "export npe_node_$step=1"
    eval "export nth_$step=1"
    eval "export memory_$step=2048M"
    if [[ "$machine" == "WCOSS2" ]]; then
      eval "export memory_$step=50GB"
    fi

elif [ $step = "eobs" -o $step = "eomg" ]; then

    export wtime_eobs="00:15:00"
    export wtime_eomg="01:00:00"
    if [ $CASE = "C768" ]; then
      export npe_eobs=200
    elif [ $CASE = "C384" ]; then
      export npe_eobs=100
    elif [ $CASE = "C192" ]; then
      export npe_eobs=40
    elif [ $CASE = "C96" -o $CASE = "C48" ]; then
      export npe_eobs=20
    fi
    export nth_eobs=2
    if [[ "$machine" = "WCOSS_DELL_P3" ]]; then export nth_eobs=7; fi
    export npe_node_eobs=$(echo "$npe_node_max / $nth_eobs" | bc)

    if [[ "$machine" = "WCOSS2" ]]; then
      export npe_eobs=480
      export nth_eobs=3
      export npe_node_eobs=40
    fi

elif [ $step = "ediag" ]; then

    export wtime_ediag="00:06:00"
    export npe_ediag=48
    export nth_ediag=1
    export npe_node_ediag=$npe_node_max
    export memory_ediag="48GB"

elif [ $step = "eupd" ]; then

    export wtime_eupd="00:40:00"
    if [ $CASE = "C768" ]; then
      export npe_eupd=480
      export nth_eupd=6
      if [[ "$machine" = "WCOSS_DELL_P3" ]]; then
	export npe_eupd=960
        export nth_eupd=7
      elif [[ "$machine" = "WCOSS2" ]]; then
        export npe_eupd=280
        export nth_eupd=16
      fi
    elif [ $CASE = "C384" ]; then
      export npe_eupd=270
      export nth_eupd=2
      if [[ "$machine" = "WCOSS_DELL_P3" ]]; then
        export nth_eupd=9
      elif [[ "$machine" = "HERA" ]]; then
        export nth_eupd=8
      elif [[ "$machine" = "WCOSS2" ]]; then
        export npe_eupd=315
        export nth_eupd=14
      fi
    elif [ $CASE = "C192" -o $CASE = "C96" -o $CASE = "C48" ]; then
      export npe_eupd=42
      export nth_eupd=2
      if [[ "$machine" = "HERA" ]]; then export nth_eupd=4 ; fi
    fi
    export npe_node_eupd=$(echo "$npe_node_max / $nth_eupd" | bc)

elif [ $step = "ecen" ]; then

    export wtime_ecen="00:10:00"
    export npe_ecen=80
    export nth_ecen=4
    if [[ "$machine" = "WCOSS_DELL_P3" ]]; then export nth_ecen=7; fi
    if [ $CASE = "C384" -o $CASE = "C192" -o $CASE = "C96" -o $CASE = "C48" ]; then export nth_ecen=2; fi
    export npe_node_ecen=$(echo "$npe_node_max / $nth_ecen" | bc)
    export nth_cycle=$nth_ecen
    export npe_node_cycle=$(echo "$npe_node_max / $nth_cycle" | bc)

elif [ $step = "esfc" ]; then

    export wtime_esfc="00:06:00"
    export npe_esfc=80
    export npe_node_esfc=$npe_esfc
    if [[ "$npe_node_esfc" -gt "$npe_node_max" ]]; then export npe_node_esfc=$npe_node_max ; fi
    export nth_esfc=1
    export nth_cycle=$nth_esfc
    export npe_node_cycle=$(echo "$npe_node_max / $nth_cycle" | bc)
    export memory_esfc="500GB"

elif [ $step = "efcs" ]; then

    export wtime_efcs="00:40:00"
    export npe_efcs=$(echo "$layout_x * $layout_y * 6" | bc)
    export nth_efcs=${nth_fv3:-2}
    export npe_node_efcs=$(echo "$npe_node_max / $nth_efcs" | bc)

elif [ $step = "epos" ]; then

    export wtime_epos="00:15:00"
    export npe_epos=80
    export nth_epos=4
    if [[ "$machine" = "WCOSS_DELL_P3" ]]; then export nth_epos=7; fi
    export npe_node_epos=$(echo "$npe_node_max / $nth_epos" | bc)

elif [ $step = "postsnd" ]; then

    export wtime_postsnd="02:00:00"
    export npe_postsnd=40
    export nth_postsnd=8
    export npe_node_postsnd=10
    export npe_postsndcfp=9
    export npe_node_postsndcfp=1
    if [ $OUTPUT_FILE == "nemsio" ]; then
        export npe_postsnd=13
        export npe_node_postsnd=4
    fi
    if [[ "$(echo "$npe_node_postsnd * $nth_postsnd" | bc)" -gt "$npe_node_max" ]]; then
        export npe_node_postsnd=$(echo "$npe_node_max / $nth_postsnd" | bc)
    fi

elif [ $step = "awips" ]; then

    export wtime_awips="03:30:00"
    export npe_awips=1
    export npe_node_awips=1
    export nth_awips=1
    export memory_awips="100GB"
    if [[ "$machine" == "WCOSS_DELL_P3" ]]; then
        export npe_awips=2
        export npe_node_awips=2
        export nth_awips=1
    fi

elif [ $step = "gempak" ]; then

    export wtime_gempak="03:00:00"
    export npe_gempak=2
    export npe_gempak_gfs=28
    export npe_node_gempak=2
    export npe_node_gempak_gfs=28
    export nth_gempak=1
    export memory_gempak="4GB"
    export memory_gempak_gfs="150GB"

else

    echo "Invalid step = $step, ABORT!"
    exit 2

fi

echo "END: config.resources"