#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         gfs_smartinitguam_on.sh
# Script description:  runs smartinit code over NDFD Hawaiian domain 
#
# Author:        Hui-Ya Chuang       Org: NP22         Date: 2009-06-25
#
# Script history log:
# 2009-06-25  Hui-Ya Chuang     
# 2012-01-18  DaNa Carlis
################################################################################

set -x

cd $DATA

let ffhr1=ffhr-1
let ffhr2=ffhr-2

if [ $ffhr -gt 0 ]; then
  # GFS only outputs 3 hourly
  hours="${ffhr}"
else
  hours="00"
fi

for fhr in $hours; do

  let check=fhr%3
  let check2=fhr%6
  let check3=fhr%12
  let check4=fhr%9
  echo "check check2 check3 check4 = " $check $check2 $check3 $check4 
  let fhr1=fhr-1
  let fhr2=fhr-2
  let fhr3=fhr-3
  let fhr6=fhr-6
  let fhr9=fhr-9
  let check5=fhr6%12
  echo "check5 " $check5

  if [ ${fhr3} -lt 10 ] ; then
   typeset -Z2 fhr3
  fi
  if [ ${fhr1} -lt 10 ] ; then
   typeset -Z2 fhr1
  fi
  if [ ${fhr2} -lt 10 ] ; then
   typeset -Z2 fhr2
  fi
  if [ ${fhr6} -lt 10 ] ; then
   typeset -Z2 fhr6
  fi
  if [ ${fhr9} -lt 10 ] ; then
   typeset -Z2 fhr9
  fi
  if [ ${fhr} -lt 10 ] ; then
   typeset -Z2 fhr
  fi

  # run post to generate height, cloud fraction, visibility, etc.
  export OUTTYP=3
  export SIGINP=$COMGFS/gfs.t${cyc}z.sf${fhr}
  export CTLFILE=$PARMgfs/gfs_downscale_cntrl.parm
  export FLXINP=$COMGFS/gfs.t${cyc}z.sfluxgrbf$fhr
  export PGBOUT=tmpfile4
  export ANOMCATSH=global_anomcat.sh # no need to run ANIMCATSH now
  export FILTER=0
  export VDATE=`${utilexec}/ndate +${fhr} ${PDY}${cyc}`
  export SFCINP=$COMGFS/gfs.t${cyc}z.bf$fhr
  export IGEN=89
  ${USHGLOBAL}/global_nceppost.sh > post${fhr}.out 
 
  grid="255 1 193 193 12350 143687 128 16794 148280 20000 0 64 2500 2500"
  $utilexec/copygb -xg "$grid" tmpfile4 gfs.t${cyc}z.smartinitin.guam.${fhr}

  rm -f tmpfile4
  

  $utilexec/grbindex gfs.t${cyc}z.smartinitin.guam.${fhr} gfs.t${cyc}z.smartinitin.guam.${fhr}.idx


  # if the forecast hour is a 3-hr time (3,15,27,39....) it already
  #   contains 3-hr accumulations;  otherwise, we need to make 3-hr
  #   accumulations.   We don't need them for in-between hours (22,23,25...),
  #   since we only gather max/min data at those hours

  if [ $fhr -ne 00 ] ; then
    if [ ${check2} -eq 0 ]; then
     grid="255 1 193 193 12350 143687 128 16794 148280 20000 0 64 2500 2500"
     $utilexec/copygb -xg "$grid" -i1,1 -k'4*-1 61' $COMGFS/gfs.t${cyc}z.master.grbf${fhr3} tmpfile4
     $utilexec/copygb -xg "$grid" -i1,1 -k'4*-1 65' $COMGFS/gfs.t${cyc}z.master.grbf${fhr3} tmpfile5
     $utilexec/copygb -xg "$grid" -i1,1 -k'4*-1 63' $COMGFS/gfs.t${cyc}z.master.grbf${fhr3} tmpfile6
     cat tmpfile4 tmpfile5 tmpfile6 > WRFPRS${fhr3}.tm00
     $utilexec/grbindex WRFPRS${fhr3}.tm00 WRFPRS${fhr3}i.tm00

     export pgm=gfs_smartmakeprecip;. prep_step
     export XLFUNIT_13="WRFPRS${fhr3}.tm00"
     export XLFUNIT_14="WRFPRS${fhr3}i.tm00"
     export XLFUNIT_15="gfs.t${cyc}z.smartinitin.guam.${fhr}"
     export XLFUNIT_16="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
     export XLFUNIT_50="3precip"
     export XLFUNIT_51="3cprecip.${fhr}"
     export XLFUNIT_52="3snow"
