#! /bin/ksh
set -x

# This script determines processor counts for various model configurations.
# It is only needed in non-NCO mode.

if [[ "$PARAFLAG" == NO ]] ; then
    exit 0
fi

#--- Determine processor configuration ---------------------------------
ATMOS_DOMAINS=${ATMOS_DOMAINS:-3}
if [[ "$ATMOS_DOMAINS" == 3 ]] ; then
    # Planned 2012 operational settings, if we can fit on 3 nodes.

    npo=0   # number of ocean processes
    npc=0   # number of coupler processes
    npw=0   # number of wave processes
    if [[ "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ]] ; then
        npa=184 # number of atmosphere processes on Jet and Zeus
    else
        npa=240 # number of atmosphere processes on Cirrus, Stratus and Vapor
    fi

    # Override those numbers if an ocean or wave model is requested:
    #                                  Coupler Atmos  Ocean Wave 
    if [ $MODEL == COUPLED ]; then     npc=1
        if [ $OCEAN == POM ]; then                    npo=1
        elif [ $OCEAN == HYCOM ]; then                npo=30
        fi
    fi
    if [ $WAVEWATCH == YES ]; then
        if [ $OCEAN == POM ]; then                           npw=4 
        else                                                 npw=4 
        fi
    fi

    if [[ $OCEAN == HYCOM ]] ; then
        nnodes=5
        affinity=cpu
    else # POM or atmos-only:
        nnodes=4
        affinity=cpu
    fi
else # 1 or 2 domains
    # 2011 operational settings

    npo=0   # number of ocean processes
    npc=0   # number of coupler processes
    npw=0   # number of wave processes
    npa=91  # number of atmosphere processes

    # Override those numbers if an ocean or wave model is requested:
    #                                  Coupler Atmos  Ocean Wave 
    if [ $MODEL == COUPLED ]; then     npc=1
        if [ $OCEAN == POM ]; then                    npo=1
        elif [ $OCEAN == HYCOM ]; then        npa=112 npo=30
        fi
    fi
    if [ $WAVEWATCH == YES ]; then
        if [ $OCEAN == POM ]; then            npa=91         npw=4
        else                                  npa=91         npw=4 
        fi
    fi

    if [[ $OCEAN == HYCOM ]] ; then
        nnodes=4
        affinity=cpu
    else # POM or atmos-only:
        nnodes=4
        affinity=cpu
    fi
fi

case "$WHERE_AM_I" in
    zeus|jet) PPN=12 ;;
    c|s) PPN=64 ;;
    gaea) PPN=24 ;;
    *) # assume ccs
        PPN=64
esac

if [[ ( "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ) && "$MODEL" == COUPLED && "$OCEAN" != HYCOM ]] ; then
    padding=$(( PPN-2 ))  # extra processors to add so that we can have
                # coupler & ocean on their own machine
    pad_subtract=$padding
else
    padding=0
    pad_subtract=0
fi

if [[ "$IOSRV_PERGRP" -le 0 || "$IOSRV_GROUPS" -le 0 ]] ; then
    echo INVALID SETTINGS FOR IO SERVERS
    echo YOU MUST HAVE IOSRV_PERGRP\>0 
    echo AND IOSRV_GROUPS\>0
    echo CONTINUING, BUT DISABLING IO SERVERS
    export IO_SERVERS=NO
fi

if [[ "$IO_SERVERS" == YES ]] ; then
    let 'npa=npa+IOSRV_GROUPS*IOSRV_PERGRP'
    if [[ ( "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ) && $(( IOSRV_PERGRP*4 )) -lt 12 && "$OCEAN" != HYCOM ]] ; then
        # Reserve whole machines for I/O servers, up to 4 server groups per machine.
        let iserve=$IOSRV_GROUPS
        iomachines=''
        while (( iserve>0 )) ; do
            if [[ "$iserve" -lt 4 ]] ; then
                padding=$(( padding + (PPN-iserve*IOSRV_PERGRP) ))
                iserve=0
                iomachines="$iomachines:$( iserve*$IOSRV_PERGRP )"
            else
                padding=$(( padding + (PPN-4*IOSRV_PERGRP) ))
                iserve=$(( iserve-2 ))
                iomachines="$iomachines:"$(( 4*$IOSRV_PERGRP ))
            fi
        done
        iomachines=$( echo "$iomachines" | cut -c2- )
    elif [[ ( "$WHERE_AM_I" == jet || "$WHERE_AM_I" == zeus ) && $(( IOSRV_PERGRP*2 )) -lt 12 && "$OCEAN" != HYCOM ]] ; then
        # Reserve whole machines for I/O servers, two server groups per machine.
        let iserve=$IOSRV_GROUPS
        iomachines=''
        while (( iserve>0 )) ; do
            if [[ "$iserve" == 1 ]] ; then
                padding=$(( padding + (PPN-IOSRV_PERGRP) ))
                iserve=$(( iserve-1  ))
                iomachines="$iomachines:$IOSRV_PERGRP"
            else
                padding=$(( padding + (PPN-2*IOSRV_PERGRP) ))
                iserve=$(( iserve-2 ))
                iomachines="$iomachines:"$(( 2*$IOSRV_PERGRP ))
            fi
        done
        iomachines=$( echo "$iomachines" | cut -c2- )
    fi
fi

export NTASKS=$(( npc+npo+npa+npw+padding ))
export NTASKS_ATMOS=$(( npa+padding-pad_subtract ))

export IOSRV_PERGRP IOSRV_GROUPS IO_SERVERS npa npc npo npw NTASKS_ATMOS
export NTASKS NNODES PPN iomachines
$USHhwrf/hwrf_stream_parse.pl "$PARMhwrf/hwrf_proc_config.in" ./$stormlabel.proc_config.in
