crashing while plotting - basemap/cartopy
Some users are experiencing crashing while plotting spatial maps. This is not a problem with PseudoNetCDF. This is often due to conflicting libraries associated with basemap and cartopy in anaconda.
- If
basemapalone is installed, everything works well.PseudoNetCDFfindsbasemapand plots accordingly. - If
cartopyis installed alone, PseudoNetCDF doesn't add maps. - If both are installed, the
basemaplibgeos expected version is not available (replaced by thecartopyversion) and the system may crash.
Basically, it is possible to create an environment where basemap is available but will fail. Because this is a deep library issue, it does not just error and return control to python. Instead, the system crashes. This is an environment problem, not a PseudoNetCDF problem.
There are two easy work arounds. The plot command accepts map_kw=False to disable mapping. This will work, but you get no map.
A work around that includes a map uses pycno. pycno is easily installable with pip and only requires pyproj for projected maps. pyproj is also easily installable with pip. See the pycno documentation for lat/long and projected plots. For projection plots and IOAPI files, you can define the projection using the PseudoNetCDF.getproj method with the withgrid=True option.
import PseudoNetCDF as pnc
import matplotlib.pyplot as plt
import pycno
cmaqpath= '...'
cmaqf = pnc.pncopen(cmaqpath, format='ioapi')
proj = cmaqf.getproj(withgrid=True)
cno = pycno.cno(proj=proj)
fig, ax = plt.subplots()
qkey = 'O3'
qvar = cmaqf.variables[qkey]
p = ax.pcolormesh(qvar[0, 0])
cno.draw(ax=ax)
fig.colorbar(p, label=qvar.units.strip())
fig.savefig(f'{qkey}.png')```
p.s., I think this is related to incompatible GEOS libraries, which should only matter when shapely optimizations are on... maybe. Maybe try import shapely; shapely.speedups.disable() before running with cartopy and see if that helps