####  UNIX Script Documentation Block
#
# Script name:   ingest_avhrr.sh
#
# JIF contact:  O'Reilly	Org: NP11        Date: 2007-10-30
#
# Abstract: This script ingests NESDIS GVF files and copies them into
#   the NCEP /dcom tank database on the IBM CCS machines.  No conversion is
#   performed.
#
# Script history log:
# 2006-08-23  Patrick O'Reilly  original version for implementation (modified
#                                from ingest_script_o3.sh)
#
# Usage: ingest_script_o3.sh $1 $2
#
#   Script parameters: $1 - dummy (not used here)
#                      $2 - full path definition for GVF file
#
#   Modules and files referenced:
#     scripts     : $USHbufr/ingest_gross_window_orbits
#                   $DATA/postmsg
#     executables : none
#
# Remarks:
#
#   Invoked by the script ingest_translate_orbits.
#
#   Imported Variables that must be passed in:
#      DATA     - path to current working directory
#      USHbufr  - path to ush scripts
#      TANKDIR  - path to output IEEE and BUFR tank (e.g., /dcom/us007003)
#      ndayarch - number of days that input files ftp'd from the remote machine
#                 will be kept in non-production directory TANKDIR
#                 (if CLEAN=YES)
#
#   Imported Variables that can be passed in:
#      CLEAN    - switch to clean up "old" GVF files stored in non-
#                 production directory TANKDIR (default = 'YES')
#                 (does not affect production where TANKDIR begins with
#                 "/dcom")
#
#   Condition codes:
#     0 - no problem encountered
#   > 0 - some problem encountered
#     Specifically:   1 - input GVF data file not found
#                     9 - file is outside gross window limits
#                    10 - error copying output file to target directory
#
# Attributes:
#   Language: aix unix script
#   Machine:  NCEP CCS
####

set -aux

cd $DATA
pwd

#  Set environment variables for processing
#  ----------------------------------------

gvffile=$DATA/$2
CLEAN=${CLEAN:-YES}

set +x
echo
echo "gvffile = $DATA/$2"
echo
set -x


#  Check for existence of input GVF data file
#  -------------------------------------------

if [ -s $gvffile ] ; then
  set +x
  echo
  echo "Input data file $gvffile exists"
  echo
  set -x
else
  set +x
  echo
  echo "Input data file $gvffile NOT FOUND - ABORTING"
  echo
  set -x
  exit 1
fi


#----------------------------------------------------------
#

cd $TANKDIR
if [ ! -d $TANKDIR/$TANKFILE ] ; then
   mkdir -p $TANKDIR/$TANKFILE
fi
cd -

newfile=$(basename $gvffile)

msg="copying $newfile"
$DATA/postmsg "$jlogfile" "$msg"

if [ ! -e $TANKDIR/$TANKFILE ] ; then
   mkdir -p $TANKDIR/$TANKFILE
fi

cp $gvffile $TANKDIR/$TANKFILE/$newfile

retc=$?
if [ $retc -ne 0 ] ; then
   set +x
   echo
   echo "FAILURE IN FINAL COPY ---- ABORT"
   echo
   set -x
   exit 10
fi

#  Clean out the /dcom/us007003/avhrr subdirectory
#  of files greater than thirty days old
#  -------------------------------------------------

find $TANKDIR/$TANKFILE -type f -mtime +$ndayarch -exec rm {} \;

#  End of script
#  -------------

exit 0
