#!/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_dryt.out .
cp ${FIXspc}/spc_combine_pops_dryt.out combine_pops_dryt.out
#

echo "Looking for the file ${model}"

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

$GEMEXE/gdlist << EOF
 GDATTIM  = last
 GLEVEL   = 0
 GVCORD   = n
 GFUNC    = drytp2
 GDFILE   = $workdir/$model
 GAREA    = grid
 PROJ     = 
 SCALE    = 0
 OUTPUT   = f/${workdir}/file1.out
 
 r
 
 GFUNC    = sub(100,p03mp01)
 OUTPUT   = f/${workdir}/file2.out
 
 r
 
 ex
 
EOF
$GEMEXE/gpend
#
# Now run the fortran software to read files and calibrate thunder pops...
#
export pgm=spc_calibrate2_dryt
. prep_step

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

${EXECspc}/spc_calibrate2_dryt 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

# While here...go ahead and combine the wet and dry probs now...
$GEMEXE/gddiag << EOF

 GDFILE   = $workdir/$model
 GDOUTF   = $workdir/$model
 GFUNC    = smax(caltrw,caldry)
 GDATTIM  = LAST
 GLEVEL   = 0
 GVCORD   = n
 GRDNAM   = calenh
 GPACK    =  

r

 gfunc = sub(add(caldry,caltrw),quo(mul(caldry,caltrw),100))
 grdnam = calenh2

r

 gfunc = sub(caldry,quo(mul(caldry,caltrw),50))
 grdnam = caldry2

r

 gfunc = smax(smax(calenh,caltrwmid),caltrwhigh)
 grdnam = calenh3

r

 gfunc = smax(caltrwhr,caldry)
 grdnam = calenh4

r

# gfunc = smax(caltrwhrrgn,caldry)
# grdnam = calenh5

#r

# gfunc = smax(caltrwhrrgn2,caldry)
# grdnam = calenh6

#r

! gfunc = smax(caltrwhrrgn3,caldry)
! changed to new region 3 dryt parameter 7/13/2005...
 gfunc = smax(caltrwhrrgn3,caldryhrrgn3)
 grdnam = calenh7

r

ex

EOF

$GEMEXE/gpend
