#!/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 ${FIXspc}/spc_dep_param dep_param.input
#cp ${modpath}/combine_probs_svr_layer??.out .
cp ${FIXspc}/spc_combine_probs_svr_layer01.out combine_probs_svr_layer01.out
cp ${FIXspc}/spc_combine_probs_svr_layer02.out combine_probs_svr_layer02.out
cp ${FIXspc}/spc_combine_probs_svr_layer03.out combine_probs_svr_layer03.out
cp ${FIXspc}/spc_combine_probs_svr_layer04.out combine_probs_svr_layer04.out
cp ${FIXspc}/spc_combine_probs_svr_layer05.out combine_probs_svr_layer05.out
cp ${FIXspc}/spc_combine_probs_svr_layer06.out combine_probs_svr_layer06.out
cp ${FIXspc}/spc_combine_probs_svr_layer07.out combine_probs_svr_layer07.out
cp ${FIXspc}/spc_combine_probs_svr_layer08.out combine_probs_svr_layer08.out
cp ${FIXspc}/spc_combine_probs_svr_layer09.out combine_probs_svr_layer09.out
cp ${FIXspc}/spc_combine_probs_svr_layer10.out combine_probs_svr_layer10.out
cp ${FIXspc}/spc_combine_probs_svr_layer11.out combine_probs_svr_layer11.out
cp ${FIXspc}/spc_combine_probs_svr_layer12.out combine_probs_svr_layer12.out
cp ${FIXspc}/spc_combine_probs_svr_layer13.out combine_probs_svr_layer13.out
cp ${FIXspc}/spc_combine_probs_svr_layer14.out combine_probs_svr_layer14.out
cp ${FIXspc}/spc_combine_probs_svr_layer15.out combine_probs_svr_layer15.out
cp ${FIXspc}/spc_combine_probs_svr_layer16.out combine_probs_svr_layer16.out
cp ${FIXspc}/spc_combine_probs_svr_layer17.out combine_probs_svr_layer17.out
cp ${FIXspc}/spc_combine_probs_svr_layer18.out combine_probs_svr_layer18.out
cp ${FIXspc}/spc_combine_probs_svr_layer19.out combine_probs_svr_layer19.out
cp ${FIXspc}/spc_combine_probs_svr_layer20.out combine_probs_svr_layer20.out
cp ${FIXspc}/spc_combine_probs_svr_layer21.out combine_probs_svr_layer21.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
rm -f ./hicapep*.out > /dev/null 2>&1
rm -f ./eshrp*.out > /dev/null 2>&1

$GEMEXE/gdlist << EOF
 GDATTIM  = last
 GLEVEL   = 0
 GVCORD   = n
 GFUNC    = hicapep500
 GDFILE   = $workdir/$model
 GAREA    = grid
 PROJ     = 
 SCALE    = 0
 OUTPUT   = f/${workdir}/hicapep500.out
 
 r
 
 GFUNC    = hicapep1000
 OUTPUT   = f/${workdir}/hicapep1000.out
 
 r
 
 GFUNC    = hicapep2000
 OUTPUT   = f/${workdir}/hicapep2000.out
 
 r
 
 GFUNC    = hicapep3000
 OUTPUT   = f/${workdir}/hicapep3000.out
 
 r

 GFUNC    = hicapep250
 OUTPUT   = f/${workdir}/hicapep250.out

 r
 
 GFUNC    = eshrp20
 OUTPUT   = f/${workdir}/eshrp20.out
 
 r
 
 GFUNC    = eshrp30
 OUTPUT   = f/${workdir}/eshrp30.out
 
 r
 
 GFUNC    = eshrp40
 OUTPUT   = f/${workdir}/eshrp40.out
 
 r

 GFUNC    = eshrp50
 OUTPUT   = f/${workdir}/eshrp50.out 

 r

 GFUNC    = dcapep1000
 OUTPUT   = f/${workdir}/dcapep1000.out 

 r

 GFUNC    = dcapep2000
 OUTPUT   = f/${workdir}/dcapep2000.out

 r

 GFUNC    = dapelclp10
 OUTPUT   = f/${workdir}/dapelclp10.out

 r

 GFUNC    = tmpcpm25
 GLEVEL   = 500
 GVCORD   = PRES
 OUTPUT   = f/${workdir}/tmpcpm25.out

 r

 GFUNC    = tmpcpm20
 OUTPUT   = f/${workdir}/tmpcpm20.out

 r

 GFUNC    = tmpcpm15
 OUTPUT   = f/${workdir}/tmpcpm15.out

 r
 
 ex
 
