def initial_salinity_dyn(run_base_dir, mdu, static_dir, run_start): # Get some observations: usgs_init_salt = samples_from_usgs(run_start) # mooring_salt=samples_from_sfei_moorings(run_start,static_dir=static_dir) mooring_salt = samples_from_sfei_erddap(run_start) init_salt = np.concatenate((usgs_init_salt, mooring_salt)) ## g = dfm_grid.DFMGrid(os.path.join(run_base_dir, mdu['geometry', 'NetFile'])) # Above here is assembling init_salt # Below is extrapolating -- needs g cc_salt = samples_to_cells(init_salt, g) # Because DFM is going to use some interpolation, and will not reach outside # the convex hull, we have to be extra cautious and throw some points out farther # afield. xys_orig = np.loadtxt(os.path.join(static_dir, 'orig-saltopini.xyz')) combined_xys = np.concatenate((cc_salt, xys_orig), axis=0) ## return combined_xys
def run_all(run_base_dir, storm_start_h, storm_duration_h, storm_flow, sources=None, force=False): mdu = dio.MDUFile('template.mdu') mdu['geometry', 'NetFile'] = 'stein_03_net.nc' grid = dfm_grid.DFMGrid(mdu['geometry', 'NetFile']) if os.path.exists(run_base_dir): if force: shutil.rmtree(run_base_dir) # Safer - blow it away else: log.warning("Will not run %s -- already exists" % run_base_dir) return False mdu.set_time_range(start=np.datetime64('2010-01-01'), stop=np.datetime64('2010-01-05')) os.path.exists(run_base_dir) or os.makedirs(run_base_dir) mdu.set_filename(os.path.join(run_base_dir, 'flowfm.mdu')) ext_fn = mdu.filepath(['external forcing', 'ExtForceFile']) # Clear any pre-existing BC file: os.path.exists(ext_fn) and os.unlink(ext_fn) # Monkey patch the parameters: Storm.storm_flow = storm_flow Storm.storm_duration_h = storm_duration_h Storm.storm_start_h = storm_start_h bc_shp = 'forcing_with_q.shp' bc_shp_data = wkb2shp.shp2geom(bc_shp) for bc in bc_shp_data: if sources is not None and bc['name'] not in sources: print("Skipping %s" % bc['name']) continue for data_src in factory(bc): data_src.write(mdu=mdu, feature=bc, grid=grid) fixed_weir_out = "../derived" if 1: # fixed weir file is just referenced as static input shutil.copyfile(os.path.join(fixed_weir_out, 'fixed_weirs-v00.pli'), os.path.join(run_base_dir, 'fixed_weirs-v00.pli')) mdu['geometry', 'FixedWeirFile'] = 'fixed_weirs-v00.pli' mdu.write() dfm_grid.write_dfm(grid, mdu.filepath(['geometry', 'NetFile']), overwrite=True) dflowfm(mdu.filename, ['-t', '1', '--autostartstop'])
def test_mass_delete(): g=dfm_grid.DFMGrid(os.path.join(sample_data,"lsb_combined_v14_net.nc")) g.edge_to_cells() clip=(577006.59313042194, 579887.89496937161, 4143066.3785693897, 4145213.4131655102) node_to_del=np.nonzero(g.node_clip_mask(clip))[0] for n in node_to_del: g.delete_node_cascade(n) g.renumber_nodes()
from stompy.model import unstructured_diffuser from scipy.interpolate import interp1d from stompy.model.delft import dfm_grid import sfb_dfm_utils ## run_base_dir='./runs/wy2013c' static_dir='./inputs-static' run_start=np.datetime64('2012-08-01') #sfb_dfm_utils.add_initial_salinity_dyn(run_base_dir, # abs_static_dir, # mdu, # run_start) g=dfm_grid.DFMGrid(os.path.join(run_base_dir,mdu['geometry','NetFile'])) ## usgs_data_end=np.datetime64('2016-04-28') usgs_pad=np.timedelta64(30,'D') usgs_target=run_start # so we may have to grab a previous years cruise and pretend while usgs_target + usgs_pad > usgs_data_end: usgs_target -= np.timedelta64(365,'D') usgs_cruises=usgs_sfbay.cruise_dataset(usgs_target - usgs_pad, usgs_target + usgs_pad )
map_files=partitioned_map_files(output_dir) dss=[xr.open_dataset(fn) for fn in map_files] nproc=len(dss) ## # Not really ready for transcribing gigabytes. Current need is for a single # timestep, go with that. dss=[ds.isel(time=-1) for ds in dss] ## # Construct the global grid grids=[dfm_grid.DFMGrid(ds) for ds in dss] node_maps={} edge_maps={} cell_maps={} gg=unstructured_grid.UnstructuredGrid(max_sides=8) for proc in range(nproc): print("Merging grid %d"%proc) n_map,e_map,c_map = gg.add_grid(grids[proc],merge_nodes='auto') node_maps[proc]=n_map edge_maps[proc]=e_map cell_maps[proc]=c_map # I'm ending up with 2 cells more than this file:
from stompy.model.delft import dfm_grid from stompy import utils import scipy.optimize as opt import matplotlib.pyplot as plt from matplotlib import collections, patches import six ## six.moves.reload_module(unstructured_grid) six.moves.reload_module(exact_delaunay) six.moves.reload_module(dfm_grid) ## g = dfm_grid.DFMGrid("data/lsb_combined_v14_net.nc") # Trim that down to speed up things clip = (577006.59313042194, 579887.89496937161, 4143066.3785693897, 4145213.4131655102) node_to_del = np.nonzero(~utils.within_2d(g.nodes['x'], clip))[0] for n in node_to_del: g.delete_node_cascade(n) g.renumber() ## zoom = (578260, 579037, 4143970., 4144573) point_in_poly = (578895, 4144375)
def add_wind_dataset(mdu, base_wind): """ mdu: MDUFile object (assumes that mdu.base_path has been set) """ run_base_dir = mdu.base_path assert run_base_dir is not None grid_fn = mdu.filepath(['geometry', 'NetFile']) g = dfm_grid.DFMGrid(grid_fn) t_ref, run_start, run_stop = mdu.time_range() # manufacture a constant in time, constant in space wind field ds = xr.Dataset() if 'time' in base_wind.dims: ds['time'] = ('time', ), base_wind.time if ds.time.values[0] > run_start: log.warning( "In add_wind_dataset(), start time is after model start") if ds.time.values[-1] < run_stop: log.warning("In add_wind_dataset(), end time is before model end") else: DAY = np.timedelta64(1, 'D') data_start = run_start - 1 * DAY data_stop = run_stop + 1 * DAY t = np.array([data_start, data_stop]) ds['time'] = ('time', ), t xxyy = g.bounds() pad = 0.1 * (xxyy[1] - xxyy[0]) if 'x' in base_wind.dims: ds['x'] = ('x', ), base_wind.x else: ds['x'] = ('x', ), np.linspace(xxyy[0] - pad, xxyy[1] + pad, 2) if 'y' in base_wind.dims: ds['y'] = ('y', ), base_wind.y else: ds['y'] = ('y', ), np.linspace(xxyy[2] - pad, xxyy[3] + pad, 3) # Here is the magic where xarray broadcasts the dimensions as needed to get # (time,y,x) wind data. _, _, _, new_u, new_v = xr.broadcast(ds.time, ds.y, ds.x, base_wind.wind_u, base_wind.wind_v) ds['wind_u'] = new_u ds['wind_v'] = new_v count = dio.dataset_to_dfm_wind( ds, ds.time.values[0], ds.time.values[-1], target_filename_base=os.path.join(run_base_dir, "const_wind"), extra_header="# generated from xarray dataset input") assert count > 0 # and add wind to the boundary forcing wind_stanza = [ "QUANTITY=windx", "FILENAME=const_wind.amu", "FILETYPE=4", "METHOD=2", "OPERAND=O", "", "QUANTITY=windy", "FILENAME=const_wind.amv", "FILETYPE=4", "METHOD=2", "OPERAND=O", "\n" ] old_bc_fn = mdu.filepath(['external forcing', 'ExtForceFile']) with open(old_bc_fn, 'at') as fp: fp.write("\n".join(wind_stanza)) return True
g.modify_node(n, x=target) return True else: return False def nudge_cell_orthogonal(self, c): for n in self.g.cell_to_nodes(c): self.nudge_node_orthogonal(n) if 0: # dev code. from stompy.model.delft import dfm_grid import matplotlib.pyplot as plt from stompy.plot import plot_utils g = dfm_grid.DFMGrid( '/home/rusty/models/grids/lsb_combined/lsb_combined_v03_net.nc') errs = g.circumcenter_errors(radius_normalized=True) if 1: plt.figure(1).clf() fig, ax = plt.subplots(num=1) coll = g.plot_cells(values=errs, ax=ax, lw=0) plot_utils.cbar(coll) ## c = 45085 # starts with orthogonality error of 0.042 n = 39775 ##
mdu['time', 'TStart'] = 0 mdu['time', 'TStop'] = int((run_stop - run_start) / np.timedelta64(1, 'm')) mdu['geometry', 'LandBoundaryFile'] = os.path.join(rel_static_dir, "deltabay.ldb") mdu['geometry', 'Kmx'] = 10 # 10 layers # update location of the boundary conditions # this has the source/sinks which cannot be written in the new style file mdu['external forcing', 'ExtForceFile'] = os.path.basename(old_bc_fn) # Load the grid now -- it's used for clarifying some inputs, but # is also modified to deepen areas near inflows, before being written # out near the end of the script grid = dfm_grid.DFMGrid(net_file) ## bc_dir = os.path.join(run_base_dir, 'bc_files') os.path.exists(bc_dir) or os.makedirs(bc_dir) # features which have manually set locations for this grid adjusted_pli_fn = os.path.join(base_dir, 'nudged_features.pli') sfb_dfm_utils.add_sfbay_freshwater(bc_dir, run_start, run_stop, ref_date, adjusted_pli_fn, freshwater_dir=os.path.join( base_dir, 'sfbay_freshwater'),
mdu['time', 'TStart'] = 0 mdu['time', 'TStop'] = int((run_stop - run_start) / np.timedelta64(1, 'm')) mdu['geometry', 'LandBoundaryFile'] = rel_static_dir / "deltabay.ldb" mdu['geometry', 'Kmx'] = 10 # 10 layers # update location of the boundary conditions # this has the source/sinks which cannot be written in the new style file mdu['external forcing', 'ExtForceFile'] = old_bc_fn.name #%% # Load the grid now -- it's used for clarifying some inputs, but # is also modified to deepen areas near inflows, before being written # out near the end of the script grid = dfm_grid.DFMGrid(str(net_file)) ## split into relative and absolute directories (alliek Dec 2020) rel_bc_dir = 'bc_files' abs_bc_dir = run_base_dir / rel_bc_dir abs_bc_dir.exists() or abs_bc_dir.mkdir() # features which have manually set locations for this grid adjusted_pli_fn = base_dir / 'nudged_features.pli' # this line worked in emma's repo that she left on hpc because, but since she cloned rusty's # sfb_dfm_repo he made updates to add_sfbay_freshwater, so changing to work with rusty's updated # sfb_dfm_utils (alliek dec 2020) #sfb_dfm_utils.add_sfbay_freshwater(bc_dir, # run_start,run_stop,ref_date, # adjusted_pli_fn,
## map_nc = "/hpcvol1/rusty/dfm/sfb_ocean/runs/short_21_norm_orig/DFM_OUTPUT_short_21/short_21_map.nc" map_rev_nc = "/hpcvol1/rusty/dfm/sfb_ocean/runs/short_21/DFM_OUTPUT_short_21/short_21_map.nc" map_ds = xr.open_dataset(map_nc) map_rev_ds = xr.open_dataset(map_rev_nc) ## ptreyes = noaa_coops.coops_dataset('9415020', map_ds.time[0], map_ds.time[-1], ['water_level'], cache_dir='cache', days_per_request='M') g = dfm_grid.DFMGrid(map_ds) ## plt.figure(1).clf() fig, ax = plt.subplots(num=1) g.plot_edges(ax=ax, color='k', lw=0.5) ## # middle of the domain samp_point = np.array([464675., 4146109.]) samp_idx = g.select_cells_nearest(samp_point) ##
output_dir = os.path.join(run_base_dir, "DFM_OUTPUT_%s" % run_name) Nproc = 16 procs = range(Nproc) all_ds = [ xr.open_dataset( os.path.join(output_dir, '%s_%04d_20120801_000000_map.nc' % (run_name, proc))) for proc in procs ] all_g = [unstructured_grid.UnstructuredGrid.from_ugrid(ds) for ds in all_ds] # Global grid so we can pull out clusters of mass G = dfm_grid.DFMGrid(os.path.join(run_base_dir, 'sfei_v19_net.nc')) ## # Walk through the subdomains, figure out which cell in G each # subdomain cell belongs to. Could be faster, but as is takes # about 30s. global_elems = np.zeros(G.Ncells(), 'int32') - 1 for proc in procs: print(proc) g = all_g[proc] centers = g.cells_centroid() for c in range(g.Ncells()): Gcell = G.select_cells_nearest(centers[c], inside=True) assert Gcell >= 0
# SLOW! - just do this once aggregator=agg.get_hydro() ## g_agg=aggregator.grid() Nagg=1+aggregator.elt_global_to_agg_2d.max() # Get a consistent unaggregated grid -- the one from scalar_map is bad. # g_waq=dfm_grid.DFMGrid(scalar_map) # BAD # This grid matches both the scalar fields coming out of WAQ, and # the aggregation mapping waq_hyd_dir='/opt/data/delft/sfb_dfm_v2/runs/wy2013c/DFM_DELWAQ_wy2013c_adj' g_waq=dfm_grid.DFMGrid(os.path.join(waq_hyd_dir,'wy2013c_waqgeom.nc')) # hyd=waq.HydroFiles(os.path.join(waq_hyd_dir,"wy2013c.hyd")) # Not needed, right? agg_lp_2d_dir='hydro-wy2013c_adj-agg_lp_2d' hydro_2d=waq.HydroFiles('hydro-wy2013c_adj-agg_lp_2d/com-wy2013c_adj_agg_lp_2d.hyd') import lowpass_wy2013c lp_secs=lowpass_wy2013c.lp_secs lp_hyd=lowpass_wy2013c.get_hydro() ##
import shutil import six import stompy.model.delft.io as dio from stompy.spatial import wkb2shp from stompy.io.local import noaa_coops from stompy import filters from stompy.model.delft import dfm_grid ## mdu = dio.MDUFile('template.mdu') mdu['geometry', 'NetFile'] = 'stein_03_net.nc' grid = dfm_grid.DFMGrid(mdu['geometry', 'NetFile']) run_base_dir = 'runs/test02' if os.path.exists(run_base_dir): shutil.rmtree(run_base_dir) # Safer - blow it away mdu.set_time_range(start=np.datetime64('2010-01-01'), stop=np.datetime64('2010-01-05')) os.path.exists(run_base_dir) or os.makedirs(run_base_dir) mdu.set_filename(os.path.join(run_base_dir, 'flowfm.mdu')) ## - ext_fn = mdu.filepath(['external forcing', 'ExtForceFile'])
# this has the source/sinks which cannot be written in the new style file mdu['external forcing','ExtForceFile']=os.path.basename(old_bc_fn) # Load the grid now -- it's used for clarifying some inputs, but # is also modified to deepen areas near inflows, before being written # out near the end of the script # Bathy for LSB is evolving, so a few steps required to possibly update it here. net_bathy_file=net_file.replace("_net.nc","_bathy_net.nc") bathy_file="inputs-static/merged_2m.tif" assert net_bathy_file!=net_file if ( (not os.path.exists(net_bathy_file)) or (os.stat(net_file).st_mtime >= os.stat(net_bathy_file).st_mtime) or (os.stat(bathy_file).st_mtime >= os.stat(net_bathy_file).st_mtime) ): grid=dfm_grid.DFMGrid(net_file) log.info("Will update bathymetry in grid - 2 minutes?") set_bathy.set_lsb_bathy(grid) log.info("Writing updated grid/bathy") dfm_grid.write_dfm(grid,net_bathy_file,overwrite=True) # Either way, read that back in log.info("Reading grid with bathy") grid=dfm_grid.DFMGrid(net_bathy_file) ## # Write out a shapefile for the grid edges. # Run this after changing the grid. # Output written to "derived" subdirectory. edges_shp="derived/grid-edges.shp"
from stompy.grid import unstructured_grid from stompy.model.delft import dfm_grid ## # First check the global grid: g = dfm_grid.DFMGrid('runs/hor_004-restart/grid_net.nc') ## e2c = g.edge_to_cells(recalc=True) cc = g.cells_center() ## nc1 = e2c[:, 0] nc2 = e2c[:, 1] dists = utils.dist(cc[nc1] - cc[nc2]) dists[(nc1 < 0) | (nc2 < 0)] = 100 # np.nan ## plt.figure(10).clf() fig, ax = plt.subplots(num=10) ecoll = g.plot_edges(values=dists, lw=2, ax=ax) ecoll.set_clim([0, 2]) ax.axis('equal')
from stompy.model.delft import dfm_grid g=dfm_grid.DFMGrid('CacheSloughComplex_v97_bathy_net.nc')
import numpy as np import matplotlib.pyplot as plt from stompy.spatial import field from stompy.model.delft import dfm_grid from stompy.plot import plot_utils ## # This one seems like it should just be the Alviso-only grid, but plots as the # combined... #g=dfm_grid.DFMGrid('/home/rusty/models/grids/mick_alviso_v4_net.nc/mick_alviso_v4_net.nc') g = dfm_grid.DFMGrid( '/media/hpc/opt/data/delft/lsb_combined/grids/alviso2012_net.nc') g.add_cell_field('depth', g.interp_node_to_cell(g.nodes['depth'])) ## dem = field.GdalGrid('tiles_5m_20170501/merged_5m.tif') ## clip = (576264., 597495., 4122205., 4154848.) ## plt.figure(1).clf() fig, (ax, ax2) = plt.subplots(1, 2, num=1, sharex=True, sharey=True)
## run_name="short_20120801_p16" run_base_dir="runs/" +run_name output_dir=os.path.join(run_base_dir,"DFM_OUTPUT_%s"%run_name) Nproc=16 procs=range(Nproc) all_ds=[xr.open_dataset(os.path.join(output_dir,'%s_%04d_20120801_000000_map.nc'%(run_name,proc))) for proc in procs] all_g=[unstructured_grid.UnstructuredGrid.from_ugrid(ds) for ds in all_ds] # Global grid so we can pull out clusters of mass G=dfm_grid.DFMGrid('sfei_v19_net.nc') ## # Walk through the subdomains, figure out which cell in G each # subdomain cell belongs to. Could be faster, but as is takes # about 30s. global_elems=np.zeros(g.Ncells(),'int32')-1 for proc in procs: print(proc) g=all_g[proc] centers=g.cells_centroid() for c in range(g.Ncells()): Gcell=G.select_cells_nearest( centers[c],inside=True ) assert Gcell>=0
hydro_txt_fn = "../../model/untrim/ed-steady/section_hydro.txt" names = xr_transect.section_hydro_names(hydro_txt_fn) if source == 'dfm': # older run with 33 advection but higher viscosity run_name = 'hor_003' else: run_name = source.replace('dfm', 'hor_005') dfm_map = xr.open_dataset( ('../../' 'model/dfm/dfm/' 'runs/%s/DFM_OUTPUT_flowfm/flowfm_0000_20120801_000000_map.nc') % run_name) g = dfm_grid.DFMGrid(dfm_map) transects = [(hydro_txt_fn, name, dfm_map, g) for name in names] def read_transect(transect): hydro_txt_fn, section_name, dfm_map, g = transect untrim_ds = xr_transect.section_hydro_to_transect( hydro_txt_fn, section_name) line_xy = np.c_[untrim_ds.x_sample.values, untrim_ds.y_sample.values] ds = dfm.extract_transect(dfm_map.isel(time=-1), line=line_xy, dx=3, grid=g) xy = np.c_[ds.x_sample.values, ds.y_sample.values]
# Compare bathy which is already in some grids, see if any can # be borrowed. import matplotlib.pyplot as plt from stompy.model.delft import dfm_grid from stompy.grid import unstructured_grid from stompy.plot import plot_utils ## r18=dfm_grid.DFMGrid('grid-sources/r18b_net.nc') r17=dfm_grid.DFMGrid('grid-sources/cascade/r17b_net.nc') 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
os.path.join(run_base_dir, "DFM_OUTPUT_%s" % run_name, "%s_%04d_map.nc" % (run_name, proc)) for proc in range(procs) ] his_fn = os.path.join(run_base_dir, "DFM_OUTPUT_%s" % run_name, "%s_0000_his.nc" % (run_name)) ## his = xr.open_dataset(his_fn) ## ds0 = xr.open_dataset(maps[0]) g0 = dfm_grid.DFMGrid(ds0) ## # StretchCoef = 8 8 7 7 6 6 6 6 5 5 5 5 5 5 5 5 2 2 1 1 salt = ds0.sa1.isel(time=1) plt.figure(3).clf() #for lay_i in range(len(salt.laydim)): lay_i = 13 salt_layer = salt.isel(laydim=lay_i).values g0.plot_edges(color='k') coll = g0.plot_cells(values=salt_layer, mask=np.isfinite(salt_layer), cmap='jet')
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. ## if 0: g=dfm_grid.DFMGrid('lsb_v99_net.nc') g2=set_lsb_bathy(g)
# 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()
import numpy as np import matplotlib.pyplot as plt from shapely import geometry from stompy.grid import unstructured_grid from stompy.model.delft import dfm_grid from stompy.spatial import field from stompy.plot import plot_wkb from stompy import utils ## g_roms = unstructured_grid.UnstructuredGrid.from_ugrid( 'derived/matched_grid_v01.nc') g_sfb = dfm_grid.DFMGrid('../../sfb_dfm_v2/sfei_v20_net.nc') ## g_roms_poly = g_roms.boundary_polygon() g_sfb_poly = g_sfb.boundary_polygon() ## plt.figure(1).clf() fig, ax = plt.subplots(num=1) g_roms.plot_edges(ax=ax, color='b', lw=0.8) g_sfb.plot_edges(ax=ax, color='g', lw=0.8) plot_wkb.plot_polygon(g_roms_poly, facecolor='b', alpha=0.3) plot_wkb.plot_polygon(g_sfb_poly, facecolor='g', alpha=0.3)
""" Testing the python version of the coupling code, as ddcouplefm is having issues. """ import six import xarray as xr from stompy.model.delft import dfm_grid import stompy.model.delft.io as dio import stompy.model.delft.waq_scenario as waq ## # Load a global grid, which will be used for defining aggregation # (or lack thereof in this case). # This one might work, though there is also the global grid which is # written out by DFM, DFM_interpreted_idomain_spliced_grids_01_bathy_net.nc g_global = dfm_grid.DFMGrid("runs/short_test_13/spliced_grids_01_bathy_net.nc") ## six.moves.reload_module(waq) # merge_only should make this a bit faster, but also requires the exact # match of g_global # sparse_layers should be False, as parts of dwaq don't like sparse layers. # exch_z_area_constant: no need to force this to be true, and it makes writing # areas 20x slower. multi_hydro = waq.HydroMultiAggregator(run_prefix="short_test_13", path="runs/short_test_13", agg_shp=g_global, link_ownership="owner_of_min_elem", sparse_layers=False,
from stompy.model.delft import dfm_grid g=dfm_grid.DFMGrid('CacheSloughComplex_v95_net.nc') g.write_edges_shp('derived/grid-edges.shp',overwrite=True) g.write_cells_shp('derived/grid-cells.shp',overwrite=True) # g.write_nodes_shp('derived/grid-nodes.shp',overwrite=True)
from stompy.model.delft import dfm_grid import matplotlib.pyplot as plt import xarray as xr ## # g=dfm_grid.DFMGrid("/home/rusty/models/grids/mick_alviso_v4_net.nc/mick_alviso_v4_net.nc") nc_fn = "/home/rusty/models/delft/dfm/alviso/20151214/Alviso_input/alviso2012_net.nc" g = dfm_grid.DFMGrid(nc_fn) ds = xr.open_dataset(nc_fn) ## fig = plt.figure(1) fig.clf() ax = fig.add_subplot(1, 1, 1) g.plot_edges(ax=ax, color='k')
## run_name="short_20120801_p24" run_base_dir="runs/" +run_name output_dir=os.path.join(run_base_dir,"DFM_OUTPUT_%s"%run_name) Nproc=16 procs=range(Nproc) all_ds=[xr.open_dataset(os.path.join(output_dir,'short_20120801_p24_%04d_20120801_000000_map.nc'%proc)) for proc in procs] all_g=[unstructured_grid.UnstructuredGrid.from_ugrid(ds) for ds in all_ds] # Global grid so we can pull out clusters of mass G=dfm_grid.DFMGrid('sfei_v19_net.nc') ## # Walk through the subdomains, figure out which cell in G each # subdomain cell belongs to. Could be faster, but as is takes # about 30s. global_elems=np.zeros(g.Ncells(),'int32')-1 for proc in procs: print(proc) g=all_g[proc] centers=g.cells_centroid() for c in range(g.Ncells()): Gcell=G.select_cells_nearest( centers[c],inside=True ) assert Gcell>=0
import matplotlib.pyplot as plt from stompy.model.delft import dfm_grid import numpy as np import xarray as xr import gdal nw = 10 # the number of worst cell displayed var = 'mesh2d_Numlimdt' #'numlimdt' p_dir = 'E:/proj/Pescadero/Model_runs/Testing' #f_path = '/run_tide_test-v13/DFM_OUTPUT_flowfm/FlowFM_map.nc' f_path = '/run_tide_test-v13-DS/Project1.dsproj_data/flowfm/output/FlowFM_map.nc' nc_file = p_dir + f_path dta_grid = dfm_grid.DFMGrid(nc_file) xy = dta_grid.cells_center() #nc = Dataset(nc_file) nc = xr.open_dataset(nc_file) numlim = nc[var].isel(time=-1) fig, ax = plt.subplots(1, 1, figsize=(10, 8)) coll = dta_grid.plot_cells(values=numlim, ax=ax, clim=[0, 100], edgecolor='none') cbar = fig.colorbar(coll, label='Number of Times Limiting') bad_cells = numlim > 100 plt.plot(xy[bad_cells][:, 0],