################################################################
# UNIX Script Documentation Block
#
# Script name:         exsref_calfcsterr.sh.sms
# Script description:  This program is to calculate previous day 
#                      forecast error fields for bias estimation
#
# Author:      Jun Du       Org: NP21         Date: 2007-07
#
# Change log:
#  7/16/2007:  Jun Du, initial implementation for the SREF operation
#  7/28/2008:  Jun Du, increase wrf membership from 6 to 10 and reduce
#                      eta membership from 10 to 6
#  6/08/2009:  Jun Du, a check point is added to check if all fcst and analysis 
#                      data is ok; if not ok, fcst error will be assigned 
#                      to zero to avoid further contamination to future runs
#  11/02/11:   Jun Du, modified to the new 2012 NEMS-based version of SREF (v6.0.0)
#                      by reducing grouping from 4 (Eta, RSM, NMM and ARW) to
#                      3 (NMMB, NMM and ARW)
#
# Note: regional analysis is available every 3 hour at 00z, 03z, 
#       06z, 09z, 12z, 15z, and 18z each day done by an earlier job
################################################################
set -x
XLFRTEOPTS="namelist=old"
export XLFRTEOPTS
XLSMPOPTS="parthds=2"
export XLSMPOPTS


cd $DATA

# 4 days back
date=${PDYm4}${cyc}

date24=` /nwprod/util/exec/ndate +24 $date`
date48=` /nwprod/util/exec/ndate +48 $date`
date72=` /nwprod/util/exec/ndate +72 $date`
date96=` /nwprod/util/exec/ndate +96 $date`
adate1=`echo ${date} | cut -c1-8`
adate2=`echo ${date24} | cut -c1-8`
adate3=`echo ${date48} | cut -c1-8`
adate4=`echo ${date72} | cut -c1-8`
adate5=`echo ${date96} | cut -c1-8`

MODEL=C21
SYS=opl
if [ $MODEL = C21 ]; then
 fmem=21
fi

let fmax1=fmem+1 
let fmax=fmax1

# ensemble fcst initiation date:
yy=`echo ${date} | cut -c1-4`
mm=`echo ${date} | cut -c5-6`
dd=`echo ${date} | cut -c7-8`
hh=`echo ${date} | cut -c9-10`

# opl NAM and GFS fcst initiation date (00,06,12,18z)
if [ $hh = 03 -o $hh = 09 -o $hh = 15 -o $hh = 21 ]; then
 fdate=` /nwprod/util/exec/ndate +3 $date`
else
 fdate=$date
fi
fyy=`echo ${fdate} | cut -c1-4`
fmm=`echo ${fdate} | cut -c5-6`
fdd=`echo ${fdate} | cut -c7-8`
fhh=`echo ${fdate} | cut -c9-10`

# dates for anl data
HR=0
while [ $HR -le $ENDHOUR ]
do
 adate=` /nwprod/util/exec/ndate +$HR $fdate`
export  y$HR=`echo ${adate} | cut -c1-4`
export  m$HR=`echo ${adate} | cut -c5-6`
export  d$HR=`echo ${adate} | cut -c7-8`
export  h$HR=`echo ${adate} | cut -c9-10`
 HR=`expr $HR + $INCHOUR`
done

# dates for past training period
startday=24                    #in hour, 1-day back
#endday=`expr $startday + 456`  #19days=19x24 (456) hours, so 20-day training period including startday
endday=`expr $startday + 0`    #0days=0x24 (0) hours, so 1-day training period including startday
incday=24                      #24hr
 
backday=$startday
while [ $backday -le $endday ]
do
 numerator=`expr $backday - $startday + 24`
 id=`expr $numerator / 24`
 pdate=` /nwprod/util/exec/ndate -$backday $date`
 export  py$id=`echo ${pdate} | cut -c1-4`
 export  pm$id=`echo ${pdate} | cut -c5-6`
 export  pd$id=`echo ${pdate} | cut -c7-8`
 backday=`expr $backday + $incday`
done

nmbdata=${COM_IN}/sref.$yy$mm$dd/${cyc}/pgrb
nmmdata=${COM_IN}/sref.$yy$mm$dd/${cyc}/pgrb
arwdata=${COM_IN}/sref.$yy$mm$dd/${cyc}/pgrb

#################################
#yesterday=/com/sref/${envir}/sref.$py1$pm1$pd1/$hh/misc
yesterday=${COM_IN}/sref.$py1$pm1$pd1/$hh/misc

