#! /bin/ksh

##############################################################
# exnesdis_dumpbuoy.sh.sms
##############################################################
# NESDIS POC for this script is:
#  John Sapper
#  email: John.Sapper@noaa.gov
##############################################################
# History:
#  29 Feb 2000  - Converted from Cray to IBM-SP.
#  14 Dec 2004  - Took out part that puts buotmsi and buotsmv,
#		  to no longer kick off CWBUOT* jobs (JW, STC)
#  17 Mar 2006  - Added "cp" section to save data to /com so
#		  DBNet can push data to DDS, added DBNet alerts
#  13 Sep 2006  - Removed processing and ftp to CEMSCS due to
#                 full migration to DDS
#     Mar 2011  - export $err values for err_chk
#
##############################################################

set -x

cd $DATA

##############################################################
# Dump ship and buoy data
# (time 0231 is can be changed whenever a non-traditional data
# dump is needed, like with missed_data.sh.)
# PDY indicates current day, PDYm1 indicates current day minus
# one, PDYm2 indicates current day minus two.
# dumpjb {PDYm1}23.99 11.99 indicates 
# the data should be dumped
# for yesterday at 23.99z, including all data + or - 11.99 hrs
##############################################################

export dump=bufr
export USER=$DATA
export DUPC='on'
export BACK='on'
case $cyc in
  05) timex /nwprod/ush/dumpjb ${PDYm1}23.99 11.99 ships dbuoy mbuoy;;
# 05) timex /nwprod/ush/dumpjb ${PDY}07.99 07.99 ships dbuoy mbuoy;;
  11) timex /nwprod/ush/dumpjb ${PDY}12.99 12.99 ships dbuoy mbuoy;;
esac
mv $DATA/ships.ibm $DATA/ship  
mv $DATA/dbuoy.ibm $DATA/drifting_buoy
mv $DATA/mbuoy.ibm $DATA/moored_buoy

##############################################################
# Unpack ship and buoy data for DDS
##############################################################

. prep_step
export pgm=nesdis_shipbuoy_dds

export XLFUNIT_8="$DATA/ship"
export XLFUNIT_9="$DATA/drifting_buoy"
export XLFUNIT_10="$DATA/moored_buoy"
export XLFUNIT_20="ship_buoy.data"


$EXECutil/nesdis_shipbuoy_dds >>$pgmout 2> errfile
export err=$?; err_chk 


##############################################################
# Copy files to /com, alert the files to DBNet
##############################################################

if [ $SENDCOM = YES ]
then
   cp ship_buoy.data $COMOUT/ship_buoy.data
fi

if [ $SENDDBN = YES ]
then
    $DBNROOT/bin/dbn_alert MODEL DDS_BUOY $job \
     $COMOUT/ship_buoy.data
fi

##################################################################
# Process icegrb file; copy to /com, alert to DBNet
##################################################################

if [ $cyc -eq 11 ] ; then
 WGRIB=$EXECutil/wgrib
 cp $COMMRF/engice.t00z.grb ice.cur
 $WGRIB ice.cur | $WGRIB -i ice.cur -o ice.km50 -text
 date '+%y%m%d' > datefile.new

 # First call to older program
 $EXECutil/nesdis_getice >> $pgmout 2>errfile
 export err=$?; err_chk
 
 # Second call to newer program which will replace above
 $EXECutil/nesdis_getice_new >> $pgmout 2>errfile
 export err=$?; err_chk

 if [ $SENDCOM = YES ] ; then
   cp ice2.km50 $COMOUT/ice2.km50
   cp new.ice.km50 $COMOUT/new.ice.km50
 fi

 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL DDS_BUOY $job $COMOUT/ice2.km50
    $DBNROOT/bin/dbn_alert MODEL DDS_BUOY $job $COMOUT/new.ice.km50
 fi

fi

exit
