#ksh
# Script that converts SMOS NRT BUFR product in GRIB
# Patricia de Rosnay (Patricia.Rosnay@ecmwf.int)
# October 2011
##################################################################
# Requested tools:
# Metview: http://www.ecmwf.int/publications/manuals/metview/index.html
# SCIN: ECMWF interpolation software. Not yet available outside ECMWF, 
# so skip this part if not at ECMWF


bin_angle="1"         # define binning size (will use angle +/-bin_angle)
reg_grid_resol="0.25" # regular grid output resolution- to be defined in all cases
#grid="regular"        # Convert BUFR to GRIB on a regular grid. Select if do not have scin available
grid="gaussian"       # Convert BUFR to GRIB on a Gaussian Reduced grid. select only if scin installed on your computer
gr_resol="256"        # Choose the Gaussian grid resolution (exple N256 for T511)
inputfile="smosbufr"  # Name of the input BUFR file
afov="yes"            # yes for alias free field of view (recommended), no for all data 

for AA in 30 # 40 50  # Select which angles to extract
do

################## No need to modify below this line


# Step 1: Convert BUFR to GRIB on a REGULAR Lat Lon (LL) GRID using Metview

  metview -b smos_bufr2gribLL.met ${inputfile} ${AA} ${bin_angle} ${reg_grid_resol} ${afov}

# Step 2: Interpolate regular grid to Reduced Gaussian using SCIN

if [ $grid = "gaussian" ]; then
export SCIN_DISABLE_LSM=1
export SCIN_DEBUG=1
export SCIN_IGNORE_PARAM=1

# convert all the output files on a GR grid:
for outfile_LL in `ls ${inputfile}*grib` ; do
scin -i ${outfile_LL}  -o  GR_N${gr_resol}_${outfile_LL} -g ${gr_resol} -y reduced

# remove the intermediate Lat Lon file since Gaussian grid was requested.
rm -f ${outfile_LL}
done
fi


done # angle

