###############################################################################################
# Script:   cmce_adjust_tmaxmin.sh 
# Abstract: Use accumulated NCEP and CMC analysis difference to adjust CMC T2m, Tmax and Tmin
#           for each member, which will be used for downscaled tmax & tmin
# Author:   Bo Cui 
# History:  Jan. 2010 - First implementation of this new script 
###############################################################################################

### need pass the values of PDY, cyc, DATA, COM, COMINCMC

COM=${COM:-/com/gens/${envir}}
COM_IN=${COM_IN:-/com/gens/prod}

########################################
#  define ensemble members and lead time
########################################

hourlist=" 00  06  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 \
          102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 \
          204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 \
          306 312 318 324 330 336 342 348 354 360 366 372 378 384"

memberlist_ncep="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"
memberlist_cmc="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"

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

pgm=cmce_adjust_tmaxmin
pgmout=output_adjustcmc

############################################################
# define if removing analyis difference between CMC and NCEP
# 0 = don't remove difference from CMC ensemble forecast
# 1 = remove difference from CMC ensemble forecast
############################################################

ifdebias=1

for nfhrs in $hourlist; do

  echo " &namens" >>namin.adjustcmc.$nfhrs

  ifile=0

##############################
#  input CMC Ensemble forecast
##############################

  for mem in ${memberlist_cmc}; do
    ifile_cmc=$COMINCMC/cmc_ge${mem}.t${cyc}z.pgrba_bcf${nfhrs}
    if [ $nfhrs -eq 00 ]; then
      ifile_cmc=$COM_IN/cmce.$PDY/${cyc}/pgrba/cmc_ge${mem}.t${cyc}z.pgrbaf${nfhrs}
    fi
    if [ -s $ifile_cmc ]; then
      (( ifile = ifile + 1 ))
      iskip=-1
      if [ "$mem" = "c00" ]; then
        iskip=0
      fi
      echo " cfipg($ifile)='${ifile_cmc}'," >>namin.adjustcmc.$nfhrs
      echo " iskip($ifile)=${iskip}," >>namin.adjustcmc.$nfhrs
    fi
  done

########################################################################
#  input CMC 2m temperature 6h ago for tmax and tmin forecast adjustment 
########################################################################

  if [ $nfhrs -ge 06 ]; then
    nfhrsm06=`expr $nfhrs - 06`
    if [ $nfhrsm06 -le 09 ]; then
      ifile_t2m_m06=cmc_enspost.t${cyc}z.pgrba_bcf0${nfhrsm06}
    else
      ifile_t2m_m06=cmc_enspost.t${cyc}z.pgrba_bcf${nfhrsm06}
    fi
    >$ifile_t2m_m06

    for mem in ${memberlist_cmc}; do
      if [ $nfhrsm06 -le 09 ]; then
        ifile_cmcm06=$COMINCMC/cmc_ge${mem}.t${cyc}z.pgrba_bcf0$nfhrsm06
        if [ $nfhrsm06 -eq 00 ]; then
          ifile_cmcm06=$COM_IN/cmce.$PDY/${cyc}/pgrba/cmc_ge${mem}.t${cyc}z.pgrbaf0$nfhrsm06
        fi   
      else
        ifile_cmcm06=$COMINCMC/cmc_ge${mem}.t${cyc}z.pgrba_bcf$nfhrsm06
      fi
    $EXECUTIL/wgrib $ifile_cmcm06 | grep ":TMP:" | grep "2 m" | $EXECUTIL/wgrib -i $ifile_cmcm06  -grib -append -o $ifile_t2m_m06
    done
  fi     

