set -xa
date

echo "------------------------------------------------"
echo "Executing exasci_altimeter.sh.sms "
echo "This script uses the following altimeter data from bufr tanks"
echo "xx106 _cajsww    nem 031106  #> JASON-1/CNES/AVISO IGDR wind/wave  "
echo "xx108 _envsww    nem 031108  #> ENVISAT wind/wave "
echo "xx110 _njsnww    nem 031110  #> JASON-1 wind/wave direct from NAVOCEANO "
echo "xx114            nem 031114  #>  NAVOCEANO JASON-2 WIND/WAVE "
echo "and dumps them as hourly files. The hourly files are read through a decoder "
echo "program to convert them as hourly asci files and houlry GEMPAK surface files"
echo "for display purposes on NMAP2" 
echo "                            " 
echo "Author: V. Krishna Kumar"
echo "NCEP/NCO/Systems Integration Branch October 2008"
echo "------------------------------------------------"

#
# Chosen data sub types to dump from the bufr tank b031 
#
types='106 108 110 114'
######################################################
#
# Usage: dumpjb  yyyymmddhh<.hh> hh<.hh> dgrp1 dgrp2 dgrp3 ... dgrpN
#
#   Script parameters:
#     yyyymmddhh<.hh> - center of time window for dump
#     hh<.hh>         - radius of time window for dump
#
# Test only for the previous 6 hours
ccyc=`/nwprod/util/exec/ndate | cut -c9-10`
pcyc=`/nwprod/util/exec/ndate -6 | cut -c9-10`
echo "previous and current cycles   " $pcyc " " $ccyc
#
cdate=`/nwprod/util/exec/ndate`
pdate=`/nwprod/util/exec/ndate -6`
echo "previous and current dates   " $pdate " " $cdate
ix=1
pday=6
while [[ $ix -le $pday ]]  #### Create hourly data files for the previous
                           #### five cycles as well as current cycle
do
      pdate=`/nwprod/util/exec/ndate -$ix`
      ppdy=`echo $pdate | cut -c1-8`
      center=`echo $pdate | cut -c9-10`'.5'
      echo "center time is  " $center
#
export FORM=${pdate}.bufr
#
for typ in $types            #### For each cycle hour, create hourly data
                             #### files for the four bufr data types
do
    if test -s  ${typ}.${FORM}  ### test for the existence of already dumped files
    then
      echo " ${typ}.${pdate}.bufr already dumped."
      else
        $DUMP ${ppdy}${center} .5 031 $typ
        mv 031.${pdate}.bufr $typ.${pdate}.bufr
      errdmp=$?
#
      if [ "$errdmp" -ne 0 ]
         then
         msg="No $typ data for ${pdate}!"
         postmsg  "$jlogfile" "$msg"
      fi
# 
# Decode the bufr files for types (dumpjb will create ${typ}.${FORM}) 
# and convert it into ASCI hourly files
#
      if [ $typ = "106" ] ; then         #### NC031 106
         gtyp=jsn1c
         siz_typ=`ls -l $DATA/${typ}.${FORM} | awk '{print $5}'`
#
         if [ $siz_typ -eq 0 ] ; then
            rm $DATA/${typ}.${FORM}
         fi
#
         if [ $siz_typ -gt 0 ] ; then
           echo "Call bufr2asci_jwhgt executable to create $gtyp asci files"
#
           export pgm=bufr2asci_jwhgt
           . prep_step
           startmsg
           export XLFRTEOPTS="unit_vars=yes"
           export XLFUNIT_12="${typ}.${FORM}"
           export XLFUNIT_53="${gtyp}.${pdate}.sgwh"
           export XLFUNIT_54="${gtyp}.${pdate}.all"
           timex $EXECaltim/bufr2asci_jwhgt 1 > ${gtyp}_ft06 2> errfile_${gtyp}
           export err=$?;err_chk
###
### Send the hourly data sub type ascii files to $COMOUT
###
           if test "$SENDCOM" = 'YES'
           then
              cp $DATA/${gtyp}.${pdate}.sgwh $COMOUT/.
              cp $DATA/${gtyp}.${pdate}.all $COMOUT/.
              if [ $SENDDBN = "YES" ] ; then
                 $DBNROOT/bin/dbn_alert DATA MARINE_SFC_TEXT $job $COMOUT/${gtyp}.${pdate}.all
              fi
           fi
