#! /bin/csh -f

set yy=`date +%y`
set mm=`date +%m`
set dd=`date +%d`

set ymd=$yy$mm$dd

rm EGRD* latlon_* lmbc_*

###################################################################
#
#  head is used to define symbolic links.  On HP's it should be "ftn"
#  Believe most other machines (SGI's and Suns, at least) use "fort."
#
set head="fort."
#
#######################

if ( $#argv < 3 ) then
echo "need three arguments "
echo "first is starting time"
echo "second is ending fore hr"
echo "third is interval"
exit
endif

if ( $#argv == 3 ) then
echo "if include type (latlon or lmbc) as argument 4"
echo "and a gempak file name as argument 5"
echo "the file will automatically be degribbed using GEMPAKs"
echo "nagrib program"
echo " "
echo " "
echo "if you dont use GEMPAK, ignore this"
echo " "
endif

#--------------------------------------------------------------                 
set out_dir = `pwd`
setenv COMSP ${out_dir}/
echo $COMSP
setenv fcsthr 00                             
setenv tmmark t00s               

set tend = $2
set numtimes = $2
@ numtimes -= $1
@ numtimes /= $3
@ numtimes ++
set invar="$1 $numtimes $3"

echo starting at forecast time $1 and doing $numtimes at $3 hour intervals
sleep 2
echo $invar > namelist

#------------------
#
# QUILT JOB
#
#------------------

set checkval=`ls restrt*quilt.${tmmark} | wc -l`

if ($checkval >= 1) then
echo "quilting up"
ln -sf cnst.file ${head}12
../exe/quilt.x < namelist >& quilt.log
#
# rm restrt*quilt.${tmmark}
endif

#------------------------
#
# POST JOB
#
#------------------------

rm ${head}*

ln -s -f fcstdata.meso            ${head}11
ln -s -f cnst.file            ${head}12      
ln -s -f restrt${fcsthr}.${tmmark}        ${head}13
ln -s -f cntrl.parm           ${head}14
# ln -s -f cntrl.dec           ${head}14
ln -s -f wgts1                ${head}20                                  
ln -s -f omg                  ${head}81         
ln -s -f all                  ${head}82

#-----------------------------------------------------------------------        

echo start post
cat namelist
../exe/etapost_new.x < namelist >& post.log

#### Now extract a more friendly output grid 

# cp ../../copygb/copygb.x .

set gridlmb=`cat outjob_input_lmbc` 
echo $gridlmb
set gridlat=`cat outjob_input_lat`
echo $gridlat
set hr=000

while ($hr <= $tend )
if ($hr < 100 && $hr != 000) set hr=0${hr}
if ($hr < 10 && $hr != 000) set hr=0${hr}
set input=EGRD3D${hr}.t00s
set outputlmb=lmbc_$hr
set outputlat=latlon_$hr

ls -l $input
if ( $4 == lmbc ) then
../exe/copygb.x -g"$gridlmb" -s"2,-3" -x $input $outputlmb
endif

if ( $4 == latlon ) then
../exe/copygb.x -g"$gridlat" -s"3,-3" -x $input $outputlat
endif

#decimal copygb.x -g"$gridlmb" -x $input $outputlmb
#decimal copygb.x -g"$gridlat" -x $input $outputlat

@ hr += $3
end

echo "End of Output Job"                                                        

if ( $#argv == 5 ) then
./gem_degrib.com $5 $2 $3 $4
endif
exit                                                                            
