#! /bin/sh
###############################################################################
# This script  ofs_get_sstgoes retrieves the GOES SST data
# Remarks :                                                    #
# - For non-fatal errors output is witten to the seas.log file #
#
# exported vars
#   datatype
#   field
#   cycle
#
###############################################################################
set -x

echo "*** Started script $0"

# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

pastdate=$1
srtdate=$2

#

  seton='+xa'
  setoff='+xa'
  set $seton
#

  set $setoff
  echo ' '
  echo '                ************************************'
  echo '                *** extract goes obs from Analysis ***'
  echo '                ************************************'

#######################################################
#
# Input Data
#
#######################################################

#${dumpmb} ${pastdate} ${srtdate} ${datatype}

# dumpmb cannot get goes?
# use dumpjb

ADATE=$pastdate
#enddate=`$NDATE +24 $srtdate$cyc | cut -c1-8`
enddate=`${utilexec}/ndate +\`expr 24 \` ${srtdate}00`                                                                              
enddate=`echo $enddate | cut -c1-8`


while [[ $ADATE < $enddate ]]
do

################rm -f bufr_goes_2
################cp $TANK/${ADATE}/b012/xx022 bufr_goes_2

# there are more things we can do with dumpjb (outside the loop)
# and then use bufr_combfr to combine the .ibm
rm -f sstnsg.ibm
sh /nwprod/ush/dumpjb ${ADATE}12 11.999 sstnsg
# ask Jeff W. why 11.999 is best.. (expect long detailed explanation)

# attach file numbers 
export pgm=ofs_bufr_decdst
. prep_step

export XLFRTEOPTS="unit_vars=yes"  
export XLFUNIT_24="sstnsg.ibm"  
export XLFUNIT_51=goes.${field}.${ADATE}.txt

# GOES bias is set in 4th column of parm/ofs_atl.platform.dat
# and read/used in boxin.f

cat > goes.in <<EOF
${lonobsmin}
${lonobsmax}
${latobsmin}
${latobsmax}
EOF

# run decoder
startmsg

$EXECofs/ofs_bufr_decdst < goes.in  >> $pgmout 2> errfile
export err=$?; err_chk

cat goes.${field}.${ADATE}.txt >> tmp.file

#ADATE=`$NDATE +24 $ADATE$cyc | cut -c1-8`
ADATE=`${utilexec}/ndate +\`expr 24 \` ${ADATE}00 | cut -c1-8`

done

mv tmp.file goes.${field}.${srtdate}.txt

echo "*** Finished script $0"
