Exemple #1
0
    def run(self, args):
        fmt, path = args[0].split(':')
        log.info("Writing %s as %s" % (path, fmt))

        g = stack[-1]  # by default, do not pop from stack

        if fmt in ['suntans_classic', 'suntans']:
            g.write_suntans(path, overwrite=self.clobber)
        elif fmt == 'suntans_hybrid':
            g.write_suntans_hybrid(path, overwrite=self.clobber)
        elif fmt == 'ugrid':
            g.write_ugrid(path, overwrite=self.clobber)
        elif fmt == 'untrim':
            g.write_untrim08(path, overwrite=self.clobber)
        elif fmt == 'cell_shp':
            g.write_cells_shp(path, overwrite=self.clobber)
        elif fmt == 'boundary_shp':
            g.write_shore_shp(path, overwrite=self.clobber)
        elif fmt == 'edge_shp':
            g.write_edges_shp(path, overwrite=self.clobber)
        elif fmt == 'node_shp':
            g.write_node_shp(path, overwrite=self.clobber)
        elif fmt == 'fishptm':
            g.write_ptm_gridfile(path, overwrite=self.clobber)
        elif fmt == 'dfm':
            from stompy.model.delft import dfm_grid
            if not path.endswith('_net.nc'):
                log.warning(
                    "Writing DFM grid to filename not ending in '_net.nc'")
            dfm_grid.write_dfm(g, path, overwrite=self.clobber)
        else:
            log.error("Did not understand format %s" % fmt)
            log.error("Possible formats are: %s" % self.format_list())
            sys.exit(1)
Exemple #2
0
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'])
Exemple #3
0
                                  grid=g,
                                  dredge_depth=dredge_depth,
                                  old_bc_fn=old_bc_fn,
                                  all_flows_unit=False,
                                  time_offset=np.datetime64('2016-01-01') -
                                  np.datetime64('2017-01-01'))

# ---------- END SF FRESH, POTW, DELTA

##

if 1:
    mdu['geometry',
        'NetFile'] = os.path.basename(ugrid_file).replace('.nc', '_net.nc')
    dfm_grid.write_dfm(g,
                       os.path.join(run_base_dir, mdu['geometry', 'NetFile']),
                       overwrite=True)

# This step is pretty slow the first time around.
if use_wind:
    coamps.add_coamps_to_mdu(mdu, run_base_dir, g, use_existing=True)

##

# Setting a full 3D initial condition requires a partitioned
# run, so go ahead partition now:

dfm_output_count = 0


def dflowfm(mdu_fn, args=['--autostartstop']):
#!/usr/bin/env python
"""
Read a suntans grid in the current directory and write a DFM grid, output_net.nc
"""

from stompy.grid import unstructured_grid
from stompy.model.delft import dfm_grid

ug = unstructured_grid.SuntansGrid(".")

dfm_grid.write_dfm(ug, "output_net.nc")
Exemple #5
0
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.
Exemple #6
0
##
if 1:
    lines = [
        "QUANTITY=frictioncoefficient",
        "FILENAME=%s/friction12e.xyz" % rel_static_dir, "FILETYPE=7",
        "METHOD=5", "OPERAND=O", ""
    ]
    with open(old_bc_fn, 'at') as fp:
        fp.write("\n".join(lines))

if 1:  # Copy grid file into run directory and update mdu
    mdu['geometry', 'NetFile'] = os.path.basename(net_file)
    dest = os.path.join(run_base_dir, mdu['geometry', 'NetFile'])
    # write out the modified grid
    dfm_grid.write_dfm(grid, dest, overwrite=True)

sfb_dfm_utils.add_initial_salinity_dyn(run_base_dir, abs_static_dir, mdu,
                                       run_start)

# WIND
ludwig_ok = sfb_dfm_utils.add_erddap_ludwig_wind(run_base_dir, run_start,
                                                 run_stop, old_bc_fn)
if not ludwig_ok:
    const_ok = sfb_dfm_utils.add_constant_wind(run_base_dir, mdu, [0, 0],
                                               run_start, run_stop)
    assert const_ok
else:
    assert ludwig_ok  # or see lsb_dfm.py for constant field.

##
Exemple #7
0
coll=g.plot_cells(mask=cells_to_dredge,ax=ax,color='orange',lw=3)
coll.set_edgecolors('orange')

##

Gdredge=dfm_grid.DFMGrid('sfei_v19_net.nc')

nodes_to_dredge=np.concatenate( [G.cell_to_nodes(c)
                                 for c in cells_to_dredge] )
nodes_to_dredge=np.unique(nodes_to_dredge)

dredge_depth=-0.5

Gdredge.nodes['depth'][nodes_to_dredge] = np.minimum(Gdredge.nodes['depth'][nodes_to_dredge],
                                                     dredge_depth)
# No cell depth in this grid, so no need to dredge that
dfm_grid.write_dfm(Gdredge,'sfei_v19_dredge_net.nc')

##

# Plot nice version of that
plt.figure(4).clf()
fig,ax=plt.subplots(num=4)

Gdredge.contourf_node_values( Gdredge.nodes['depth'],
                              np.linspace(-2,3.5,12),
                              extend='both',
                              ax=ax)
ax.axis('equal')
ax.axis((587218.08074605372, 594548.22297297686, 4144516.3193028411, 4149286.1178460922))
Exemple #8
0
        if m:
            edge_vals[j] = m['L_err']

    plt.figure(22).clf()
    fig, ax = plt.subplots(num=22)
    g.plot_edges(ax=ax, color='k', lw=0.3, alpha=0.5)
    ecoll = g.plot_edges(ax=ax, values=edge_vals, mask=np.isfinite(edge_vals))
    #ecoll.set_clim([-2,2])
    ecoll.set_clim([-100, 100])
    ecoll.set_cmap('jet')
    ecoll.set_lw(4)
    ax.axis('equal')
    plt.colorbar(ecoll)

