#! /bin/sh

#
# Metafile Script : gempak_gif.sh.sms
#
# Log :
# D.W.Plummer/NCEP   2/97   Add log header
# D.W.Plummer/NCEP  12/97   Added NGM v. NAM plot comparison if NGM precedes NAM
# J. Carr/HPC        8/98   Changed map to medium resolution
# J. Carr/HPC        1/99   Changed contur from 1 to 2 per OM request
# J. Carr/HPC        2/99   Changed skip to 0 per OM request
# B. Gordon/NCO      5/00   Ported to IBM-SP, Standardized for production,
#                           changed gdplot_nc -> gdplot2_nc
# B. Gordon/NCO      4/01   Modified to make individual GIF images.
#
# Shirey/NCO         2/02   Modified to create web tree in /com
# L. Sager/NCO       5/05   Modified to create Eastern Pacific graphics for the 
#                           NCEP Model graphics website.
# C. Magee/NCO      12/07   Add safer copy of mdates files.                     
# B. Rakesh/NCO     10/08   change cp to cat and make unique mdates.old files,
#                           retry cat if initial try fails.

export PS4='exgempakgif:$MAPAREA$SECONDS + '

cd $DATA

set -xa
sh $utilscript/setup.sh

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

export fhr=$fstart
typeset -Z3 fhr

case $RUN in
 gfs) MOD=gfs ;;
 ruc) MOD=ruc2;;
 *)  MOD=$RUN ;;
esac

precip_inc=06
prec_name=cp
export precip_inc
export prec_name 
########################################
while [ $fhr -le $fend ] ; do

#
# Start by creating the day-of-week Title
#

echo 0${fhr}${PDY}${cyc} > dates
export XLFUNIT_55="title.output"
$utilexec/webtitle  <  dates
export TITLEIT=`cat fort.55`

#
# Wait for gempak grid for $fhr to be produced
#
  icnt=1
if [ "$MODEL" != "NWW3" ] ; then
  while [ $icnt -lt 100 ] ; do
    if [ ! -f $COMIN/${MOD}_${PDY}${cyc}f${fhr} ] ; then
      sleep 120
    else
      break    
    fi
    if [ $icnt -gt 30 ] ; then
      err_exit "ABORTING after 1 hour of waiting for $fhr ${MODEL} gempak file."
    fi
    let icnt=icnt+1
  done
else
  while [ $icnt -lt 100 ] ; do
    if [ ! -f $COMIN/${RUN2}_${PDY}${cyc} ] ; then
      sleep 120
    else
      break    
    fi
    if [ $icnt -gt 30 ] ; then
      err_exit "ABORTING after 1 hour of waiting for ${MODEL} gempak file."
    fi
    let icnt=icnt+1
  done
fi   
 
  if [ "${MODEL}" = "NAM" ] ; then
    export MODEL2=NAM44
  else
    export MODEL2=$MODEL
  fi

  #
  # Call the scripts in parallel to make the gif images
  #
if [ "$MODEL" != "NWW3" ] ; then  
  if [ "$MODEL" = "GFS" ]
  then
      $USHgempak/gempak_gif1_gfs_epac.sh
  else
      $USHgempak/gempak_gif1_epac.sh
  fi
  $USHgempak/gempak_gif2_epac.sh
else
    $USHgempak/gempak_wavgif_epac.sh
fi

################################
# Send files to com 
# If this is the beginning of a run, delete the images
################################
export IMAGDIR=${COMOUT}/$MAPAREA/$RUN/$cyc/images
if [ ${fhr} -eq 00 ] ; then
rm -rf ${COMOUT}/$MAPAREA/$RUN/$cyc
fi
if [ ! -d $IMAGDIR ] ; then
  mkdir -p -m 775 $IMAGDIR
