#!/bin/csh

# This Script will degrib the most recent NDFD grids and convert 
# from the grid from 5 km to 20 km. This will be run on hwwe to decode the ndfd.

# 12/31/03 	Modified to degrib each parameter from seperate files
# 04/14/04      Modified to send grids to the netapp
# 11/06/06	Change gdmod2 -> gddiag, remove gpend after gddiag
# 01/17/07	Move to Compute Farm
# 08/10/10	modify to include wxtype grid and wx category grids in output
# 03/12/12	Add NDFD launch to LDAD
# 5/16/12	Commented Out send to AWIPS until given go ahead
# 8/18/14	Update for 2.5km NDFD grids
# 8/21/14	inplement for 18z on 8/21/14, add 5km population and only do 6hr
# 10/27/14	Add up 24-hr SNOW TOTALS for 5km grid
# 5/15/15	Add check of the grib2 file to make sure that the correct # of messages are in file. Wait if not. 
#
# SET TEST VARIABLES
if ( ${?NOSSH} == 0 ) then
    set ssh=ssh 
    set scp=scp 
    set cp=cp
    set chmod=chmod
    set mkdir=mkdir
    set mv=mv
else 
    echo No ssh or scp allowed 
    set ssh=echo
    set scp=echo
    set cp=echo
    set chmod=echo
    set mkdir=echo
    set mv=echo
endif
if ( ${?NOALERT} == 0 ) then
    setenv DBNALERT /pub/share/ncoops/dbn_alert 
else
    echo No DBNET alerts allowed
    setenv DBNALERT echo
endif

set user=`whoami`
set HPC_PDF='/hpc/home/hpcops/tables/all_hpc/pdf'
set HPC_TMPS='/export/hmedr2/medr/tempsawips'
set NCO_NDFD='/data/model/grid/grib/ndfd'

set fullddate = `date -u +%Y%m%d`
set ddate = `date -u +%y%m%d`

set starttime = `date`

cd $HOME/work_dir

if (! -e ndfd_working/ ) then
  mkdir ndfd_working
endif
cd ndfd_working/

set work_dir = `pwd`

set fhrs='072 096 120 144 168'

#  Locate most recent NDFD directory and file.

set ndfd_dir = 'ndfd.'${fullddate}

if(! -e ${NCO_NDFD}/${ndfd_dir}) then
    clear
    echo "TODAY'S NDFD GRIDS ARE UNAVAILABLE. "
    echo "${fullddate} unavailable." > nogrid.txt
    mail -s "NDFD missing" christopher.bailey@noaa.gov < nogrid.txt
    cd $HOME/scripts/
    degrib_ndfd_2p5_fromftp
    exit
endif

set ndfd_cur=`/bin/ls -1 ${NCO_NDFD}/${ndfd_dir} | grep "kwbc_ndfd_ak" | tail -1`

# Grab 2-digit time from end of last file

set ndfd_cur1 = `echo ${ndfd_cur} | cut -c11-12`

if (${ndfd_cur1} == 'pr' || ${ndfd_cur1} == 'ak') then
    set fileid = `echo ${ndfd_cur} | cut -c14-15`
else 
    set fileid = `echo ${ndfd_cur} | cut -c11-12`
endif

echo ${fileid}

# Set hours to decode the 06Z, 12Z, 18Z, 00Z NDFD grib files only

if (.${fileid} == '.01' || .${fileid} == '.02' || .${fileid} == '.03' || .${fileid} == '.04' || .${fileid} == '.05') then 
    set fileid = '00'
else if (.${fileid} == '.07' || .${fileid} == '.08' || .${fileid} == '.09' || .${fileid} == '.10' || .${fileid} == '.11') then              
    set fileid = '06'
else if (.${fileid} == '.13' || .${fileid} == '.14' || .${fileid} == '.15' || .${fileid} == '.16' || .${fileid} == '.17') then  
    set fileid = '12'
else if (.${fileid} == '.19' || .${fileid} == '.20' || .${fileid} == '.21' || .${fileid} == '.22' || .${fileid} == '.23') then  
    set fileid = '18'
