#####################################################################
echo "------------------------------------------------"
echo "GFS postprocessing"
echo "------------------------------------------------"
echo "History: July 2007 - First implementation of this new script."
echo "2007-09-26 S. Lilly -- Added logic to print data that is"
echo "                       -2 - +2 hours of the current time (00)"
echo "                       Also added logic to generate SAMER and"
echo "                       and AFRICA backgrounds."
echo "2007-12-20 C. Magee -- Add safer copies of mdates files."
#####################################################################

set -x

sh $utilscript/setup.sh
msg="Begin job for $job"
postmsg "$jlogfile" "$msg"

cd $DATA

######################################################################################
#  Enable white map backgrounds.
######################################################################################

cp /nw${envir}/gempak/fix/coltbl.xwp.wbg coltbl.xwp

######################################################################################
#   Setting the DATTIM -2 - +2 hours of the current time (00)
######################################################################################

export ndate=`/nwprod/util/ush/finddate.sh $PDY d-1 | cut -c3-8`
export pdate=`echo $PDY | cut -c3-8`

typeset -Z2 a b c d

if [ $cyc -eq 00 ]
then
  a=`expr 24 - 2`
  export PDYA=$ndate/${a}${cyc}
  b=`expr 24 - 1`
  export PDYB=$ndate/${b}${cyc}
else
  a=`expr ${cyc} - 2`
  export PDYA=$pdate/${a}00
  b=`expr ${cyc} - 1`
  export PDYB=$pdate/${b}00
fi

c=`expr ${cyc} + 1`
export PDYC=$pdate/${c}00
d=`expr ${cyc} + 2`
export PDYD=$pdate/${d}00


######################################################################################
#  Run the rdbfua program to generate an ASCII radiosonde data file for snedit to read.  
#  This also generates the sonde.ids.tbl which is used by build_model_page
#  to create the interactive maps for the upa and skew-t charts.           
######################################################################################
 
 sort -k 2n,2 /nwprod/dictionaries/metar.tbl > metar_stnm.tbl

 #cp $COMIN/${NMODEL}.$cycle.adpupa.tm00.bufr_d fort.40
 cp /com/${NMODEL}/prod/${NMODEL}.${PDY}/${NMODEL}.$cycle.adpupa.tm00.bufr_d fort.40

$EXECgraphics/rdbfua

export filesize=` ls -l rdbfua.out | awk '{print $5}' `

######################################################################################
#  Run the snedit program to generate a GEMPAK radiosonde data file for snmap to read.
#  Only run snedit if rdbfua.out contained upper air data.
######################################################################################

if [ $filesize -gt 40 ]
then

. /nwprod/gempak/.gempak

#############################################################################
#  bump up number of stations for snedit from 200 to 500 in TIMSTN.
#############################################################################
$GEMEXE/snedit << EOF_SNEDIT
SNEFIL=rdbfua.out
SNFILE=snedit.gem
TIMSTN=5/500
r

e
EOF_SNEDIT
fi
######################################################################################
#  Initialize some variables for snmap.
######################################################################################

export RSHPDY=`echo $PDY | cut -c5-``echo $PDY | cut -c3-4`

export footer_date="`echo ${PDY} | cut -c5-6`/`echo ${PDY} | cut -c7-8`/`echo ${PDY} | cut -c1-4` ${cyc}UTC"

export model_caps=`echo ${NMODEL} | tr '[a-z]' '[A-Z]'`

mapbkgs="USA CN AK SA AF MXCB"

export levels="000 925 850 700 500 400 300 250 200 150 100"
 
for mapbkg in ${mapbkgs}
do
    latlon=0
    case ${mapbkg} in
      USA) titlearea="united states"
           title_caps="UNITED STATES"
           garea="23;-126;50;-66"
           ;;
      CN) titlearea="canada"
          title_caps="CANADA"
          garea="42;-142;73;-51"
          ;;
      AK) titlearea="alaska"
          title_caps="ALASKA"
          garea="50;-188;73;-128"
          ;;
      SA) titlearea="sameru"
          title_caps="SOUTH AMERICA"
          garea="-57;-89;13;-32"
          ;;
      AF) titlearea="africu"
          title_caps="AFRICA"
          garea="-38;-22;38;52"
          ;;

      *) titlearea="mexico and caribbean"
         title_caps="MEXICO AND CARIBBEAN"
         garea="7;-119;33;-56"
         latlon=10
         ;;
    esac

    MAPAREA=`echo ${titlearea} | cut  -c1-6`
    export IMAGDIR=${COMOUT}/$MAPAREA/$RUN/$cyc/images
