def contourf(self, var='depth', it=None, **kwargs): x = kwargs.get('x',self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get('y',self._obj.SCHISM_hgrid_node_y[:].values) try: t = kwargs.get('t',self._obj.time.values) except: pass tes = kwargs.get('tri3',self._obj.SCHISM_hgrid_face_nodes.values[:,:4]) # sort out quads try: mask = np.isnan(tes)[:,3] tr3 = tes[mask][:,:3] tr3_ = quads_to_tris(tes[~mask]) tri3 = np.append(tr3,tr3_,axis=0).astype(int) except: tri3 = tes.astype(int) z = kwargs.get('z',self._obj[var].values[it,:].flatten()) nodes = pd.DataFrame({'longitude':x,'latitude':y, '{}'.format(var):z}) elems = pd.DataFrame(tri3, columns=['a', 'b', 'c']) width = kwargs.get('width', 800) height = kwargs.get('height', 600) opts.defaults(opts.WMTS(width=width, height=height)) tiles = gv.WMTS('https://maps.wikimedia.org/osm-intl/{Z}/{X}/{Y}@2x.png') points = gv.operation.project_points(gv.Points(nodes, vdims=['{}'.format(var)])) trimesh = gv.TriMesh((elems, points)) return tiles * rasterize(trimesh, aggregator='mean').opts(colorbar=True, cmap='Viridis', padding=.1, tools=['hover'])
def frames(self,var='depth', tiles=False, **kwargs): x = kwargs.get('x',self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get('y',self._obj.SCHISM_hgrid_node_y[:].values) try: t = kwargs.get('t',self._obj.time.values) except: pass tes = kwargs.get('tri3',self._obj.SCHISM_hgrid_face_nodes.values[:,:4]) # sort out quads try: mask = np.isnan(tes)[:,3] tr3 = tes[mask][:,:3] tr3_ = quads_to_tris(tes[~mask]) if tr3_ : tri3 = np.append(tr3,tr3_,axis=0).astype(int) else: tri3 = tr3.astype(int) except: tri3 = tes.astype(int) times=kwargs.get('times',self._obj.time.values) z = kwargs.get('z',self._obj[var].values[0,:].flatten()) zmin = self._obj[var].values.min() zmax = self._obj[var].values.max() nodes = pd.DataFrame({'longitude':x,'latitude':y, '{}'.format(var):z}) elems = pd.DataFrame(tri3, columns=['a', 'b', 'c']) width = kwargs.get('width', 800) height = kwargs.get('height', 600) opts.defaults(opts.WMTS(width=width, height=height)) tile = gv.WMTS('https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png') points = gv.operation.project_points(gv.Points(nodes, vdims=['{}'.format(var)])) trimesh = gv.TriMesh((elems, points)) def time_mesh(time): points.data[var] = self._obj[var].sel(time=time).values return gv.TriMesh((elems, points))#, crs=ccrs.GOOGLE_MERCATOR) meshes = hv.DynamicMap(time_mesh, kdims=['Time']).redim.values(Time=times) imesh = rasterize(meshes, aggregator='mean').opts(cmap='viridis', colorbar=True, padding=.1, tools=['hover'], clim=(zmin, zmax)) if tiles: return hv.output(tile*imesh, holomap='scrubber', fps=1) else: return hv.output(imesh.opts(width=width,height=height), holomap='scrubber', fps=1)
def frames(self, **kwargs): cr = kwargs.get('coastlines', None) c_attrs = kwargs.get('coastlines_attrs', {}) x = kwargs.get('x', self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get('y', self._obj.SCHISM_hgrid_node_y[:].values) t = kwargs.get('t', self._obj.time.values) tes = kwargs.get('tes', self._obj.SCHISM_hgrid_face_nodes.values[:, :4]) # sort out quads try: mask = np.isnan(tes)[:, 3] tr3 = tes[mask][:, :3] tr3_ = quads_to_tris(tes[~mask]) if tr3_: tri3 = np.append(tr3, tr3_, axis=0).astype(int) else: tri3 = tr3.astype(int) except: tri3 = tes.astype(int) var = kwargs.get('var', 'depth') z = kwargs.get('z', self._obj[var].values) # set figure size xr = x.max() - x.min() yr = y.max() - y.min() ratio = yr / xr xf = 12 yf = np.ceil(12 * ratio).astype(int) fig = plt.figure(figsize=(xf, yf)) vmin = kwargs.get('vmin', z.min()) vmax = kwargs.get('vmax', z.max()) nv = kwargs.get('nv', 10) title = kwargs.get('title', None) vrange = np.linspace(vmin, vmax, nv, endpoint=True) #optional mask for the data mask = kwargs.get('mask', None) if 'mask' in kwargs: z = np.ma.masked_array(z, mask) z = z.filled(fill_value=-99999) ## CHOOSE YOUR PROJECTION # ax = plt.axes(projection=ccrs.Orthographic(grid_x.mean(), grid_y.mean())) # ax = plt.axes(projection=ccrs.PlateCarree()) # ax.background_patch.set_facecolor('k') # Limit the extent of the map to a small longitude/latitude range. # ax.set_extent([x.min(), x.max(), y.min(), y.max()]) ax = plt.axes() ax.set_aspect('equal') ims = [] for i in range(len(t)): im = ax.tricontourf(x, y, tri3, z[i, :], vrange, vmin=vmin, vmax=vmax) #, transform=ccrs.PlateCarree()) # im = ax.contourf(x,y,z[i,:,:],v,vmin=v1,vmax=v2,latlon=True) add_arts = im.collections text = 'time={}'.format(t[i]) #te = ax.text(90, 90, text) an = ax.annotate(text, xy=(0.05, -.1), xycoords='axes fraction') ims.append(add_arts + [an]) # if cr is not None: TO DO # try: # coastl = gp.GeoDataFrame.from_file(cr) # except: # coastl = gp.GeoDataFrame(cr) # coastl.plot(ax=ax, **c_attrs) if title: ax.set_title(title) #ax.set_global() #ax.coastlines('50m') #cbar_ax = fig.add_axes([0.05, 0.05, 0.85, 0.05]) cbar = fig.colorbar(im, ticks=vrange, orientation='vertical', fraction=0.017, pad=0.04) #plt.colorbar() v = animation.ArtistAnimation(fig, ims, interval=200, blit=False, repeat=False) plt.close() return v
def contourf(self, it=None, **kwargs): x = kwargs.get('x', self._obj.SCHISM_hgrid_node_x[:].values) y = kwargs.get('y', self._obj.SCHISM_hgrid_node_y[:].values) try: t = kwargs.get('t', self._obj.time.values) except: pass tes = kwargs.get('tes', self._obj.SCHISM_hgrid_face_nodes.values[:, :4]) # sort out quads try: mask = np.isnan(tes)[:, 3] tr3 = tes[mask][:, :3] tr3_ = quads_to_tris(tes[~mask]) if tr3_: tri3 = np.append(tr3, tr3_, axis=0).astype(int) else: tri3 = tr3.astype(int) except: tri3 = tes.astype(int) var = kwargs.get('var', 'depth') z = kwargs.get('z', self._obj[var].values[it, :].flatten()) vmin = kwargs.get('vmin', z.min()) vmax = kwargs.get('vmax', z.max()) nv = kwargs.get('nv', 10) title = kwargs.get('title', 'contourf plot for {}'.format(var)) vrange = np.linspace(vmin, vmax, nv, endpoint=True) ## CHOOSE YOUR PROJECTION # ax = plt.axes(projection=ccrs.Orthographic(grid_x.mean(), grid_y.mean())) # [fig,ax] = kwargs.get('figure',[plt.figure(figsize=(12,8)),plt.axes(projection=ccrs.PlateCarree())]) # ax.set_extent([x.min(), x.max(), y.min(), y.max()]) # ax.background_patch.set_facecolor('k') fig = plt.figure(figsize=(12, 8)) ax = plt.axes() #optional mask for the data mask = kwargs.get('mask', None) if 'mask' in kwargs: z = np.ma.masked_array(z, mask) z = z.filled(fill_value=-99999) xy = kwargs.get('xy', (.3, 1.05)) for val in [ 'x', 'y', 't', 'it', 'z', 'vmin', 'vmax', 'title', 'nv', 'tes', 'mask', 'xy', 'var', 'figure' ]: try: del kwargs[val] except: pass ax.set_aspect('equal') p = ax.tricontourf(x, y, tri3, z, vrange, vmin=vmin, vmax=vmax, **kwargs) #, transform=ccrs.PlateCarree() ) cbar = fig.colorbar(p, ticks=vrange, orientation='vertical') if it: text = 'time={}'.format(t[it]) an = ax.annotate(text, xy=xy, xycoords='axes fraction') ax.set_title(title, pad=30) ax.set_xlabel('Longitude (degrees)') ax.set_ylabel('Latitude (degrees)') return p #fig, ax