endif

if ("."${1} == ".") then
#    set fileid=`echo ${ndfd_cur} | cut -c11-12`
else
    set fileid=${1}
endif

set ndfd_cur="kwbc_ndfd_${fileid}"
# Perform wgrib2 on ndfd grib2 file to determine # on messages in file
# After 15 after hour, degrib anyway
set mm = ` date -u +%M`

if ( `expr $mm + 0` < 15 ) then
    wgrib2 ${NCO_NDFD}/${ndfd_dir}/${ndfd_cur} > wgrib2.out
    set nummsg = `tail -1 wgrib2.out | awk -F':' '{print $1}'`
 	
    if ( `expr ${fileid} + 0` == 0 && $nummsg < 496 ) then
        exit(2)
    else if ( `expr ${fileid} + 0` == 6 && $nummsg < 480 ) then
        exit(2)
    else if ( `expr ${fileid} + 0` == 12 && $nummsg < 462 ) then
        exit(2)
    else if ( `expr ${fileid} + 0` == 18 && $nummsg < 446 ) then
        exit(2)
    endif
endif

set nextppt = 0
set nextppt = `chkrflg degrib_ndfd ${fullddate}${fileid} ${nextppt} 4`
set cstatus = $status
if ($cstatus == 0) then

# Remove old files from working directory
rm -f ndfd_*

echo $ndfd_dir
echo $ndfd_cur
echo $fileid

#   DeGRIB2 into GEMPAK.

rm -f ndfd.grdlist

cp ${OPSDIR}/tables/medr3/deltas/g2varswmo2.tbl .
cp ${OPSDIR}/tables/medr3/deltas/g2varswmo1.tbl .
cp ${OPSDIR}/tables/medr3/deltas/g2vcrdwmo1.tbl .

rm -f ${ndfd_cur}.grd

nagrib2 << EOF
GBFILE   = ${NCO_NDFD}/${ndfd_dir}/${ndfd_cur}
GDOUTF   = ${ndfd_cur}.grd
PROJ     = 
GRDAREA  =
KXKY     =
MAXGRD   = 1000
CPYFIL   = gds
GAREA    = grid
OUTPUT   = f/ndfd.grdlist
G2TBLS   = g2vars.tbl
G2DIAG   =
OVERWR   = NO
r

G2TBLS   = g2varswmo2.tbl
OUTPUT   = f/ndfd1.grdlist
r

ex
EOF

gpend

rm -f ndfd_2p5.grd
cp ${OPSDIR}/tables/all_hpc/pdf/ndfdg2n.pdf .

gdcfil << EOF
GDOUTF   = ndfd_2p5.grd
PROJ     =  
GRDAREA  =  
KXKY     =  
MAXGRD   = 500
CPYFIL   = ${ndfd_cur}.grd
ANLYSS   = 4/2;2;2;2
r

exit
EOF

ndfdg2n << EOF
GBFILE = ${NCO_NDFD}/${ndfd_dir}/${ndfd_cur}
GDOUTF = ndfd_2p5.grd
MAXGRD = 1000
GAREA  = grid
GSKIP  =
OUTPUT = f/wx.grdlist
OVERWR = no
GPARM  = wxnm;pp12
r

exit
EOF

gpend

# SET INITIAL NDFD TIME

set gemdtm=`cat ndfd.grdlist | head -3 | tail -1 | awk '{print $8}'`

set ndfd_initYmdh=20`echo $gemdtm | cut -d"/" -f1`
set ndfd_initYmdh=$ndfd_initYmdh`echo $gemdtm | cut -d "/" -f2 | cut -c1-2`
# Set initial time in YYMMDD/HH format

set ndfd_gemdtm=`echo $ndfd_initYmdh | cut -c3-8`/`echo $ndfd_initYmdh | cut -c9-10`

echo $ndfd_gemdtm

# Loop through WXTYPE times in ndfd.grd and run group_ndfd_wx1 program

gdinfo << EOF
GDFILE   = ndfd_2p5.grd
LSTALL   = YES
OUTPUT   = f/wx.grdlist
GDATTIM  = all
GLEVEL   = all
GVCORD   = all
GFUNC    = all
r

