#################################################################
# Script:   dvrtma_debias_alaska.sh
# Abstract: Product downscale ensemble products for Alaska region
# Author:   Bo Cui
# History:  January 2010 - First implementation of this new script
##################################################################

set -x

echo "----------------------------------------"
echo "Enter sub script dvrtma_debias_alaska.sh"
echo "----------------------------------------"

##############################################
# define exec variable, and entry grib utility 
##############################################

export NDATE=$EXECUTIL/ndate
export COPYGB=$EXECUTIL/copygb
export DVWIND=$USHrtma/dvrtma_debias_alaska_wind10m.sh

export pgm1=dvrtma_debias_alaska

pgmout=output_ds

. prep_step

###
#  step 1: downscale ensemble forecast: tmax and tmin                       
###

mkdir -p $DATA/tmpdir_01
cd $DATA/tmpdir_01           

/nwprod/util/ush/setup.sh

( $USHrtma/dvrtma_debias_alaska_tmaxmin.sh ) &

###
# step 2: downscale ensemble forecast: T2m, 10m U & V, surface pressure
###

mkdir -p $DATA/tmpdir_02
cd $DATA/tmpdir_02           

/nwprod/util/ush/setup.sh

( for nens in $memberlist; do

  mkdir -p $DATA/tmpdir_02/tmpdir_02_$nens
  cd $DATA/tmpdir_02/tmpdir_02_$nens

  /nwprod/util/ush/setup.sh

  ( for nfhrs in $hourlist; do

###
#  set the index ( exist of downscaling vector )  as default, 0
###

    cstart=0

###
#  downscaling vector entry
###

    cyc_verf=`$NDATE +$nfhrs $PDY$cyc | cut -c9-10`
    echo $nfhrs $cyc_verf

    ifile=dvrtma.t${cyc_verf}z.ndgd_alaska 

    if [ -s $COM/naefs.$PDY/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDY/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm1/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm1/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm2/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm2/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm3/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm3/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm4/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm4/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm5/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm5/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm6/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm6/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm7/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm7/${cyc_verf}/ndgd/$ifile $ifile
    else
      echo " There is no Bias Estimation at " ${cyc_verf}Z 
      cstart=1
    fi

    echo "&message"  >input.$nfhrs.$nens
    echo " icstart=${cstart}," >> input.$nfhrs.$nens
    echo " prodcenter='${nens}'," >> input.$nfhrs.$nens
    echo "/" >>input.$nfhrs.$nens

###
#  downscale ensemble forecasting output
###

    ofile1=${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}_part1

###
#  check pgrba forecast file, interpolated on grids of 5.953km for Alaska region
###
   
    grid='255 5 825 553 40530 181429 8 210000 5953 5953 0 64'
    infile=$COMIN/${nens}.t${cyc}z.pgrba_bcf${nfhrs} 
    #if [ $cstart -eq 1 ]; then
    #  infile=$COMINNCEP/${nens}.t${cyc}z.pgrba_bcf${nfhrs} 
    #else 
    #  infile=$COMIN/${nens}.t${cyc}z.pgrba_bcf${nfhrs}
    #fi
    outfile=${nens}.t${cyc}z.pgrba_bcf${nfhrs}_temp
    cfile=${nens}.t${cyc}z.pgrba_bcf${nfhrs}_5km 

    if [[ ! -s $infile ]]
    then
       echo "Input pgrba_bc files not available"
       export err=1; $DATA/err_chk
    fi

    rm fort.*

    >$outfile
    $EXECUTIL/wgrib $infile | grep ":PRES"                | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile
    $EXECUTIL/wgrib $infile | grep ":TMP:"  | grep "2 m"  | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile
#   $EXECUTIL/wgrib $infile | grep ":UGRD:" | grep "10 m" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile
#   $EXECUTIL/wgrib $infile | grep ":VGRD:" | grep "10 m" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile

    $COPYGB -g"$grid" -v "33*2" -i1,1 -x $outfile $cfile

###
# downscale ensemble forecast: T2m, 10m U & V, surface pressure
###

    if [ "$nens" = "cmc_gespr" -o "$nens" = "gespr" -o "$nens" = "naefs_gespr" ]; then
      cp $cfile $ofile1
    else
      ln -sf $ifile fort.11
      ln -sf $cfile fort.12
      ln -sf $ofile1 fort.51
      startmsg
      $EXECrtma/$pgm1   <input.$nfhrs.$nens  > $pgmout.$nfhrs.$nens.part1  2> errfile
      export err=$?;$DATA/err_chk
    fi 

  done ) &

done  ) &

###
#  step 3: downscale ensemble forecast: wind speed and direction            
###

mkdir -p $DATA/tmpdir_03
cd $DATA/tmpdir_03           
/nwprod/util/ush/setup.sh

( $USHrtma/dvrtma_debias_alaska_wind10m.sh ) &

wait
cd $DATA

##############################################
# combine 3 downscaled forecast files together
##############################################

for nfhrs in $hourlist; do
  for nens in $memberlist; do
    ofile1=tmpdir_02/tmpdir_02_$nens/${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}_part1
    ofile2=tmpdir_03/${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}_part2
    ofile3=tmpdir_01/${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}_tmax 
    ofile4=tmpdir_01/${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}_tmin 
    ofile=${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}

    rm $ofile
    cat $ofile1 $ofile2 >$ofile

    if [ -s $ofile3 ]; then
      cat $ofile3 >>$ofile
    fi
    if [ -s $ofile4 ]; then
      cat $ofile4 >>$ofile
    fi
  done
done

set +x
echo " "
echo "Leaving sub script dvrtma_debias_alaska.sh"
echo " "
set -x