#
         else
           msg="${typ}.${FORM} file size is zero bytes - asci file can not be created"
           echo $msg
           postmsg "$jlogfile" "$msg"
         fi
#
      elif [ $typ = "108" ] ; then       #### NC031 108
         gtyp=env
         siz_typ=`ls -l $DATA/${typ}.${FORM} | awk '{print $5}'`
#
         if [ $siz_typ -eq 0 ] ; then
            rm $DATA/${typ}.${FORM}
         fi
#
         if [ $siz_typ -gt 0 ] ; then
           echo "Call bufr2asci_wndwave executable to create $gtyp asci files"
#
           export pgm=bufr2asci_wndwave
           . prep_step
           startmsg
           export XLFRTEOPTS="unit_vars=yes"
           export XLFUNIT_12="${typ}.${FORM}"
           export XLFUNIT_53="${gtyp}.${pdate}.sgwh"
           export XLFUNIT_54="${gtyp}.${pdate}.all"
           timex $EXECaltim/bufr2asci_wndwave 1 > ${gtyp}_ft06 2> errfile_${gtyp}
           export err=$?;err_chk
###
### Send the hourly data sub type ascii files to $COMOUT
###
           if test "$SENDCOM" = 'YES'
           then
              cp $DATA/${gtyp}.${pdate}.sgwh $COMOUT/.
              cp $DATA/${gtyp}.${pdate}.all $COMOUT/.
              if [ $SENDDBN = "YES" ] ; then
                 $DBNROOT/bin/dbn_alert DATA MARINE_SFC_TEXT $job $COMOUT/${gtyp}.${pdate}.all
              fi
           fi
#
         else
           msg="${typ}.${FORM} file size is zero bytes - asci file can not be created"
           echo $msg
           postmsg "$jlogfile" "$msg"
         fi
#
      elif [ $typ = "110" ] ; then      #### NC031 110
         gtyp=jsn1n
         siz_typ=`ls -l $DATA/${typ}.${FORM} | awk '{print $5}'`
#
         if [ $siz_typ -eq 0 ] ; then
            rm $DATA/${typ}.${FORM}
         fi 
#
         if [ $siz_typ -gt 0 ] ; then
           echo "Call bufr2asci_wndwave executable to create $gtyp asci files"
#
           export pgm=bufr2asci_wndwave
           . prep_step
           startmsg
           export XLFRTEOPTS="unit_vars=yes"
           export XLFUNIT_12="${typ}.${FORM}"
           export XLFUNIT_53="${gtyp}.${pdate}.sgwh"
           export XLFUNIT_54="${gtyp}.${pdate}.all"
           timex $EXECaltim/bufr2asci_wndwave 1 > ${gtyp}_ft06 2> errfile_${gtyp}
           export err=$?;err_chk
###
### Send the hourly data sub type ascii files to $COMOUT
###
           if test "$SENDCOM" = 'YES'
           then
              cp $DATA/${gtyp}.${pdate}.sgwh $COMOUT/.
              cp $DATA/${gtyp}.${pdate}.all $COMOUT/.
              if [ $SENDDBN = "YES" ] ; then
                 $DBNROOT/bin/dbn_alert DATA MARINE_SFC_TEXT $job $COMOUT/${gtyp}.${pdate}.all
              fi
           fi
#
         else
           msg="${typ}.${FORM} file size is zero bytes - asci file can not be created"
           echo $msg
           postmsg "$jlogfile" "$msg"
         fi
#
      elif [ $typ = "114" ] ; then      #### NC031 114
         gtyp=jsn2n
         siz_typ=`ls -l $DATA/${typ}.${FORM} | awk '{print $5}'`
#
         if [ $siz_typ -eq 0 ] ; then
            rm $DATA/${typ}.${FORM}
         fi
#
         if [ $siz_typ -gt 0 ] ; then
           echo "Call bufr2asci_wndwave executable to create $gtyp asci files"
#
           export pgm=bufr2asci_wndwave
           . prep_step
           startmsg
           export XLFRTEOPTS="unit_vars=yes"
           export XLFUNIT_12="${typ}.${FORM}"
           export XLFUNIT_53="${gtyp}.${pdate}.sgwh"
           export XLFUNIT_54="${gtyp}.${pdate}.all"
           timex $EXECaltim/bufr2asci_wndwave 1 > ${gtyp}_ft06 2> errfile_${gtyp}
           export err=$?;err_chk
