#!/bin/ksh
##########################################################################
####  UNIX Script Documentation Block                                    #
#                                                                        #
# Script name:         rtma_post.sh                                      #
# Script description: -Estimates the Analysis error for the RTMA         #
#                     -convert rtma output into grib1 and grib2 format   #
#                                                                        #
# Author:      Manuel Pondeca        Org: NP22         Date: 2005-10-30  #
#                                                                        #
# Script history log:                                                    #
# 2005-10-30  Manuel Pondeca                                             #
#                                                                        #
##########################################################################

set -x
msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

cd $DATA

CDATE=$PDY$cyc
. $COMIN/rtma.t${cyc}z.envir.sh #- added to recover RAP CYCLE and FCST hour
#========================================================================
# (1) read in from original unformatted gsi ob stats files and write out
# formatted, streamlined versions of these files. Besides (lat,lon),
# output files will also contain location in grid units (xx,yy) for each
# observation.
#
# (2) read in bckg std error files for psi,chi,ps,tv,pseudorh
# in (ny,nx) configuration and (1) transpose files to
# (nx,ny) configuration and (2) get estimates of error files
# for u,v,ps,t,q.
#
# (3) perform cressman or barnes analysis to get gridded estimate of
# analysis errors for u,v,t,ps,q
#========================================================================
 cp $COMIN/${RUN}.t${cyc}z.gridname_input gridname_input

 cp  $COMIN/${RUN}.t${cyc}z.diag_conv_anl.Z diag_conv_anl.Z
 uncompress diag_conv_anl.Z

 cp  $COMIN/${RUN}.t${cyc}z.bckg_dxdy.dat bckg_dxdy.dat
 cp  $COMIN/${RUN}.t${cyc}z.bckg_qsat.dat bckg_qsat.dat
 cp  $COMIN/${RUN}.t${cyc}z.bckg_psfc.dat bckg_psfc.dat
 cp  $COMIN/${RUN}.t${cyc}z.bckgvar_psi.dat bckgvar.dat_psi
 cp  $COMIN/${RUN}.t${cyc}z.bckgvar_chi.dat bckgvar.dat_chi
 cp  $COMIN/${RUN}.t${cyc}z.bckgvar_ps.dat bckgvar.dat_ps
 cp  $COMIN/${RUN}.t${cyc}z.bckgvar_t0.dat bckgvar.dat_t0
 cp  $COMIN/${RUN}.t${cyc}z.bckgvar_pseudorh.dat bckgvar.dat_pseudorh
 cp  $COMIN/${RUN}.t${cyc}z.bckg_z.dat bckg_z.dat

 cp $FIXrtma/rtma_errfield.dat_bckup errfield.dat_clim 

 cp $COMIN/${RUN}.t${cyc}z.used_iterations.dat used_iterations.dat
 nfiles=`cat used_iterations.dat`
 ic=0
 while [ $ic -le $(($nfiles-1)) ] ; do
     if [ $ic -le 9 ] ; then
         icount=00$ic
     fi
     if [[($ic -ge 10) && ($ic -le 99)]] ; then
         icount=0$ic
     fi
     if [ $ic -ge 100 ] ; then
         icount=$ic
     fi
     cp $COMIN/${RUN}.t${cyc}z.gradx.dat_01_${icount} gradx.dat_01_${icount}
     cp $COMIN/${RUN}.t${cyc}z.grady.dat_01_${icount} grady.dat_01_${icount}
     let "ic=ic+1"
 done
 cp grady.dat_01_001 grady.dat

 cp $FIXrtma/rtma_latlon_mpfactor.dat rtma_latlon_mpfactor.dat

 cp  $COMIN/${RUN}.t${cyc}z.siganl siganl
 cp  $COMIN/${RUN}.t${cyc}z.sigf03 sigf03

 cp $COMIN/${RUN}.t${cyc}z.slabs.dat slabs.dat
 cp $COMIN/${RUN}.t${cyc}z.slabs2.dat slabs2.dat

 FHH=$RAPFHH

cat << EOF > bckgerr_infl_input
 &bckgerr_infl
   rinflu=0.29,
   rinflv=0.29,
   rinflt=1.00,
   rinflq=1.00,
   rinflp=1.00,
   itdiff=01
   efold=1.5
/
EOF
#************************************************************
#
#************************************************************

#==> generate input file used for cressman/barnes analysis

