#!/bin/csh -f

set pathname=`pwd`/../../data/grib
set cyc=${1}
set int=${3}
set endtime=${2}
set type=${4}
set tvalsave=0

set ROOT=`pwd`/..

set ROOT=..
set pathname=../../data/grib

if ($type == 104) then
set bname=eta.t${cyc}z.grbgrd
set EXE=$ROOT/exe/dgeta2model.exe
endif

if ($type == 212) then
set bname=eta.t${cyc}z.awip3d
set EXE=$ROOT/exe/dgeta2model.exe
endif

if ($type == 221) then
set bname=eta.t${cyc}z.awip32
set EXE=$ROOT/exe/dgeta2model.exe
endif

if ($type == tile) then
set EXE=$ROOT/exe/dgeta2model_221_tile.exe
endif

if ($type == avn) then
set bname=gblav.t${cyc}z.pgrbf
set EXE=$ROOT/exe/dgeta2model_gbl.exe
endif

if ($type == wafs) then
set bname=wafs.T${cyc}Z.F
set EXE=$ROOT/exe/dgeta2model_wafs.exe
endif

if ($type == lmbc) then
set bname=lmbc_0
# set EXE=$ROOT/exe/dgeta2model_nest.exe
set EXE=$ROOT/exe/dgeta2model.exe
endif

if ($type == latlon) then
set bname=latlon_0
# set EXE=$ROOT/exe/dgeta2model_nest.exe
set EXE=$ROOT/exe/dgeta2model.exe
endif

#ioannis
if ($type == ecmwf) then
set bname=${5}
set initdate=${6}
set EXE=$ROOT/exe/dgeta2model_ecmwf.exe
endif
#ioannis

echo for $cyc data will handle data from 00 to $endtime at $int hour intervals

while ($tvalsave <= $endtime)

if ($tvalsave <= 9) then
set tval=0${tvalsave}
else
set tval=${tvalsave}
endif

if ($type == 104 || $type == 212 || $type == 221) then
set filename=${bname}${tval}.tm00
endif

if ($type == avn || $type == wafs) then
set filename=${bname}${tval}
endif

if ($type == lmbc || $type == latlon) then
set filename=${bname}${tval}
endif

#ioannis
if ($type == ecmwf) then
set filename=${bname}
endif
#ioannis

if ($type != tile) then
set GRIBFILE=${pathname}/${filename}
echo " Begin processing for "$filename
if ($type == ecmwf) then
echo "Initial time="$initdate"  Forecast time="$tval
endif
else
set GRIBFILE="not"
endif

set OUTDIR=$ROOT'/../data/prep'

# Degrib data.

if ( -e $GRIBFILE || $type == "tile" ) then
echo " "
else
echo " "$GRIBFILE" grib file not found."
exit 0
endif

# Convert degribbed data to unformatted format for local area model use.

echo " "
echo " Start degrib to formatted data conversion process..."
 
if ($type != ecmwf && $type != wafs && $type != tile) then
$EXE << endin
$GRIBFILE
$OUTDIR
endin
endif

if ($type == ecmwf) then
echo "ECMWF data"
$EXE << endin
$GRIBFILE
$OUTDIR
$initdate
$cyc
$tval
endin
endif

if ($type == wafs) then
$EXE << endin
$GRIBFILE
$OUTDIR
37
38
39
40
41
42
43
44
-9
endin
endif

if ($type == tile) then
$EXE << endin
`cat $pathname/tile.list.${tval}`
9999
$OUTDIR
endin
endif
 
# Cleanup.

if ($tvalsave <= $endtime) then
@ tvalsave += $int
endif

end

exit 0
