#!/bin/sh
#
# Adapted to compute the layer roadsnow accumulation probabilities.  DRB.  8/8/2005.
#
# 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

if [ "$the_run" = '09' ]; then
   echo " spc_cal_roadsnow.sh runs at 09Z ..."
   # set the group 1 (daytime) output hours...
   if [ "$thisrun" = '06' -o "$thisrun" = '09' -o "$thisrun" = '12' -o "$thisrun" = '15'  -o "$thisrun" = '30' -o "$thisrun" = '33' -o "$thisrun" = '36'  -o "$thisrun" = '39' -o "$thisrun" = '54' -o "$thisrun" = '57' -o "$thisrun" = '60'  -o "$thisrun" = '63' -o "$thisrun" = '78' -o "$thisrun" = '81' -o "$thisrun" = '84' -o "$thisrun" = '87' ]; then

      # day time...
      grouptime=01
   else
      # night time
      grouptime=02
   fi

elif [ "$the_run" = '21' ]; then
   echo " spc_cal_roadsnow.sh runs at 21Z ..."
   # set the group 1 (daytime) output hours...
   if [ "$thisrun" = '06' -o "$thisrun" = '09' -o "$thisrun" = '12' -o "$thisrun" = '15'  -o "$thisrun" = '30' -o "$thisrun" = '33' -o "$thisrun" = '36'  -o "$thisrun" = '39' -o "$thisrun" = '54' -o "$thisrun" = '57' -o "$thisrun" = '60'  -o "$thisrun" = '63' -o "$thisrun" = '78' -o "$thisrun" = '81' -o "$thisrun" = '84' -o "$thisrun" = '87' ]; then
       # night time...
       grouptime=02
   else
       # day time...
       grouptime=01
   fi

else
   grouptime=01
   echo " "
   echo "Could not properly identify day or night group; set to day time and run."
   echo " "
fi

echo " "
echo " spc_cal_roadsnow : This is an f${thisrun} forecast so using group time= $grouptime"
echo " "
 
# 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

#
thesnomon=`date +%b`
if [ "$thesnomon" = 'Apr' ]; then
   thesnomon="apr" 
elif [ "$thesnomon" = 'Mar' ]; then
   thesnomon="mar"
elif [ "$thesnomon" = 'Feb' ]; then
   thesnomon="feb"
elif [ "$thesnomon" = 'Jan' ]; then
   thesnomon="jan"
elif [ "$thesnomon" = 'Dec' ]; then
   thesnomon="dec"
elif [ "$thesnomon" = 'Nov' ]; then
   thesnomon="nov"
else 
   thesnomon="oct"
fi

cp ${FIXspc}/spc_slope_inter.info.snow ./slope_inter.info.snow

cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer01_group01_${thesnomon} ./combine_probs_roadsnow.out.layer01_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer01_group02_${thesnomon} ./combine_probs_roadsnow.out.layer01_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer02_group01_${thesnomon} ./combine_probs_roadsnow.out.layer02_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer02_group02_${thesnomon} ./combine_probs_roadsnow.out.layer02_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer03_group01_${thesnomon} ./combine_probs_roadsnow.out.layer03_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer03_group02_${thesnomon} ./combine_probs_roadsnow.out.layer03_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer04_group01_${thesnomon} ./combine_probs_roadsnow.out.layer04_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer04_group02_${thesnomon} ./combine_probs_roadsnow.out.layer04_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer05_group01_${thesnomon} ./combine_probs_roadsnow.out.layer05_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer05_group02_${thesnomon} ./combine_probs_roadsnow.out.layer05_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer06_group01_${thesnomon} ./combine_probs_roadsnow.out.layer06_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer06_group02_${thesnomon} ./combine_probs_roadsnow.out.layer06_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer07_group01_${thesnomon} ./combine_probs_roadsnow.out.layer07_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer07_group02_${thesnomon} ./combine_probs_roadsnow.out.layer07_group02
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer08_group01_${thesnomon} ./combine_probs_roadsnow.out.layer08_group01
cp ${FIXspc}/spc_combine_probs_roadsnow.out.layer08_group02_${thesnomon} ./combine_probs_roadsnow.out.layer08_group02

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 ./rsaeap*.out > /dev/null 2>&1
rm -f ./ptype*.out > /dev/null 2>&1
rm -f ./p03mp01*.out > /dev/null 2>&1
rm -f ./rsapp*.out > /dev/null 2>&1
rm -f ./p03mx*.out > /dev/null 2>&1

