""" Fix for bad longitudes in the reanalysis Med grib files This routine uses the pygrib module, available here https://github.com/jswhit/pygrib """ import pygrib3 import numpy as np from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt # open the grib file and pick off the second record # (the values for the first record are all zeroes) gribs=pygrib.open('multi_reanal.med_10m.hs.197901.grb2') grb=gribs[2] data=grb.values # extract the grib lat/lon values lat, lon = grb.latlons() # get some of the grib settings. The last grib longitude is ok lonmax=grb['longitudeOfLastGridPointInDegrees'] delta=grb['jDirectionIncrementInDegrees'] # build a new lon vector from the endpoint and delta increment lon2=np.arange(lonmax-delta*lon.shape[1],lonmax,delta) # pull a lat vector from the 2-d field lat2=lat[:,0] # now plot up some data on a map. This will create a PNG file # called med_map.png in your local directory. fig=plt.figure() m=Basemap(projection='mill',resolution='l', llcrnrlon=lon2.min(), urcrnrlon=lon2.max(), llcrnrlat=lat2.min(),urcrnrlat=lat2.max()) x,y=m(*np.meshgrid(lon2,lat2)) m.pcolormesh(x,y,data,shading='flat') m.drawcoastlines() m.fillcontinents() m.drawparallels(np.arange(-90,91,5),labels=[1,0,0,0]) m.drawmeridians(np.arange(-180,180,15),labels=[0,0,0,1]) m.colorbar() plt.title(grb.parameterName) plt.savefig('med_map.png')