###########################################################################
# CMC and NCEP analysis difference input, first step is to judge valid time
###########################################################################

  cyc_verf=`$NDATE +$nfhrs $PDY$cyc | cut -c9-10`
  ifile_anldiff=ncepcmc_glbanl.t${cyc_verf}z.pgrba_mdf00

  echo $COM_IN/gefs.$PDYm2/${cyc_verf}/pgrba/$ifile_anldiff

  if [ -s $COM_IN/gefs.$PDYm2/${cyc_verf}/pgrba/$ifile_anldiff ]; then
    cp $COM_IN/gefs.$PDYm2/${cyc_verf}/pgrba/$ifile_anldiff  . 
  elif [ -s $COM_IN/gefs.$PDYm3/${cyc_verf}/pgrba/$ifile_anldiff ]; then
    cp $COM_IN/gefs.$PDYm3/${cyc_verf}/pgrba/$ifile_anldiff  . 
  elif [ -s $COM_IN/gefs.$PDYm4/${cyc_verf}/pgrba/$ifile_anldiff ]; then
    cp $COM_IN/gefs.$PDYm4/${cyc_verf}/pgrba/$ifile_anldiff  . 
  elif [ -s $COM_IN/gefs.$PDYm5/${cyc_verf}/pgrba/$ifile_anldiff ]; then
    cp $COM_IN/gefs.$PDYm5/${cyc_verf}/pgrba/$ifile_anldiff  . 
  elif [ -s $COM_IN/gefs.$PDYm6/${cyc_verf}/pgrba/$ifile_anldiff ]; then
    cp $COM_IN/gefs.$PDYm6/${cyc_verf}/pgrba/$ifile_anldiff  . 
  elif [ -s $COM_IN/gefs.$PDYm7/${cyc_verf}/pgrba/$ifile_anldiff ]; then
    cp $COM_IN/gefs.$PDYm7/${cyc_verf}/pgrba/$ifile_anldiff  . 
  fi

#################################################
# CMC and NCEP analysis difference (6h ago) input
#################################################

  if [ $nfhrs -ge 06 ]; then
    pdy_verf=`$NDATE +$nfhrs $PDY$cyc`
    cyc_verfm06=`$NDATE -6 $pdy_verf | cut -c9-10`
    ifile_anldiff_m06=ncepcmc_glbanl.t${cyc_verfm06}z.pgrba_mdf00

    echo $COM_IN/gefs.$PDYm2/${cyc_verfm06}/pgrba/$ifile_anldiff_m06

    if [ -s $COM_IN/gefs.$PDYm2/${cyc_verfm06}/pgrba/$ifile_anldiff_m06 ]; then
      cp $COM_IN/gefs.$PDYm2/${cyc_verfm06}/pgrba/$ifile_anldiff_m06  . 
    elif [ -s $COM_IN/gefs.$PDYm3/${cyc_verfm06}/pgrba/$ifile_anldiff_m06 ]; then
      cp $COM_IN/gefs.$PDYm3/${cyc_verfm06}/pgrba/$ifile_anldiff_m06  . 
    elif [ -s $COM_IN/gefs.$PDYm4/${cyc_verfm06}/pgrba/$ifile_anldiff_m06 ]; then
      cp $COM_IN/gefs.$PDYm4/${cyc_verfm06}/pgrba/$ifile_anldiff_m06  . 
    elif [ -s $COM_IN/gefs.$PDYm5/${cyc_verfm06}/pgrba/$ifile_anldiff_m06 ]; then
      cp $COM_IN/gefs.$PDYm5/${cyc_verfm06}/pgrba/$ifile_anldiff_m06  . 
    elif [ -s $COM_IN/gefs.$PDYm6/${cyc_verfm06}/pgrba/$ifile_anldiff_m06 ]; then
      cp $COM_IN/gefs.$PDYm6/${cyc_verfm06}/pgrba/$ifile_anldiff_m06  . 
    elif [ -s $COM_IN/gefs.$PDYm7/${cyc_verfm06}/pgrba/$ifile_anldiff_m06 ]; then
      cp $COM_IN/gefs.$PDYm7/${cyc_verfm06}/pgrba/$ifile_anldiff_m06  . 
    fi
  fi

####################
# set up input files 
####################

  echo " nfiles=${ifile}," >>namin.adjustcmc.$nfhrs
  echo " ifdebias=${ifdebias}," >>namin.adjustcmc.$nfhrs

  echo " cfipg1='${ifile_t2m_m06}'," >>namin.adjustcmc.$nfhrs
  echo " cfipg2='${ifile_anldiff}'," >>namin.adjustcmc.$nfhrs
  echo " cfipg3='${ifile_anldiff_m06}'," >>namin.adjustcmc.$nfhrs
  echo " ifhr=$nfhrs," >>namin.adjustcmc.$nfhrs

#####################
# set up output files 
#####################

  echo " cfopg1='cmce.t${cyc}z.pgrba_bcf${nfhrs}_temp'," >>namin.adjustcmc.$nfhrs

  echo " /" >>namin.adjustcmc.$nfhrs

  startmsg
  $EXECrtma/$pgm <namin.adjustcmc.$nfhrs > $pgmout.${nfhrs}  2> errfile
  export err=$?;$DATA/err_chk

done

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