$GEMEXE/gdlist << EOF
 GDATTIM  = last
 GLEVEL   = 0
 GVCORD   = n
 GFUNC    = ptypefz
 GDFILE   = $workdir/$model
 GAREA    = grid
 PROJ     = 
 SCALE    = 0
 OUTPUT   = f/${workdir}/ptypefz.out
 
 r
 
 GFUNC    = ptypecfz
 OUTPUT   = f/${workdir}/ptypecfz.out
 
 r
 
 GFUNC    = p03mp01
 OUTPUT   = f/${workdir}/p03mp01.out
 
 r
 
 GFUNC    = rsaeap1
 OUTPUT   = f/${workdir}/rsaeap1.out
 
 r

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

 r
 
 GFUNC    = rsaeap4
 OUTPUT   = f/${workdir}/rsaeap4.out
 
 r
 
 GFUNC    = rsapp1
 OUTPUT   = f/${workdir}/rsapp1.out
 
 r
 
 GFUNC    = p03mx
 OUTPUT   = f/${workdir}/p03mx.out
 
 r
 
 ex
 
EOF
$GEMEXE/gpend
#
# Now run the fortran software to read files and calibrate thunder pops...
#
rm -f ./combine_probs_snow.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 ./ptypefz.out ]; then
   echo "The ptypefz.out grid is available"
else
   echo "Copy a zero grid in for ptypefz now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./ptypefz.out
fi

if [ -s ./ptypecfz.out ]; then
   echo "The ptypecfz.out grid is available"
else
   echo "Copy a zero grid in for ptypecfz now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./ptypecfz.out
fi

if [ -s ./p03mp01.out ]; then
   echo "The p03mp01.out grid is available"
else
   echo "Copy a zero grid in for ptypecfz now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./p03mp01.out
fi

if [ -s ./rsaeap1.out ]; then
   echo "The rsaeap1.out grid is available"
else
   echo "Copy a zero grid in for rsaeap1 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./rsaeap1.out
fi

if [ -s ./rsaeap2.out ]; then
   echo "The rsaeap2.out grid is available"
else
   echo "Copy a zero grid in for rsaeap2 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./rsaeap2.out
fi

if [ -s ./rsaeap4.out ]; then
   echo "The rsaeap4.out grid is available"
else
   echo "Copy a zero grid in for rsaeap4 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./rsaeap4.out
fi

if [ -s ./rsapp1.out ]; then
   echo "The rsapp1.out grid is available"
else
   echo "Copy a zero grid in for rsapp1 now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./rsapp1.out
fi

if [ -s ./p03mx.out ]; then
   echo "The p03mx.out grid is available"
else
   echo "Copy a zero grid in for p03mx now"
   cp ${FIXspc}/spc_zerogrid_flag_212 ./p03mx.out
fi

#
# Multiple the Czys precip type by the prob of precip to get a true frozen probability...
rm -f ./file3.out > /dev/null 2>&1
rm -f ./ptypecfzorig.out > /dev/null 2>&1
cp ./ptypecfz.out ptypecfzorig.out

export pgm=spc_multiple_czys_p03m
. prep_step

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

${EXECspc}/spc_multiple_czys_p03m ./ptypecfz.out ./p03mp01.out ${date} ${the_run} f0${thisrun} CZYS mul >>$pgmout 2>errfile
export err=$?;err_chk

rm -f ./ptypecfz.out > /dev/null 2>&1
mv ./file3.out ./ptypecfz.out

# UNCONDITIONAL PROBABILITIES...
cp ./combine_probs_roadsnow.out.layer01_group${grouptime} ./combine_probs_snow.out

export pgm=spc_calibrate_snow 
. prep_step

msg="$job: spc_calibrate_snow - layer01 at $the_run f$chour started"
postmsg "$jlogfile" "$msg"
${EXECspc}/spc_calibrate_snow rsaeap1.out ptypefz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk

mv ./file3.out ./layer01.pred

cp ./combine_probs_roadsnow.out.layer02_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsaeap2.out ptypefz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer02.pred

cp ./combine_probs_roadsnow.out.layer03_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsaeap4.out ptypefz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer03.pred

cp ./combine_probs_roadsnow.out.layer04_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsaeap1.out ptypecfz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer04.pred

cp ./combine_probs_roadsnow.out.layer05_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsaeap2.out ptypecfz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer05.pred

cp ./combine_probs_roadsnow.out.layer06_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsaeap4.out ptypecfz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer06.pred

cp ./combine_probs_roadsnow.out.layer07_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsapp1.out ptypefz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer07.pred

cp ./combine_probs_roadsnow.out.layer08_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow rsapp1.out ptypecfz.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer08.pred


