set -x

DATE=$1
domain=$2

echo $DATE > datem00

CYC=00

COMN=/com/nam/prod
COMG=/com/gfs/prod
COMR=/com/rtma/prod

DATEM03=`/nwprod/util/exec/ndate +3 $DATE`
echo $DATEM03 > datem03
DATEM06=`/nwprod/util/exec/ndate +6 $DATE`
echo $DATEM06 > datem06
DATEM09=`/nwprod/util/exec/ndate +9 $DATE`
echo $DATEM09 > datem09
DATEM12=`/nwprod/util/exec/ndate +12 $DATE`
echo $DATEM12 > datem12

PDY00=`cut -c 1-8 datem00`
HH00=`cut -c 9-10 datem00`
PDY03=`cut -c 1-8 datem03`
HH03=`cut -c 9-10 datem03`
PDY06=`cut -c 1-8 datem06`
HH06=`cut -c 9-10 datem06`
PDY09=`cut -c 1-8 datem09`
HH09=`cut -c 9-10 datem09`
PDY12=`cut -c 1-8 datem12`
HH12=`cut -c 9-10 datem12`

case $network in
  global)    bufrnet=meso;;
  globalak)  bufrnet=meso;;
  global40)  bufrnet=meso;;
  gdas)      bufrnet=meso;;
  gfse)      bufrnet=meso;;
  ozone)     bufrnet=aqm;;
  aqm)       bufrnet=aqm;;
  pm)        bufrnet=aqm;;
  meso)      bufrnet=meso;;
  satimg)    bufrnet=satimg;;
   *)        bufrnet=meso;;
esac

if [ "$bufrnet" = "satimg" ]
then
  case $HH00 in
      00) ndas1=$COMG/gfs.${PDY00}/gfs.t${HH00}z.geoimr.tm00.bufr_d;;
      06) ndas1=$COMG/gfs.${PDY00}/gfs.t${HH00}z.geoimr.tm00.bufr_d;;
      12) ndas1=$COMG/gfs.${PDY00}/gfs.t${HH00}z.geoimr.tm00.bufr_d;;
      18) ndas1=$COMG/gfs.${PDY00}/gfs.t${HH00}z.geoimr.tm00.bufr_d;;
  esac

elif [ "$bufrnet" = "global" ]
then
  case $HH00 in
    00) ndas1=$COMG/gdas.$PDY00/gdas1.t${HH00}z.prepbufr
        ndas2=$COMG/gfs.$PDY00/gfs.t${HH00}z.prepbufr
        ndas3=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12;;
    03) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    06) ndas1=$COMG/gdas.$PDY00/gdas1.t${HH00}z.prepbufr
        ndas2=$COMG/gfs.$PDY00/gfs.t${HH00}z.prepbufr
        ndas3=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12;;
    09) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    12) ndas1=$COMG/gdas.$PDY00/gdas1.t${HH00}z.prepbufr
        ndas2=$COMG/gfs.$PDY00/gfs.t${HH00}z.prepbufr
        ndas3=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12;;
    15) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    18) ndas1=$COMG/gdas.$PDY00/gdas1.t${HH00}z.prepbufr
        ndas2=$COMG/gfs.$PDY00/gfs.t${HH00}z.prepbufr
        ndas3=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12;;
    21) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
  esac
elif [ $bufrnet = "aqm" ]
then
  vday=`echo $DATE |cut -c1-8`
  vdaym1=`sh /nwprod/util/ush/finddate.sh $vday d-1`
  vdayp1=`sh /nwprod/util/ush/finddate.sh $vday d+1`

  case $domain in
    ozone) filnam=aqm.t12z.prepbufr.tm00
           COMBUFR_IN1=$COMBUFR/hourly.$vdaym1
           COMBUFR_IN2=$COMBUFR/hourly.$vday;;
    pm)    filnam=aqm.t12z.anowpm.pb.tm024
           COMBUFR=/stmp/wx20wh/CHKOUT_TMP/com/hourly/prod
           COMBUFR_IN1=$COMBUFR/hourly.$vday
           COMBUFR_IN2=$COMBUFR/hourly.$vdayp1;;
  esac

  if [ $vcyc = "00" ]
  then
    ndas1=$COMBUFR_IN1/${filnam}
  else
    ndas1=$COMBUFR_IN2/${filnam} 
  fi

else
  case $HH00 in
    00) ndas1=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12
        ndas2=$COMN/ndas.$PDY06/ndas.t${HH06}z.prepbufr.tm06
        ndas3=$COMN/nam.$PDY00/nam.t${HH00}z.prepbufr.tm00;;
    01) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    02) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    03) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    04) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    05) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    06) ndas1=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12
        ndas2=$COMN/ndas.$PDY06/ndas.t${HH06}z.prepbufr.tm06
        ndas3=$COMN/nam.$PDY00/nam.t${HH00}z.prepbufr.tm00;;
    07) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    08) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    09) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    10) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    11) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    12) ndas1=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12
        ndas2=$COMN/ndas.$PDY06/ndas.t${HH06}z.prepbufr.tm06
        ndas3=$COMN/nam.$PDY00/nam.t${HH00}z.prepbufr.tm00;;
    13) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    14) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    15) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    16) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    17) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    18) ndas1=$COMN/ndas.$PDY12/ndas.t${HH12}z.prepbufr.tm12
        ndas2=$COMN/ndas.$PDY06/ndas.t${HH06}z.prepbufr.tm06
        ndas3=$COMN/nam.$PDY00/nam.t${HH00}z.prepbufr.tm00;;
    19) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    20) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    21) ndas1=$COMN/ndas.$PDY09/ndas.t${HH09}z.prepbufr.tm09
        ndas2=$COMN/ndas.$PDY03/ndas.t${HH03}z.prepbufr.tm03;;
    22) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
    23) ndas1=$COMR/rtma.$PDY00/rtma.t${HH00}z.prepbufr.tm00;;
  esac
fi

if [ -s $ndas1 ]
then
  cp $ndas1 prepda.${DATE}
  chmod 700 prepda.${DATE}
elif [ -s $ndas2 ]
  then
    cp $ndas2 prepda.${DATE}
    chmod 700 prepda.${DATE}
  else
  if [ -s $ndas3 ]
  then
    cp $ndas3 prepda.${DATE}
    chmod 700 prepda.${DATE}
  fi
fi

ls -1 prepda.${DATE}
err3=$?

if [ $err3 -ne 0 ] ; then
  cp $COMG/gdas.${PDY00}/gdas1.t${HH00}z.prepbufr prepda.${DATE}
  err4=$?
  if [ $err4 -ne 0 ] ; then
    cp $COMG/gfs.${PDY00}/gfs.t${HH00}z.prepbufr prepda.${DATE}
  fi
fi

if [ $NET = "ngac" ]
then
cyc=$HH00                                   # center hour of dump (eg, '12' for 12z)
CYMD=${vday}$cyc
window_radius=1.5                            # 3 hour window == 1.5 hour radius
rc_dump=0
sh /nwprod/ush/dumpjb $CYMD $window_radius aodmod
rc_dump=$?
if [ $rc_dump -eq 0 ] ; then
  mv ../aodmod.ibm verf.t${cyc}z.aodmod.tm00.bufr_d
  mv ../aodmod.out verf.t${cyc}z.aodmod.tm00.bufr_d.out
fi                                           # rc_dump = 0

cp verf.t${cyc}z.aodmod.tm00.bufr_d prepda.${DATE}

fi

exit