$EXECgfs/gfs_smartmakeprecip <<EOF > makeprecip3.out
$fhr -$fhr3
EOF
export err=$?;err_chk

     cat 3precip 3cprecip.${fhr} 3snow  >> gfs.t${cyc}z.smartinitin.guam.${fhr}
     $utilexec/grbindex gfs.t${cyc}z.smartinitin.guam.${fhr} gfs.t${cyc}z.smartinitin.guam.${fhr}.idx
    fi
  fi

  # at 12-hr "on" times, we need 6 hour buckets as well
  # GFS does 6 hour bucket already
  #  we don't need them at 6-hr times (18,30,42...) because
  #  those files already have 6-hr buckets

  if [ $fhr -ne 00 -a ${check2} -eq 0 ]; then
    #GFS already has 6 hour bucket at these forecasts so just extract precip and snow   
    $utilexec/wgrib -s gfs.t${cyc}z.smartinitin.guam.${fhr} | egrep "(:APCP:)" \
    | $utilexec/wgrib -i -grib -o 6precip gfs.t${cyc}z.smartinitin.guam.${fhr} 
    $utilexec/grbindex 6precip 6precipi
    $utilexec/wgrib -s gfs.t${cyc}z.smartinitin.guam.${fhr} | egrep "(:WEASD:)" \
    | $utilexec/wgrib -i -grib -o 6snow gfs.t${cyc}z.smartinitin.guam.${fhr}
    $utilexec/grbindex 6snow 6snowi
      
 fi
 
  if [ $fhr -ne 00 ] ; then
    if [ ${fhr} -gt 17 -a ${check5} -eq 0 ]; then
    echo fhr= $fhr
# make 12 hr bucket 
     grid="255 1 193 193 12350 143687 128 16794 148280 20000 0 64 2500 2500"
     $utilexec/copygb -xg "$grid" -i1,1 -k'4*-1 61' $COMGFS/gfs.t${cyc}z.master.grbf${fhr6} tmpfile4
     $utilexec/copygb -xg "$grid" -i1,1 -k'4*-1 65' $COMGFS/gfs.t${cyc}z.master.grbf${fhr6} tmpfile5
     $utilexec/copygb -xg "$grid" -i1,1 -k'4*-1 63' $COMGFS/gfs.t${cyc}z.master.grbf${fhr6} tmpfile6
     cat tmpfile4 tmpfile5 tmpfile6 > WRFPRS${fhr6}.tm00
     $utilexec/grbindex WRFPRS${fhr6}.tm00 WRFPRS${fhr6}i.tm00

     export pgm=gfs_smartmakeprecip;. prep_step
     export XLFUNIT_13="WRFPRS${fhr6}.tm00"
     export XLFUNIT_14="WRFPRS${fhr6}i.tm00"
     export XLFUNIT_15="gfs.t${cyc}z.smartinitin.guam.${fhr}"
     export XLFUNIT_16="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
     export XLFUNIT_50="12precip"
     export XLFUNIT_51="12cprecip.${fhr}"
     export XLFUNIT_52="12snow"