#echo " $py1 $py2 $py3 $py4 $py5 $py6 $py7 $py8 $py9 $py10 $py11 $py12 $py13 $py14 $py15 $py16 $py17 $py18 $py19 $py20 
#       $pm1 $pm2 $pm3 $pm4 $pm5 $pm6 $pm7 $pm8 $pm9 $pm10 $pm11 $pm12 $pm13 $pm14 $pm15 $pm16 $pm17 $pm18 $pm19 $pm20 
#       $pd1 $pd2 $pd3 $pd4 $pd5 $pd6 $pd7 $pd8 $pd9 $pd10 $pd11 $pd12 $pd13 $pd14 $pd15 $pd16 $pd17 $pd18 $pd19 $pd20 " > pastinfo 

echo " $py1 $pm1 $pd1 " > pastinfo 

echo " $y0 $y3 $y6 $y9 $y12 $y15 $y18 $y21 $y24 $y27 $y30 $y33 $y36 $y39 $y42 $y45 $y48 $y51 $y54 $y57 $y60 $y63 $y66 $y69 $y72 $y75 $y78 $y81 $y84
       $m0 $m3 $m6 $m9 $m12 $m15 $m18 $m21 $m24 $m27 $m30 $m33 $m36 $m39 $m42 $m45 $m48 $m51 $m54 $m57 $m60 $m63 $m66 $m69 $m72 $m75 $m78 $m81 $m84
       $d0 $d3 $d6 $d9 $d12 $d15 $d18 $d21 $d24 $d27 $d30 $d33 $d36 $d39 $d42 $d45 $d48 $d51 $d54 $d57 $d60 $d63 $d66 $d69 $d72 $d75 $d78 $d81 $d84
       $h0 $h3 $h6 $h9 $h12 $h15 $h18 $h21 $h24 $h27 $h30 $h33 $h36 $h39 $h42 $h45 $h48 $h51 $h54 $h57 $h60 $h63 $h66 $h69 $h72 $h75 $h78 $h81 $h84
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       03,06,09,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87
       00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 " > headinfo 

ln -f -s headinfo fort.7
ln -f -s pastinfo fort.8

if [ $hh = 03 -o $hh = 09 -o $hh = 15  -o $hh = 21 ]; then
 HR=03
else
 HR=00
fi 
while [ $HR -le $ENDHOUR ]
do
 if [ $MODEL = C21 ]; then
  echo 'fetching $MODEL ensemble data'
  if [ $SYS = opl ]; then
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.ctl.f$HR  r_gribawips1.f$HR
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.n1.f$HR   r_gribawips2.f$HR
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.p1.f$HR   r_gribawips3.f$HR
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.n2.f$HR   r_gribawips4.f$HR
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.p2.f$HR   r_gribawips5.f$HR
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.n3.f$HR   r_gribawips6.f$HR
  cp $nmbdata/sref_nmb.t${hh}z.pgrb212.p3.f$HR   r_gribawips7.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.ctl.f$HR  r_gribawips8.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.n1.f$HR   r_gribawips9.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.p1.f$HR   r_gribawips10.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.n2.f$HR   r_gribawips11.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.p2.f$HR   r_gribawips12.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.n3.f$HR   r_gribawips13.f$HR
  cp $nmmdata/sref_nmm.t${hh}z.pgrb212.p3.f$HR   r_gribawips14.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.ctl.f$HR   r_gribawips15.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.n1.f$HR    r_gribawips16.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.p1.f$HR    r_gribawips17.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.n2.f$HR    r_gribawips18.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.p2.f$HR    r_gribawips19.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.n3.f$HR    r_gribawips20.f$HR
  cp $arwdata/sref_em.t${hh}z.pgrb212.p3.f$HR    r_gribawips21.f$HR
  fi
  echo 'fetching $MODEL ensemble data finished'
 fi
 HR=`expr $HR + $INCHOUR`
 if [ $HR -lt 10 ];then HR=0$HR;fi
done

# get regional analysis
#-----------------
echo 'beginning untar regional analysis...'
#tar -xvf ${diranl}/REGanl.f00_84.tar
if [ $hh = 03 -o $hh = 09 -o $hh = 15 -o $hh = 21 ]; then
 HR=03
else
 HR=00
fi 
FHR=00
while [ $HR -le $ENDHOUR ]
do
 mv grib212.ndas.f$FHR r_gribawips$fmax1.f$HR
 FHR=`expr $FHR + $INCHOUR`
 HR=`expr $HR + $INCHOUR`
 if [ $FHR -lt 10 ];then FHR=0$FHR;fi
 if [ $HR -lt 10 ];then HR=0$HR;fi
done
rm -f *ndas*
echo 'end of untar the regional analysis'

# obtain past bias data:
rm -f grb_*
group="01 02 03"
for subgroup in $group
do
                                                                                                                                                  