# CONDITIONAL PROBABILITIES...ASSUME PROB FROZEN 100% SO RSAE AND RSAP CONTROLLING FACTORS WHERE P03MX > 0.
cp ./combine_probs_roadsnow.out.layer01_group${grouptime} ./combine_probs_snow.out

export pgm=spc_calibrate_snow_cond
. prep_step

msg="$job: spc_calibrate_snow_cond - layer01con $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_calibrate_snow_cond rsaeap1.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer01con.pred

cp ./combine_probs_roadsnow.out.layer02_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsaeap2.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer02con.pred

cp ./combine_probs_roadsnow.out.layer03_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsaeap4.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer03con.pred

cp ./combine_probs_roadsnow.out.layer04_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsaeap1.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer04con.pred

cp ./combine_probs_roadsnow.out.layer05_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsaeap2.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer05con.pred

cp ./combine_probs_roadsnow.out.layer06_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsaeap4.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer06con.pred

cp ./combine_probs_roadsnow.out.layer07_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsapp1.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer07con.pred

cp ./combine_probs_roadsnow.out.layer08_group${grouptime} ./combine_probs_snow.out
. prep_step
${EXECspc}/spc_calibrate_snow_cond rsapp1.out ptypecfzorig.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer08con.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 $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_add_sub_maxmin ./layer01.pred ./layer02.pred ${date} ${the_run} f${chour} calrds max
export err=$?;err_chk

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

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

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

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

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

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

# Get the power mean...go ahead and put the max prob contributor into the file anyway...
rm -f ./power_mean.out > /dev/null 2>&1

export pgm=spc_power_mean
. prep_step

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

${EXECspc}/spc_power_mean ./layer01.pred ./layer02.pred ./layer03.pred ./layer04.pred ./layer05.pred ./layer06.pred ./layer07.pred ./layer08.pred ${date} ${the_run} f${chour} calrds
export err=$?;err_chk

# apply linear regression adjustment to the forecast now...
export pgm=spc_adjust_slope_inter
. prep_step

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

${EXECspc}/spc_adjust_slope_inter ./file3.out ${date} ${the_run} f${chour} calrds ./slope_inter.info.snow
export err=$?;err_chk

# Now use gdedit to put the file into the sref file...
$GEMEXE/gdedit << EOF
! GDEFIL   = ./file3.out
 GDEFIL   = ./power_mean.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 GDEFIL   = ./layermax.out
 
 r
 
 ex
 
EOF

cp ./power_mean.out ./file_snow_${chour}.out
cp ./power_mean.out $COMOUT/file_snow_${chour}.out

# Now...make the normalized calculation of calrds for Pete and winter wx experiment...10/19/2005...
monthnor=`date +%m`
if [ "$monthnor" = '10' ]; then
    norprob=15
elif [ "$monthnor" = '11' ]; then
    norprob=19
elif [ "$monthnor" = '12' ]; then
    norprob=23
elif [ "$monthnor" = '01' ]; then
    norprob=28
elif [ "$monthnor" = '02' ]; then
    norprob=23
elif [ "$monthnor" = '03' ]; then
    norprob=20
else
    norprob=6
fi

echo " spc_cal_roadsnow.sh : Using a normalizing probability of $norprob"

$GEMEXE/gddiag << EOF

 GDFILE   = $workdir/$model
 GDOUTF   = $workdir/$model
 GFUNC    = quo(calrds,${norprob})
 GDATTIM  = LAST
 GLEVEL   = 0
 GVCORD   = n
 GRDNAM   = calrdsnor
 GRDTYP   = S
 GPACK    =  
 GRDHDR   =  
 PROJ     =  
 GRDAREA  =  
 KXKY     =  
 MAXGRD   =  
 CPYFIL   =  
 ANLYSS   = 

r

 GFUNC    = quo(mul(100,quo(calrds,ptypefz)),${norprob})
 GRDNAM   = calrdsnor2

r

ex

EOF
$GEMEXE/gpend

# Repeat for CONDITIONAL probabilities...
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 - the layer01 - 08  at $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_add_sub_maxmin ./layer01con.pred ./layer02con.pred ${date} ${the_run} f${chour} calrdc max
export err=$?;err_chk

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

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

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

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

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

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

# Get the power mean...go ahead and put the max prob contributor into the file anyway...
rm -f ./power_mean.out > /dev/null 2>&1

export pgm=spc_power_mean
. prep_step

