#----------------------------------------------------------------------
# Read and plots the given 2D variable read off a GRIB1 file 
# using PyNIO/matplotlib.
#
# Curvilinear grid, 113 x 151
#----------------------------------------------------------------------
import numpy as np
import Nio, Ngl

# Read data
dir      = "../Data/"
filename = "ruc.grb"
a   = Nio.open_file("%s/%s" %(dir,filename))
lat = a.variables["gridlat_236"][:]
lon = a.variables["gridlon_236"][:]

var_name = "PRES_236_MTHE"           # Pick a variable to plot
var      = a.variables[var_name][:]
#  print var.min(),var.max()

wks = Ngl.open_wks("png","grib1_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.lbOrientation      = "horizontal"

res.sfXArray           = lon
res.sfYArray           = lat

#---Map options
res.mpLimitMode        = "LatLon"
res.mpMinLatF          = lat.min()-1
res.mpMaxLatF          = lat.max()+1
res.mpMinLonF          = lon.min()-1
res.mpMaxLonF          = lon.max()+1
res.mpDataBaseVersion  = "MediumRes"

#---Main Title
res.tiMainString          = ("%s : %s") % (filename,var_name)
#res.tiMainFontHeightF     = 0.015

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

Ngl.end()