###
### Send the hourly data sub type ascii files to $COMOUT
###
           if test "$SENDCOM" = 'YES'
           then
              cp $DATA/${gtyp}.${pdate}.sgwh $COMOUT/.
              cp $DATA/${gtyp}.${pdate}.all $COMOUT/.
              if [ $SENDDBN = "YES" ] ; then
                 $DBNROOT/bin/dbn_alert DATA MARINE_SFC_TEXT $job $COMOUT/${gtyp}.${pdate}.all
              fi
           fi
#
         else
           msg="${typ}.${FORM} file size is zero bytes - asci file can not be created"
           echo $msg
           postmsg "$jlogfile" "$msg"
         fi
#
      else        #### None of the wanted data types exist in the bufr tank!!!!!
#
           msg="None of the wanted data types exist in the bufr tank for ${ppdy}!"
           postmsg  "$jlogfile" "$msg"
#
      fi           ### Loop ends for the chosen data types 
#
    fi             ### Loop ends for test for the existence of already dumped files
#
done   #### Created the four data types hourly files for each cycle hour
#
    ix=`expr $ix + 1`
#
done   #### Created the previous five cycles hourly files
     cp $FIXaltim/*altimeter*pack $DATA/.
#
# Convert all the asci files containing multi-fields into GEMPAK surface files
#
     tot_asci_files=`ls *.all`
#
     for file in $tot_asci_files
     do
#
     styp=`echo $file | awk '{print $1}' | cut -c1-5`     
#
     if [ $styp = "jsn1c" ] ; then
#
cat <<\gemEOF >gem_namlst_jsn1c
 &INPARM npgiv=11/
gemEOF
#
     gemlist=gem_namlst_jsn1c
     packfile=jsn1c.altimeter.pack
     cpdy=`echo $file | awk '{print $1}' | cut -c9-14`
     hhmm=`echo $file | awk '{print $1}' | cut -c15-16`'00'
#
echo "Input gemlist file for sffdsf  " $gemlist
export pgm;. prep_step
export XLFUNIT_9="$gemlist"
$EXECaltim/sffdsf << EOF >> sffdsf.record.jsn1c
 SFEFIL   = $file
 SFOUTF   = $file.sfc
 AREA     = -90;-180;90;180
 SFPRMF   = $packfile
 DATTIM   = $cpdy/$hhmm
 TIMSTN   = 1/3999
 FORMAT   = mklein
r

EOF
#
  if test "$SENDCOM" = 'YES'
  then
    cp $DATA/$file.sfc $COMOUT/.
    if [ $SENDDBN = "YES" ] ; then
       $DBNROOT/bin/dbn_alert DATA MARINE_SFC_GEMPAK $job $COMOUT/$file.sfc
    fi
  fi
#
     else
#
cat <<\gemEOF >gem_namlst_osats
 &INPARM npgiv=9/
gemEOF
     gemlist=gem_namlst_osats
     packfile=osats.altimeter.pack
        styps=`echo $styp | cut -c1-3`
        if [ $styps = "env" ] ; then
           cpdy=`echo $file | awk '{print $1}' | cut -c7-12`
           hhmm=`echo $file | awk '{print $1}' | cut -c13-14`'00'
        else
           cpdy=`echo $file | awk '{print $1}' | cut -c9-14`
           hhmm=`echo $file | awk '{print $1}' | cut -c15-16`'00'
        fi
#
echo "Input gemlist file for sffdsf  " $gemlist
export pgm;. prep_step
export XLFUNIT_9="$gemlist"

$EXECaltim/sffdsf << EOF >> sffdsf.record.osats
 SFEFIL   = $file
 SFOUTF   = $file.sfc
 AREA     = -90;-180;90;180
 SFPRMF   = $packfile
 DATTIM   = $cpdy/$hhmm
 TIMSTN   = 1/3999
 FORMAT   = mklein
r

EOF
#
#
  if test "$SENDCOM" = 'YES'
  then
    cp $DATA/$file.sfc $COMOUT/.
    if [ $SENDDBN = "YES" ] ; then
       $DBNROOT/bin/dbn_alert DATA MARINE_SFC_GEMPAK $job $COMOUT/$file.sfc
    fi
  fi
#
     fi
#
done

msg="Altimeter data processing has completed in the script exasci_altimeter.sh.sms successfully !"
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

exit 0
