import xarray as xr
import numpy as np
import cfgrib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
import metpy.calc as mpcalc
import geocat.comp
import cmaps
from metpy.units import units
shpName = 'Province_9.shp'
reader = Reader(shpName)
provinces = cfeature.ShapelyFeature(reader.geometries(), ccrs.PlateCarree(), edgecolor='k', facecolor='none')
levels = [1000., 975., 950., 925., 900., 850., 800., 750., 700., 650.,
600., 550., 500., 450., 400., 350., 300., 250., 200.]
ptop = 200*100
g = 9.8
files = 'ERA5/'
file = 'pl_2014070506.grib'
file_sl = "sl_2014070506.grib"
ds = cfgrib.open_datasets(files+file)
ds_sl = cfgrib.open_datasets(files+file_sl)
u_levels = ds[0].u.sel(isobaricInhPa=levels)
v_levels = ds[0].v.sel(isobaricInhPa=levels)
q_levels = ds[0].q.sel(isobaricInhPa=levels)
psfc = ds_sl[4].sp
plev = u_levels.coords["isobaricInhPa"] * 100
plev.attrs['units'] = 'Pa'
q_levels = q_levels * 1000
q_levels.attrs['units'] = 'g/kg'
dp = geocat.comp.dpres_plevel(plev, psfc, ptop)
dpg = dp / g
dpg.attrs["units"] = "kg/m2"
uq = u_levels * q_levels
vq = v_levels * q_levels
uq.attrs["units"] = "("+u_levels.units+")("+q_levels.units+")"
vq.attrs["units"] = "("+v_levels.units+")("+q_levels.units+")"
uq_dpg = uq * dpg.data
vq_dpg = vq * dpg.data
iuq = uq_dpg.sum(axis=0)
ivq = vq_dpg.sum(axis=0)
iuq.attrs["units"] = "[m/s][g/kg]"
ivq.attrs["units"] = "[m/s][g/kg]"
lons = u_levels.coords['longitude'][:]
lats = v_levels.coords['latitude'][:]
dx, dy = mpcalc.lat_lon_grid_deltas(lons, lats)
duvq = mpcalc.divergence(uq/g, vq/g, dx=dx[np.newaxis,:,:], dy=dy[np.newaxis,:,:])
duvq.attrs["units"] = "g/(kg-s)"
duvq_dpg = duvq * dpg.data
iduvq = duvq_dpg.sum(axis=0)
iduvq = iduvq * units('kg/m^2')
iduvq.attrs["units"] = "g/(m2-s)"
leftlon, rightlon, lowerlat, upperlat = 80, 100, 20, 40
img_extent = [leftlon, rightlon, lowerlat, upperlat]
levs = np.arange(-0.3, 0.3+0.05, 0.05)
fig = plt.figure(figsize=(12,9))
ax = plt.subplot(111, projection=ccrs.PlateCarree())
ax.set_extent(img_extent)
ax.coastlines('50m', linewidth=0.8)
mfc_contourf = ax.contourf(lons, lats,
iduvq,
cmap=cmaps.MPL_seismic,
levels=levs,
extend='both', transform=ccrs.PlateCarree())
mf_stream = ax.streamplot(lons, lats,
iuq.data, ivq.data,
color='black', transform=ccrs.PlateCarree())
gl = ax.gridlines(draw_labels=True, dms=False,
linestyle='--', color='gray',
x_inline=False, y_inline=False)
gl.top_labels = False
gl.right_labels = False
ax.add_feature(provinces, linewidth=1)
position = fig.add_axes([ax.get_position().x0,
ax.get_position().y0-0.08,
ax.get_position().width,
0.02])
cb = fig.colorbar(mfc_contourf, orientation='horizontal', cax=position)
cb.set_label('%s'%iduvq.units, fontsize=12)
plt.savefig('./plo99.png',dpi=800,bbox_inches='tight',pad_inches=0)
plt.show()