#!/bin/tcsh
#
#

if( $# < 1 ) then
        echo $0 filename_of_MANAL
        exit
endif



set WGRIB = './wgrib'
set INFILE = $1
# set TMPDIR = $$_temp
set TMPDIR = TESTDIR
set PUDDING = './pudding'
set PSFCPROGRAM  = './psfc'
set CHECKHGT = './check_hgt'
set CHK100 = "./check_under100"
set XNUM = 361
set YNUM = 289
set TOPO = "/home/tama/JMA_DATA/MANAL_200509/topo/MANAL_TOPO.grb"


if( -d $TMPDIR ) then 
	echo 'Somthing wrong'
	echo 'Delete all'
	rm -rf $TMPDIR
	mkdir $TMPDIR
else
	echo "Making temporally directory"
	mkdir $TMPDIR
endif

foreach  item ( HGT UGRD VGRD TMP RH P )

  if (-f $TMPDIR/${item}) then
  	echo "Somethig wrong! $TMPDIR/${item} exist!"
	echo "rm $TMPDIR/${item} exist!"
	rm $TMPDIR/${item}
  endif

  foreach p ( 1000\ mb   950\ mb    925\ mb    900\ mb    850\ mb\
  		800\ mb   700\ mb   600\ mb    500\ mb    400\ mb\
		300\ mb   250\ mb   200\ mb    150\ mb    100\ mb\
		 70\ mb    50\ mb    30\ mb     20\ mb     10\ mb sfc )
		set pressure = `echo $p | sed "s/ mb//"`
		echo $pressure "hPa data file created"
	if( "${item}" == "P" ) then
		if( "$p" == "sfc" ) then
		#  get sea surface pressure
		     $WGRIB $INFILE | grep "MSL" | grep "PRMSL"\
		     	| $WGRIB  $INFILE -i -bin -nh -o $TMPDIR/P_MSL
		# get height of the ground
		     $WGRIB $TOPO | grep "HGT" \
		     	| $WGRIB $TOPO -i -bin -nh -o $TMPDIR/TOPO_1
		     $CHECKHGT $TMPDIR/TOPO_1 $TMPDIR/TOPO

		# make P at surface
		$PSFCPROGRAM $XNUM $YNUM $TMPDIR/P_MSL\
			$TMPDIR/TOPO $TMPDIR/TMP_sfc $TMPDIR/RH_sfc\
			$TMPDIR/${item}_$p

		else
		$PUDDING $XNUM $YNUM ${pressure}00 $TMPDIR/${item}_${pressure}
		endif

	else if ( "${item}" == "RH" && ("$p" == "250 mb" || "$p" == "200 mb"\
	      || "$p" == "150 mb" || "$p" == "100 mb" || "$p" == "70 mb" \
	      || "$p" == "50 mb" || "$p" == "30 mb" || "$p" == "20 mb" \
	      || "$p" == "10 mb") ) then
		$PUDDING $XNUM $YNUM 0 $TMPDIR/${item}_${pressure}

	else if ( "${item}" == "HGT" && "$p" == "sfc" ) then
		 # get height of the ground
		     $WGRIB $TOPO | grep "HGT" \
		        | $WGRIB $TOPO -i -bin -nh -o $TMPDIR/${item}_${pressure}_1
		     $CHECKHGT $TMPDIR/${item}_${pressure}_1 $TMPDIR/${item}_${pressure}
	else

	     $WGRIB $INFILE | grep "${item}" | grep "\:$p"\
		| $WGRIB  $INFILE -i -bin -nh -o $TMPDIR/${item}_${pressure}
     	endif
     
     if( "${item}" == "RH" ) then
     	$CHK100 $TMPDIR/${item}_${pressure} $TMPDIR/${item}_${pressure}.chk
        mv $TMPDIR/${item}_${pressure}.chk $TMPDIR/${item}_${pressure}
     endif
   end
end


echo "You can check each data under " ${TMPDIR} using "CheckBinData.tcsh".
echo "Gathering data in one file"


# MANAL_2005081200.grb
set OUTFILE = data.gpv`echo $1:t:r| sed 's/MANAL_//'`00.bin
echo $OUTFILE
rm $OUTFILE

foreach  item ( HGT UGRD VGRD P TMP RH )
  foreach p ( sfc 1000\ mb   950\ mb    925\ mb    900\ mb    850\ mb\
  		800\ mb   700\ mb   600\ mb    500\ mb    400\ mb\
		300\ mb   250\ mb   200\ mb    150\ mb    100\ mb\
		 70\ mb    50\ mb    30\ mb     20\ mb     10\ mb )
		set pressure = `echo $p | sed "s/ mb//"`
		ls -l $TMPDIR/${item}_${pressure}

   cat $TMPDIR/${item}_${pressure} >> $OUTFILE
 end
end


echo "For a sample "
echo "gpvvar = 'xoxxxxx' "
echo "exbvar = 'ooxoooxxxx' "