fi
pwd
mv *${fhr}*.gif $IMAGDIR
###############################################
# Update date file if it does not exists in
# local working directory.
###############################################
if [ ! -s mdates.${RUN} ] ; then
  cat /com/nawips/${envir}/gifdate/webgif_mdates.${RUN} > .mdates.${RUN}.$MAPAREA.old
  errcat=$?
  if [ $errcat -eq 0 ] ; then
    mv .mdates.${RUN}.$MAPAREA.old mdates.${RUN}.$MAPAREA.old
  else
    err_exit "cp of mdates to local dir failed"
  fi
  if [ $RUN = "ruc" ] ; then
     cat mdates.old .mdates.${RUN}.$MAPAREA.old
     errcat=$?
     if [ $errcat -eq 0 ] ; then
       mv .mdates.${RUN}.$MAPAREA.old mdates.${RUN}.$MAPAREA.old
     else
       err_exit "cat of mdates to local ruc dir failed"
     fi
     md=`echo $PDY | cut -c5-8`
     yr=`echo $PDY | cut -c3-4`
     mdy=${md}${yr}
     export pgm=ruc_hours
     $USHgempak/ruc_hours $cyc $mdy
     export err=$?
     if [ $err -eq 0 ] ; then
       cp index_namer_ruc.shtml ${COMOUT}/$MAPAREA/$RUN/index/.
       cp index_namer_ruc.shtml ${COMOUT}/$MAPAREA/$RUN/index/index_namer_ruc_body.shtml
       mv .mdates.${RUN}.$MAPAREA.old /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN}.$MAPAREA 
       errmv=$?
       if [ $errmv -eq 0 ] ; then
         mv /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN} /com/nawips/${envir}/gifdate/webgif_mdates.${RUN}
       else
         err_exit "mv of mdates back to /com/nawips for RUC failed"
       fi
#
#      Remove the four hour old ruc directory
#
       if [ -f fourhour.old ] ; then
         four=`cat fourhour.old`
         # rm -rf ${COMOUT}/$MAPAREA/$RUN/$four
       fi
     else
       msg='Error return from $USHgempak/ruc_hours'
       echo $msg $err ${MAPAREA} ${RUN} ${cyc}
       postmsg "$jlogfile" "$msg"
     fi
     err_chk
  else
    oldline=`grep $RUN mdates.${RUN}.$MAPAREA.old`
    olddate=`grep $RUN mdates.${RUN}.$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.${RUN}.$MAPAREA.old | sed s/${olddate}/${newdate}/g`
#
# do this sed twice -- once to .mdates.${RUN} to update .webgif_mdates.${RUN}.MAPAREA,
# once to save to mdates.${RUN} (without .) for use in later
# "cat mdates.${RUN}" below.
#
  sed s/${oldline}/${newline}/g mdates.${RUN}.$MAPAREA.old > .mdates.${RUN}.$MAPAREA
  sed s/${oldline}/${newline}/g mdates.${RUN}.$MAPAREA.old > mdates.${RUN}.$MAPAREA
    mv .mdates.${RUN}.$MAPAREA /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN}.$MAPAREA
    errmv=$?
    if [ $errmv -eq 0 ] ; then
     mv /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN}.$MAPAREA /com/nawips/${envir}/gifdate/webgif_mdates.${RUN}
    else
      err_exit "mv of mdates back to /com/nawips failed"
    fi
  fi
fi
cat mdates.${RUN}.$MAPAREA > mdates
for fil in `ls -1 /com/nawips/${envir}/gifdate/webgif_mdates.* | grep -v ${RUN}`
do
  cat $fil >> mdates
done
#### Copy mdates to the parallel area for parallel testing--Zhu 04/08/04
cp mdates /com/web/${envir}/analysis/$MAPAREA/$RUN/$cyc/.mdates
errcp=$?
if [ $errcp -eq 0 ] ; then
  mv /com/web/${envir}/analysis/$MAPAREA/$RUN/$cyc/.mdates /com/web/${envir}/analysis/$MAPAREA/$RUN/$cyc/mdates
else
  err_exit "cp of mdates to /com/web/${envir}/analysis/$MAPAREA/$RUN/$cyc failed"
fi

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 to ${COMOUT}/$MAPAREA/$RUN/$cyc failed"
fi

###These 2 fix files are pointed to directly in the perl scripts
##cp $FIXgempak/webgif_fpdef $DATA/fpdef
##cp $FIXgempak/webgif_mtrans $DATA/mtrans

if [ "$SENDWEB" = "YES" ] ; then
###send flag to send to rzdm, it's done in exbld_html.sh.sms, comment out 
#   echo "1" > $COMOUT/../checkpoint_analysis_${MAPAREA}_${RUN}
###send flag to create html pages
    echo "${RSHPDY}" > $COMOUT/../bldhtml_flags/bld_html_${MAPAREA}_${RUN}_${cyc}  
    echo "${RSHPDY}" > /com/web/${envir}/bldhtml_flags/bld_html_${MAPAREA}_${RUN}_${cyc}  
fi

  if [ "${MODEL}" = "GFS" -a $fhr -ge 180 ] ; then
    finc=12
    export precip_inc=12
  fi
  let fhr=fhr+finc
  
  if [ "$MODEL" = "RUC2" ] ; then
     if [ $fhr -gt 03 ] ; then
        precip_inc=03
        precip_name=03
        let fhr=fhr+2
     fi
  fi

done

#    

