#!/bin/sh
#
# Reads in the 'adjust_pops.out' file.  Then runs the statistical model on the 
# cptp grids in the new sref, and outputs the result to a grid.  gdedit then
# places the grid into the gempak file for display.  DRB.  4/1/2004
#

set -x

thisrun=$1
date=$2
the_run=$3

# Switch to the hourly calenh (calenh4) as that has proven to verify very well.
# Switch to calenh7 as that is now the flagship product (8/25/2005). DRB
field=calenh7
 
if [ "$thisrun" = '00' -o "$thisrun" = '03' -o "$thisrun" = '06' -o "$thisrun" = '09' ]; then
   #no data for pcpn probs at hr 00 thru hr 09
   echo "Exit now as I can not produce probs at hour f00 thru f09"
   exit 0
fi
 
# Turn run hour from two to three digits, if necessary.
firstnum=`echo $thisrun | cut -c 1-1`
if [ "$firstnum" -eq  '0' ]; then
   newnum=`echo $thisrun | cut -c 2-2`
else
   newnum=$thisrun
fi
testnum=$newnum
if [ $testnum -lt 100 ]; then
   thisrun=0${thisrun}
fi

#cp ${modpath}/combine_pops_12hr.out .
cp ${FIXspc}/spc_combine_pops_12hr.out combine_pops_12hr.out

hour=${thisrun}
chour=$hour
model=spcsref_${date}${the_run}f${chour}

hour=`echo $hour | cut -c 2-3`
echo "hour= $hour"
#@ hourm1 = $hour - 3
hourm1=`expr $hour - 3`
echo "hourm1= $hourm1"
chourm1=0${hourm1}
if [ $hourm1 -lt 10 ]; then
    chourm1=00${hourm1}
fi

hourm2=`expr $hour - 6`
echo "hourm2= $hourm2"
chourm2=0$hourm2
if [ $hourm2 -lt 10 ]; then
    chourm2=00${hourm2}
fi

hourm3=`expr $hour - 9`
echo "hourm3= $hourm3"
chourm3=0$hourm3
if [ $hourm3 -lt 10 ]; then
    chourm3=00${hourm3}
fi

echo "Looking for the file $workdir/${model}"
echo "Looking for the file $COMIN/spcsref_${date}${the_run}f${chourm1}"
echo "Looking for the file $COMIN/spcsref_${date}${the_run}f${chourm2}"
echo "Looking for the file $COMIN/spcsref_${date}${the_run}f${chourm3}"

rm -f ./file?.out > /dev/null 2>&1

count=1
while [ $count -lt 180 ]
do
  if [ -s $workdir/$model -a -s ${COMIN}/spcsref_${date}${the_run}f${chourm1} -a -s ${COMIN}/spcsref_${date}${the_run}f${chourm2} -a -s ${COMIN}/spcsref_${date}${the_run}f${chourm3} ]; then
     echo " "
     echo "Files are ready...move on."
     # sleep a bit, just to make sure the file is complete.
     sleep 10
     break
  else
     echo " "
     echo "Files not all found...${count}"
     let "count=count+1"
     sleep 10
  fi
  # wait upto 30 minutes for previous files to finish...
  if [ $count -ge 178 ]
  then
        echo "$job: COULD NOT LOCATE: ${COMIN}/spcsref_${date}${the_run}f${chourm3}"
  fi
done

$GEMEXE/gdlist << EOF
 GDATTIM  = last
 GLEVEL   = 0
 GVCORD   = n
 GFUNC    = ${field}
 GDFILE   = $workdir/$model
 GAREA    = grid
 PROJ     = 
 SCALE    = 0
 OUTPUT   = f/${workdir}/file1.out
 
 r
 
 GDFILE   = $COMIN/spcsref_${date}${the_run}f${chourm1}
 OUTPUT   = f/${workdir}/file2.out
 
 r
 
 GDFILE   = $COMIN/spcsref_${date}${the_run}f${chourm2}
 OUTPUT   = f/${workdir}/file3.out
 
 r
 
 GDFILE   = $COMIN/spcsref_${date}${the_run}f${chourm3}
 OUTPUT   = f/${workdir}/file4.out
 
 r
 
 ex
 
EOF

$GEMEXE/gpend

#
# Now run the fortran software to read files and calibrate thunder pops...
#

export pgm=spc_calibrate2_12hr
. prep_step

msg="$job: spc_calibrate2_12hr $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_calibrate2_12hr file1.out file2.out file3.out file4.out $date $the_run f$chour >>$pgmout 2>errfile
export err=$?;err_chk
 
# Now use gdedit to put the file into the sref file...
$GEMEXE/gdedit << EOF
 GDEFIL   = ./file5.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 ex
 
EOF

$GEMEXE/gpend
cp ./file5.out ${DATA}/caltrw12_${the_run}_f$chour
cp ./file5.out ${COMOUT}/caltrw12_${the_run}_f$chour

