#!/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
 
#cp ${modpath}/combine_pops*f*.out .
cp ${FIXspc}/spc_combine_pops_${the_run}_f000.out combine_pops_${the_run}_f000.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f003.out combine_pops_${the_run}_f003.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f006.out combine_pops_${the_run}_f006.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f009.out combine_pops_${the_run}_f009.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f012.out combine_pops_${the_run}_f012.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f015.out combine_pops_${the_run}_f015.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f018.out combine_pops_${the_run}_f018.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f021.out combine_pops_${the_run}_f021.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f024.out combine_pops_${the_run}_f024.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f027.out combine_pops_${the_run}_f027.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f030.out combine_pops_${the_run}_f030.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f033.out combine_pops_${the_run}_f033.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f036.out combine_pops_${the_run}_f036.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f039.out combine_pops_${the_run}_f039.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f042.out combine_pops_${the_run}_f042.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f045.out combine_pops_${the_run}_f045.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f048.out combine_pops_${the_run}_f048.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f051.out combine_pops_${the_run}_f051.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f054.out combine_pops_${the_run}_f054.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f057.out combine_pops_${the_run}_f057.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f060.out combine_pops_${the_run}_f060.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f063.out combine_pops_${the_run}_f063.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f066.out combine_pops_${the_run}_f066.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f069.out combine_pops_${the_run}_f069.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f072.out combine_pops_${the_run}_f072.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f075.out combine_pops_${the_run}_f075.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f078.out combine_pops_${the_run}_f078.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f081.out combine_pops_${the_run}_f081.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f084.out combine_pops_${the_run}_f084.out
cp ${FIXspc}/spc_combine_pops_${the_run}_f087.out combine_pops_${the_run}_f087.out
cp ${FIXspc}/spc_combine_pops_dryt_rgn3_21_f030.out combine_pops_dryt_rgn3_21_f030.out
 
hour=${thisrun}
chour=$hour

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

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...
#
export pgm=spc_calibrate2_hrly
. prep_step

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

${EXECspc}/spc_calibrate2_hrly file1.out file2.out $date $the_run f$chour
#
# 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