EOF
$GEMEXE/gpend
#
# Now run the fortran software to read files and calibrate thunder pops...
#
rm -f ./combine_probs_svr1.out > /dev/null 2>&1

# gempak has trouble with grids all of the same value.  thus, if 0% everywhere, grid will not be avbl.  
# copy over a zero grid just in case that occurred...can happen with cape > 3000.

if [ -s ./hicapep500.out ]; then
   echo "The hicapep500.out grid is available"
else
   echo "Copy a zero grid in for hicapep500 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./hicapep500.out
fi
 
if [ -s ./hicapep1000.out ]; then
   echo "The hicapep1000.out grid is available"
else
   echo "Copy a zero grid in for hicapep1000 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./hicapep1000.out
fi
 
if [ -s ./hicapep2000.out ];  then
   echo "The hicapep2000.out grid is available"
else
   echo "Copy a zero grid in for hicapep2000 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./hicapep2000.out
fi
 
if [ -s ./hicapep3000.out ]; then
   echo "The hicapep3000.out grid is available"
else
   echo "Copy a zero grid in for hicapep3000 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./hicapep3000.out
fi
 
if [ -s ./eshrp20.out ]; then
   echo "The eshrp20.out grid is available"
else
   echo "Copy a zero grid in for eshrp20 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./eshrp20.out
fi
 
if [ -s ./eshrp30.out ]; then
   echo "The eshrp30.out grid is available"
else
   echo "Copy a zero grid in for eshrp30 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./eshrp30.out
fi
 
if [ -s ./eshrp40.out ]; then
   echo "The eshrp40.out grid is available"
else
   echo "Copy a zero grid in for eshrp40 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./eshrp40.out
fi
 
if [ -s ./eshrp50.out ]; then
   echo "The eshrp50.out grid is available"
else
   echo "Copy a zero grid in for eshrp50 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./eshrp50.out
fi

cp ./combine_probs_svr_layer01.out ./combine_probs_svr1.out

export pgm=spc_calibrate2_svr
. prep_step

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

${EXECspc}/spc_calibrate2_svr hicapep500.out eshrp30.out $date $the_run f$chour >>$pgmout 2>errfile
export err=$?;err_chk

mv ./file3.out ./layer01.pred


cp ./combine_probs_svr_layer02.out ./combine_probs_svr1.out

. prep_step

${EXECspc}/spc_calibrate2_svr hicapep500.out eshrp40.out $date $the_run f$chour
export err=$?;err_chk

mv ./file3.out ./layer02.pred


cp ./combine_probs_svr_layer03.out ./combine_probs_svr1.out
. prep_step

${EXECspc}/spc_calibrate2_svr hicapep1000.out eshrp30.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer03.pred


cp ./combine_probs_svr_layer04.out ./combine_probs_svr1.out

. prep_step

${EXECspc}/spc_calibrate2_svr hicapep1000.out eshrp40.out $date $the_run f$chour
export err=$?;err_chk

mv ./file3.out ./layer04.pred

cp ./combine_probs_svr_layer05.out ./combine_probs_svr1.out

. prep_step

${EXECspc}/spc_calibrate2_svr hicapep2000.out eshrp30.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer05.pred


