#!/bin/bash 

START=`pwd`
OUTDIR='../output'

# READ PARAMS
hr=t${1}z 		# t00z - GFS INIT TIME
endtime=${2}		# 00 
int=${3}		# 03
model=ifs
type2=${4}
tval_count=00
folder=${5}

# CLEAN PREVIOUS FILES
cd $folder
rm -f *.dump
rm -f *.gfs

cd $START
rm -f gfsconv.out
gfortran -O ifs2model_rrtm.f -o ifs2model_rrtm.exe

echo "Read hours from $tval_count to $endtime"

while [ $tval_count -le $endtime ]
do
	echo "Processing Hour $tval_count"

        tval=$(printf "%.3d" $tval_count)
	ROOT=`pwd`

	filename=${model}.${hr}.${type2}${tval}
	GRIBFILE=${folder}/${filename}
	EXTEN='.dump'

	echo " ***************************************************"
	echo " **********  GRIB grid processor *******************"
	echo " ******* Dump data from $filename file ******"
	echo " ***************************************************"

	if [ ! -e $GRIBFILE ] 
	then
		echo "Missing input file $GRIBFILE"
       		exit 
	fi

	# GETTING DATE
	wgrib -s $GRIBFILE | head -1 | cut -d: -f3 | cut -d= -f2 > date.txt
	# GETTING DATA
	wgrib $GRIBFILE > content.txt	
	
	cat content.txt | while read line
    	do
		code=`echo ${line} | gawk -F":" '{print $1}'` 
  		variable=`echo ${line} | gawk -F":" '{print $4}'`
  		level=`echo ${line} | gawk -F":" '{print $12}' | gawk -F" " '{print $1}'`
		
		if [ $variable = "SKT" -a $level = "sfc" ]
		then
			echo $code $variable $level
      			echo " Dumping SST/TS from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SST_TS$EXTEN > /dev/null
  		fi
  		
		if [ $variable = "STL1" -a $level = "0-7" ] 
		then
			echo $code $variable $level
		      	echo " Dumping SOILT level 1 from gribfile "
		      	wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILT1$EXTEN > /dev/null
		fi
		
  		if [ $variable = "STL2" -a $level = "7-28" ] 
  		then
  			echo $code $variable $level
      			echo " Dumping SOILT level 2 from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILT2$EXTEN > /dev/null
  		fi
  		
  		if [ $variable = "STL3" -a $level = "28-100" ] 
  		then
  			echo $code $variable $level
		      	echo " Dumping SOILT level 3 from gribfile "
		      	wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILT3$EXTEN > /dev/null
		fi
		
		if [ $variable = "STL4" -a $level = "100-255" ] 
		then
			echo $code $variable $level
      			echo " Dumping SOILT level 4 from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILT4$EXTEN > /dev/null
  		fi
  	
  		if [ $variable = "SWVL1" -a $level = "0-7" ] 
		then
			echo $code $variable $level
		      	echo " Dumping SOILW level 1 from gribfile "
		      	wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILW1$EXTEN > /dev/null
		fi
		
  		if [ $variable = "SWVL2" -a $level = "7-28" ] 
  		then
  			echo $code $variable $level
      			echo " Dumping SOILW level 2 from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILW2$EXTEN > /dev/null
  		fi
  		
  		if [ $variable = "SWVL3" -a $level = "28-100" ]
  		then
  			echo $code $variable $level
		      	echo " Dumping SOILW level 3 from gribfile "
		      	wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILW3$EXTEN > /dev/null
		fi
		
		if [ $variable = "SWVL4" -a $level = "100-255" ] 
		then
			echo $code $variable $level
      			echo " Dumping SOILW level 4 from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_SOILW4$EXTEN > /dev/null
  		fi

  		if [ $variable = "SD" -a $level = "sfc" ] 
  		then
  			echo $code $variable $level
      			echo " Dumping SNOW from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_WEASD$EXTEN > /dev/null
  		fi

  		if [ $variable = "CI" -a $level = "sfc" ] 
  		then
  			echo $code $variable $level
      			echo " Dumping SEAICE from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_ICEC$EXTEN > /dev/null
  		fi

  		if [ $variable = "MSL" -a $level = "sfc" ] 
   		then
   			echo $code $variable $level
      			echo " Dumping PRMSL from gribfile "
      			wgrib -d $code $GRIBFILE -o $OUTDIR/"$tval"_PRMSL$EXTEN > /dev/null
      		fi
  	done	

	tac content.txt | while read line
    	do
    		code=`echo ${line} | gawk -F":" '{print $1}'` 
  		variable=`echo ${line} | gawk -F":" '{print $4}'`
  		level=`echo ${line} | gawk -F":" '{print $12}' | gawk -F" " '{print $1}'`
  	        units=`echo ${line} | gawk -F":" '{print $12}' | gawk -F" " '{print $2}'`

  		if [ "$variable" = "U" -a "$units" = "mb" ] 
  		then
    			for levs in 1000 950 925 900 850 800 700 600 500 400 300 250 200 150 100 70 50 30 20 10 7 5 3 2 1
    			do
    				if [ $level = $levs ]
    				then
					echo $code $variable $level $units $levs
					echo " Dumping Upper level fields U Level: " $levs
      					wgrib -d $code $GRIBFILE -append -o $OUTDIR/"$tval"_UU$EXTEN > /dev/null
    				fi
    			done
  		fi
  		
  		if [ "$variable" = "V" -a "$units" = "mb" ] 
  		then
    			for levs in 1000 950 925 900 850 800 700 600 500 400 300 250 200 150 100 70 50 30 20 10 7 5 3 2 1
    			do
    				if [ $level = $levs ]
    				then
					echo $code $variable $level $levs
		      			echo " Dumping Upper level fields V Level: " $levs
		      			wgrib -d $code $GRIBFILE -append -o $OUTDIR/"$tval"_VV$EXTEN > /dev/null
		    		fi
		    	done
		fi
		
		if [ "$variable" = "T" -a "$units" = "mb" ] 
  		then
    			for levs in 1000 950 925 900 850 800 700 600 500 400 300 250 200 150 100 70 50 30 20 10 7 5 3 2 1
    			do
    				if [ $level = $levs ]
    				then
					echo $code $variable $level $levs
		      			echo " Dumping Upper level fields T Level: " $levs
		      			wgrib -d $code $GRIBFILE -append -o $OUTDIR/"$tval"_TT$EXTEN > /dev/null
		    		fi
		    	done
		fi   

		if [ "$variable" = "Z" -a "$units" = "mb" ] 
  		then
    			for levs in 1000 950 925 900 850 800 700 600 500 400 300 250 200 150 100 70 50 30 20 10 7 5 3 2 1
    			do
    				if [ $level = $levs ]
    				then
					echo $code $variable $level $levs
		      			echo " Dumping Upper level fields HGT Level: " $levs
		      			wgrib -d $code $GRIBFILE -append -o $OUTDIR/"$tval"_HH$EXTEN > /dev/null
		    		fi
		    	done
		fi  

		if [ "$variable" = "R" -a "$units" = "mb" ] 
  		then
    			for levs in 1000 950 925 900 850 800 700 600 500 400 300 250 200 150 100 70 50 30 20 10 7 5 3 2 1
    			do
    				if [ $level = $levs ]
    				then
					echo $code $variable $level $levs
		      			echo " Dumping Upper level fields RH Level: " $levs
		      			wgrib -d $code $GRIBFILE -append -o $OUTDIR/"$tval"_SH$EXTEN > /dev/null
		    		fi
		    	done
		fi  	

    	done

	rm content.txt

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

	echo " "
	echo " Start degrib to formatted data conversion process for timestep " $tval
	echo " "
 
	$ROOT/ifs2model_rrtm.exe $tval >> gfsconv.out || exit 1

	#this might be big, let's first list, than remove it
	ls -l $OUTDIR/*$EXTEN
	rm -f $OUTDIR/*$EXTEN

	ls -1 $OUTDIR/*.gfs > flist

	tval_count=$(($tval_count + $int))

done

rm gfsconv.out
rm date.txt
rm $ROOT/ifs2model_rrtm.exe

exit
