echo "-------------------------------------------------------------------"
echo "Executing exhrly_realtime_asci2gempak.sh   XXXX"
echo "This script reads the asci file output (hrly_alldatatype_${cycle})"
echo "(the hourly dump counts) from the script ex_hrly_realtime.sh.sms and"
echo "converts to a gempak surface file with extension sfc which will be used"
echo "later on 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 "Modified to extend time series out to 3 days May 2010"
echo "--------------------------------------------------------------------"

cd $DATA

########################################
set -xa
msg="HAS BEGUN!"
########################################

### Copy hrly_alldatatype_${cycle} files
### from current PDY directory to the work 
### directory and rename it with $PDY extension 

cp $COMIN/hrly_alldatatype_t*z .
for cfiles in `ls hrly_alldatatype_t*z`
do
   cp $cfiles $cfiles.${PDY}
done

rm -rf hrly_alldatatype_t*z

### Exact File Name in the temp directory
### hrly_alldatatype_thhz.yyyymmdd

lsfilec=`ls hrly_alldatatype*${PDY}`
bhourc=`echo $lsfilec | cut -c19-20`
bdatec=`echo $lsfilec | cut -c25-30`
echo $bhourc $bdatec

lrfilec=`ls -r hrly_alldatatype*${PDY}`
ehourc=`echo $lrfilec | cut -c19-20`
edatec=`echo $lrfilec | cut -c25-30`
echo $ehourc $edatec

cdiff=`expr $ehourc - $bhourc`
cfilen=`expr $cdiff + 1`
pdiff=`expr 24 - $cdiff`
pfilen=$pdiff
echo "Current number of files available for $PDY is $cfilen"
echo "Current number of files required for $PDYm3 is $pfilen"

### Copy hrly_alldatatype_${cycle} files
### from previous day PDYm1 directory to the work 
### directory and rename it with $PDYm1 extension

PDYlist="m1 m2 m3"

for pdyl in $PDYlist
do
   echo "$COMIN${pdyl}"
done

cp $COMINm1/hrly_alldatatype_t*z .
for pfiles in `ls hrly_alldatatype_t*z`
do
   cp $pfiles $pfiles.${PDYm1}
done

cp $COMINm2/hrly_alldatatype_t*z .
for tfiles in `ls hrly_alldatatype_t*z`
do
   cp $tfiles $tfiles.${PDYm2}
done

cp $COMINm3/hrly_alldatatype_t*z .
for ufiles in `ls hrly_alldatatype_t*z`
do
   cp $ufiles $ufiles.${PDYm3}
done

rm -rf hrly_alldatatype_t*z

### Exact File Name in the temp directory
### hrly_alldatatype_thhz.yyyymmdd

ls hrly_alldatatype*${PDYm3} | tail -$pdiff > file_list_convert_temp
ls hrly_alldatatype*${PDYm2} >> file_list_convert_temp
ls hrly_alldatatype*${PDYm1} >> file_list_convert_temp
ls hrly_alldatatype*${PDY} >> file_list_convert_temp

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

### Produce namelist file for the gempak sffdsf   
### npgiv is the number of parameters to be plotted 

cat <<\gemEOF >gem_namlst
 &INPARM npgiv=2 /
gemEOF

ix=0
for gfile in `cat file_list_convert_temp`
do 
   ix=`expr $ix + 1`
   if [ $ix -lt 10 ]; then
      ix='0'$ix
   fi
   lf=`ls $gfile`

### Exact File Name in the directory
### hrly_alldatatype_thhz.yyyymmdd

   scent=`echo $lf |cut -c23-24`
   syear=`echo $lf |cut -c25-26`
   smonth=`echo $lf |cut -c27-28`
   sdate=`echo $lf |cut -c29-30`
   shour=`echo $lf |cut -c19-20`

   cpdy=$syear$smonth$sdate 
   lpdy=$scent$syear$smonth$sdate
   hhmm=$shour'00'

### Change lower case letters to upper case letters - otherwise
### gempak screams and fails!!!

   tr '[a-z]' '[A-Z]' < $gfile > ${NETH}_alldatatype_${lpdy}_file${ix}_t${shour}z

### 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
   #/nw${envir}/exec/sffdsf << EOF > sffdsf.record
   /nwprod/exec/sffdsf << EOF > sffdsf.record

SFEFIL   = ${NETH}_alldatatype_${lpdy}_file${ix}_t${shour}z
SFOUTF   = ${NETH}_alldatatype.sfc
AREA     = -90;-180;90;180
SFPRMF   = gem.realtime.data.2.pack
DATTIM   = $cpdy/$hhmm
TIMSTN   = 100/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
#KK# postmsg "$jlogfile" "$msg"

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