cp ./combine_probs_svr_layer06.out ./combine_probs_svr1.out

. prep_step

${EXECspc}/spc_calibrate2_svr hicapep2000.out eshrp40.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer06.pred


cp ./combine_probs_svr_layer07.out ./combine_probs_svr1.out

. prep_step

${EXECspc}/spc_calibrate2_svr hicapep3000.out eshrp20.out $date $the_run f$chour
export err=$?;err_chk

mv ./file3.out ./layer07.pred


cp ./combine_probs_svr_layer08.out ./combine_probs_svr1.out

. prep_step

${EXECspc}/spc_calibrate2_svr hicapep3000.out eshrp30.out $date $the_run f$chour
export err=$?;err_chk

mv ./file3.out ./layer08.pred

cp ./combine_probs_svr_layer09.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep3000.out eshrp40.out $date $the_run f$chour
export err=$?;err_chk

mv ./file3.out ./layer09.pred

cp ./combine_probs_svr_layer10.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep250.out eshrp30.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer10.pred

cp ./combine_probs_svr_layer11.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep250.out eshrp40.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer11.pred

cp ./combine_probs_svr_layer12.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep250.out eshrp50.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer12.pred

cp ./combine_probs_svr_layer13.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep500.out dcapep1000.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer13.pred

cp ./combine_probs_svr_layer14.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep500.out dcapep2000.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer14.pred

cp ./combine_probs_svr_layer15.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep1000.out dcapep1000.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer15.pred

cp ./combine_probs_svr_layer16.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep1000.out dcapep2000.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer16.pred

cp ./combine_probs_svr_layer17.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep500.out dapelclp10.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer17.pred

cp ./combine_probs_svr_layer18.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep1000.out dapelclp10.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer18.pred

cp ./combine_probs_svr_layer19.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep500.out tmpcpm15.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer19.pred

cp ./combine_probs_svr_layer20.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr hicapep500.out tmpcpm20.out $date $the_run f$chour 
export err=$?;err_chk

mv ./file3.out ./layer20.pred


export pgm=spc_calibrate2_svr
. prep_step

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

cp ./combine_probs_svr_layer21.out ./combine_probs_svr1.out
$EXECspc/spc_calibrate2_svr hicapep500.out tmpcpm25.out $date $the_run f$chour >>$pgmout 2>errfile
export err=$?;err_chk

mv ./file3.out ./layer21.pred

#
# Now...I simply need to go through each grid and find the max value at each grid point...
rm -f ./file3.out > /dev/null 2>&1
rm -f ./layermax.out > /dev/null 2>&1

export pgm=spc_add_sub_maxmin
. prep_step

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

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer01.pred ./layer02.pred ${date} ${the_run} f${chour} calsvr max >>$pgmout 2>errfile
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer03.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer04.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer05.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer06.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer07.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer08.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer09.pred ./file3.out ${date} ${the_run} f${chour} calsvr max
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer10.pred ./file3.out ${date} ${the_run} f${chour} calsvr max
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer11.pred ./file3.out ${date} ${the_run} f${chour} calsvr max
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer12.pred ./file3.out ${date} ${the_run} f${chour} calsvr max
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer13.pred ./file3.out ${date} ${the_run} f${chour} calsvr max
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer14.pred ./file3.out ${date} ${the_run} f${chour} calsvr max
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer15.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer16.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer17.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer18.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer19.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer20.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
export err=$?;err_chk

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

. prep_step
${EXECspc}/spc_add_sub_maxmin ./layer21.pred ./file3.out ${date} ${the_run} f${chour} calsvr max 
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
 
 GDEFIL   = ./layermax.out
 
 r
 
 ex
 
EOF

