#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exndas_getpcp.sh.sms
# Script description:  Gets 4 km NCEP stage 2/4 precipitation analysis and
#                      interpolates it to the operational Nam grid
#
# Author:        Rogers/Lin       Org: NP22         Date: 2001-06-11
#
# Abstract: Get hourly NCEP Stage2/4 precipitation analysis and interpolate to
#           the Nam model grid
#
# Script history log:
# 2001-06-11  Eric Rogers / Ying Lin
# 2006-01-17  Ying Lin / Eric Rogers modified to output binary file for WRF-NMM
# 2007-06-28  Ying Lin add use of 12-36h 00Z NAM precip for OCONUS 
# 2009-04-16  Y. Lin devided the old pcpprep.f into merge2n4.f and pcpprep.f. 
#             

set -x
cd $DATA 

delt=`echo $tmmark|cut -c 3-4`
daym1=`/nwprod/util/exec/ndate -24 $CYCLE | cut -c 1-8`
daym0=`echo $CYCLE | cut -c 1-8`

# Specs for B-grid and George Gayno's copygb.  These will need to be changed
# if the B-grid specs change or when the B-grid compatible copygb is 
# operational. 

# These will be used when George's new ip/w3 libs are put into parallel
BGRID="255 205 954 835 -7491 -144134 136 54000 -106000 126 108 64 44539 14802"
COPYGB=${COPYGB:-$utilexec/copygb}
export COM_HRLY_ST4=${COM_HRLY_ST4:-/com/hourly/prod}
export COM_HRLY_PCP=${COM_HRLY_PCP:-/com/hourly/${envir}}

# Get stage 2/4 data from com

ihrs="1 2 3"
for ihr in $ihrs
do
  tinc=`expr -$delt + $ihr`
  time=`/nwprod/util/exec/ndate $tinc $CYCLE`
  day=`echo $time | cut -c 1-8`
  PDY=`echo $time | cut -c 1-8`
  ST2file[$ihr]=ST2ml${time}.Grb
  ST4file[$ihr]=ST4.${time}.01h
  nampcpfile[$ihr]=nampcp.${time}

  cp $COM_HRLY_ST4/nam_pcpn_anal.${day}/${ST2file[$ihr]}.Z .
  cp $COM_HRLY_ST4/nam_pcpn_anal.${day}/${ST4file[$ihr]}.Z .

# This should point to test or para until implementation
# 1/29/08 : Fix for 18z NDAS, nampcp files for tm06, tm03 should
# come from the current day's /com/hourly directory!

  if [ $cyc -eq 18 ] ; then
    if [ $tmmark = tm06 -o $tmmark = tm03 ] ; then
      cp $COM_HRLY_PCP/nam_pcpn_anal.${daym0}/${nampcpfile[$ihr]} .
    else
      cp $COM_HRLY_PCP/nam_pcpn_anal.${daym1}/${nampcpfile[$ihr]} .
    fi
  else
    cp $COM_HRLY_PCP/nam_pcpn_anal.${daym1}/${nampcpfile[$ihr]} .
  fi

# $COPYGB -g "${BGRID}" -i3 -x ${nampcpfile[$ihr]} ${nampcpfile[$ihr]}.bgrid

  cp ${ST2file[$ihr]}.Z $COMOUT/${RUN}.t${cyc}z.${ST2file[$ihr]}.${tmmark}.Z
  cp ${ST4file[$ihr]}.Z $COMOUT/${RUN}.t${cyc}z.${ST4file[$ihr]}.${tmmark}.Z
  cp ${nampcpfile[$ihr]} $COMOUT/${RUN}.t${cyc}z.${nampcpfile[$ihr]}.${tmmark}
  uncompress ${ST2file[$ihr]}.Z ${ST4file[$ihr]}.Z
done

# Interpolate 4 km precipitation analyses to nam grid
# If any input data are missing, the code writes a null
# file so it should never end with a non-zero condition code

cp $gespath/ndas.hold/pcpbudget_history .

ihrs="1 2 3"
for ihr in $ihrs
do
  tinc=`expr -$delt + $ihr`
  time=`/nwprod/util/exec/ndate $tinc $CYCLE`
  day=`echo $time | cut -c 1-8`
  PDY=`echo $time | cut -c 1-8`
  ST2file[$ihr]=ST2ml${time}.Grb
  ST4file[$ihr]=ST4.${time}.01h
  nampcpfile[$ihr]=nampcp.${time}

# Merge Stage 2 and 4 files:

  if [[ -s ${ST2file[$ihr]} || -s ${ST4file[$ihr]} ]]; then

    export pgm=nam_mergest2n4
    . prep_step
    ln -sf ${ST2file[$ihr]}         fort.11
    ln -sf ${ST4file[$ihr]}         fort.12
    ln -sf $FIXnam/stage3_mask.grb  fort.13
    ln -sf st2n4.$time              fort.51
    
    startmsg
    $EXECnam/nam_merge2n4 >> $pgmout 2>errfile
    export err=$?;err_chk

# Map to B-grid:
    $COPYGB -g "${BGRID}" -i3 -x st2n4.$time st2n4.$time.bgrid

  fi

  rm fort.*

  export pgm=nam_pcpprep
  . prep_step
  ln -sf st2n4.$time.bgrid         fort.11
  ln -sf ${nampcpfile[$ihr]}       fort.12
  ln -sf pcpbudget_history         fort.13
  ln -sf pcp.$tmmark.hr${ihr}.bin  fort.51
  ln -sf pcp.$tmmark.hr${ihr}.grb  fort.52

  startmsg
  $EXECnam/nam_pcpprep >> $pgmout 2>errfile
  export err=$?;err_chk

  $utilexec/wgrib -V pcp.$tmmark.hr${ihr}.grb
  cp pcp.$tmmark.hr${ihr}.bin $COMOUT/ndas.t${cyc}z.pcp.$tmmark.hr${ihr}.${CDATE}.bin
  cp pcp.$tmmark.hr${ihr}.grb $COMOUT/ndas.t${cyc}z.pcp.$tmmark.hr${ihr}.${CDATE}.grb

done

echo Exiting $0

exit
