#!/bin/sh


input_file=$1
gfs_file=$2

outdir=.

# Define temp files
geohgt_key="HGT:surface"
geohgt_file=$outdir/geohgt_grib2.dat

tmpsfc_key="TMP:surface"
tmpsfc_file=$outdir/tmp_sfc_grib2.dat

tmpbgr_key="TMP:0-0.1 m below ground"
tmpbgr_file=$outdir/tmp_bgr_grib2.dat

press_key="PRES:surface"
press_file=$outdir/press_grib2.dat

rh_key="RH:2 m above ground"
rh_file=$outdir/rh_grib2.dat

pwater_key="PWAT:entire atmosphere"
pwater_file=$outdir/p_water_grib2.dat

cwater_key="CWAT:entire atmosphere"
cwater_file=$outdir/c_water_grib2.dat

soilw_key="SOILW:0-0.1 m below ground"
soilw_file=$outdir/soil_w_grib2.dat

uwind_key="UGRD:10 m above ground"
uwind_file=$outdir/u_wind_grib2.dat

vwind_key="VGRD:10 m above ground"
vwind_file=$outdir/v_wind_grib2.dat

#  Geopotential Height, surface
wgrib2 $input_file -match "$geohgt_key" -bin $geohgt_file
gtime=$(wgrib2 $input_file -t -match "$geohgt_key")

#  Temperature,surface
wgrib2 $input_file -match "$tmpsfc_key" -bin $tmpsfc_file
gtime=$(wgrib2 $input_file -t -match "$tmpsfc_key")

#  Temperature,0-0.1 m below ground
wgrib2 $input_file -match "$tmpbgr_key" -bin $tmpbgr_file
gtime=$(wgrib2 $input_file -t -match "$tmpbgr_key")
if [ ! -s $tmpbgr_file ]
then
    tmpbgr_key="TSOIL:0-0.1 m below ground"
    wgrib2 $input_file -match "$tmpbgr_key" -bin $tmpbgr_file
    gtime=$(wgrib2 $input_file -t -match "$tmpbgr_key")
fi

#  Surface Pressure
wgrib2 $input_file -match "$press_key" -bin $press_file
gtime=$(wgrib2 $input_file -t -match "$press_key")

#  Surface Relative Humidity
wgrib2 $input_file -match "$rh_key" -bin $rh_file
gtime=$(wgrib2 $input_file -t -match "$rh_key")

#  Precipitable water
wgrib2 $input_file -match "$pwater_key" -bin $pwater_file
gtime=$(wgrib2 $input_file -t -match "$pwater_key")

#  Cloud water
wgrib2 $input_file -match "$cwater_key" -bin $cwater_file
gtime=$(wgrib2 $input_file -t -match "$cwater_key")

#  Soil Moisture
wgrib2 $input_file -match "$soilw_key" -bin $soilw_file
gtime=$(wgrib2 $input_file -t -match "$soilw_key")

#  U wind
wgrib2 $input_file -match "$uwind_key" -bin $uwind_file
gtime=$(wgrib2 $input_file -t -match "$uwind_key")

#  V wind
wgrib2 $input_file -match "$vwind_key" -bin $vwind_file
gtime=$(wgrib2 $input_file -t -match "$vwind_key")


#Profile datasets

# Geopotential Height
geohgt_prl_file=$outdir/geohgt_prl_grib2.dat

wgrib2 $input_file -match "HGT:1000 mb" -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:975 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:950 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:925 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:900 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:850 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:800 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:750 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:700 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:650 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:600 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:550 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:500 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:450 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:400 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:350 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:300 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:250 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:200 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:150 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:100 mb" -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:70 mb"  -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:50 mb"  -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:30 mb"  -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:20 mb"  -append -bin $geohgt_prl_file
wgrib2 $input_file -match "HGT:10 mb"  -append -bin $geohgt_prl_file


# Temperature
tmp_prl_file=$outdir/tmp_prl_grib2.dat

wgrib2 $input_file -match "TMP:1000 mb" -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:975 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:950 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:925 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:900 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:850 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:800 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:750 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:700 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:650 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:600 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:550 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:500 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:450 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:400 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:350 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:300 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:250 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:200 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:150 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:100 mb" -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:70 mb"  -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:50 mb"  -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:30 mb"  -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:20 mb"  -append -bin $tmp_prl_file
wgrib2 $input_file -match "TMP:10 mb"  -append -bin $tmp_prl_file


# Relative Humidity
rh_prl_file=$outdir/rh_prl_grib2.dat

wgrib2 $input_file -match "RH:1000 mb" -bin $rh_prl_file
wgrib2 $input_file -match "RH:975 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:950 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:925 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:900 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:850 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:800 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:750 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:700 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:650 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:600 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:550 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:500 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:450 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:400 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:350 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:300 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:250 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:200 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:150 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:100 mb" -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:70 mb"  -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:50 mb"  -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:30 mb"  -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:20 mb"  -append -bin $rh_prl_file
wgrib2 $input_file -match "RH:10 mb"  -append -bin $rh_prl_file


# Cloud Mixing Ratio
clwmr_prl_file=$outdir/clwmr_prl_grib2.dat