cat << EOF > cress_barnesparm.anl
 &cress_barnes
   radius_u=20.,rvicinity_u=0.5,rinovmax_u=100.,
   radius_v=20.,rvicinity_v=0.5,rinovmax_v=100.,
   radius_t=18.,rvicinity_t=0.5,rinovmax_t=100.,
   radius_p=18.,rvicinity_p=0.5,rinovmax_p=10000.,
   radius_q=18.,rvicinity_q=0.5,rinovmax_q=100.
/
EOF

#==> generate input file used for lanczos analysis error

cat << EOF > lanczosparm.anl
 &anlerrmethod
   projway=.true.
/
EOF
#************************************************************
#
#************************************************************
#==> generate input file needed for conversion to grib format

  YYYY=`echo $RAPCYCLE |cut -c1-4`
  MM=`echo $RAPCYCLE |cut -c5-6`
  DD=`echo $RAPCYCLE |cut -c7-8`
  HH=`echo $RAPCYCLE |cut -c9-10`
  LSTATUS=.true.
 
cat << EOF > parm_ptime_namelist
&ptimeinfo
    iyear01=${YYYY},
    imonth01=${MM},
    iday01=${DD},
    ihour01=${HH},
    ifcsthr=${FHH},
    guessfcst=$LSTATUS
/
EOF

cp diag_conv_anl diag_conv.dat
cp parm_ptime_namelist fort.9
cp $FIXrtma/rtma_grib.table_V2 GRIBTABLE

cp  $COMIN/${RUN}.t${cyc}z.diag_conv_ges.Z diag_conv_ges.Z
uncompress diag_conv_ges.Z

cp  $COMIN/${RUN}.t${cyc}z.diag_conv_02.Z diag_conv_02.Z
uncompress diag_conv_02.Z

mv diag_conv_ges diag_conv_ges.dat
mv diag_conv_02 diag_conv_02.dat

#Note:
# bmax(1) - maximum allowed bias for wind speed (m/s)
# bmax(2) - maximum allowed bias for temperature (K)
# bmax(3) - maximum allowed bias for psfc (Pa)
# bmax(4) - maximum allowed bias for spec. hum. (g/kg)
# bmax(5) - maximum allowed bias for wind speed (satellite data) (m/s)
# nt      - number of additional past cycles
#

  cyc1=`/nwprod/util/exec/ndate -01 $CDATE`
  cyc2=`/nwprod/util/exec/ndate -01 $cyc1`
  cyc3=`/nwprod/util/exec/ndate -01 $cyc2`
  cyc4=`/nwprod/util/exec/ndate -01 $cyc3`
  cyc5=`/nwprod/util/exec/ndate -01 $cyc4`
  cyc6=`/nwprod/util/exec/ndate -01 $cyc5`

  nt0=6

cat << EOF > rj_param_input
 &rj_param
   tcyc='${CDATE}',
   wmax280=5.0,  tmax180=10., pmax180=3000., qmax180=5.e-03, spdmax280=15.,
   wmax281=5.0,  tmax181=10., pmax181=3000., qmax181=5.e-03, spdmax281=15.,
   wmax282=5.0,  tmax182=10., pmax182=3000., qmax182=5.e-03, spdmax282=15.,
   wmax283=5.0,  tmax183=10., pmax183=3000., qmax183=5.e-03, spdmax283=15.,
   wmax284=5.0,  tmax184=10., pmax184=3000., qmax184=5.e-03, spdmax284=15.,
   wmax285=5.0,  tmax185=10., pmax185=3000., qmax185=5.e-03, spdmax285=15.,
   wmax286=5.0,  tmax186=10., pmax186=3000., qmax186=5.e-03, spdmax286=15.,
   wmax287=100., tmax187=10., pmax187=3000., qmax187=5.e-03, spdmax287=15.,
   wmax288=3.0,  tmax188=10., pmax188=3000., qmax188=5.e-03, spdmax288=15.,
   wmax289=5.0,  tmax189=10., pmax189=3000., qmax189=5.e-03, spdmax289=15.,
   ntw=1,
   ntt=3,
   ntp=3,
   ntq=3,
   ntspd=3,
   cycall(1)='${cyc1}',
   cycall(2)='${cyc2}',
   cycall(3)='${cyc3}',
   cycall(4)='${cyc4}',
   cycall(5)='${cyc5}',
   cycall(6)='${cyc6}'