##

g.add_node_field('depth', node_depths, on_exists='overwrite')

# bathy2 => with dredged out CSC region
dfm_grid.write_dfm(g,
                   'CacheSloughComplex_v100_bathy2_sparse_net.nc',
                   overwrite=True)

##

g.add_cell_field('depth',
                 g.interp_node_to_cell(node_depths),
                 on_exists='overwrite')

g.write_cells_shp('derived/grid-cells.shp', overwrite=True)
Exemple #9
0
from sfb_dfm_utils import ca_roms
dem = ca_roms.coastal_dem()

sel_nodes = np.nonzero(g_complete.nodes['src'] == 3)[0]

sel_node_depth = dem(g_complete.nodes['x'][sel_nodes])
splice_bathy[sel_nodes] = sel_node_depth

##

g_complete.add_node_field('depth', splice_bathy)

##

g_complete.write_ugrid('spliced_grids_01_bathy.nc', overwrite=True)
dfm_grid.write_dfm(g_complete, 'spliced_grids_01_net.nc', overwrite=True)

##

plt.clf()
g_complete.contourf_node_values((-g_complete.nodes['depth']).clip(0,
                                                                  np.inf)**0.2,
                                30,
                                cmap='jet',
                                extend='both')
g_complete.plot_edges(lw=0.5, color='k', alpha=0.3)

##
fig = plt.gcf()
ax = plt.gca()
Exemple #10
0
##
if 1:
    lines = [
        "QUANTITY=frictioncoefficient",
        "FILENAME=%s/friction12e.xyz" % rel_static_dir, "FILETYPE=7",
        "METHOD=5", "OPERAND=O", ""
    ]
    with open(str(old_bc_fn), 'at') as fp:
        fp.write("\n".join(lines))

if 1:  # Copy grid file into run directory and update mdu
    mdu['geometry', 'NetFile'] = net_file.name
    dest = run_base_dir / mdu['geometry', 'NetFile']
    # write out the modified grid
    dfm_grid.write_dfm(grid, str(dest), overwrite=True)

# update to work with rusty's changes to sfb_dfm_utils
#sfb_dfm_utils.add_initial_salinity_dyn(run_base_dir,
#                                       abs_static_dir,
#                                       mdu,
#                                       run_start)
sfb_dfm_utils.add_initial_salinity(str(run_base_dir),
                                   str(abs_static_dir),
                                   str(old_bc_fn),
                                   all_flows_unit=ALL_FLOWS_UNIT)

# WIND
#ludwig_ok=sfb_dfm_utils.add_erddap_ludwig_wind(run_base_dir,
#                                               run_start,run_stop,
#                                               old_bc_fn)
Exemple #11
0
from bcs import *


def factory(feat):
    code = '[' + feat['src'] + ']'
    return eval(code)


bc_shp = 'forcing.shp'
bc_shp_data = wkb2shp.shp2geom(bc_shp)

for bc in bc_shp_data:
    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)

##
Exemple #12
0
# 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"

if ( (not os.path.exists(edges_shp))
     or (os.stat(net_file).st_mtime >= os.stat(edges_shp).st_mtime) ):
    log.info("Writing new edge shapefile")
Exemple #13
0
ncoll.set_clim([-1, 2])
ncoll.set_cmap('jet')

# ecoll=g.plot_edges(values=node_mean)
# ecoll.set_clim([-1,2])
# ecoll.set_cmap('jet')

ecoll = g.plot_edges(values=errors)
ecoll.set_clim([-1, 1])
ecoll.set_cmap('seismic')
# g.plot_edges(lw=0.4,color='k')

# plt.axis( (568259., 568614., 4150269., 4150641.) )
plt.axis((569564.3238793822, 569761.5851150813, 4151337.141236072,
          4151543.8487844667))

##

g.add_node_field('depth', conn_depth, on_exists='overwrite')

# g.add_edge_field('depth_mean', g.nodes['depth'][g.edges['nodes']].mean(axis=1),on_exists='overwrite' )

##

grid_out_fn = 'dfm/stein_04_net.nc'

if os.path.exists(grid_out_fn):
    os.unlink(grid_out_fn)

dfm_grid.write_dfm(g, grid_out_fn)
Exemple #14
0
    # Manually drawn polygon around Napa, and only muck with those nodes:
    plt.figure(2).clf()
    fig, ax = plt.subplots(num=2)

    g.contourf_node_values(g.nodes['depth'],
                           np.linspace(-10, 2, 20),
                           extend='both',
                           cmap='jet')
    ax.axis('equal')

    pnts = plot_utils.draw_polyline(ax=ax)
else:
    pnts = np.array([[565555.92717692, 4214418.23511226],
                     [566275.67385296, 4213788.45677072],
                     [567295.31497736, 4214388.24566743],
                     [563996.47604549, 4226863.85471885],
                     [559707.98543407, 4227793.52750873],
                     [559108.19653736, 4226264.06582214],
                     [563456.66603846, 4216757.4118094],
                     [565136.07494923, 4215167.97123314]])

##

poly = geometry.Polygon(pnts)
sel_nodes = g.select_nodes_intersecting(poly)
g.nodes['depth'][sel_nodes] = dem(g.nodes['x'][sel_nodes])

##

dfm_grid.write_dfm(g, 'sfei_v20_net.nc')