def fig_dist(C,num=2,log=False,title="",local_max=False, direction=False): fig=plt.figure(num) fig.clf() fig.set_size_inches([6,9],forward=True) ax=fig.add_subplot(1,1,1) cax=fig.add_axes([0.05,0.6,0.03,0.35]) fig.subplots_adjust(left=0,right=1,top=1,bottom=0) if log: C=np.log10(C.clip(1e-10,np.inf)) label='log$_{10}$' else: label='linear' ccoll=g.plot_cells(values=C,ax=ax,cmap=cmap) ccoll.set_lw(0.05) ccoll.set_edgecolor('face') # plt.colorbar(ccoll,cax=cax,label=label) plot_utils.cbar(ccoll,cax=cax,label=label) if local_max: is_local_max=np.ones(g.Ncells(),np.bool8) e2c=g.edge_to_cells() internal=e2c.min(axis=1)>=0 c1=e2c[internal,0] c2=e2c[internal,1] c1_less=C[c1]<C[c2] is_local_max[ c1[c1_less] ]=False c2_less=C[c2]<C[c1] is_local_max[ c2[c2_less] ]=False cc=g.cells_center() ax.plot(cc[is_local_max,0],cc[is_local_max,1],'ko') if direction: nbrhood=4 idxs=np.arange(g.Ncells()) np.random.shuffle(idxs) samp_cells=idxs[:1000] cc=g.cells_center() XY=cc[samp_cells] UV=np.nan*XY for i,c in utils.progress(enumerate(samp_cells)): cells=[c] for _ in range(nbrhood): cells=np.unique(list(cells) + [c for c0 in cells for c in g.cell_to_cells(c0) if c>=0] ) #y=C[cells] #X=np.c_[cc[cells]-cc[cells].mean(axis=0), # np.ones(len(cells))] # location and bias #beta_hat=np.linalg.lstsq(X,y,rcond=None)[0] #UV[i,:]=beta_hat[:2] # [gradx,grady] low=cells[ np.argmin(C[cells]) ] high=cells[ np.argmax(C[cells]) ] UV[i,:]=(cc[high]-cc[low])/(C[high]-C[low]) ax.quiver( XY[:,0], XY[:,1], UV[:,0], UV[:,1],pivot='tip',scale=60,width=0.005) ax.xaxis.set_visible(0) ax.yaxis.set_visible(0) ax.axis('equal') ax.text(0.5,0.98,title,transform=ax.transAxes,va='top',ha='center') return fig
# 30s. comp_at_rma = mbf(rma_xyz[:, :2]) ## plt.figure(1).clf() fig, (ax, ax_diff) = plt.subplots(2, 1, num=1, sharex=True, sharey=True) coll1 = ax.scatter(rma_xyz[:, 0], rma_xyz[:, 1], 40, rma_xyz[:, 2], lw=0) coll2 = ax_diff.scatter(rma_xyz[:, 0], rma_xyz[:, 1], 40, comp_at_rma - rma_xyz[:, 2], lw=0) ax.axis('equal') ax.axis((572834., 598820., 4131083., 4161534.)) coll1.set_clim([-10, 5]) coll2.set_clim([-2, 2]) plot_utils.cbar(coll1, ax=ax) plot_utils.cbar(coll2, ax=ax_diff) ## # Similarly, bring in xyz points for LSB grid from stompy.model.delft import dfm_grid g = dfm_grid.DFMGrid()
## delta = xyz_samples[:, 2] - dem(xyz_samples[:, :2]) ## plt.figure(1).clf() fig, (ax, ax2) = plt.subplots(1, 2, num=1, sharex=True, sharey=True) fig.set_size_inches([10, 7], forward=True) scat = ax.scatter(xyz_samples[:, 0], xyz_samples[:, 1], 15, xyz_samples[:, 2], cmap='jet') plot_utils.cbar(scat, label='m NAVD88', ax=ax, orientation='horizontal') img = dem.plot(ax=ax, interpolation='nearest', cmap='jet') crange = [-6, 2.25] scat.set_clim(crange) img.set_clim(crange) div_cmap = scmap.load_gradient('ViBlGrWhYeOrRe.cpt') # And show the difference: scat = ax2.scatter(xyz_samples[:, 0], xyz_samples[:, 1], 15, delta, cmap='seismic')
ds=xr.open_dataset('sfei_v18_net.nc') # g=unstructured_grid.UnstructuredGrid.from_ugrid(ds) g=dfm_grid.DFMGrid(ds) ## plt.figure(10).clf() fig,ax=plt.subplots(num=10) g.plot_edges(lw=0.5,color='k') # just need to update NetNode_z: ncoll=g.plot_nodes(values=ds.NetNode_z) plot_utils.cbar(ncoll) ## rect_1=(593511.,594720.,4136812.,4137722.) rect_2=(589736., 591613., 4138290, 4139705) node_xy=np.c_[ds.NetNode_x.values,ds.NetNode_y.values] for xxyy in [rect_1,rect_2]: sel=utils.within_2d(node_xy,xxyy) ds.NetNode_z.values[sel]=-1 ## ds.to_netcdf('dredged-sfei_v18_net.nc', format='NETCDF3_CLASSIC')
## cell_values = np.zeros(iz.t_dual.Ncells(), 'f8') cell_values[:] = np.nan cell_values[iz.t_net.nodes['dual_cell']] = iz.t_net.nodes['int_value'] plt.figure(11).clf() fig, ax = plt.subplots(num=11) ccoll = iz.t_dual.plot_cells(values=cell_values, mask=np.isfinite(cell_values), clip=clip, cmap=cmap, ax=ax) ccoll.set_clim([-8, 3]) plot_utils.cbar(ccoll) ax.plot(xyz_input[:, 0], xyz_input[:, 1], 'k.', ms=0.3) ax.axis('equal') ax.axis((646917., 647721, 4185492., 4186106.)) ## f = iz.as_raster() # f.write_gdal("zhang-dem-bidir.tif") # f.write_gdal("zhang-dem.tif") # at least the bidirectional approach sufferrs from too-high # a search_n. Since it doesn't need a large number of samples
def set_lsb_bathy(grid): check_bathy() # First, load in the original sfb_dfm grid to get bathymetry log.info("Loading SFB DFM v2 grid") sfb_dfm_grid=xr.open_dataset('sfb_dfm/sfei_v19_net.nc') sfb_X=np.c_[ sfb_dfm_grid.NetNode_x.values, sfb_dfm_grid.NetNode_y.values] sfb_dfm_field=field.XYZField(X=sfb_X, F=sfb_dfm_grid.NetNode_z.values) lsb_X=grid.nodes['x'] # This will set all points within the convex hull of the original # grid. These elevations are used in the output (a) outside the # LSB merged_2m DEM, and (b) to prioritize which nodes to use when # moving depths from edges to nodes. # Could argue that it would be better to pull point elevations here # from the DEM where they overlap. Probably makes very little difference lsb_z=sfb_dfm_field(lsb_X) # still some nans at this point # log.info("Loading LSB dem from %s"%merged_2m_path) dem=field.GdalGrid(merged_2m_path) # # Use the 2m DEM to find an effective minimum depth for each edge covered # by the DEM. edge_min_depths=depth_connectivity.edge_connection_depth(grid,dem,centers='lowest') # move those edge depths to node depths node_depths=depth_connectivity.greedy_edgemin_to_node(grid,lsb_z,edge_min_depths) # Still have some nodes with nan depth, first try filling in with the DEM. missing=np.isnan(node_depths) node_depths[missing]=dem( lsb_X[missing,:] ) # And wrap it up with a more forgiving interpolation from the original sfb_dfm_v2 # grid (about 12 points on the convex hull) still_missing=np.isnan(node_depths) node_depths[still_missing]=sfb_dfm_field.interpolate( lsb_X[still_missing,:], 'nearest' ) assert np.isnan(node_depths).sum()==0 # Update the grid grid.nodes['depth']=node_depths if 0: # caller is going to deal with I/O out_file='lsb_v99_bathy_net.nc' os.path.exists(out_file) and os.unlink(out_file) dfm_grid.write_dfm(grid,out_file) if 0: # plot for development. plt.figure(10).clf() fig,ax=plt.subplots(num=10) edge_mins=grid.nodes['depth'][grid.edges['nodes']].min(axis=1) ecoll=grid.plot_edges(lw=1.5,values=edge_mins) ncoll=grid.plot_nodes(values=grid.nodes['depth']) plt.setp([ecoll,ncoll],clim=[-3,3]) plot_utils.cbar(ncoll,extras=[ecoll]) # Modified in place, but return just in case return grid # QED.
sch=unstructured_grid.UnstructuredGrid.from_ugrid('grid-sources/schout_161.nc') sch_ds=xr.open_dataset('grid-sources/schout_161.nc') ## zoom=(608898.0112964935, 631668.6427644436, 4265130.767323407, 4297737.460249973) plt.figure(20).clf() fig,axs=plt.subplots(1,3,num=20,sharex=True,sharey=True) colls=[] for ax,g,name in zip(axs, [r18,r17,sch], ["r18","r17","sch"]): g.plot_edges(ax=ax,lw=0.3,color='k',clip=zoom) if 'depth' in g.nodes.dtype.names: if name=='sch': sgn=-1 else: sgn=1 colls.append( g.plot_nodes(ax=ax,values=sgn*g.nodes['depth'],clip=zoom, cmap='jet') ) axs[0].axis('equal') cax=fig.add_axes( [0.94,0.3,0.02,0.5] ) plot_utils.cbar(colls[0],extras=colls[1:],cax=cax) fig.subplots_adjust(right=0.9)
dDdn=(D[c2]-D[c1])/dg gradD=U_perot(g,g.edges_length()*dDdn,g.cells_area()) gradmag=utils.mag(gradD) return gradmag ## from matplotlib.colors import LogNorm for v in ds.data_vars: if not v.startswith('dist'): continue print(v) gradmag=dist_ratio(ds[v].values) fig=plt.figure(1) fig.clf() ax=fig.add_axes([0,0,1,1]) cax=fig.add_axes([0.05,0.05,0.03,0.25]) ccoll=g.plot_cells(values=gradmag,cmap='jet',norm=LogNorm(vmin=0.1,vmax=10),ax=ax) # ccoll.set_clim([0.1,10]) plot_utils.cbar(ccoll,cax=cax) cax.set_title(r'$|| \nabla D ||$') ax.axis('equal') ax.axis( (601757, 628944., 4220689, 4249561) ) ax.text(0.05,0.9,v,transform=ax.transAxes) fig.savefig('gradient_%s.png'%v,dpi=200)
## plt.figure(1).clf() fig, ax = plt.subplots(num=1) tidx = 0 ccoll0 = transport.g.plot_cells(values=cont.history[tidx][1], ax=ax, cmap='seismic') ccoll0.set_clim([0.7, 1.3]) dt64 = np.datetime64( transport.hydro.time0) + transport.times[tidx] * np.timedelta64(1, 's') time_str = str(dt64)[:16] txt = ax.set_title(time_str) plot_utils.cbar(ccoll0, ax=ax) ax.xaxis.set_visible(0) ax.yaxis.set_visible(0) ax.set_facecolor("0.8") ## for tidx in range(0, len(cont.history), 2): ccoll0.set_array(cont.history[tidx][1]) dt64 = np.datetime64(transport.hydro.time0 ) + transport.times[tidx] * np.timedelta64(1, 's') time_str = str(dt64)[:16] txt = ax.set_title(time_str) plt.draw() plt.pause(0.5)
scat_snr = ax_snr.scatter(X, Z, 30, snr, cmap='jet', label='__nolabel__') scat_snr2 = ax_snr_norm.scatter(X, Z, 30, snr_norm, cmap='jet', label='__nolabel__') # scat_snr.set_clim([-1,1]) plot_utils.cbar(scat_snr, label='SNR', ax=ax_snr) plot_utils.cbar(scat_snr2, label='SNR*dist', ax=ax_snr_norm) scat_snr2.set_clim([55, 65]) for ax in [ax_snr, ax_snr_norm]: #if 'depth_vb' in ds: # ax.plot( ds_lateral, -ds.depth_vb,'k-',label='VB depth') if 'depth_bt' in ds: ax.plot(ds_lateral, -ds.depth_bt, 'k--', label='BT depth') ax.legend(fontsize=9) ax.set_ylabel('Depth (m)') # Just bottom axis ax.set_xlabel('Across-channel (m)') ax_snr.axis(xmin=xmin, xmax=xmax, ymin=zmin, ymax=zmax)
# color='b',alpha=0.5, # units='xy',angles='xy',scale_units='xy',scale=2e-4) # ecoll=g.plot_edges(values=J0) # ccoll=g.plot_cells(values=div_J0,zorder=-1) # ccoll=g.plot_cells(values=P[:model.Nelement],zorder=-1) #ccoll=g.plot_cells(values=P2,zorder=-1,ax=ax,cmap='jet') div_J0_2 = T2.dot(J0_2) depth_error = dt * div_J0_2 / self.element_area ccoll = g.plot_cells(values=depth_error, zorder=-1, ax=ax, cmap='seismic') ccoll.set_clim([-500, 500]) water_depth = V0_2 / self.element_area ccoll_depth = g.plot_cells(values=water_depth, zorder=-1, ax=ax2, cmap='jet') plot_utils.cbar(ccoll, ax=ax, label='Depth error/incr.') plot_utils.cbar(ccoll_depth, ax=ax2, label='water column depth') ax.axis('equal') # Started with rms depth error of 796!? print("Depth error rms: %.3f" % utils.rms(depth_error)) ## # Smoothing the depth error: # Should depth or volume get smoothed? Probably volume, so that using # a # Probably to
# proc 22, which has Alviso Slough proc=22 cell_depth=all_ds[proc].FlowElem_bl.values node_depth=all_ds[proc].NetNode_z.values ccoll=all_g[proc].plot_cells(ax=ax,lw=0.5,values=cell_depth,cmap='jet',zorder=0) ncoll=all_g[proc].plot_nodes(ax=ax,values=node_depth,cmap='jet',zorder=2,lw=1) #ncoll.set_markeredgecolor('k') plt.setp([ccoll,ncoll],edgecolor='face',clim=[-2,2]) img=lsb_dem.crop(all_g[proc].bounds()).plot(ax=ax_dem,vmin=-2,vmax=2,cmap='jet') from stompy.plot import plot_utils cax=fig.add_axes([0.9,0.1,0.02,0.5]) plot_utils.cbar(ccoll,extras=[ncoll,img],cax=cax) ## # Is this a problem with dry cells not accepting flow? pli=dio.read_pli("runs/short_20120801_p24/RioVista_flow.pli") pnts=pli[0][1] ax.plot(pnts[:,0],pnts[:,1],'g-o') ## # Something related to which links are "boundary" links? # nNetLink is 5096, also number of edges in grid. # nFlowLink is 4240
plt.figure(1).clf() fig, (ax, ax2) = plt.subplots(1, 2, num=1, sharex=True, sharey=True) img = dem.plot(ax=ax, interpolation='nearest', aspect='auto') #coll=g.plot_cells(values=g.cells['depth'],lw=0,ax=ax2,clip=clip) coll = g.contourf_node_values(g.nodes['depth'], np.linspace(-1.2, 2.5, 20), extend='both') cax = fig.add_axes([0.93, 0.1, 0.02, 0.4]) cbar = plot_utils.cbar(img, cax=cax, extras=[coll], ax=ax2, label='Bed elev. (m NAVD88)') plt.setp([coll, img], clim=[-1.2, 2.5]) ax.axis('equal') fig.subplots_adjust(left=0.05) ax.axis((587196., 592615., 4139820., 4146642.)) # z01: used the full grid, already merged # fig.savefig('compare_to_alviso_grid_z02.png') ##
cf=CompositeField(shp_fn='sources_v03.shp', factory=factory, priority_field='priority', data_mode='data_mode', alpha_mode='alpha_mode') bounds=[590000,591000,4142000,4143000] dem=cf.to_grid(dx=2,dy=2,bounds=bounds) ## plt.figure(1).clf() fig,ax=plt.subplots(num=1) img=dem.plot(ax=ax,interpolation='nearest',vmin=-5,vmax=4) plot_utils.cbar(img,ax=ax) for src_i in range(len(cf.sources)): if cf.sources['priority'][src_i]<0: continue plot_wkb.plot_wkb( cf.sources['geom'][src_i], ax=ax, fc='none',ec='k',lw=0.5) ## # looking pretty good, about ready to scale it up... # The test tile took 10.8s, with all the time in mask_outside. # So far so good - last thing is to bring in the holey USGS 2m, so it can sit # on top of the pond bathy.
from stompy.plot import plot_utils from stompy.model.delft import dfm_grid ## g06 = dfm_grid.DFMGrid( 'runs/20180807_grid98_06/CacheSloughComplex_v98_bathy_sparse_net.nc') g07 = dfm_grid.DFMGrid( 'runs/20180807_grid98_07/CacheSloughComplex_v98_bathy2_sparse_net.nc') ## plt.figure(1).clf() fig, ax = plt.subplots(1, 1, num=1) g06.plot_edges(color='k', lw=0.4) ncoll = g06.plot_nodes(values=(g07.nodes['depth'] - g06.nodes['depth']), ax=ax) plot_utils.cbar(ncoll, label='Depth 7 - depth 6') ncoll.set_clim([-5, 5]) ncoll.set_cmap('seismic') ax.axis('equal')