#----------------------------------------------------------------------
# Read and plot the given 2D variable read off a GRIB2 file 
# using PyNIO / PyNGL.
#
# See grib2_plot_var_mpl.py for a matplotlib/basemap example.
#----------------------------------------------------------------------
import numpy as np
import Nio, Ngl

# Read data
dir      = "../Data/"
filename = "gdas1.t18z.sfluxgrbf03.grib2"
a   = Nio.open_file("%s/%s" %(dir,filename))

var_name = "TMP_P0_L103_GGA0"
var = a.variables[var_name][:]
lat = a.variables["lat_0"][:]
lon = a.variables["lon_0"][:]

title = ("%s : %s") % (filename,var_name)

#  print var.min(),var.max()

wks = Ngl.open_wks("png","grib2_plot_%s_ngl" % var_name)

#---Set some plot options
res                   = Ngl.Resources()

# Contour options
res.cnFillOn           = True          # turn on contour fill
res.cnLinesOn          = False         # turn off contour lines
res.cnLineLabelsOn     = False         # turn off line labels
res.cnFillPalette      = "MPL_jet"
res.cnFillMode         = "RasterFill"  # Can be much faster than "AreaFill"

res.sfXArray           = lon
res.sfYArray           = lat

res.lbOrientation      = "horizontal"
res.lbLabelFontHeightF = 0.01

# Main Title
res.tiMainString          = title
res.tiMainFontHeightF     = 0.015

plot = Ngl.contour_map(wks,var,res)

Ngl.end()

