#!/bin/sh
#########################################################################
#									#
# Script:  exgefsmos_gempak.sh.sms					#
#									#
#  This script reads ensemble MOS text files and transfers the data	#
#  into GEMPAK surface data files.					#
#									#
#  Required inputs are cycle date (YYYYMMDD) and cycle time (HH).  A	#
#  third optional input is the specific member to process, given as	#
#  geXXX, where XXX is the member designation (e.g., P01, p02, ...      #
#									#
# Log:									#
# K. Brill/HPC		12/28/01					#
# K. Brill/HPC		 6/28/02  Transfer LDM ready text file to ncodas#
# 				  for display in NWX			#
# L. Lin  /NCO          02/22/06  for gefs                              #
#########################################################################  

set -x

cd $DATA

msg="$job started"
sh $utilscript/postmsg.sh "$jlogfile" "$msg"

curtime=`echo $PDY | cut -c3-`/${cyc}00

#  Assign gefs ensemble member name(s).

if [ $cyc -eq 00 ] ; then
  ensmems="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"
else
  ensmems="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"
fi

#  Setup output file naming convention:

outfilbase=${PDY}${cyc}.mm

for mem in $ensmems ; do
  ucmem=`echo $mem | tr '[a-z]' '[A-Z]'`

# Read from the ensemble text mex file.

  txtfil=$COMIN/mdl_ge${mem}mex.txt
  export pgm=mostxt; startmsg
  $EXEChpc/hpc_mostxt << EOF >> $pgmout 2> errfile
$txtfil
$ucmem
EOF
  export err=$?;err_chk
  if [ -s mostxt.out ]; then

# Create the GEMPAK surface file for this member.
# Use the N-AWIPS decoder for new MOS, dcxmos.

    sfc=${outfilbase}${mem}
    txt=${outfilbase}${mem}mos
    cp mostxt.out $txt
    dcxmos -c $curtime $sfc < mostxt.out
    /bin/rm -f mostxt.out

    if [ -s $sfc ] ; then
      if [ "$SENDCOM" = "YES" ] ; then
        mv $sfc $COMOUT/$sfc
	mv $txt $COMOUT/$txt
        if [ "$SENDDBN" = "YES" ] ; then
          $DBNROOT/bin/dbn_alert MODEL SFC_MDLGEFS $job $COMOUT/$sfc
          #$DBNROOT/bin/dbn_alert MODEL LDM_MDLGEFS $job $COMOUT/$txt
	fi
      fi
    else
      err_exit "$sfc not found"
    fi
  else
    err_exit "mostxt.out not created"
  fi
done

msg="$job completed normally"
sh $utilscript/postmsg.sh "$jlogfile" "$msg"