msg="$job: spc_power_mean - at $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_power_mean ./layer01con.pred ./layer02con.pred ./layer03con.pred ./layer04con.pred ./layer05con.pred ./layer06con.pred ./layer07con.pred ./layer08con.pred ${date} ${the_run} f${chour} calrdc
export err=$?;err_chk

# apply linear regression adjustment to the forecast now...
export pgm=spc_adjust_slope_inter
. prep_step

msg="$job: spc_adjust_slope_inter - at $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_adjust_slope_inter ./file3.out ${date} ${the_run} f${chour} calrdc ./slope_inter.info.snow
export err=$?;err_chk

# Now use gdedit to put the file into the sref file...
$GEMEXE/gdedit << EOF
! GDEFIL   = ./file3.out
 GDEFIL   = ./power_mean.out
 GDFILE   = $workdir/$model
 GPACK    =  
 
 r
 
 GDEFIL   = ./layermax.out
 
 r
 
 ex
 
EOF


cp ./power_mean.out ./file_consnow_${chour}.out
cp ./power_mean.out ${COMOUT}/file_consnow_${chour}.out

echo "Using a normalizing probability of $norprob on conditional value"

$GEMEXE/gddiag << EOF

 GDFILE   = $workdir/$model
 GDOUTF   = $workdir/$model
 GFUNC    = quo(calrdc,${norprob})
 GDATTIM  = LAST
 GLEVEL   = 0
 GVCORD   = n
 GRDNAM   = calrdcnor
 GRDTYP   = S
 GPACK    = 
 GRDHDR   =
 PROJ     = 
 GRDAREA  = 
 KXKY     =
 MAXGRD   =
 CPYFIL   = 
 ANLYSS   =

r

ex

EOF

$GEMEXE/gpend

# now...combine grids assuming independence to build 6 and 12 hour probabilities...
f1=999
if [ "$chour" -eq "006" ]; then
    f1=00
    f2=00
    f3=03
    f4=06
fi

if [ "$chour" -eq "009" ]; then
    f1=00
    f2=03
    f3=06
    f4=09
fi

if [ "$chour" -eq "012" ]; then
    f1=03
    f2=06
    f3=09
    f4=12
fi

if [ "$chour" -eq "015" ]; then
    f1=06
    f2=09
    f3=12
    f4=15
fi

if [ "$chour" -eq "018" ]; then
    f1=09
    f2=12
    f3=15
    f4=18
fi

if [ "$chour" -eq "021" ]; then
    f1=12
    f2=15
    f3=18
    f4=21
fi

if [ "$chour" -eq "024" ]; then
    f1=15
    f2=18
    f3=21
    f4=24
fi

if [ "$chour" -eq "027" ]; then
    f1=18
    f2=21
    f3=24
    f4=27
fi

if [ "$chour" -eq "030" ]; then
    f1=21
    f2=24
    f3=27
    f4=30
fi

if [ "$chour" -eq "033" ]; then
    f1=24
    f2=27
    f3=30
    f4=33
fi

if [ "$chour" -eq "036" ]; then
    f1=27
    f2=30
    f3=33
    f4=36
fi

if [ "$chour" -eq "039" ]; then
    f1=30
    f2=33
    f3=36
    f4=39
fi

if [ "$chour" -eq "042" ]; then
    f1=33
    f2=36
    f3=39
    f4=42
fi

if [ "$chour" -eq "045" ]; then
    f1=36
    f2=39
    f3=42
    f4=45
fi

if [ "$chour" -eq "048" ]; then
    f1=39
    f2=42
    f3=45
    f4=48
fi

if [ "$chour" -eq "051" ]; then
    f1=42
    f2=45
    f3=48
    f4=51
fi

if [ "$chour" -eq "054" ]; then
    f1=45
    f2=48
    f3=51
    f4=54
fi

if [ "$chour" -eq "057" ]; then
    f1=48
    f2=51
    f3=54
    f4=57
fi

if [ "$chour" -eq "060" ]; then
    f1=51
    f2=54
    f3=57
    f4=60
fi

if [ "$chour" -eq "063" ]; then
    f1=54
    f2=57
    f3=60
    f4=63
fi

if [ "$chour" -eq "066" ]; then
    f1=57
    f2=60
    f3=63
    f4=66
fi

if [ "$chour" -eq "069" ]; then
    f1=60
    f2=63
    f3=66
    f4=69
fi

if [ "$chour" -eq "072" ]; then
    f1=63
    f2=66
    f3=69
    f4=72
fi

if [ "$chour" -eq "075" ]; then
    f1=66
    f2=69
    f3=72
    f4=75
fi