################################################################
#   only run script if rdbfua.out contained upper air data.
################################################################
    if [ $filesize -gt 40 ]
    then
      for level in ${levels}
      do

      lev=`echo $level | cut -b 1-2`u

      if [ $level -eq 000 ]
      then
        level="1000"
      fi

      giffil=${NMODEL}_${lev}_`echo ${titlearea} | cut  -c1-6`_000l.gif
 
      export level giffil MAPAREA title_caps garea

      . $USHgempak/gempak_upapgif.sh

      done  # all levels
    fi
################################################################
# Send files to com only if rdbfua.out contained upper air data.
# If this is the beginning of a run, delete the images
################################################################
rm -fr ${COMOUT}/$MAPAREA/$RUN/$cyc
if [ ! -d $IMAGDIR ] ; then
    mkdir -p -m 775 $IMAGDIR
fi
pwd
if [ $filesize -gt 40 ]
then
  mv *.gif $IMAGDIR
  cp sonde.ids.tbl ${COMOUT}/$MAPAREA/$RUN/$cyc
fi
#############################################
# UPDATE DATE FILE IF IT DOES NOT EXISTS IN
# LOCAL WORKING DIRECTORY
#############################################

if [ ! -s mdates.${RUN2} ] ; then
  cp /com/nawips/${envir}/gifdate/webgif_mdates.${RUN2} .mdates.${RUN2}.$MAPAREA.old
  errcp=$?
  if [ $errcp -eq 0 ] ; then
    mv .mdates.${RUN2}.$MAPAREA.old mdates.${RUN2}.$MAPAREA.old
  else
    err_exit "cp of mdates to local dir failed"
  fi
  oldline=`grep $RUN mdates.${RUN2}.$MAPAREA.old`
  olddate=`grep $RUN mdates.${RUN2}.$MAPAREA.old| awk -F: '{print $1"\n"$2"\n"$3"\n"$4"\n"$5}' | grep ^${cyc}`
  yy=`echo $PDY | cut -c3-4`
  mm=`echo $PDY | cut -c5-6`
  dd=`echo $PDY | cut -c7-8`
  newdate="${cyc}-${mm}${dd}${yy}"
  newline=`grep $RUN mdates.${RUN2}.$MAPAREA.old| sed s/${olddate}/${newdate}/g`
  sed s/${oldline}/${newline}/g mdates.${RUN2}.$MAPAREA.old > mdates.${RUN2}
  cp mdates.${RUN2} /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN2}
  errcp=$?
  if [ $errcp -eq 0 ] ; then
    mv /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN2} /com/nawips/${envir}/gifdate/webgif_mdates.${RUN2}  
  else
    err_exit "cp of mdates back to /com/nawips failed"
  fi
fi

cat mdates.${RUN2} > mdates
for fil in `ls -1 /com/nawips/${envir}/gifdate/webgif_mdates.* | grep -v ${RUN}`
do
  cat $fil >> mdates
done
cp mdates ${COMOUT}/$MAPAREA/$RUN/$cyc/.mdates
errcp=$?
if [ $errcp -eq 0 ] ; then
  mv ${COMOUT}/$MAPAREA/$RUN/$cyc/.mdates ${COMOUT}/$MAPAREA/$RUN/$cyc/mdates
else
  err_exit "cp of mdates back to $COMOUT/$MAPAREA/$RUN/$cyc failed"
fi

if [ "$SENDWEB" = "YES" ] ; then
###send flag to send to rzdm
#   echo "1" > $COMOUT/../checkpoint_analysis_${MAPAREA}_${RUN}
###send flag to create html pages
   echo "${RSHPDY}" > $COMOUT/../bldhtml_flags/bld_html_${MAPAREA}_${RUN}_${cyc}
fi

done  # all mapbkgs


rm -f coltbl.xwp
#####################################################################
# GOOD RUN
set +x
echo "**********GFS upper air plotted data gempak COMPLETED NORMALLY"
echo "**********GFS upper air plotted data gempak COMPLETED NORMALLY"
echo "**********GFS upper air plotted data gempak COMPLETED NORMALLY"
set -x
#####################################################################

cat $pgmout

msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

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