# Also output the calsvr file as the conditional severe for 24 hour calculation...
$GEMEXE/gdlist << EOF
 GDATTIM  = last
 GLEVEL   = 0
 GVCORD   = n
 GFUNC    = mul(mul(calsvr,calenh7),0.01)
 GDFILE   = $workdir/$model
 GAREA    = grid
 PROJ     = 
 SCALE    = 0
 OUTPUT   = f/${COMOUT}/file_svr_${chour}.out
 
 r

 GFUNC    = calsvr 
 OUTPUT   = f/${COMOUT}/file_condsvr_${chour}.out

 r

ex

EOF

$GEMEXE/gpend


f1=999
f5=999

if [ "$chour" -eq "012" ]; then
    f1=999
    f2=999
    f3=999
    f4=999
    f5=03
    f6=06
    f7=09
    f8=12
elif [ "$chour" -eq "015" ]; then
    f1=999
    f2=999
    f3=999
    f4=999
    f5=06
    f6=09
    f7=12
    f8=15
elif [ "$chour" -eq  "018" ]; then
    f1=999
    f2=999
    f3=999
    f4=999
    f5=09
    f6=12
    f7=15
    f8=18
elif [ "$chour" -eq "021" ]; then
    f1=999
    f2=999
    f3=999
    f4=999
    f5=12
    f6=15
    f7=18
    f8=21
elif [ "$chour" -eq "024" ]; then
    f1=03
    f2=06
    f3=09
    f4=12
    f5=15
    f6=18
    f7=21
    f8=24
elif [ "$chour" -eq "027" ]; then
    f1=06
    f2=09
    f3=12
    f4=15
    f5=18
    f6=21
    f7=24
    f8=27
elif [ "$chour" -eq "030" ]; then
   f1=09  
   f2=12
   f3=15
   f4=18
   f5=21
   f6=24
   f7=27
   f8=30
elif [ "$chour" -eq "033" ]; then
   f1=12  
   f2=15
   f3=18
   f4=21
   f5=24
   f6=27
   f7=30
   f8=33
elif [ "$chour" -eq "036" ]; then
   f1=15  
   f2=18
   f3=21
   f4=24
   f5=27
   f6=30
   f7=33
   f8=36
elif [ "$chour" -eq "039" ]; then
   f1=18  
   f2=21
   f3=24
   f4=27
   f5=30
   f6=33
   f7=36
   f8=39
elif [ "$chour" -eq "042" ]; then
   f1=21  
   f2=24
   f3=27
   f4=30
   f5=33
   f6=36
   f7=39
   f8=42
elif [ "$chour" -eq "045" ]; then
   f1=24  
   f2=27
   f3=30
   f4=33
   f5=36
   f6=39
   f7=42
   f8=45
elif [ "$chour" -eq "048" ]; then
   f1=27  
   f2=30
   f3=33
   f4=36
   f5=39
   f6=42
   f7=45
   f8=48
elif [ "$chour" -eq "051" ]; then
   f1=30  
   f2=33
   f3=36
   f4=39
   f5=42
   f6=45
   f7=48
   f8=51
elif [ "$chour" -eq "054" ]; then
   f1=33  
   f2=36
   f3=39
   f4=42
   f5=45
   f6=48
   f7=51
   f8=54
elif [ "$chour" -eq "057" ]; then
   f1=36  
   f2=39
   f3=42
   f4=45
   f5=48
   f6=51
   f7=54
   f8=57
elif [ "$chour" -eq "060" ]; then
   f1=39  
   f2=42
   f3=45
   f4=48
   f5=51
   f6=54
   f7=57
   f8=60
elif [ "$chour" -eq "063" ]; then
   f1=42  
   f2=45
   f3=48
   f4=51
   f5=54
   f6=57
   f7=60
   f8=63
elif [ "$chour" -eq "066" ]; then
   f1=45
   f2=48
   f3=51
   f4=54 
   f5=57
   f6=60
   f7=63
   f8=66
elif [ "$chour" -eq "069" ]; then
   f1=48
   f2=51
   f3=54
   f4=57
   f5=60
   f6=63
   f7=66
   f8=69
