#----------------------------------------------------------------------
# Read and plots GRIB2 data using PyNIO/matplotlib.
#
# Curvilinear grid, 461 x 421
#----------------------------------------------------------------------
import numpy as np
import Nio
from mpl_toolkits.basemap import Basemap, cm
import matplotlib.pyplot as plt

#---Read data
dir      = "./"
filename = "MET9_IR108_cosmode_0909210000.grb2"
a    = Nio.open_file(dir+filename)
sbt  = a.variables["SBTMP_P31_GRLL0"][:]
lat  = a.variables["gridlat_0"][:]
lon  = a.variables["gridlon_0"][:]

# make figure
fig = plt.figure(figsize = (15,7))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
ax.set_title("%s" % filename)

map = Basemap(projection='cyl',llcrnrlat=lat.min()-1,urcrnrlat=lat.max()+1,
              resolution='l',  llcrnrlon=lon.min()-1,urcrnrlon=lon.max()+1)

x, y = map(lon,lat)

# Add map stuff
map.drawcoastlines()
map.drawparallels(np.arange(-90.,90.,5.), labels=[1,0,0,0])
map.drawmeridians(np.arange(-180.,180.,5.), labels=[0,0,0,1])

# Add contours
clevs = np.arange(10, 210, 10)
cf = map.contourf(x,y,sbt,clevs,cmap=plt.cm.jet,)

# Add colorbar
cb = map.colorbar(cf,"bottom", size="7%", pad="10%")

# Draw plot
#plt.show()
fig.savefig("grib2_plot_unrotated_mpl.png",bbox_inches='tight')
plt.close(fig)