$EXECgfs/gfs_smartmakeprecip <<EOF > makeprecip12.out
$fhr $fhr6
EOF
export err=$?;err_chk

     cat 12precip 12cprecip.${fhr} 12snow >> gfs.t${cyc}z.smartinitin.guam.${fhr}
     $utilexec/grbindex gfs.t${cyc}z.smartinitin.guam.${fhr} gfs.t${cyc}z.smartinitin.guam.${fhr}.idx

    fi
  fi 

  cp /com/date/t${cyc}z DATE

  # at 12-hr times, we need to make 12-hr max/min temps
  #   we also need 3 and 6-hr buckets

  if [ $fhr -ne 00 ]; then 
   if [ ${fhr} -gt 17 -a ${check5} -eq 0 ]; then    
    echo fhr= $fhr
    echo " downscaling for 18 30 42 54..."  

    rm -f MAXMIN1 MAXMIN2 MAXMIN3 MAXMIN4 MAXMIN5
    rm -f MAXMIN1i MAXMIN2i MAXMIN3i MAXMIN4i MAXMIN5i
    #if [ $fhr -gt 17 ]; then
    #  cp MAXMIN${fhr2}.tm00 MAXMIN2
    #  cp MAXMIN${fhr1}.tm00 MAXMIN1
    #  $utilexec/grbindex MAXMIN1 MAXMIN1i
    #  $utilexec/grbindex MAXMIN2 MAXMIN2i
    #fi
    cp $COMOUT/gfs.t${cyc}z.smartguam${fhr3}.tm00 MAXMIN3
    cp $COMOUT/gfs.t${cyc}z.smartguam${fhr6}.tm00 MAXMIN4
    cp $COMOUT/gfs.t${cyc}z.smartguam${fhr9}.tm00 MAXMIN5

    $utilexec/grbindex MAXMIN3 MAXMIN3i
    $utilexec/grbindex MAXMIN4 MAXMIN4i
    $utilexec/grbindex MAXMIN5 MAXMIN5i

    export pgm=gfs_smartinitguam;. prep_step
    export XLFUNIT_11="gfs.t${cyc}z.smartinitin.guam.${fhr}"
    export XLFUNIT_12="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
    export XLFUNIT_13="SREFPCP"
    export XLFUNIT_14="SREFPCPi"
    export XLFUNIT_15="6precip"
    export XLFUNIT_16="6precipi"
    export XLFUNIT_17="6snow"
    export XLFUNIT_18="6snowi" 
    export XLFUNIT_19="12precip"
    export XLFUNIT_20="12precipi"
    export XLFUNIT_21="MAXMIN1"
    export XLFUNIT_22="MAXMIN2"
    export XLFUNIT_23="MAXMIN3"
    export XLFUNIT_24="MAXMIN4"
    export XLFUNIT_25="MAXMIN5"
    export XLFUNIT_26="MAXMIN1i"
    export XLFUNIT_27="MAXMIN2i"
    export XLFUNIT_28="MAXMIN3i"
    export XLFUNIT_29="MAXMIN4i"
    export XLFUNIT_30="MAXMIN5i"
    export XLFUNIT_46="$FIXgfs/gfs_mask.guam.grb"
    export XLFUNIT_47="$FIXgfs/gfs_mask.guam.grbi"
    export XLFUNIT_48="$FIXgfs/gfs_orog.guam.grb"
    export XLFUNIT_49="$FIXgfs/gfs_orog.guam.grbi"
$EXECgfs/gfs_smartinitguam <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk
#  add processing for conversion to grib2
$utilexec/cnvgrib -g12 -p40 MESOGUAM${fhr}.tm00 gfs.t${cyc}z.smartguam${fhr}.tm00.grib2

if [ -s MESOGUAM${fhr}.tm00 ]
then
# Processing grids for AWIPS
pgm=tocgrib2
export pgm;. prep_step
startmsg

export XLFUNIT_11=gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
export XLFUNIT_31=" "
export XLFUNIT_51=grib2.t${cyc}z.smartguamf${fhr}

$utilexec/tocgrib2 <$PARMutil/grib2_gfs_smartguamofff${fhr}.199 >> $pgmout 2> errfile
echo " error from tocgrib=",$err

mv MESOGUAM${fhr}.tm00 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00
mv gfs.t${cyc}z.smartguam${fhr}.tm00.grib2 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
mv grib2.t${cyc}z.smartguamf${fhr} $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}

if [ $SENDDBN_NTC = YES ]
then
  $DBNROOT/bin/dbn_alert NTC_LOW ${DBN_ALERT_TYPE1} $job $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}
fi

if [ $SENDDBN_GB2 = YES ]
then
  $DBNROOT/bin/dbn_alert MODEL ${DBN_ALERT_TYPE2} $job $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
fi
fi
  # for all forecast hours divisible by 3 except for (3,15,27....), we need
  #    3-hr buckets and max/min temp data for the previous 2 hours

   elif [ ${check2} -eq 0 ] ; then
    echo fhr= $fhr
    echo " downscaling for 6 12 24 36... hr" 

    rm -f MAXMIN2 MAXMIN1 MAXMIN2i MAXMIN1i
    if [ $fhr -le 12 ]; then
      cp MAXMIN${fhr2}.tm00 MAXMIN2
      cp MAXMIN${fhr1}.tm00 MAXMIN1
      $utilexec/grbindex MAXMIN2 MAXMIN2i
      $utilexec/grbindex MAXMIN1 MAXMIN1i
    fi
    export pgm=gfs_smartinitguam;. prep_step
    export XLFUNIT_11="gfs.t${cyc}z.smartinitin.guam.${fhr}"
    export XLFUNIT_12="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
    export XLFUNIT_13="SREFPCP"
    export XLFUNIT_14="SREFPCPi"
    export XLFUNIT_15="6precip"
    export XLFUNIT_16="6precipi"
    export XLFUNIT_17="6snow"
    export XLFUNIT_18="6snowi"
    export XLFUNIT_19="MAXMIN2"
    export XLFUNIT_20="MAXMIN1"
    export XLFUNIT_21="MAXMIN2i"
    export XLFUNIT_22="MAXMIN1i"
    export XLFUNIT_46="$FIXgfs/gfs_mask.guam.grb"
    export XLFUNIT_47="$FIXgfs/gfs_mask.guam.grbi"
    export XLFUNIT_48="$FIXgfs/gfs_orog.guam.grb"
    export XLFUNIT_49="$FIXgfs/gfs_orog.guam.grbi"