elif [ "$chour" -eq "072" ]; then
   f1=51
   f2=54
   f3=57
   f4=60
   f5=63
   f6=66
   f7=69
   f8=72
elif [ "$chour" -eq "075" ]; then
   f1=54
   f2=57
   f3=60
   f4=63
   f5=66
   f6=69
   f7=72
   f8=75
elif [ "$chour" -eq "078" ]; then
   f1=57
   f2=60
   f3=63
   f4=66
   f5=69
   f6=72
   f7=75
   f8=78
elif [ "$chour" -eq "081" ]; then
   f1=60
   f2=63
   f3=66
   f4=69
   f5=72
   f6=75
   f7=78
   f8=81
elif [ "$chour" -eq "084" ]; then
   f1=63
   f2=66
   f3=69
   f4=72
   f5=75
   f6=78
   f7=81
   f8=84
elif [ "$chour" -eq "087" ]; then
   f1=66
   f2=69
   f3=72
   f4=75
   f5=78
   f6=81
   f7=84
   f8=87
fi   

svrcnt=0

if [ ${f5} -eq 999 ]; then
   echo "This grid is before hour 12 so finish and exit now."
   exit 0
else
   echo "Run the 12 hour probability of severe calculation."
fi 

if [ ${f1} -eq 999 ]; then
     echo "This grid is before hour 24 so only do the 12 hour calculation."
     c24hour=FALSE
else 
     c24hour=TRUE
fi


while [ $svrcnt -lt 90 ]
do

   if [ $c24hour = FALSE ]; then
      # only do the 12 hour calculation...then exit
      echo " .. less than 24 hours: only do the 12 hour calculation... "
      if [ -s ${COMOUT}/file_svr_0${f5}.out -a -s ${COMOUT}/file_svr_0${f6}.out -a -s ${COMOUT}/file_svr_0${f7}.out -a -s ${COMOUT}/file_svr_0${f8}.out ]; then
        #
        # 12 hour
        #
        rm -f ./file_svr_12h.out  > /dev/null 2>&1

        export pgm=spc_combine_svr_dependence_12hr
        . prep_step

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

        ${EXECspc}/spc_combine_svr_dependence_12hr ${COMOUT}/file_svr_0${f5}.out \
         ${COMOUT}/file_svr_0${f6}.out ${COMOUT}/file_svr_0${f7}.out \
         ${COMOUT}/file_svr_0${f8}.out $date $the_run f$chour >>$pgmout 2>errfile
        export err=$?;err_chk


$GEMEXE/gdedit << EOF
 GDEFIL   = ./file_svr_12h.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 ex
 
EOF

        ${EXECspc}/spc_combine_svr_dependence_12hr ${COMOUT}/file_condsvr_0${f5}.out \
         ${COMOUT}/file_condsvr_0${f6}.out ${COMOUT}/file_condsvr_0${f7}.out \
         ${COMOUT}/file_condsvr_0${f8}.out $date $the_run f$chour >>$pgmout 2>errfile
        export err=$?;err_chk

        sed -e 's/CALSVR12/CALCONSVR12/g' ./file_svr_12h.out > ./file_svr_12hc.out

$GEMEXE/gdedit << EOF
 GDEFIL   = ./file_svr_12hc.out
 GDFILE   = $workdir/$model
 GPACK    =

 r

 ex

EOF