exit
EOF

set wxhr = `grep WXNM wx.grdlist | awk '{print $2}'`

foreach wxtime (${wxhr})

echo $wxtime

set init_ymd = `echo ${wxtime} | cut -c1-6`
set init_hr = `echo ${wxtime} | cut -c8-9`
set fhour = `echo $wxtime | cut -c13-15`

if ( `expr ${fhour} % 6` == 0 ) then

echo $init_ymd $init_hr $fhour
group_ndfd_wx1 ${init_ymd} ${init_hr} ${fhour}

endif

end

set pophr = `grep PP000000PA ndfd.grdlist | awk '{print $8}'`
#set pophr = `grep PP000254PA ndfd.grdlist | awk '{print $8}'`
foreach poptime (${pophr})

set fhr = `echo $poptime | cut -c 13-15`
if ( `expr ${fhr} % 6` == 0 ) then

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ${ndfd_cur}.grd
!GFUNC    = PP000254PA
GFUNC    = PP000000PA
GDATTIM  = ${poptime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = PP12
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
GRDAREA  =
KXKY     =
MAXGRD   = 2000
CPYFIL   = 
ANLYSS   = 4/2;2;2;2
r

exit
EOF

endif

end

set pophr = `grep P00M ndfd.grdlist | awk '{print $8}'`

foreach poptime (${pophr})

set fhr = `echo $poptime | cut -c 13-15`
if ( `expr ${fhr} % 6` == 0 ) then

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ${ndfd_cur}.grd
GFUNC    = P00M
GDATTIM  = ${poptime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = P06M
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
GRDAREA  =
KXKY     =
MAXGRD   = 2000
CPYFIL   = 
ANLYSS   = 4/2;2;2;2
r

exit

EOF

endif

end

# REMOVE OLDEST NDFD FILE FROM DIRECTORY

set dirlist = `ls -1 $GRPHGD/medr5km/ndfd`

if ($#dirlist > 8) then
    rm -f $GRPHGD/medr5km/ndfd/${dirlist[1]}
endif

set dirlist = `ls -1 $HOME/data/ndfd`

if ($#dirlist > 6) then
    rm -f $HOME/data/ndfd/${dirlist[1]}
endif

gc $GRPHGD/medr5km/ndfd/ ndfd_YYYYMMDDHH.grd 5 r
gc ${GRPHGD}/medr5km/ndfd_2p5/ ndfd_YYYYMMDDHH.grd 5 r

# REMOVE OLDER NDFD FILES USING KEITH'S GC SCRIPT

gc $HOME/data/ndfd ndfd_YYYYMMDDHH.grd 8 r

# COPY 5KM GRID TO $GRPHGD/medr5km/ndfd

cp ${ndfd_cur}.grd ${GRPHGD}/medr5km/ndfd_2p5/ndfd_${fullddate}${fileid}.grd
#cp ${ndfd_cur}.grd ${HOME}/data/ndfd/ndfd_${fullddate}${fileid}.grd

else if ($cstatus != 1) then
exit
endif

# CREATE SFC FILE USING GDGSFC on the 5KM GRID AND STORE ON HPC1#############################

set nextppt = `chkrflg degrib_ndfd ${fullddate}${fileid} ${nextppt} 4`
set cstatus = $status
if ($cstatus == 0) then

rm -f ndfd_stn.gem

sfcfil << EOF
SFOUTF   = ndfd_stn.gem
SFPRMF   = tmxk;tmnk;sped;drct;tclo;pp1e
STNFIL   = ${OPSDIR}/tables/medr/stns/hpcmedr.stn
SHIPFL   = NO
TIMSTN   = 20/0
SFFSRC   =
r

exit

EOF

gpend

# GET LIST OF TIMES FROM MEDR FORECAST

#gdinfo << EOF
#GDFILE   = ${ndfd_cur}.grd
#GDFILE   = ${HPC_TMPS}/current.grid
#LSTALL   = YES
#OUTPUT   = f/cur.list
#GDATTIM  = all
#GLEVEL   = all
#GVCORD   = all
#GFUNC    = all
#r
#
#ex
#
#EOF
#
#gpend

rm -f ndfd20.grd

# LOOP THRU FORECAST TIMES

foreach forecast_hrs ($fhrs)
    echo $forecast_hrs
    set yr=`datetime ${ddate}/1200 ${forecast_hrs} %Y`
    set mn=`datetime ${ddate}/1200 ${forecast_hrs} %m`
    set dy=`datetime ${ddate}/1200 ${forecast_hrs} %d`
    
    set vld_gdtm="${yr}${mn}${dy}/1200"
    
# remove slash
    set vld_Ymdh=`echo $vld_gdtm | cut -c1-8``echo $vld_gdtm | cut -c10-11`
    
    # Get Hour Difference between valid time and NDFD Initial time
    # Results in NDFD Forecast Hour
    
    set ndfd_fhr=`datedifh $vld_Ymdh $ndfd_initYmdh`
    set ndfd_tmax_fhr=`expr $ndfd_fhr + 12`

gdgsfc_nc << EOF
GDFILE   = ${ndfd_cur}.grd
GDATTIM  = ${ndfd_gemdtm}F$ndfd_fhr
GVCORD   = none
GLEVEL   = 0
GFUNC    = tmnk
SCALE    = 0
SFFILE   = ndfd_stn.gem
SFPARM   = tmnk
r

GDATTIM  = ${ndfd_gemdtm}F$ndfd_tmax_fhr
GFUNC    = tmxk
SFPARM   = tmxk
r

gdattim  = ${ndfd_gemdtm}F$ndfd_fhr
gfunc    = PP000000PA
!gfunc    = PP000254PA
sfparm   = pp1e
r

GDATTIM  = ${ndfd_gemdtm}F$ndfd_tmax_fhr
gfunc    = PP000000PA
!gfunc    = PP000254PA
sfparm   = pp1e
r

gdattim  = ${ndfd_gemdtm}F$ndfd_fhr
gfunc    = cld
sfparm   = tclo
r

GDATTIM  = ${ndfd_gemdtm}F$ndfd_tmax_fhr
gfunc    = cld
sfparm   = tclo
r

gdattim  = ${ndfd_gemdtm}F$ndfd_fhr
gfunc    = sped
sfparm   = sped
r

gdattim  = ${ndfd_gemdtm}F$ndfd_fhr
gfunc    = drct
sfparm   = drct
r

exit

EOF

end

set ndfd_looptime = `grep TMPK ndfd.grdlist | awk '{print $8}'`

foreach ndfdtime (${ndfd_looptime})

#   Remap data to the 20-km grid (215) and 5km grid.

set fhr = `echo $ndfdtime | cut -c 13-15`
if ( `expr ${fhr} % 6` == 0 ) then

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ndfd20.grd
GFUNC    = tmpk
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   =
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
GRDAREA  =
KXKY     =
MAXGRD   = 2000
CPYFIL   = #215
ANLYSS   = 4/2;2;2;2
r

GDOUTF   = ndfd5.grd
CPYFIL   = #1
r

GDOUTF   = ndfd20.grd
GFUNC    = dwpk
r

GDOUTF   = ndfd5.grd
r

GDOUTF   = ndfd20.grd
GFUNC    = cld
r

GDOUTF   = ndfd5.grd
r

GDOUTF   = ndfd20.grd
GFUNC    = drct
r

GDOUTF   = ndfd5.grd
r

GDOUTF   = ndfd20.grd
GFUNC    = sped
r

GDOUTF   = ndfd5.grd
r

exit

EOF

endif

end

set ndfd_looptime = `grep HGHTWW ndfd1.grdlist | awk '{print $8}'`
foreach ndfdtime (${ndfd_looptime})

#   Remap data to the 20-km grid (215).

set fhr = `echo $ndfdtime | cut -c 13-15`
if ( `expr ${fhr} % 3` == 0 ) then

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ndfd20.grd
GFUNC    = hghtww
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   =
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
r

GDOUTF   = ndfd5.grd
r

exit

EOF

endif

end

set ndfd_looptime = `grep TMXK ndfd.grdlist | awk '{print $8}'`
foreach ndfdtime (${ndfd_looptime})

#   Remap data to the 20-km grid (215).

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ndfd20.grd
GFUNC    = tmxk
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = 
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
r

GDOUTF   = ndfd5.grd
r

exit

EOF

end

set ndfd_looptime = `grep TMNK ndfd.grdlist | awk '{print $8}'`
foreach ndfdtime (${ndfd_looptime})

#   Remap data to the 20-km grid (215).

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ndfd20.grd
GFUNC    = tmnk
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = 
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
r

GDOUTF   = ndfd5.grd
r

exit

EOF

end

set ndfd_looptime = `grep P00M ndfd.grdlist | awk '{print $8}'`
foreach ndfdtime (${ndfd_looptime})

#   Remap data to the 20-km grid (215).

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ndfd20.grd
GFUNC    = p00m
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = p06m
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
r

GDOUTF   = ndfd5.grd
r

GDOUTF   = ndfd20.grd
GFUNC    = snow
GRDNAM   = 
r

GDOUTF   = ndfd5.grd
r

GDOUTF   = ndfd20.grd
GFUNC    = w06m
GRDNAM   =
r

GDOUTF   = ndfd5.grd
r

ex
EOF

end

set ndfd_looptime = `grep PP000000PA ndfd.grdlist | awk '{print $8}'`
#set ndfd_looptime = `grep PP000254PA ndfd.grdlist | awk '{print $8}'`
foreach ndfdtime (${ndfd_looptime})

#   Remap data to the 20-km grid (215).

gddiag << EOF
GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ndfd20.grd
GFUNC    = pp12
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = 
GRDTYP   = S
GPACK    = grib
GRDHDR   =
PROJ     =
r

GDOUTF   = ndfd5.grd
r

exit

EOF

end

# Move WX grids to 20km grid
set ndfd_looptime = `grep WXNM wx.grdlist | awk '{print $2}'`
foreach ndfdtime (${ndfd_looptime})

set fhr = `echo $ndfdtime | cut -c 13-15`
if ( `expr ${fhr} % 6` == 0 ) then

# Move WX grids to old file

gddiag << EOF
GDFILE   = ndfd_2p5.grd
!GDOUTF   = ${ndfd_cur}.grd
GDOUTF   = ndfd5.grd
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GPACK    = grib
GRDHDR   = 1
GFUNC    = WX
r

GFUNC    = RAINH
r

GFUNC    = RAINL
r

GFUNC    = SNOWL
r

GFUNC    = SNOWH
r

GFUNC    = ICEH
r
 
GFUNC    = ICEL
r

GFUNC    = MIXL
r

GFUNC    = MIXH
r

GFUNC    = TSTM
r

GFUNC    = STSM
r

exit
EOF

#   Remap data to the 20-km grid (215).

gddiag << EOF
GDFILE   = ndfd_2p5.grd
GDOUTF   = ndfd20.grd
GFUNC    = WX
GDATTIM  = ${ndfdtime}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = 
CPYFIL   = ndfd20.grd
GRDTYP   = S
GPACK    = grib
GRDHDR   = 1
r

GFUNC    = RAINH
r

GFUNC    = RAINL
r

GFUNC    = SNOWL
r

GFUNC    = SNOWH
r

GFUNC    = ICEH
r

GFUNC    = ICEL
r

GFUNC    = MIXL
r

GFUNC    = MIXH
r

GFUNC    = TSTM
r

GFUNC    = STSM
r

exit

EOF

endif

end

# get a list of times from the NDFD file

gdinfo << EOF
GDFILE   = ${ndfd_cur}.grd
LSTALL   = YES
OUTPUT   = f
GDATTIM  = all
GLEVEL   = all
GVCORD   = all
GFUNC    = all
r

ex
EOF

gpend


set snowtime_list = `cat gdinfo.fil | grep 'SNOW ' | awk '{print $2}'`
set num_snowtimes = `echo ${#snowtime_list}`
set cnt_snowtimes = 1

# Loop Thru times list
foreach snow_dattim (${snowtime_list})

# Number of current time in list
 set cnt_snowtimes = `expr ${cnt_snowtimes} + 1`
  
 if ( ${cnt_snowtimes} > 4 ) then

# Loop thru 4 time periods, adding up a 24 hour snow total

  foreach period (4 3 2 1) 

   set period1 = `expr ${cnt_snowtimes} - ${period}`

   if(${period} == 4) then

    gddiag << EOF
GDFILE   = ndfd20.grd
GDOUTF   = ndfd20.grd
!GFUNC    = mul(snow,39.3701)
GFUNC    = snow
GDATTIM  = ${snowtime_list[${period1}]}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = s24i^${snow_dattim}
GPACK    = grib
GRDHDR   =  
r

GDFILE   = ndfd5.grd
GDOUTF   = ndfd5.grd
r


GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ${ndfd_cur}.grd
r

exit
EOF

   else

gddiag << EOF
GDFILE   = ndfd20.grd
GDOUTF   = ndfd20.grd
GFUNC    = add(mul(snow,39.3701),s24i^${snow_dattim})
GFUNC    = add(snow,s24i^${snow_dattim})
GDATTIM  = ${snowtime_list[${period1}]}
GLEVEL   = 0
GVCORD   = none
GRDNAM   = s24i^${snow_dattim}
GPACK    = grib
GRDHDR   =  
r

GDFILE   = ndfd5.grd
GDOUTF   = ndfd5.grd
r

GDFILE   = ${ndfd_cur}.grd
GDOUTF   = ${ndfd_cur}.grd
r

exit
EOF

endif

  end

 endif

end

# set NDFD base time

set gemdtm=`cat gdinfo.fil | tail -1 | awk '{print $2}'`
set ndfd_initYmdh=20`echo $gemdtm | cut -d"/" -f1`
set ndfd_initYmdh=$ndfd_initYmdh`echo $gemdtm | cut -d "/" -f2 | cut -c1-2`

chmod 777 ndfd20.grd

# RENAME 20-KM GRID FILE

mv ndfd20.grd ndfd_${ndfd_initYmdh}

# SEND NDFD GRID TO NETAPP

chmod 777 ndfd_stn.gem

mv ndfd_stn.gem ndfd_${ndfd_initYmdh}.sfc

# DBNET ALERT FIELS TO NCOSRV

setenv DBNROOT /dbnet/dbnet_ops

$DBNALERT MODEL NDFD_GEMPAK sending_to_ncosrv ${work_dir}/ndfd_${ndfd_initYmdh} 

sleep 10

$DBNALERT MODEL NDFD_SFC sending_to_ncosrv ${work_dir}/ndfd_${ndfd_initYmdh}.sfc

sleep 10

$cp ${ndfd_cur}.grd ${GRPHGD}/medr5km/ndfd_2p5/ndfd_${fullddate}${fileid}.grd
$cp ${ndfd_cur}.grd ${HOME}/data/ndfd/ndfd_${fullddate}${fileid}.grd
mv  ${ndfd_cur}.grd /hpcnfs2/data/ndfd/ndfd_${fullddate}${fileid}.grd

mv ndfd5.grd ${GRPHGD}/medr5km/ndfd/ndfd_${fullddate}${fileid}.grd

rm -f ${ndfd_cur}.grd
rm -f ndfd_${ndfd_initYmdh}
rm -f ndfd_${ndfd_initYmdh}.sfc
rm -f local_*.txt
rm -f ndfd20_tmp.grd
rm -f ndfd_2p5.grd

else if ($cstatus != 1) then
exit
endif

set nextppt = `chkrflg degrib_ndfd ${fullddate}${fileid} ${nextppt} 4 D`

set endtime = `date`
echo " "
echo "Start Time = ${starttime}"
echo "End Time   = ${endtime}"

cd $HOME/movetoawips2/ndfdtoawips
copy_ndfdgrib2.csh

#$HOME/scripts/degrib_ndfd_2p5_fromftp


exit
