#! /bin/sh

#
# Metafile Script : drfice_gif.sh.sms
#
# Log :
# D.W.Plummer/NCEP   2/97   Add log header
# D.W.Plummer/NCEP  12/97   Added NGM v. ETA plot comparison if NGM precedes ETA
# 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
# Magee/NCO         12/07   Add safer copy of mdates files.    

export PS4='exdrficegif:$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

#
# Wait for gempak grid for $fhr to be produced
#
  icnt=1
  while [ $icnt -lt 100 ] ; do
    if [ ! -f $COMIN/global.tran.${PDY} ] ; 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

########################################################
#
#  Convert Polar Drift Ice data to FOS format and decode
#
########################################################

shdate=`echo $PDY | cut -c3-`

$GEMEXE/tran2fos $COMIN/global.tran.$PDY  global.fos.$PDY    
$GEMEXE/dcidft -c ${shdate}/0000 drftice.idft < global.fos.$PDY    

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
ib=`cat fort.55 | cut -b 18-33`
ic=`cat fort.55 | cut -b 45-48`
id=`cat fort.55 | cut -b 39-43`
ie=`cat fort.55 | cut -b 49-54`
TITLEIT=`echo MMAB POLAR ICE DRIFT \(10*NM\)  ${ib} ${ic}/${id}${ie}`

  #
  # Call the script to make the sea drift ice graphics
  #

  $USHgempak/gempak_drfgif.sh      

################################
# 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 $fstart ] ; 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
  cp /com/nawips/${envir}/gifdate/webgif_mdates.${RUN} .mdates.old
  errcp=$?
  if [ $errcp -eq 0 ] ; then
    mv .mdates.old mdates.old
  else
    err_exit "cp of mdates to local dir failed"
  fi
  oldline=`grep $RUN mdates.old`
  olddate=`grep $RUN mdates.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.old | sed s/${olddate}/${newdate}/g`
  sed s/${oldline}/${newline}/g mdates.old > mdates.${RUN}
  cp mdates.${RUN} /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN}
  errcp=$?
  if [ $errcp -eq 0 ] ; then
    mv /com/nawips/${envir}/gifdate/.webgif_mdates.${RUN} /com/nawips/${envir}/gifdate/webgif_mdates.${RUN}
  else
    err_exit "cp of mdates back to /com/nawips failed"
  fi
fi
cat mdates.${RUN} > 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

###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
    echo "1" > $COMOUT/../checkpoint_analysis_${MAPAREA}_${RUN}
###send flag to create html pages
    echo "${RSHPDY}" > $COMOUT/../bldhtml_flags/bld_html_${MAPAREA}_${RUN}_${cyc}  
fi

  let fhr=fhr+finc
  
done

#