$EXECgfs/gfs_smartinitguam <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk
# - add processing for conversion to grib2

 $utilexec/cnvgrib -g12 -p40 MESOGUAM${fhr}.tm00 gfs.t${cyc}z.smartguam${fhr}.tm00.grib2

if [ -s MESOGUAM${fhr}.tm00 ]
then
 # Processing grids for AWIPS
 pgm=tocgrib2
 export pgm;. prep_step
 startmsg

 export XLFUNIT_11=gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
 export XLFUNIT_31=" "
 export XLFUNIT_51=grib2.t${cyc}z.smartguamf${fhr}

 $utilexec/tocgrib2 <$PARMutil/grib2_gfs_smartguamofff${fhr}.199 >> $pgmout 2> errfile
 echo " error from tocgrib=",$err

 mv MESOGUAM${fhr}.tm00 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00
 mv gfs.t${cyc}z.smartguam${fhr}.tm00.grib2 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
 mv grib2.t${cyc}z.smartguamf${fhr} $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}

if [ $SENDDBN_NTC = YES ]
then
  $DBNROOT/bin/dbn_alert NTC_LOW ${DBN_ALERT_TYPE1} $job $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}
fi

if [ $SENDDBN_GB2 = YES ]
then
  $DBNROOT/bin/dbn_alert MODEL ${DBN_ALERT_TYPE2} $job $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
fi
fi
   #fi  # for fhr ne 0
  #  for forecast hours 3,15,27,39.... the files already have 3-hr buckets,
  #   but we need max/min temp data for the previous 2 hours

   elif [ ${check} -eq 0 ]; then
    echo fhr= $fhr
    echo " downscaling for 3 9 15 21 27 39.... hr" 
    rm -f MAXMIN2 MAXMIN1 MAXMIN2i MAXMIN1i
    if [ $fhr -lt 12 ]; then
      cp MAXMIN${fhr2}.tm00 MAXMIN2
      cp MAXMIN${fhr1}.tm00 MAXMIN1
      $utilexec/grbindex MAXMIN2 MAXMIN2i
      $utilexec/grbindex MAXMIN1 MAXMIN1i
    fi

    export pgm=gfs_smartinitguam;. prep_step 
    export XLFUNIT_11="gfs.t${cyc}z.smartinitin.guam.${fhr}"
    export XLFUNIT_12="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
    export XLFUNIT_13="SREFPCP"
    export XLFUNIT_14="SREFPCPi"
    export XLFUNIT_15="MAXMIN2"
    export XLFUNIT_16="MAXMIN1"
    export XLFUNIT_17="MAXMIN2i"
    export XLFUNIT_18="MAXMIN1i"
    export XLFUNIT_46="$FIXgfs/gfs_mask.guam.grb"
    export XLFUNIT_47="$FIXgfs/gfs_mask.guam.grbi"
    export XLFUNIT_48="$FIXgfs/gfs_orog.guam.grb"
    export XLFUNIT_49="$FIXgfs/gfs_orog.guam.grbi"
$EXECgfs/gfs_smartinitguam <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
  export err=$?;err_chk

  # - add processing for conversion to grib2

  $utilexec/cnvgrib -g12 -p40 MESOGUAM${fhr}.tm00 gfs.t${cyc}z.smartguam${fhr}.tm00.grib2

