#!/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_probs_int_layer??.out .
cp ${FIXspc}/spc_combine_probs_int_layer01.out combine_probs_int_layer01.out
cp ${FIXspc}/spc_combine_probs_int_layer02.out combine_probs_int_layer02.out
cp ${FIXspc}/spc_combine_probs_int_layer03.out combine_probs_int_layer03.out
cp ${FIXspc}/spc_combine_probs_int_layer04.out combine_probs_int_layer04.out
cp ${FIXspc}/spc_combine_probs_int_layer05.out combine_probs_int_layer05.out
cp ${FIXspc}/spc_combine_probs_int_layer06.out combine_probs_int_layer06.out
cp ${FIXspc}/spc_combine_probs_int_layer07.out combine_probs_int_layer07.out
cp ${FIXspc}/spc_combine_probs_int_layer08.out combine_probs_int_layer08.out
cp ${FIXspc}/spc_combine_probs_int_layer09.out combine_probs_int_layer09.out
cp ${FIXspc}/spc_combine_probs_int_layer10.out combine_probs_int_layer10.out
cp ${FIXspc}/spc_combine_probs_int_layer11.out combine_probs_int_layer11.out
cp ${FIXspc}/spc_combine_probs_int_layer12.out combine_probs_int_layer12.out
cp ${FIXspc}/spc_combine_probs_int_layer13.out combine_probs_int_layer13.out
cp ${FIXspc}/spc_combine_probs_int_layer14.out combine_probs_int_layer14.out
cp ${FIXspc}/spc_combine_probs_int_layer15.out combine_probs_int_layer15.out
cp ${FIXspc}/spc_combine_probs_int_layer16.out combine_probs_int_layer16.out
cp ${FIXspc}/spc_combine_probs_int_layer17.out combine_probs_int_layer17.out
cp ${FIXspc}/spc_combine_probs_int_layer18.out combine_probs_int_layer18.out
cp ${FIXspc}/spc_combine_probs_int_layer19.out combine_probs_int_layer19.out
cp ${FIXspc}/spc_combine_probs_int_layer20.out combine_probs_int_layer20.out
cp ${FIXspc}/spc_combine_probs_int_layer21.out combine_probs_int_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 ./cptpp*.out > /dev/null 2>&1
rm -f ./p03mp*.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}/cptpp1.out
 
 r
 
 GFUNC    = cptpp50
 OUTPUT   = f/${workdir}/cptpp50.out
 
 r
 
 GFUNC    = cptpp100
 OUTPUT   = f/${workdir}/cptpp100.out
 
 r
 
 GFUNC    = cptpp200
 OUTPUT   = f/${workdir}/cptpp200.out
 
 r

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

 r
 
 GFUNC    = cptpp400
 OUTPUT   = f/${workdir}/cptpp400.out
 
 r
 
 GFUNC    = cptpp500
 OUTPUT   = f/${workdir}/cptpp500.out
 
 r
 
 GFUNC    = p03mp01
 OUTPUT   = f/${workdir}/p03mp01.out
 
 r

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

 r

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

 r

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

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

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

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

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

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

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

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

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

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

#
rm -f ./layer??.pred > /dev/null 2>&1

cp ./combine_probs_int_layer01.out ./combine_probs_svr1.out

export pgm=spc_calibrate2_svr
. prep_step

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

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

mv ./file3.out ./layer01.pred

cp ./combine_probs_int_layer02.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp50.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer02.pred

cp ./combine_probs_int_layer03.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp100.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer03.pred

cp ./combine_probs_int_layer04.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp200.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer04.pred

cp ./combine_probs_int_layer05.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp300.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer05.pred

cp ./combine_probs_int_layer06.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp400.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer06.pred

cp ./combine_probs_int_layer07.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp500.out p03mp01.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer07.pred

cp ./combine_probs_int_layer08.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp1.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer08.pred

cp ./combine_probs_int_layer09.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp50.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer09.pred

cp ./combine_probs_int_layer10.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp100.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer10.pred

cp ./combine_probs_int_layer11.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp200.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer11.pred

cp ./combine_probs_int_layer12.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp300.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer12.pred

cp ./combine_probs_int_layer13.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp400.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer13.pred

cp ./combine_probs_int_layer14.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp500.out p03mp10.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer14.pred

cp ./combine_probs_int_layer15.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp1.out p03mp25.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer15.pred

cp ./combine_probs_int_layer16.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp50.out p03mp25.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer16.pred

cp ./combine_probs_int_layer17.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp100.out p03mp25.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer17.pred

cp ./combine_probs_int_layer18.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp200.out p03mp25.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer18.pred

cp ./combine_probs_int_layer19.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp300.out p03mp25.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer19.pred

cp ./combine_probs_int_layer20.out ./combine_probs_svr1.out
. prep_step
${EXECspc}/spc_calibrate2_svr cptpp400.out p03mp25.out $date $the_run f$chour
export err=$?;err_chk
mv ./file3.out ./layer20.pred

cp ./combine_probs_int_layer21.out ./combine_probs_svr1.out
msg="$job: spc_calibrate2_svr - the layer21 at $the_run f$chour started"
postmsg "$jlogfile" "$msg"

. prep_step
${EXECspc}/spc_calibrate2_svr cptpp500.out p03mp25.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
rm -f ./power_mean_den.out > /dev/null 2>&1

export pgm=spc_power_mean_den
. prep_step

msg="$job: spc_power_mean_den - the layer21 at $the_run f$chour started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_power_mean_den ./layer01.pred ./layer02.pred ./layer03.pred \
           ./layer04.pred ./layer05.pred ./layer06.pred ./layer07.pred \
           ./layer08.pred ./layer09.pred ./layer10.pred ./layer11.pred \
           ./layer12.pred ./layer13.pred ./layer14.pred ./layer15.pred \
           ./layer16.pred ./layer17.pred ./layer18.pred ./layer19.pred \
           ./layer20.pred ./layer21.pred ${date} ${the_run} \
           f${chour} calden >>$pgmout 2>errfile

export err=$?;err_chk

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

