#!/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
 
if [ "$thisrun" = '00' ]; then
   #no data for pcpn probs at hr 00
   echo "Exit now as I can not produce probs at hour f00"
   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

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

#cp ${modpath}/combine_pops_rgn3_${the_run}_f${chour}.out.gz .
cp ${FIXspc}/spc_combine_pops_rgn3_${the_run}_f${chour}.out.gz combine_pops_rgn3_${the_run}_f${chour}.out.gz

echo "Looking for the file ${model}"

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

$GEMEXE/gdlist << EOF
 GDATTIM  = last
 GLEVEL   = 0
 GVCORD   = n
 GFUNC    = cptpp1
 GDFILE   = $workdir/$model
 GAREA    = grid
 PROJ     = 
 SCALE    = 0
 OUTPUT   = f/${workdir}/file1.out
 
 r
 
 GFUNC    = p03mp01
 OUTPUT   = f/${workdir}/file2.out
 
 r
 
 ex
 
EOF
$GEMEXE/gpend
#
# Now run the fortran software to read files and calibrate thunder pops...
#
# unzip the file...
rm -f ./combine_pops_rgn3_${the_run}_f${chour}.out > /dev/null 2>&1
gunzip ./combine_pops_rgn3_${the_run}_f${chour}.out.gz

export pgm=spc_calibrate2_hrly_rgn3
. prep_step

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

${EXECspc}/spc_calibrate2_hrly_rgn3 file1.out file2.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   = ./file3.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 ex
 
EOF

$GEMEXE/gpend