/
EOF

    GESDIR=/nwges/$envir/rtma.${PDY}
    mkdir -p $GESDIR                      #NOTE: THIS IS PLACE TO STORE DYNAMICALLY 
                                          #GENERATED STATISTICAL FILES
                                          #STATISTICAL FILES FROM UP TO SIX PAST ANALYSES ARE
                                          #USED IN CURRENT ANALYSIS

   cp diag_conv_ges.dat diag_conv_for_rj.dat
   
   it=1
   while [ $it -le $nt0 ] ; do
     if [ $it -eq 1 ] ; then cycit=$cyc1 ; fi ; 
     if [ $it -eq 2 ] ; then cycit=$cyc2 ; fi ; 
     if [ $it -eq 3 ] ; then cycit=$cyc3 ; fi ; 
     if [ $it -eq 4 ] ; then cycit=$cyc4 ; fi ; 
     if [ $it -eq 5 ] ; then cycit=$cyc5 ; fi ; 
     if [ $it -eq 6 ] ; then cycit=$cyc6 ; fi ; 
  
     PDY0=` echo $cycit | cut -c1-8`
     GESDIR0=/nwges/$envir/rtma.${PDY0}
     if [ -e $GESDIR0/stats_w_rj.out_${cycit} ] ; then    #NOTE: DO NOT REMOVE "if" statement
       cp $GESDIR0/stats_w_rj.out_${cycit} .
     fi

     if [ -e $GESDIR0/stats_t_rj.out_${cycit} ] ; then    #NOTE: DO NOT REMOVE "if" statement
       cp $GESDIR0/stats_t_rj.out_${cycit} .
     fi

     if [ -e $GESDIR0/stats_ps_rj.out_${cycit} ] ; then   #NOTE: DO NOT REMOVE "if" statement
       cp $GESDIR0/stats_ps_rj.out_${cycit} .
     fi

     if [ -e $GESDIR0/stats_q_rj.out_${cycit} ] ; then    #NOTE: DO NOT REMOVE "if" statement
       cp $GESDIR0/stats_q_rj.out_${cycit} .
     fi
     
     if [ -e $GESDIR0/stats_spd_rj.out_${cycit} ] ; then  #NOTE: DO NOT REMOVE "if" statement
       cp $GESDIR0/stats_spd_rj.out_${cycit} .
     fi

     let "it=it+1"
   done

if [ -e $COMIN/${RUN}.t${cyc}z.t_rejectlist ] ; then
   cp $COMIN/${RUN}.t${cyc}z.t_rejectlist t_rejectlist
fi
if [ -e $COMIN/${RUN}.t${cyc}z.q_rejectlist ] ; then
   cp $COMIN/${RUN}.t${cyc}z.q_rejectlist q_rejectlist
fi
if [ -e $COMIN/${RUN}.t${cyc}z.p_rejectlist ] ; then
   cp $COMIN/${RUN}.t${cyc}z.p_rejectlist p_rejectlist
fi
if [ -e $COMIN/${RUN}.t${cyc}z.w_rejectlist ] ; then
   cp $COMIN/${RUN}.t${cyc}z.w_rejectlist w_rejectlist
fi

mkdir d_bckg d_anl d_err

export pgm=rtma_post
. prep_step

poe $EXECrtma/rtma_post <cress_barnesparm.anl>>$pgmout 2>errfile 
export err=$?; err_chk
cat $pgmout

cp w_rjlist.txt_dynamic_$CDATE     $GESDIR/.                #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp t_rjlist.txt_dynamic_$CDATE     $GESDIR/.                #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp p_rjlist.txt_dynamic_$CDATE     $GESDIR/.                #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp q_rjlist.txt_dynamic_$CDATE     $GESDIR/.                #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp mass_rjlist.txt_dynamic_$CDATE  $GESDIR/.                #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp wind_rjlist.txt_dynamic_$CDATE  $GESDIR/.                #USED IN NEXT ANALYSIS WITH THIS FILENAMING

cp stats_w_rj.out          $GESDIR/stats_w_rj.out_$CDATE    #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp stats_t_rj.out          $GESDIR/stats_t_rj.out_$CDATE    #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp stats_ps_rj.out         $GESDIR/stats_ps_rj.out_$CDATE   #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp stats_q_rj.out          $GESDIR/stats_q_rj.out_$CDATE    #USED IN NEXT ANALYSIS WITH THIS FILENAMING
cp stats_spd_rj.out        $GESDIR/stats_spd_rj.out_$CDATE  #USED IN NEXT ANALYSIS WITH THIS FILENAMING
 
