#! /bin/ksh

set -x

set -u -e

ymdh="$1"
oceandest="$2"
yyyy=$( echo "$ymdh" | cut -c1-4 )

function copy_loop { 
    # This function copies the loop current files from a FIX or
    # real-time directory.  It assumes the naming schemes used by the
    # EMC HWRF group.

    set -x

    if [[ "$yyyy" -lt 2008 ]] ; then
        cp $1/hwrf_gfdl_loop_current_rmy5_AL${sno}${yy}.dat \
            $oceandest/gfdl_loop_current_rmy5.dat
        cp $1/hwrf_gfdl_loop_current_wc_ring_rmy5_AL${sno}${yy}.dat \
            $oceandest/gfdl_loop_current_wc_ring_rmy5.dat
    else
        cp $1/hwrf_gfdl_loop_current_rmy5.dat.${yyyymmdd}         \
            $oceandest/gfdl_loop_current_rmy5.dat
        cp $1/hwrf_gfdl_loop_current_wc_ring_rmy5.dat.${yyyymmdd} \
            $oceandest/gfdl_loop_current_wc_ring_rmy5.dat
    fi
}

# First, try to get the data from the fix directory:
copy_loop "$FIXhwrf"

# For FORECAST cases or recent HISTORY cases, that should fail.

# We will then get the data from a real-time location:
if [[ ! -s $oceandest/gfdl_loop_current_rmy5.dat || 
      ! -s $oceandest/gfdl_loop_current_wc_ring_rmy5.dat ]] ; then
    # The loop current was not in a fix file, so try to get it
    # from a real-time location.  
    case "$WHERE_AM_I" in
        jet) copy_loop "${JET_LOOP_COPY:-$GFS_COM_ROOT/loop-current}" ;;
        zeus) copy_loop "${ZEUS_LOOP_COPY:-$GFS_COM_ROOT/loop-current}" ;;
        *) 
            # Not Jet or Zeus so we must be on CCS.
            
            # Copying the correct loop current file from CCS in
            # real-time is a little more complicated, so that is
            # handled by another script:
            if ( ! $USHhwrf/hwrf_copy_loop_current.sh "$ymdh" \
                    $oceandest/gfdl_loop_current_rmy5.dat \
                    $oceandest/gfdl_loop_current_wc_ring_rmy5.dat ) ; then
                # Something went wrong when copying the loop current file.
                if [[ "$yyyymmdd$cyc" -ge $( ndate -720 ) ]] ; then
                    # For the past 30 days, default to the current
                    # day's loop current files.
                    echo "UNABLE TO COPY LOOP CURRENT FILES.  USING FILES FOR THE CURRENT DAY." 1>&2
                    echo "UNABLE TO COPY LOOP CURRENT FILES.  USING FILES FOR THE CURRENT DAY."
                    cp ${COMTPC}/gfdl_loop_current_rmy5.dat         \
                        $oceandest/gfdl_loop_current_rmy5.dat
                    
                    cp ${COMTPC}/gfdl_loop_current_wc_ring_rmy5.dat \
                        $oceandest/gfdl_loop_current_wc_ring_rmy5.dat
                else
                    # For cycles more than 30 days ago, revert to
                    # climatology for the loop current:
                    echo "UNABLE TO COPY LOOP CURRENT FILES.  REVERTING TO CLIMATOLOGY." 1>&2
                    echo "UNABLE TO COPY LOOP CURRENT FILES.  REVERTING TO CLIMATOLOGY."
                    rm -f \
                        $oceandest/gfdl_loop_current_rmy5.dat \
                        $oceandest/gfdl_loop_current_wc_ring_rmy5.dat
                fi
            fi
            ;;
    esac
fi
exit 0
