Skip to content

Instantly share code, notes, and snippets.

@perillaroc
Created September 23, 2018 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perillaroc/398827bd2cd75f4551ccdddfd018bba1 to your computer and use it in GitHub Desktop.
Save perillaroc/398827bd2cd75f4551ccdddfd018bba1 to your computer and use it in GitHub Desktop.
Use Matplotlib to draw a contour plot
# coding: utf-8
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import nuwe_pyeccodes
file_path = "some/file/path/to/gmf.gra.2018081800003.grb2"
message_number = 85
grib_file = nuwe_pyeccodes.GribFileHandler()
grib_file.openFile(file_path)
grib_message = None
for i in range(0, message_number):
grib_message = grib_file.next()
left_lon = grib_message.getDouble('longitudeOfFirstGridPointInDegrees')
right_lon = grib_message.getDouble('longitudeOfLastGridPointInDegrees')
lon_step = grib_message.getDouble('iDirectionIncrementInDegrees')
nx = grib_message.getLong('Ni')
lon_array = np.arange(left_lon, right_lon + lon_step / 2, lon_step)
top_lat = grib_message.getDouble('latitudeOfFirstGridPointInDegrees')
bottom_lat = grib_message.getDouble('latitudeOfLastGridPointInDegrees')
lat_step = grib_message.getDouble('jDirectionIncrementInDegrees')
ny = grib_message.getLong('Nj')
lat_array = np.arange(top_lat, bottom_lat - lat_step / 2, -lat_step)
lons, lats = np.meshgrid(lon_array, lat_array)
values = grib_message.getDoubleArray('values')
grid_values = values.reshape(ny, nx)
ax = plt.subplot(111, projection=ccrs.PlateCarree(central_longitude=180))
ax.contourf(
lons, lats, grid_values,
transform=ccrs.PlateCarree(),
cmap='rainbow'
)
ax.coastlines()
ax.gridlines()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment