#! /bin/sh

set +x
echo "--------------------------------------------------------------"
echo "hysplit_prep_chkmiss.sh                                       "
echo "--------------------------------------------------------------"
#echo "  If grib file is missing, get forecast from previous cycle   "
#echo "    If 4 cycles missing, exit, do not use earlier forecasts   "
#echo "  Write filename to file for read in calling script.          "
#echo "--------------------------------------------------------------"
#echo "  April 2008 - to NCEP   "
#echo "  Sept  2008 - change from 4 to 3 cycles missing based on RUC"
#echo "--------------------------------------------------------------"
set -x

  rm -f MDLfile

  if [ ! $# -eq 4 ]; then
     echo "Usage: $0 filename day cyc fcst-hr"
     exit 2
  fi

  MDLFILE=$1
  daytmp=$2
  cyctmp=$3
  fhrtmp=$4

  if [ ! -f $MDLFILE ]; then
    echo "$0 - $MDLFILE not found - will try previous cycle"
#    echo "$MDLFILE not found - will try previous cycle" | mail -s "hysplit_prep_chkmisss.sh"\
#                Barbara.Stunder@noaa.gov
    tmp=`/nwprod/util/exec/ndate -${CYC_INT} ${daytmp}${cyctmp}`
    xcyc=`echo $tmp | cut -c9-10`
    xday=`echo $tmp | cut -c1-8`
    let fhrtmp=$fhrtmp+${CYC_INT}
    if [ $fhrtmp -lt 10 ]; then
       fhrtmp="0"${fhrtmp}
    fi 
    MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${prefix}.t${xcyc}z.${fname}${fhrtmp}${sfx}
    if [ "${mdl}" = "sref" ]; then
       MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${xcyc}/pgrb/${mdl2}.t${xcyc}z.${fname}.${mem}.f${fhrtmp}
    fi
    if [ ! -f $MDLFILE ]; then
     #echo "$MDLFILE not found"
      tmp=`/nwprod/util/exec/ndate -${CYC_INT} ${xday}${xcyc}`
      xcyc=`echo $tmp | cut -c9-10`
      xday=`echo $tmp | cut -c1-8`
      let fhrtmp=${fhrtmp}+${CYC_INT}
      if [ $fhrtmp -lt 10 ]; then
         fhrtmp="0"${fhrtmp}
      fi 
      MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${prefix}.t${xcyc}z.${fname}${fhrtmp}${sfx}
      if [ "${mdl}" = "sref" ]; then
         MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${xcyc}/pgrb/${mdl2}.t${xcyc}z.${fname}.${mem}.f${fhrtmp}
      fi
      if [ ! -f $MDLFILE ]; then
       #echo "$MDLFILE not found"
        tmp=`/nwprod/util/exec/ndate -${CYC_INT} ${xday}${xcyc}`
        xcyc=`echo $tmp | cut -c9-10`
        xday=`echo $tmp | cut -c1-8`
        let fhrtmp=${fhrtmp}+${CYC_INT}
        if [ $fhrtmp -lt 10 ]; then
           fhrtmp="0"${fhrtmp}
        fi 
        MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${prefix}.t${xcyc}z.${fname}${fhrtmp}${sfx}
        if [ "${mdl}" = "sref" ]; then
           MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${xcyc}/pgrb/${mdl2}.t${xcyc}z.${fname}.${mem}.f${fhrtmp}
        fi
       #if [ ! -f $MDLFILE ]; then
       # #echo "$MDLFILE not found"
       #  tmp=`/nwprod/util/exec/ndate -${CYC_INT} ${xday}${xcyc}`
       #  xcyc=`echo $tmp | cut -c9-10`
       #  xday=`echo $tmp | cut -c1-8`
       #  let fhrtmp=${fhrtmp}+${CYC_INT}
       #  if [ $fhrtmp -lt 10 ]; then
       #     fhrtmp="0"${fhrtmp}
       #  fi 
       #  MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${prefix}.t${xcyc}z.${fname}${fhrtmp}${sfx}
       #  if [ "${mdl}" = "sref" ]; then
       #     MDLFILE=${COMMDLIN}/${mdlday}.${xday}/${xcyc}/pgrb/}${mdl2}.t${xcyc}z.${fname}.${mem}.f${fhrtmp}
       #  fi
       #  if [ ! -f $MDLFILE ]; then
       #     msg="FATAL ERROR - More than 4 cycles missing - $MDLFILE not found"
       #     $DATA/postmsg "$jlogfile" "$msg"
       #     exit 1
       #  fi
       #fi
          if [ ! -f $MDLFILE ]; then
             msg="FATAL ERROR - More than 3 cycles missing - $MDLFILE not found"
             $DATA/postmsg "$jlogfile" "$msg"
             exit 1
          fi
      fi
    fi
  fi
  echo ${MDLFILE} > MDLfile.${lab}

  exit
