#!/bin/csh
rm *~

set vname  = "PRES"
set vrec   = "1"
set dname  = "anl_surf125"
set idir   = "../data/"
set odir   = "../binary/"
set SYEAR  = 2007
set EYEAR  = 2009

@ ddy=${SYEAR}
while($ddy <= ${EYEAR})
set YEAR = ${ddy}
rm -v ${odir}/${vname}_${YEAR}.bin

@ ddm=1
while($ddm <= 12)
set ddm2 = $ddm

if($ddm < 10) then
set MONTH=0${ddm2}
else
set MONTH=${ddm2}
endif

set DAYMAX=31
if($ddm == 4) set DAYMAX=30
if($ddm == 6) set DAYMAX=30
if($ddm == 9) set DAYMAX=30
if($ddm == 11) set DAYMAX=30

@ d1=${ddy} % 4
if(($ddm == 2) && ($d1 != 0)) set DAYMAX=28
if(($ddm == 2) && ($d1 == 0)) set DAYMAX=29

@ ddd=1
while($ddd <= $DAYMAX)
set ddd2 = $ddd

if($ddd < 10) then
set DAY=0${ddd2}
else
set DAY=${ddd2}
endif

set DATE=${YEAR}${MONTH}${DAY}
echo ${idir}/${dname}/${YEAR}${MONTH}/${dname}.${DATE}

# +++ wgrib for checking data format
#wgrib -V data/${YEAR}${MONTH}/anl_p.${DATE}00 > log.txt
wgrib -V ${idir}/${dname}/${YEAR}${MONTH}/${dname}.${DATE}00 > log_${dname}.txt
# +++ wgrib for extract data in binary file
wgrib  -d ${vrec} -bin -nh ${idir}/${dname}/${YEAR}${MONTH}/${dname}.${DATE}00 -append -o ${odir}/${vname}_${YEAR}.bin
wgrib  -d ${vrec} -bin -nh ${idir}/${dname}/${YEAR}${MONTH}/${dname}.${DATE}06 -append -o ${odir}/${vname}_${YEAR}.bin
wgrib  -d ${vrec} -bin -nh ${idir}/${dname}/${YEAR}${MONTH}/${dname}.${DATE}12 -append -o ${odir}/${vname}_${YEAR}.bin
wgrib  -d ${vrec} -bin -nh ${idir}/${dname}/${YEAR}${MONTH}/${dname}.${DATE}18 -append -o ${odir}/${vname}_${YEAR}.bin
@ ddd ++
end
@ ddm ++
end
@ ddy ++
end