if [ "$chour" -eq "078" ]; then
    f1=69
    f2=72
    f3=75
    f4=78
fi

if [ "$chour" -eq "081" ]; then
    f1=72
    f2=75
    f3=78
    f4=81
fi

if [ "$chour" -eq "084" ]; then
    f1=75
    f2=78
    f3=81
    f4=84
fi

if [ "$chour" -eq "087" ]; then
    f1=78
    f2=81
    f3=84
    f4=87
fi


if [ ${f1} -eq 999 ]; then
   echo "This grid is before hour 6 so finish and exit now."
   $GEMEXE/gpend
   exit 0
fi

snocnt=0
# run the software to combine the 6-hr grids...

while [ $snocnt -lt 60 ]
do

   if [ -s ${COMOUT}/file_consnow_0${f3}.out -a -s ${COMOUT}/file_consnow_0${f4}.out ]; then
       rm -f ./file_snow_06h.out > /dev/null 2>&1

        export pgm=spc_combine_snow_6h_independent
        . prep_step

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

        ${EXECspc}/spc_combine_snow_6h_independent ${COMOUT}/file_snow_0${f3}.out \
                     ${COMOUT}/file_snow_0${f4}.out $date $the_run f$chour >>$pgmout 2>errfile
        export err=$?;err_chk

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

 ex
 
EOF

        # CONDITIONAL...
        rm -f ./file_snow_06h.out > /dev/null 2>&1
        . prep_step
        ${EXECspc}/spc_combine_snow_6h_independent ${COMOUT}/file_consnow_0${f3}.out \
                    ${COMOUT}/file_consnow_0${f4}.out $date $the_run f$chour >>$pgmout 2>errfile
        export err=$?;err_chk

        # change the gempak name...
        rm -f ./file_consnow_06hour.out > /dev/null 2>&1
        sed -e 's/CALRDS06/CALRDSCON06/g' ./file_snow_06h.out > ./file_consnow_06hour.out
        mv ./file_consnow_06hour.out ./file_snow_06h.out 

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

 ex
 
EOF

        break

   else

      let "snocnt=snocnt+1"
      # wait up to 10 minutes for previous grids to be produced...
      if [ $snocnt -lt 58 ];  then
         echo " 6hr snow - Waiting for a previous grid to finish...count is $snocnt of 60"
         sleep 10
      fi
   fi

done

if [ ${f4} -eq 06 -o ${f4} -eq 09 ]; then
    echo "This grid is before hour 12 so finish and exit now."
    $GEMEXE/gpend
    exit 0
fi

snocnt=0
# run the software to combine the 6-hr grids...

while [ $snocnt -lt 60 ]
do

   if [ -s ${COMOUT}/file_snow_0${f1}.out -a -s ${COMOUT}/file_snow_0${f2}.out -a -s ${COMOUT}/file_snow_0${f3}.out -a -s ${COMOUT}/file_snow_0${f4}.out ]; then
       rm -f ./file_snow_12h.out > /dev/null 2>&1

        export pgm=spc_combine_snow_12h_independent
        . prep_step

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

        ${EXECspc}/spc_combine_snow_12h_independent ${COMOUT}/file_snow_0${f1}.out \
                    ${COMOUT}/file_snow_0${f2}.out ${COMOUT}/file_snow_0${f3}.out \
                    ${COMOUT}/file_snow_0${f4}.out $date $the_run f$chour >>$pgmout 2>errfile
        export err=$?;err_chk

        # CONDITIONAL...
        rm -f ./file_snow_12h.out > /dev/null 2>&1
        . prep_step
        ${EXECspc}/spc_combine_snow_12h_independent ${COMOUT}/file_consnow_0${f1}.out \
                    ${COMOUT}/file_consnow_0${f2}.out ${COMOUT}/file_consnow_0${f3}.out \
                    ${COMOUT}/file_consnow_0${f4}.out $date $the_run f$chour >>$pgmout 2>errfile
        export err=$?;err_chk

        # change the gempak name...
        rm -f ./file_consnow_12hour.out > /dev/null 2>&1
        sed -e 's/CALRDS12/CALRDSCON12/g' ./file_snow_12h.out > ./file_consnow_12hour.out
        mv ./file_consnow_12hour.out ./file_snow_12h.out 
  
        break

    else

      let "snocnt=snocnt+1"
      # wait up to 10 minutes for previous grids to be produced...
      if [ $snocnt -lt 58 ];  then
         echo " 12hr snow - Waiting for a previous grid to finish...count is $snocnt of 60"
         sleep 10
      fi
    fi

done

$GEMEXE/gpend