echo DONE > post_done

#******************************************************************
#---wrap-up
#******************************************************************

$utilexec/cnvgrib -g21 bckg.grib2 bckg.grib1
$utilexec/cnvgrib -g21 reduced_anl.grib2 anl.grib1
$utilexec/cnvgrib -g21 reduced_anlerr.grib2 anlerr.grib1

#####################################################################
#    Process TMP, DPT, WIND RTMA  GRIB2 FOR AWIPS
#####################################################################

export pgm=tocgrib2
. prep_step
export XLFUNIT_11="anl.grib2"
export XLFUNIT_31=" "
export XLFUNIT_51="grib2.t${cyc}z.awprtmar2dvaranl.227"

startmsg

$utilexec/tocgrib2 < $utilparm/grib2_rtma2dvaranl.227 >> $pgmout 2>errfile
export err=$?;err_chk

if [ $SENDCOM = YES ]
then
   cp bckg.grib1 $COMOUT/${RUN}.t${cyc}z.2dvarges_ndfd.grb1
   cp anl.grib1 $COMOUT/${RUN}.t${cyc}z.2dvaranl_ndfd.grb1
   cp anlerr.grib1 $COMOUT/${RUN}.t${cyc}z.2dvaranlerr_ndfd.grb1
   cp bckg.grib2 $COMOUT/${RUN}.t${cyc}z.2dvarges_ndfd.grb2
   cp anl.grib2 $COMOUT/${RUN}.t${cyc}z.2dvaranl_ndfd.grb2
   cp grib2.t${cyc}z.awprtmar2dvaranl.227  $pcom/grib2.t${cyc}z.awprtmar2dvaranl.227
   cp ps_obs.listing_iter_01 $COMOUT/${RUN}.t${cyc}z.ps_obs.listing_iter_01
   cp t_obs.listing_iter_01 $COMOUT/${RUN}.t${cyc}z.t_obs.listing_iter_01
   cp q_obs.listing_iter_01 $COMOUT/${RUN}.t${cyc}z.q_obs.listing_iter_01
   cp u_obs.listing_iter_01 $COMOUT/${RUN}.t${cyc}z.u_obs.listing_iter_01
   cp v_obs.listing_iter_01 $COMOUT/${RUN}.t${cyc}z.v_obs.listing_iter_01
   cp spd_obs.listing_iter_01 $COMOUT/${RUN}.t${cyc}z.spd_obs.listing_iter_01
   cp ps_obs.listing_iter_02 $COMOUT/${RUN}.t${cyc}z.ps_obs.listing_iter_02
   cp t_obs.listing_iter_02 $COMOUT/${RUN}.t${cyc}z.t_obs.listing_iter_02
   cp q_obs.listing_iter_02 $COMOUT/${RUN}.t${cyc}z.q_obs.listing_iter_02
   cp u_obs.listing_iter_02 $COMOUT/${RUN}.t${cyc}z.u_obs.listing_iter_02
   cp v_obs.listing_iter_02 $COMOUT/${RUN}.t${cyc}z.v_obs.listing_iter_02
   cp spd_obs.listing_iter_02 $COMOUT/${RUN}.t${cyc}z.spd_obs.listing_iter_02
   cp w_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.w_rjlist_dynamic
   cp t_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.t_rjlist_dynamic
   cp p_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.p_rjlist_dynamic
   cp q_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.q_rjlist_dynamic
   cp spd_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.spd_rjlist_dynamic
   cp mass_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.mass_rjlist_dynamic
   cp wind_rjlist.txt_dynamic_$CDATE $COMOUT/${RUN}.t${cyc}z.wind_rjlist_dynamic

   if [ $SENDDBN = YES ]
   then
       $DBNROOT/bin/dbn_alert MODEL RTMA_GRIB $job $COMOUT/${RUN}.t${cyc}z.2dvarges_ndfd.grb2
       $DBNROOT/bin/dbn_alert MODEL RTMA_GRIB $job $COMOUT/${RUN}.t${cyc}z.2dvaranl_ndfd.grb2
#
#      SEND RTMA (TMP, DPT, WIND ) GRIB2 FILE TO AWIPS
#
       $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $pcom/grib2.t${cyc}z.awprtmar2dvaranl.227
   fi

fi

echo EXITING $0

########################################################

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