if [ -s MESOGUAM${fhr}.tm00 ]
then
  # Processing grids for AWIPS
  pgm=tocgrib2
  export pgm;. prep_step
  startmsg

  export XLFUNIT_11=gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
  export XLFUNIT_31=" "
  export XLFUNIT_51=grib2.t${cyc}z.smartguamf${fhr}

  $utilexec/tocgrib2 <$PARMutil/grib2_gfs_smartguamofff${fhr}.199 >> $pgmout 2> errfile
  echo " error from tocgrib=",$err

  mv MESOGUAM${fhr}.tm00 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00
  mv gfs.t${cyc}z.smartguam${fhr}.tm00.grib2 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
  mv grib2.t${cyc}z.smartguamf${fhr} $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}
  
  if [ $SENDDBN_NTC = YES ]
  then
    $DBNROOT/bin/dbn_alert NTC_LOW ${DBN_ALERT_TYPE1} $job $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}
  fi

  if [ $SENDDBN_GB2 = YES ]
  then
    $DBNROOT/bin/dbn_alert MODEL ${DBN_ALERT_TYPE2} $job $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
  fi
fi

  #  for all "in-between" forecast hours (13,14,16....), we don't need
  #    any special data
   else
    echo fhr= $fhr
    echo " downscaling for in between hours"  
    export pgm=gfs_smartinitguam;. prep_step
    export XLFUNIT_11="gfs.t${cyc}z.smartinitin.guam.${fhr}"
    export XLFUNIT_12="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
    export XLFUNIT_46="$FIXgfs/gfs_mask.guam.grb"
    export XLFUNIT_47="$FIXgfs/gfs_mask.guam.grbi"
    export XLFUNIT_48="$FIXgfs/gfs_orog.guam.grb"
    export XLFUNIT_49="$FIXgfs/gfs_orog.guam.grbi"
$EXECgfs/gfs_smartinitguam <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk
if [ $fhr -lt 12 ] ; then
# - add processing for conversion to grib2
 $utilexec/cnvgrib -g12 -p40 MESOGUAM${fhr}.tm00 gfs.t${cyc}z.smartguam${fhr}.tm00.grib2

 mv MESOGUAM${fhr}.tm00 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00
 mv gfs.t${cyc}z.smartguam${fhr}.tm00.grib2 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2

if [ $SENDDBN_GB2 = YES ]
then
  $DBNROOT/bin/dbn_alert MODEL ${DBN_ALERT_TYPE2} $job $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
fi

fi

   fi # end of different cases for non zero ffhr
  else # ffhr=0
    echo fhr= $fhr
    echo " downscaling for 0 hr"  
    export pgm=gfs_smartinitguam;. prep_step
    export XLFUNIT_11="gfs.t${cyc}z.smartinitin.guam.${fhr}"
    export XLFUNIT_12="gfs.t${cyc}z.smartinitin.guam.${fhr}.idx"
    export XLFUNIT_46="$FIXgfs/gfs_mask.guam.grb"
    export XLFUNIT_47="$FIXgfs/gfs_mask.guam.grbi"
    export XLFUNIT_48="$FIXgfs/gfs_orog.guam.grb"
    export XLFUNIT_49="$FIXgfs/gfs_orog.guam.grbi"
$EXECgfs/gfs_smartinitguam <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk
if [ $fhr -lt 12 ] ; then
 # - add processing for conversion to grib2
 $utilexec/cnvgrib -g12 -p40 MESOGUAM${fhr}.tm00 gfs.t${cyc}z.smartguam${fhr}.tm00.grib2

if [ -s MESOGUAM${fhr}.tm00 ]
  then
# Processing grids for AWIPS
 pgm=tocgrib2
 export pgm;. prep_step
 startmsg

 export XLFUNIT_11=gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
 export XLFUNIT_31=" "
 export XLFUNIT_51=grib2.t${cyc}z.smartguamf${fhr}

 $utilexec/tocgrib2 <$PARMutil/grib2_gfs_smartguamofff${fhr}.199 >> $pgmout 2> errfile
 echo " error from tocgrib=",$err

 mv MESOGUAM${fhr}.tm00 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00
 mv gfs.t${cyc}z.smartguam${fhr}.tm00.grib2 $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
 mv grib2.t${cyc}z.smartguamf${fhr} $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}

  if [ $SENDDBN_NTC = YES ]
  then
    $DBNROOT/bin/dbn_alert NTC_LOW ${DBN_ALERT_TYPE1} $job $pcom/grib2.awpgfssmart.guam${fhr}_awips_f${fhr}_${cyc}
  fi

  if [ $SENDDBN_GB2 = YES ]
  then
    $DBNROOT/bin/dbn_alert MODEL ${DBN_ALERT_TYPE2} $job $COMOUT/gfs.t${cyc}z.smartguam${fhr}.tm00.grib2
  fi

  fi # end check for MESOGUAM${fhr}.tm00

fi # end of check for fhr > 12

fi
done

exit
