####  UNIX Script Documentation Block
#
# Script name:   ingest_goes_new.sh
#
# JIF contact:  O'Reilly     Org: NP11        Date: 2006-09-27
#
# Abstract: This script reads GOES-11 and GOES-12 files and copies them into
#   the NCEP /dcom tank database on the IBM CCS machines.  No conversion is
#   performed.  
#
# Script history log:
# 2006-09-27  Patrick O'Reilly   original version for implementation
# 2006-11-14  Patrick O'Reilly	 added cleanup for goes_ir directories
#
# Usage: ingest_goes_new.sh $1 $2
#
#   Script parameters: $1 - dummy (not used here)
#                      $2 - full path definition for GOES file
#
#   Modules and files referenced:
#     scripts     : $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)
#      TANKFILE - name of directory in TANKDIR to place the files
#      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" output IEEE 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 GOES data file not found
#                    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
#  ----------------------------------------

goesfile=$DATA/$2
CLEAN=${CLEAN:-NO}

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


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

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

#----------------------------------------------------------
#
newfile=$(basename $goesfile | cut -f5-7 -d"." | tr '[A-Z]' '[a-z]' )
TANKSUBDIR=$(basename $goesfile | cut -f2 -d"." | tr '[A-Z]' '[a-z]' )

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

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

cp $goesfile $TANKDIR/$TANKFILE/$TANKSUBDIR/$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/goes_ir subdirectory
#  of files greater than one day old
#  -------------------------------------------------

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

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

exit 0
