#! /bin/ksh

####################################################################
# hwrf_ocean_init.sh 
######################
# This script copies the loop current files for a specific cycle from
# /tpc/... to the files specified by $2 and $3.  The YYYYMMDDHH cycle
# date is given in $1 (hour is optional).  The copying command
# defaults to "cp -pf" but can be overriden in argument $4.
# If present, $5 specifies the text to prepend to the target filenames
# when copying.  
#
# Script returns status 0 on success and non-zero on failure.
#
# Author : Sam Trahan September 20, 2010
###################################################################

set -x

# Make sure /nwprod stuff is in the path:
export PATH=$PATH:${USHhwrf}:${utilities}:${utilexec}:${NWPROD}/hwrf.${HWRF_VERSION}/exec

# Determine which cycle we're looking for and decompose the cycle into
# YMD, hour and other things:
now=$( ndate )
if [[ -z "$1" ]] ; then
    echo NO ANALYSIS DATE/TIME SPECIFIED.  ABORTING.
    echo NO ANALYSIS DATE/TIME SPECIFIED.  ABORTING. 1>&2
    exit 19
else
    anlymdh="$1"
    anlymd=$( echo $anlymdh | cut -c1-8 )
    anltime=$( echo $anlymdh | cut -c9-10 )
fi
anlyear=$( echo $anlymdh | cut -c1-4 )

anlymd0="${anlymd}00"

# File to link to the loop current files:
target_ring_rmy5="$2"
target_rmy5="$3"

# Text to prepend to target filename:
target_pre=""

if [[ -z "$target_ring_rmy5" ]] ; then
    echo NO FILE SPECIFIED FOR RECEIVING ring_rmy5 FILE
    echo NO FILE SPECIFIED FOR RECEIVING ring_rmy5 FILE 1>&2
    exit 13
fi

if [[ -z "$target_rmy5" ]] ; then
    echo NO FILE SPECIFIED FOR RECEIVING rmy5 FILE
    echo NO FILE SPECIFIED FOR RECEIVING rmy5 FILE 1>&2
    exit 17
fi

# How to copy the data:
copymethod="${4:-cp -pf}"

# Search for the most recent loop current file
most_recent_time=1000000000
most_recent_ring_rmy5=NOT_FOUND
most_recent_rmy5=NOT_FOUND
for file in \
    /tpc/save/guidance/storm-data/ncep/archive/gfdl_loop_current_wc_ring_rmy5.dat.* \
    /tpc/save/guidance/storm-data/ncep/gfdl_loop_current_wc_ring_rmy5.dat*
  do

  # Loop current files have the time in the last line:
  time=$( echo $( tail -1 $file ) )

  # Make sure the time contains an hour:
  if (( time < 21500000 )) ; then
      # Probably missing an hour, so assume 00:
      echo $anlymdh: FILE DOES NOT CONTAIN HOUR.  ASSUMING 00: $file
      time="${time}00"
  fi

  # Get the other file (without the wc_ring in the name):
  file_rmy5=$( echo "$file" | sed 's,gfdl_loop_current_wc_ring_rmy5,gfdl_loop_current_rmy5,g' )
  if [[ "$?" != 0 ]] ; then
      echo $anlymdh: UNABLE TO PARSE FILENAME TO CREATE current_rmy5 FILENAME: $file
      continue
  fi

  # Make sure we have a time, and both files are non-empty:
  if [[ -z "$time" ]] ; then
      echo $anlymdh: CANNOT GET TIME FROM FILE: $file
      continue
  fi
  if [[ ! -s "$file" ]] ; then
      echo $anlymdh: FILE EMPTY: $file
      continue
  fi
  if [[ ! -s "$file_rmy5" ]] ; then
      echo $anlymdh: FILE EMPTY: $file
      continue
  fi

  if [[ "$time" -le "$anlymdh" ]] ; then
      if [[ "$time" -gt "$most_recent_time" ]] ; then
          most_recent_time="$time"
          most_recent_ring_rmy5="$file"
          most_recent_rmy5="$file_rmy5"
      fi
  fi
done

# Now copy to jet:
if [[ "$most_recent_ring_rmy5" != NOT_FOUND && "$most_recent_rmy5" != NOT_FOUND ]] ; then
    if ( ! $copymethod "$most_recent_ring_rmy5" $targetpre"$target_ring_rmy5" ) ; then
        echo $anlymdh: UNABLE TO FIND LOOP CURRENT DATA 1>&2
        exit 22
    fi
    if ( ! $copymethod "$most_recent_rmy5" $targetpre"$target_rmy5" ) ; then
        echo $anlymdh: UNABLE TO FIND LOOP CURRENT DATA 1>&2
        exit 21
    fi
else
    echo $anlymdh: UNABLE TO FIND LOOP CURRENT DATA 1>&2
    exit 19
fi

# Success.  Now we rejoice by sending this message to stdout and stderr:
echo $anlymdh: LOOP CURRENT DATA LINKED.
echo $anlymdh: LOOP CURRENT DATA LINKED. 1>&2
exit 0