wgrib2 $input_file -match "CLWMR:1000 mb" -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:975 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:950 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:925 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:900 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:850 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:800 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:750 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:700 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:650 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:600 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:550 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:500 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:450 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:400 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:350 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:300 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:250 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:200 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:150 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:100 mb" -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:70 mb"  -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:50 mb"  -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:30 mb"  -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:20 mb"  -append -bin $clwmr_prl_file
wgrib2 $input_file -match "CLWMR:10 mb"  -append -bin $clwmr_prl_file


#  Temperature,surface (GFS)
#wgrib2 $gfs_file -match "TMP:surface" -bin $outdir/tmp_sfc_gfs_grib2.dat

#  Soil Moisture, below surface (GFS)
#wgrib2 $gfs_file -match "SOILW:0-0.1 m below ground" \
#-bin $outdir/soilw_gfs_grib2.dat

#  Snow (GFS)
if [ $gfs_file != "NONE" ]
then
    echo gfs_file: $gfs_file
    wgrib2 $gfs_file -match "WEASD:surface" -bin $outdir/snow_gfs_grib2.dat

# Not currently used
#  Temperature, below surface (GFS)
    wgrib2 $gfs_file -match "TMP:0-0.1 m below ground" -bin $outdir/tmp_bsfc_gfs_grib2.dat

    if [ ! -s "$outdir/tmp_bsfc_gfs_grib2.dat" ]
    then
        wgrib2 $gfs_file -match "TSOIL:0-0.1 m below ground" -bin $outdir/tmp_bsfc_gfs_grib2.dat
    fi

fi

#  Precipitable water, entire atmosphere (GFS)
#wgrib2 $gfs_file -match "PWAT:entire atmosphere" \
#-bin $outdir/pwater_gfs_grib2.dat


echo gtime: $gtime

if [ $gfs_file != "NONE" ]
then
    echo ancnrt_aquarius junk \
	./geohgt_grib2.dat ./tmp_sfc_grib2.dat ./press_grib2.dat ./rh_grib2.dat \
	./p_water_grib2.dat ./c_water_grib2.dat ./soil_w_grib2.dat \
	./u_wind_grib2.dat ./v_wind_grib2.dat \
	./geohgt_prl_grib2.dat ./tmp_prl_grib2.dat \
	./rh_prl_grib2.dat ./clwmr_prl_grib2.dat \
	./snow_gfs_grib2.dat tmp_bgr_grib2.dat \
	./ -2 $gtime

    ancnrt_aquarius junk \
	./geohgt_grib2.dat ./tmp_sfc_grib2.dat ./press_grib2.dat ./rh_grib2.dat \
	./p_water_grib2.dat ./c_water_grib2.dat ./soil_w_grib2.dat \
	./u_wind_grib2.dat ./v_wind_grib2.dat \
	./geohgt_prl_grib2.dat ./tmp_prl_grib2.dat \
	./rh_prl_grib2.dat ./clwmr_prl_grib2.dat \
	./snow_gfs_grib2.dat tmp_bgr_grib2.dat \
	./ -2 $gtime
else
    echo ancnrt_aquarius junk \
	./geohgt_grib2.dat ./tmp_sfc_grib2.dat ./press_grib2.dat ./rh_grib2.dat \
	./p_water_grib2.dat ./c_water_grib2.dat ./soil_w_grib2.dat \
	./u_wind_grib2.dat ./v_wind_grib2.dat \
	./geohgt_prl_grib2.dat ./tmp_prl_grib2.dat \
	./rh_prl_grib2.dat ./clwmr_prl_grib2.dat \
        NONE NONE \
	./ -2 $gtime

    ancnrt_aquarius junk \
	./geohgt_grib2.dat ./tmp_sfc_grib2.dat ./press_grib2.dat ./rh_grib2.dat \
	./p_water_grib2.dat ./c_water_grib2.dat ./soil_w_grib2.dat \
	./u_wind_grib2.dat ./v_wind_grib2.dat \
	./geohgt_prl_grib2.dat ./tmp_prl_grib2.dat \
	./rh_prl_grib2.dat ./clwmr_prl_grib2.dat \
        NONE NONE \
	./ -2 $gtime
fi

#./pwater_gfs_grib2.dat \

#./tmp_sfc_gfs_grib2.dat ./tmp_bsfc_gfs_grib2.dat \
#./soilw_gfs_grib2.dat \

rm ./geohgt_grib2.dat ./tmp_sfc_grib2.dat ./press_grib2.dat ./rh_grib2.dat \
./p_water_grib2.dat ./c_water_grib2.dat ./soil_w_grib2.dat \
./u_wind_grib2.dat ./v_wind_grib2.dat \
./geohgt_prl_grib2.dat ./tmp_prl_grib2.dat \
./rh_prl_grib2.dat ./clwmr_prl_grib2.dat ./tmp_bsfc_gfs_grib2.dat ./tmp_bgr_grib2.dat \

if [ $gfs_file != "NONE" ]
then
    rm ./snow_gfs_grib2.dat
fi

#./tmp_sfc_gfs_grib2.dat
#./soilw_gfs_grib2.dat \

#./pwater_gfs_grib2.dat

exit