$GEMEXE/gpend

      exit 0

    fi


  elif [ $c24hour = TRUE ]; then
    # run the software to combine the 24-hr grids...
    echo " ... more or equal 24 hours: run the software to combine the 24-hr grids... "

    if [ -s ${COMOUT}/file_svr_0${f1}.out -a -s ${COMOUT}/file_svr_0${f2}.out \
         -a -s ${COMOUT}/file_svr_0${f3}.out -a -s ${COMOUT}/file_svr_0${f4}.out \
         -a -s ${COMOUT}/file_svr_0${f5}.out -a -s ${COMOUT}/file_svr_0${f6}.out \
         -a -s ${COMOUT}/file_svr_0${f7}.out -a -s ${COMOUT}/file_svr_0${f8}.out ]; then
     #
     # 24 hour
     #
     rm -f ./file_svr_24h.out > /dev/null 2>&1

     export pgm=spc_combine_svr_dependence
     . prep_step

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

     ${EXECspc}/spc_combine_svr_dependence ${COMOUT}/file_svr_0${f1}.out \
      ${COMOUT}/file_svr_0${f2}.out ${COMOUT}/file_svr_0${f3}.out ${COMOUT}/file_svr_0${f4}.out \
      ${COMOUT}/file_svr_0${f5}.out ${COMOUT}/file_svr_0${f6}.out ${COMOUT}/file_svr_0${f7}.out \
      ${COMOUT}/file_svr_0${f8}.out $date $the_run f$chour >>$pgmout 2>errfile
      #export err=$?;err_chk

$GEMEXE/gdedit << EOF
 GDEFIL   = ./file_svr_24h.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 ex
 
EOF

     ${EXECspc}/spc_combine_svr_dependence ${COMOUT}/file_condsvr_0${f1}.out \
      ${COMOUT}/file_condsvr_0${f2}.out ${COMOUT}/file_condsvr_0${f3}.out  \
      ${COMOUT}/file_condsvr_0${f4}.out ${COMOUT}/file_condsvr_0${f5}.out  \
      ${COMOUT}/file_condsvr_0${f6}.out ${COMOUT}/file_condsvr_0${f7}.out \
      ${COMOUT}/file_condsvr_0${f8}.out $date $the_run f$chour >>$pgmout 2>errfile
      #export err=$?;err_chk

     sed -e 's/CALSVR24/CALCONSVR24/g' ./file_svr_24h.out > ./file_svr_24hc.out

$GEMEXE/gdedit << EOF
 GDEFIL   = ./file_svr_24hc.out
 GDFILE   = $workdir/$model
 GPACK    =

 r

 ex

EOF

    #
    # 12 hour
    #
    rm -f ./file_svr_12h.out > /dev/null 2>&1

    export pgm=spc_combine_svr_dependence_12hr
    . prep_step

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

    ${EXECspc}/spc_combine_svr_dependence_12hr ${COMOUT}/file_svr_0${f5}.out \
     ${COMOUT}/file_svr_0${f6}.out ${COMOUT}/file_svr_0${f7}.out \
     ${COMOUT}/file_svr_0${f8}.out $date $the_run f$chour >>$pgmout 2>errfile
    export err=$?;err_chk


$GEMEXE/gdedit << EOF
 GDEFIL   = ./file_svr_12h.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 ex
 
EOF

     ${EXECspc}/spc_combine_svr_dependence_12hr ${COMOUT}/file_condsvr_0${f5}.out \
      ${COMOUT}/file_condsvr_0${f6}.out ${COMOUT}/file_condsvr_0${f7}.out \
      ${COMOUT}/file_condsvr_0${f8}.out $date $the_run f$chour >>$pgmout 2>errfile
     export err=$?;err_chk

     sed -e 's/CALSVR12/CALCONSVR12/g' ./file_svr_12h.out > ./file_svr_12hc.out

$GEMEXE/gdedit << EOF
 GDEFIL   = ./file_svr_12hc.out
 GDFILE   = $workdir/$model
 GPACK    =

 r

 ex

EOF


$GEMEXE/gpend

      exit 0
    fi
  fi

  let "svrcnt=svrcnt+1"
  # wait up to 15 minutes for previous grids to be produced...
  if [ $svrcnt -lt 88 ]; then
    echo "Waiting for a previous grid to finish...count is $svrcnt of 90"
    sleep 10
  fi
  
done


