echo "--------------------------------------------------------------------"
echo "Executing XXXXXXXXX exmodels_${RUN}_asci2gempak.sh XXXXXXXXX"
echo "This script reads the asci file output (${NETMOD}_alldatatype*"
echo "(only for three models nam,gfs and gdas counts) from the script"
echo "exmodels_${RUN}.sh.sms and converts to a gempak surface"
echo "file with extension sfc which will be used later on"
echo "for plotting time series with gempak sfgram program"
echo "This script converts an asci file of the type below"
echo "NUM STNID       LAT     LON          CCOUNT    MCOUNT ... ..."
echo "1   SYNOPR      26.00  -129.00        1540      1546  ... ..."
echo "2   SYNOP       27.00  -128.00       14335     14444  ... ..."
echo " ... ... ..."
echo "to a gempak file (.sfc surface files ) which can be used for"
echo "making time series using the gempak sfgram program"
echo "Author: V. Krishna Kumar"
echo "NCO/Data Management & Quality Assessment Branch September 2004"
echo "NCO/System Integration Branch July 2008"
echo "Modified to automate plotting data types from bufr dumplist"
echo "Modified to extend the time series to 30 days for all models May 2010"
echo "---------------------------------------------------------------------"

cd $DATA

########################################
set -x
msg="HAS BEGUN!"
########################################
date

. /nwprod/gempak/.gempak

cp $EXECPREP/sffdsf .
cp $FIXPREP/sffdsf.nts .
cp $FIXPREP/sffdsf.pdf .
cp $FIXPREP/sffdsf.prm .
cp $FIXPREP/sffdsf.err .
cp $FIXPREP/gem.realtime.data.2.pack .

yymm=`echo $PDY | cut -c3-6`
hhmm=$cyc'00'

### Produce namelist file for the gempak sffdsf.
### npgiv is the number of parameters to be plotted.
### The two parameters are (1) ccnt (current count) 
### and (2) mcnt (monthly count).
  
cat <<\gemEOF >gem_namlst
 &INPARM npgiv=2 /
gemEOF

DATEminus=`sh /nwprod/util/ush/finddate.sh $PDY s-30`
flist='30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00'
for fl in $flist
do
   ls ${NETMOD}_alldatatype_filem${fl}_t${cyc}z >> file_list_${NETMOD}
done

### Remove the zero size files before making the surface file
### for time series

rfiles=`cat file_list_${NETMOD}`
for rfile in $rfiles
do
   rfile_size=`ls -l $rfile | awk '{print $5}'`
   if [ $rfile_size -eq 0 ]; then
      rm -rf $rfile
   fi
done

### Get the last file number

lfilen=`ls *alldatatype*z | wc -l`

gfiles=`cat file_list_${NETMOD}`
for gfile in $gfiles
do 
   lf=`ls $gfile`
   zf=`echo $lf |cut -c25-26`
   mdate=`echo $lf |cut -c22-23`
   if [ $mdate -gt 0 ]; then
      date_str=`echo $DATEminus | awk -v day=$mdate '{ print $day }'`
      cpdy=`echo ${date_str} | cut -c3-8`
   else
      cpdy=`echo $PDY | cut -c3-8`
   fi

### Get the first file number

   ffilen=$mdate
   echo $cpdy $ffilen $lfilen
    
### Conversion gempak program from asci data to gempak extension surface

   export pgm=sffdsf
   . prep_step
   startmsg

   export XLFUNIT_9="gem_namlst"

   $EXECPREP/sffdsf << EOF > sffdsf.record

SFEFIL   = $gfile
SFOUTF   = ${NETMOD}_alldatatype_alldays_${cycle}.sfc
AREA     = -90;-180;90;180
SFPRMF   = gem.realtime.data.2.pack
DATTIM   = $cpdy/$hhmm
TIMSTN   = 50/3999
FORMAT   = mklein
r

EOF

   export err=$?;err_chk

done
    
#####################################################################
# GOOD RUN
set +x
echo "************** $job COMPLETED NORMALLY ON THE IBM"
echo "************** $job COMPLETED NORMALLY ON THE IBM"
echo "************** $job COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
echo $msg
$DATAO/postmsg "$jlogfile" "$msg"

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