mrayson / sfoda

Stuff for Ocean Data Analysis (time-series, ocean model IO, mapping, etc)
Other
4 stars 1 forks source link

contourf breaking with suntans accessor #10

Open apatlpo opened 1 year ago

apatlpo commented 1 year ago

contourf breaks when called via the suntans accessor:

ds = xr.open_zarr(...)

da = ds["eta"][1, ...]
vmin, vmax = float(da.min()), float(da.max())
lvls = np.arange(vmin,vmax,.01)
fig, ax, tri, cbar = ds.suntans.contourf(da, clevs=lvls, cmap="RdBu")

produces

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 6
      4 lvls = np.arange(-1,1,.1)
      5 lvls = np.arange(vmin,vmax,.01)
----> 6 fig, ax, tri, cbar = ds.suntans.contourf(da, clevs=lvls, cmap="RdBu")

File ~/nwa/nwatools/uplot.py:143, in Plot.contourf(self, zdata, clevs, xlims, ylims, colorbar, filled, **kwargs)
    137 def contourf(self, zdata, clevs=20, \
    138     xlims=None, ylims=None, colorbar=True, filled=True,\
    139     **kwargs):
    140     """
    141     Filled contour plot
    142     """
--> 143     self.build_tri_voronoi()
    145     fig = plt.gcf()
    146     ax = fig.gca()

File ~/nwa/nwatools/uplot.py:294, in Plot.build_tri_voronoi(self)
    292     print('Computing delaunay triangulation and computing mask...')
    293 pts = np.vstack([self.xv,self.yv]).T
--> 294 D = spatial.Delaunay(pts)
    295 self._triv =tri.Triangulation(self.xv,self.yv,D.simplices)
    297 # Compute a mask by removing triangles outside of the polygon

File _qhull.pyx:1841, in scipy.spatial._qhull.Delaunay.__init__()

File _qhull.pyx:280, in scipy.spatial._qhull._Qhull.__init__()

ValueError: Points cannot contain NaN

Pocking around a bit indicates self.xv and self.yv are both None.

A quick fix seems to assign values in the suntans accessor instantiation, e.g.:

        UPlot.__init__(self, self._obj['xp'].values, 
                       self._obj['yp'].values, self._obj['cells'].values, 
                       nfaces=self._obj['nfaces'].values,
            _FillValue=-999999)
        self.xv = self._obj['xv'].values
        self.yv = self._obj['yv'].values