#ENDHOUR=87
#INCHOUR=03
 HR=03
 while [ $HR -le $ENDHOUR ]
 do
 cat $yesterday/grb_pasterro$subgroup.f$HR >> yesterdayerro.$subgroup
 cat $yesterday/grb_pastfcst$subgroup.f$HR >> yesterdayfcst.$subgroup
 cat $yesterday/grb_pastsprd$subgroup.f$HR >> yesterdaysprd.$subgroup
 HR=`expr $HR + $INCHOUR`
 if [ $HR -lt 10 ];then HR=0$HR;fi
 done
 /nwprod/util/exec/grbindex yesterdayerro.$subgroup yesterdayerro.$subgroup.i
 /nwprod/util/exec/grbindex yesterdayfcst.$subgroup yesterdayfcst.$subgroup.i
 /nwprod/util/exec/grbindex yesterdaysprd.$subgroup yesterdaysprd.$subgroup.i
                                                                                                                                                  
done

##############################
dataOK=0
fnum=1
while [ ${fnum} -le ${fmax} ]
do

# check if data is all ok
 HR=03
 while [ $HR -le $ENDHOUR ]
 do
  str=`ls -s r_gribawips${fnum}.f$HR`  #get file size in block (1024 bytes)
  set -A fsize $str
  if [ -s r_gribawips${fnum}.f$HR ] && [ ${fsize[0]} -gt 0 ] ; then
   echo r_gribawips${fnum}.f$HR "is OK"
  else
   echo r_gribawips${fnum}.f$HR "not exist"
   dataOK=1
  fi
 HR=`expr $HR + $INCHOUR`
 if [ $HR -lt 10 ];then HR=0$HR;fi
 done

   if [ ${fnum} -le 9 ]; then
cat r_gribawips${fnum}.f00 r_gribawips${fnum}.f03 \
    r_gribawips${fnum}.f06 r_gribawips${fnum}.f09 \
    r_gribawips${fnum}.f12 r_gribawips${fnum}.f15 \
    r_gribawips${fnum}.f18 r_gribawips${fnum}.f21 \
    r_gribawips${fnum}.f24 r_gribawips${fnum}.f27 \
    r_gribawips${fnum}.f30 r_gribawips${fnum}.f33 \
    r_gribawips${fnum}.f36 r_gribawips${fnum}.f39 \
    r_gribawips${fnum}.f42 r_gribawips${fnum}.f45 \
    r_gribawips${fnum}.f48 r_gribawips${fnum}.f51 \
    r_gribawips${fnum}.f54 r_gribawips${fnum}.f57 \
    r_gribawips${fnum}.f60 r_gribawips${fnum}.f63 \
    r_gribawips${fnum}.f66 r_gribawips${fnum}.f69 \
    r_gribawips${fnum}.f72 r_gribawips${fnum}.f75 \
    r_gribawips${fnum}.f78 r_gribawips${fnum}.f81 \
    r_gribawips${fnum}.f84 r_gribawips${fnum}.f87 > r_gribawips0${fnum}
    $utilexec/grbindex r_gribawips0${fnum} r_gribawips0${fnum}.i
   else
cat r_gribawips${fnum}.f00 r_gribawips${fnum}.f03 \
    r_gribawips${fnum}.f06 r_gribawips${fnum}.f09 \
    r_gribawips${fnum}.f12 r_gribawips${fnum}.f15 \
    r_gribawips${fnum}.f18 r_gribawips${fnum}.f21 \
    r_gribawips${fnum}.f24 r_gribawips${fnum}.f27 \
    r_gribawips${fnum}.f30 r_gribawips${fnum}.f33 \
    r_gribawips${fnum}.f36 r_gribawips${fnum}.f39 \
    r_gribawips${fnum}.f42 r_gribawips${fnum}.f45 \
    r_gribawips${fnum}.f48 r_gribawips${fnum}.f51 \
    r_gribawips${fnum}.f54 r_gribawips${fnum}.f57 \
    r_gribawips${fnum}.f60 r_gribawips${fnum}.f63 \
    r_gribawips${fnum}.f66 r_gribawips${fnum}.f69 \
    r_gribawips${fnum}.f72 r_gribawips${fnum}.f75 \
    r_gribawips${fnum}.f78 r_gribawips${fnum}.f81 \
    r_gribawips${fnum}.f84 r_gribawips${fnum}.f87 > r_gribawips${fnum}
    $utilexec/grbindex r_gribawips${fnum} r_gribawips${fnum}.i
   fi
 let fnum=fnum+1
done
rm -f *awips*.f*

##############################
cp $PARMsref/sref_bias_variable.tbl variable.tbl

cat <<paramEOF >input
 &namin
   yy1=${yy},mm1=${mm},dd1=${dd},hh1=${hh},nvar0=1,nvar=32,dataOK=$dataOK
 &end
paramEOF

$EXECsref/sref_cal_fcsterr <input >error.out
export err=$?; err_chk

cp asc_* $COMOUT/.
cp grb_pasterro* $COMOUT/.
cp grb_pastfcst* $COMOUT/.
cp grb_pastsprd* $COMOUT/.
cp asc_pastcorr* $COMOUT/.

exit
