#! /bin/csh -x

#VARIAVEIS
set cycle=$1
set hrlast=$2
set type=$3
set int=$4
if ($type == cpteta || $type == cpteta20) then
set cycle=${Run_Date}
endif
 
#################################
#
# set tilelist="07 08 09 10 11 13 14 15 16 17 19 20 21 22 23"
set tilelist="10 11 12 16 17 18 22 23 24"
#
#################################

set ymd=`date +%Y%m%d`

set hr=00
set hrsave=00

if ($type == 104 || $type == 212 || $type == 221 || $type == tile) then
set mod=eta
endif

if ($type == avn || $type == wafs) then
set mod=avn
endif
if ($type == cpteta || $type == cpteta20) then
set mod=cpteta
endif

cd ${data_grib}
set pathname=`pwd`
rm tile.list.*

echo for the ${1}Z cycle getting data to $hrlast hr at a $int hr interval

while ($hrsave <= $hrlast)
if ($hrsave < 10 && $hrsave != 00) set hr=0${hrsave}
if ($hrsave >= 10) set hr=${hrsave}

if ($type == 104) then
set fget=eta.t${cycle}z.grbgrd${hr}.tm00
set dir=/pub/data/nccf/com/${mod}/prod/${mod}.$ymd
endif

if ($type == 212) then
set fget=eta.t${cycle}z.awip3d${hr}.tm00
set dir=/pub/data/nccf/com/${mod}/prod/${mod}.$ymd
endif

if ($type == 221) then
set fget=eta.t${cycle}z.awip32${hr}.tm00
set dir=/pub/data/nccf/com/${mod}/prod/${mod}.$ymd
endif

if ($type == tile) then
set fget=""
set fbase=eta.t${cycle}z.awip32${hr}
set dir=/pub/data/nccf/com/${mod}/prod/${mod}.$ymd/tiles.t${cycle}z
endif

if ($type == avn) then
set fget=gblav.t${cycle}z.pgrbf${hr}
set dir=/pub/data/nccf/com/${mod}/prod/${mod}.$ymd
endif

if ($type == lmbc) then
set fget=${type}_0${hr}
endif

if ($type == latlon) then
set fget=${type}_0${hr}
endif

if ($type == wafs) then
set fget=xtrn.wfsavn${cycle}${hr}
set dir=/pub/data/nccf/pcom/${mod}
endif

if ($type == cpteta || $type == cpteta20) then
if ($hrsave == 00) set hr=00${hrsave}
if ($hrsave < 10 &&  $hrsave != 00) set hr=000${hrsave}
if ($hrsave < 100 && $hrsave > 10 ) set hr=00${hrsave}
if ($hrsave < 1000 && $hrsave > 100 ) set hr=0${hrsave}
set fget=${mod}${cycle}.pgrbf${hr}
set dir=/pub/produtos/prod_eta/eta_sx6/EtaWS/${cycle}
set dirsst=/pub/produtos/prod_eta/eta_sx6/EtaWS/SST
set dicn=`echo ${cycle} |cut -c1-8`
set sstget=gdas1.T00Z.sstgrb
endif


if ($type != tile && $type != lmbc && $type != latlon) then
echo attempting to get /com/${mod}/prod/${mod}.$ymd/$fget
endif

if ($type == wafs) then
ftp -in ftpprd.ncep.noaa.gov << endftp
user ftp `whoami`@
binary
cd ${dir}
mget ${fget}*
bye
endftp
endif

if ($type == tile) then
foreach tc (`echo $tilelist`)
echo ${pathname}/$fbase.$tc >> tile.list.${hr}
ftp -in ftpprd.ncep.noaa.gov << endftp
user ftp `whoami`@
binary
cd $dir
get ${fbase}.$tc
bye
endftp
end
endif

if ($type != wafs && $type != lmbc && $type != latlon  && $type != tile \
&& $type != cpteta && $type != cpteta20) then

echo to grab 

ftp -in ftpprd.ncep.noaa.gov << endftp
user ftp `whoami`@
binary
cd $dir
mget ${fget}
bye
endftp
endif

if ($type == cpteta || $type == cpteta20) then
ftp -in 150.163.141.140 << endftp
  user anonymous cptec@
  binary 
  cd ${dir}
  mget ${fget}*
  bye
endftp

  if ( $hr == 0000 ) then
ftp -in 150.163.141.140 << endftp1
    user anonymous cptec@
    binary
    cd ${dirsst}
    get ${sstget} ${sstget}.${cycle}
    bye
endftp1
  endif
endif

if ($type == lmbc || $type == latlon) then
cp ../../eta/runs/$fget .
endif

@ hrsave += $int

if ($type == wafs) then
if ( $hr == 12 || $hr == 18 || $hr == 24 || $hr == 30 ) then
cat xtrn.wfsavn${cycle}${hr}a.* xtrn.wfsavn${cycle}${hr}b.* \
		> wafs.T${cycle}Z.F${hr}
else
mv xtrn.wfsavn${cycle}${hr}* wafs.T${cycle}Z.F${hr}
endif
endif
echo $hrsave
end
