Repository navigation
Description
Version
v2023.08.4
How did you install UXarray?
Conda
What happened?
I am probably doing something unintelligent, but say I want to print the polylines assocated with a uxarray grid. In theory, this should work with mpl + cartopy...
import uxarray as ux
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
base_path = "./"
grid_path = base_path + "outCSne30.ug"
data_path = base_path + "outCSne30_vortex.nc"
uxds = ux.open_dataset(grid_path, data_path)
projection = ccrs.PlateCarree() # also tried ccrs.Orthographic(central_longitude=0, central_latitude=90)
lc = uxds.uxgrid.to_linecollection(projection=projection,override=True)
#segments = lc.get_segments()
#print(segments)
fig, ax = plt.subplots(1, 1, figsize=(10, 10), constrained_layout=True,
subplot_kw={'projection': ccrs.Orthographic(central_longitude=0, central_latitude=90)})
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.COASTLINE)
ax.add_collection(lc)
ax.set_global()
plt.title("Grid Orthographic Projection")
plt.show()
It gives me the projection but a blank graph:
I can sneak around this by creating the LineCollection with uxarray and then unpacking it and redefining it with a PC projection. The below now gives me grid lines, but it doesn't appear to wrap accurately along the date line (-180/180).
import uxarray as ux
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.collections import LineCollection
base_path = "./"
grid_path = base_path + "outCSne30.ug"
data_path = base_path + "outCSne30_vortex.nc"
uxds = ux.open_dataset(grid_path, data_path)
lc_original = uxds.uxgrid.to_linecollection()
segments = lc_original.get_segments()
projection = ccrs.PlateCarree()
lc = LineCollection(segments, linewidths=0.5, colors='black', transform=projection)
fig, ax = plt.subplots(1, 1, figsize=(10, 10), constrained_layout=True,
subplot_kw={'projection': ccrs.Orthographic(central_longitude=0, central_latitude=90)})
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.COASTLINE)
ax.add_collection(lc)
ax.set_global()
plt.title("Grid Orthographic Projection")
plt.show()
So TL;DR two problems.
- Trouble getting to_linecollection() to be transformed correctly
- Lines not correctly wrapping around edges of mesh
If it's just something silly on my end (50/50 chance!), maybe we could add some additional lines to the mpl.ipynb notebook to help.
This may be tied to the periodic_elements option in to_polycollection() although the artifacts here aren't as pervasive.
What did you expect to happen?
Expected to use to_linecollection() to create a grid skeleton I could then draw with different projections using mpl + cartopy.
Can you provide a MCVE to repoduce the bug?
import uxarray as ux
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.collections import LineCollection
base_path = "./"
grid_path = base_path + "outCSne30.ug"
data_path = base_path + "outCSne30_vortex.nc"
uxds = ux.open_dataset(grid_path, data_path)
lc_original = uxds.uxgrid.to_linecollection()
segments = lc_original.get_segments()
projection = ccrs.PlateCarree()
lc = LineCollection(segments, linewidths=0.5, colors='black', transform=projection)
fig, ax = plt.subplots(1, 1, figsize=(10, 10), constrained_layout=True,
subplot_kw={'projection': ccrs.Orthographic(central_longitude=0, central_latitude=90)})
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.COASTLINE)
ax.add_collection(lc)
ax.set_global()
plt.title("Grid Orthographic Projection")
plt.show()Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Projects
- StatusShow more project fields✅ Done
Activity
philipc2 commentedon Oct 1, 2024
Hi @zarzycki
By default, the values along the date line are being excluded, since they require additional processing to split when we convert our data into geometries. This is indicated by the
periodic_elementsparameter.Here's a reference.
This works with your work around.
There appears to be a bug in the projection code. I'll get this fixed as part of #922
zarzycki commentedon Oct 1, 2024
Thanks @philipc2!
The user API doesn't mention periodic_elements: https://uxarray.readthedocs.io/en/v2024.02.0/user_api/generated/uxarray.Grid.to_linecollection.html
Although I thought I tried it and got an error. Maybe I messed something up...
FWIW, while that plot looks better, there does appear to be a weird collection of line segments right at the pole. Any idea if that's the mesh or the plotting? I can look later when I have some free time, too.
philipc2 commentedon Oct 1, 2024
That appears to be an older release. here's our latest documentation:
https://uxarray.readthedocs.io/en/v2024.08.2/user_api/generated/uxarray.Grid.to_linecollection.html
That's most likely a slight artifact of the split polygons. I can look into it further.
zarzycki commentedon Oct 1, 2024
Gah, I blame Google but I should have checked that.