def test_load_shp_query(): feats = wkb2shp.shp2geom("data/dem_sources.shp") queries = ["priority < 100", "start_date is null"] # This appears not to be supported yet. unclear. # 'start_date is null or (cast(start_date as character) < "2014-09-01")'] for query in queries: print(query) feat_sel = wkb2shp.shp2geom("data/dem_sources.shp", query=query) assert len(feats) > len(feat_sel)
def clean_shapefile(shp_in): """ break multipolygons into individual polygons. """ from stompy.spatial import wkb2shp geoms = wkb2shp.shp2geom(agg_grid_shp) multi_count = 0 new_geoms = [] for fi, feat in enumerate(geoms): if feat['geom'].type == 'Polygon': new_geoms.append(feat['geom']) else: multi_count += 1 for g in feat['geom'].geoms: new_geoms.append(g) if multi_count: cleaned = agg_grid_shp.replace('.shp', '-cleaned.shp') assert cleaned != agg_grid_shp wkb2shp.wkb2shp(cleaned, new_geoms, overwrite=True) return cleaned else: return shp_in
def test_dumbarton(): shp=os.path.join( os.path.dirname(__file__), 'data','dumbarton.shp') features=wkb2shp.shp2geom(shp) geom = features['geom'][0] dumbarton = np.array(geom.exterior) density = field.ConstantField(250.0) p=paver.Paving(dumbarton, density,label='dumbarton') p.pave_all()
def samples_from_sfei_moorings(run_start, static_dir): """ return [N,3] array of salinity data from SFEI moorings appropriate for given date. Note that this may have no data, but will be returned as a [0,3] array """ # And pull some SFEI data: mooring_xy = [] mooring_salt = [] L2_dir = '/opt/data/sfei/moored_sensors_csv/L2/' # tuples (<name in observation points shapefile>, <L2 data file name> ) sfei_moorings = [('ALV', "ALV_all_data_L2.csv"), ('SFEI_Coyote', "COY_all_data_L2.csv"), ('DB', "DMB_all_data_L2.csv"), ('SFEI_Guadalupe', "GL_all_data_L2.csv"), ('SFEI_Mowry', "MOW_all_data_L2.csv"), ('SFEI_Newark', "NW_all_data_L2.csv"), ('SFEI_A8Notch', "POND_all_data_L2.csv"), ('SMB', "SM_all_data_L2.csv")] # lat/lon from observation-points # FRAGILE - FIX! obs_shp = wkb2shp.shp2geom( os.path.join(static_dir, "observation-points.shp")) for name, l2_file in sfei_moorings: print(name) fn = os.path.join(L2_dir, l2_file) if not os.path.exists(fn): logger.warning( "No SFEI mooring data - will not be able to initialize LSB initial condition" ) continue sfei = pd.read_csv(fn, parse_dates=['Datetime', 'dt'], low_memory=False) sfei_salt = sfei['S_PSU'] valid = ~(sfei_salt.isnull()) # limit to data within 20 days of the request sfei_salt_now = utils.interp_near(utils.to_dnum(run_start), utils.to_dnum(sfei.Datetime[valid]), sfei_salt[valid], max_dx=20.0) geom = obs_shp['geom'][np.nonzero(obs_shp['name'] == name)[0][0]] xy = np.array(geom) if np.isfinite(sfei_salt_now): mooring_xy.append(xy) mooring_salt.append(sfei_salt_now) if len(mooring_xy): xy = np.array(mooring_xy) sfei_init_salt = np.c_[xy[:, 0], xy[:, 1], mooring_salt] else: sfei_init_salt = np.zeros((0, 3), 'f8') return sfei_init_salt
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 load_shp(self, subset=None): if self.regions_shp is not None: self.shp_data = wkb2shp.shp2geom(self.regions_shp) else: self.shp_data = self.regions_data if subset is not None: self.shp_data = self.shp_data[subset] self.regions = [Region(rec) for rec in self.shp_data]
def raise_inlet(self,slr): scen_shp=wkb2shp.shp2geom(os.path.join(local_config.bathy_dir,"dem-scenarios.shp")) poly=scen_shp['geom'][ scen_shp['src_name']=='slr_region'][0] inlet_nodes=self.grid.select_nodes_intersecting(poly) max_dist=25.0 dists=self.grid.distance_transform_nodes(inlet_nodes,max_dist) weights=(max_dist-dists).clip(0) / max_dist assert np.all( np.isfinite(weights) ) assert weights.max()<=1.0 assert weights.min()>=0.0 self.grid.nodes['node_z_bed'][:] += weights*self.slr
def gen_polygons(path): shp_fn = os.path.join(os.path.dirname(__file__), "gis/poly-features.shp") polys = wkb2shp.shp2geom(shp_fn) for feat in polys: print(feat['name']) pol_fn = os.path.join(path, feat['name'] + ".pol") with open(pol_fn, 'wt') as fp: fp.write(f"* Transcribed from {shp_fn}\n") fp.write(f"{feat['name']}\n") pnts = np.array(feat['geom'].exterior) fp.write(f"{len(pnts)} 2\n") for xy in pnts: fp.write("%.3f %.3f\n" % (xy[0], xy[1]))
def init_parameters(self): params = super(Scen, self).init_parameters() params['MaxIter'] = 100 params['Tolerance'] = 1.0e-7 params['Iteration Report'] = 0 params['ONLY_ACTIVE'] = 1.0 params['ACTIVE_DynDepth'] = 1 params['ACTIVE_TotDepth'] = 1 #params['ACTIVE_ATMDEP_NO3']=1 params['ACTIVE_Nitrif_NH4'] = 1 # disable regular nitrification params['RcNit'] = 0.0 params['RcNit20'] = 0.0 # params['fAtmDepNO3']=0.01 # g m-2 d-1 polys = wkb2shp.shp2geom('../../dflowfm/gis/poly-features.shp') for poly in polys: if poly['name'] == 'calhoun_marsh': g = self.hydro.grid() cells = g.select_cells_intersecting(poly['geom']) cell_rates = np.zeros(g.Ncells()) # for atm dep: g m-2 d-1 # for nitrif: mg/l d-1 # hmm - I'm getting a max value of 11.6 over a two day run. # growth in one of those vernal ponds is linear, so that # much is good. cell_rates[cells] = 0.01 self.hydro.infer_2d_elements() seg_rates = cell_rates[self.hydro.seg_to_2d_element] # 001: # params['fAtmDepNO3']=waq.ParameterSpatial(per_segment=seg_rates) # 002: params['ZNit'] = waq.ParameterSpatial(per_segment=seg_rates) params['NH4'] = 10.0 # always plenty of NH4 print("Set rate in %d/%d elements" % (cells.sum(), g.Ncells())) if self.hydro.n_exch_z > 0: # if we don't have vertical diffusion specified, this will # raise an error, but that's good. 3D runs should have it. params['ACTIVE_VertDisp'] = 1.0 return params
## #grid=unstructured_grid.UnstructuredGrid.read_ugrid("../../../model/grid/snubby_junction/snubby-06.nc") grid = unstructured_grid.UnstructuredGrid.read_ugrid( "../../../model/suntans/snubby-08-edit60-with_bathysmooth.nc") grid_poly = grid.boundary_polygon() ## rx_locs = pd.read_csv("../../../field/hor_yaps/yap-positions.csv") ## # Legal delta boundary: delta_bdry = wkb2shp.shp2geom("../../../gis/Legal_Delta_Boundary.shp", target_srs='EPSG:26910')['geom'][0] ## ca_shp = wkb2shp.shp2geom("../../../gis/CA_State/CA_State_TIGER2016.shp", target_srs='EPSG:26910') ## from stompy.spatial import wkb2shp if 0: # Is the cascade grid of any use here? cgrid = unstructured_grid.UnstructuredGrid.read_dfm( "../../../../cascade/rmk_validation_setups/wy2011/r18b_net.nc") cgrid_poly = cgrid.boundary_polygon() wkb2shp.wkb2shp('r18b_net_outline.shp', [cgrid_poly], overwrite=True) if 0: sfbo_grid = unstructured_grid.UnstructuredGrid.read_ugrid(
############ COMPARISONS ################## run_name = "short_test_18" run_base_dir = "runs/%s" % run_name mdu_fn = os.path.join(run_base_dir, "%s.mdu" % run_name) mdu = dio.MDUFile(mdu_fn) ## utm2ll = proj_utils.mapper('EPSG:26910', "WGS84") ## # Comparisons across ROMS, NOAA tides, NOAA ADCPs, and this model obs_pnts = wkb2shp.shp2geom( "/opt/data/delft/sfb_dfm_v2/inputs-static/observation-points.shp") ## def load_subroms(): ca_roms_files = ca_roms.fetch_ca_roms(run_start, run_stop) sub_vars = ['zeta', 'salt'] ds = xr.open_dataset(ca_roms_files[0]) lat_idxs = np.searchsorted(ds.lat, [dfm_ll[:, 1].min(), dfm_ll[:, 1].max()]) lon_idxs = np.searchsorted( ds.lon % 360, [dfm_ll[:, 0].min() % 360, dfm_ll[:, 0].max() % 360]) lat_slice = slice(lat_idxs[0], 1 + lat_idxs[1])
"cbec_vs_lidar2011-north_marsh_pond.png"), dpi=200) ## And identify peak of histogram: if 0: plt.figure(3).clf() valid = np.isfinite(delta.F) plt.hist(delta.F[valid], bins=np.linspace(-0.2, 0.1, 500)) plt.grid() plt.savefig(os.path.join(fig_dir, "cbec_vs_lidar2011-histogram.png"), dpi=200) ## # Bring in the point data from the later sample: all_points = wkb2shp.shp2geom('../data/cbec/cbec-survey/All_Points.shp', target_srs='EPSG:26910') xyz = np.array([np.array(p) for p in all_points['geom']]) xyz[:, 2] = all_points['Z_m'] ## if 0: plt.figure(4).clf() fig, axs = plt.subplots(1, 3, num=4) fig.set_size_inches([8.5, 5.], forward=True) for ax, dem in zip(axs.ravel(), [cbec_dem, lidar2017, lidar2011]): img = dem.plot(ax=ax, cmap='gray', vmin=-1, vmax=5.) dem_at_xyz = dem(xyz[:, :2]) delta = xyz[:, 2] - dem_at_xyz
bathy_existing_fn = os.path.join(bathy_dir, 'compiled-dem-existing-20210920-1m.tif') ## gen_weirs = True gen_grids = True dems = [(bathy_asbuilt_fn, 'asbuilt'), (bathy_existing_fn, 'existing')] if gen_grids: grid_dir = "../../../grid" grid_fn = os.path.join(grid_dir, 'quad_tri_v21-edit08.nc') g = unstructured_grid.UnstructuredGrid.read_ugrid(grid_fn) g.renumber() if gen_weirs: lines = wkb2shp.shp2geom('line_features.shp') for dem_fn, name in dems: if not os.path.exists(dem_fn): print("---- DEM file %s doesn't exist ----" % dem_fn) continue print("Loading DEM for %s" % name) dem = field.GdalGrid(dem_fn) if gen_weirs: print("Generating fixed weirs") fixed_weir_fn = "fixed_weirs-%s.pliz" % name fixed_weirs = [] # suitable for write_pli dx = 5.0 # m. discretize lines at this resolution for i in range(len(lines)): feat = lines[i]
import matplotlib.pyplot as plt import stompy.plot.cmap as scmap from stompy.spatial import field from stompy import utils import numpy as np import six import os turbo = scmap.load_gradient('turbo.cpt') ## fig_dir = "figs-20201230" os.path.exists(fig_dir) or os.makedirs(fig_dir) ## all_points = wkb2shp.shp2geom('../data/cbec/cbec-survey/All_Points.shp', target_srs='EPSG:26910') xyz = np.array([np.array(p) for p in all_points['geom']]) xyz[:, 2] = all_points['Z_m'] ## gen = unstructured_grid.UnstructuredGrid.read_pickle( 'cbec-survey-interp-grid15.pkl') ## samples = pd.DataFrame(dict(x=xyz[:, 0], y=xyz[:, 1], value=xyz[:, 2])) sqg = quads.SimpleQuadGen(gen, cells=[1, 2, 3, 4, 5, 6], nom_res=2.0) oink = interp_orthogonal.OrthoInterpolator(sqg, samples)
from stompy.spatial import wkb2shp, linestring_utils import matplotlib.pyplot as plt from shapely import geometry from stompy.plot import plot_wkb from stompy.spatial import constrained_delaunay, field import six six.moves.reload_module(unstructured_grid) six.moves.reload_module(exact_delaunay) six.moves.reload_module(constrained_delaunay) six.moves.reload_module(linestring_utils) ## centerlines = wkb2shp.shp2geom('centerlines.shp') bounds = wkb2shp.shp2geom('quad_bounds.shp') lin_scale = constrained_delaunay.ConstrainedXYZField.read_shps( ["quad_scale.shp"], value_field='scale') tel_scale = field.ApolloniusField.read_shps(['quad_tele_scale.shp'], value_field='scale') scale = field.BinopField(lin_scale, np.minimum, tel_scale) def process_quad_patch(name, M=None): center_idx = np.nonzero(centerlines['name'] == name)[0][0] centerline = centerlines['geom'][center_idx] if M is None: M = centerlines['rows'][center_idx]
""" Run each source for multiple durations. """ import six import stein_dfm_script from stompy.spatial import wkb2shp six.moves.reload_module(stein_dfm_script) # about 1.5GB per run. sources = wkb2shp.shp2geom('forcing_with_q.shp') # Set BCs from shapefile from bcs import * for start_h in [42, 45, 48]: for duration_h in [3, 6, 7.3]: for source in sources: name = source['name'] objs = stein_dfm_script.factory(source) flow = None for obj in objs: if isinstance(obj, Storm): flow = obj.storm_flow break else: # This source is not a stormwater source continue
mdu['geometry', 'FixedWeirFile'] = os.path.join(rel_static_dir, 'SBlevees_tdk.pli') if 1: # evaporation was a bit out of control in south bay - try scaling back just # the evaporation some. This is a punt! sfb_dfm_utils.add_cimis_evap_precip(run_base_dir, mdu, scale_evap=0.5) if 1: # output locations mdu['output', 'CrsFile'] = os.path.join(rel_static_dir, "SB-observationcrosssection.pli") ## if 1: # Observation points taken from shapefile for easier editing/comparisons in GIS obs_pnts = wkb2shp.shp2geom(obs_shp_fn) obs_fn = 'observation_pnts.xyn' with open(os.path.join(run_base_dir, obs_fn), 'wt') as fp: for idx, row in enumerate(obs_pnts): xy = np.array(row['geom']) fp.write("%12g %12g '%s'\n" % (xy[0], xy[1], row['name'])) mdu['output', 'ObsFile'] = obs_fn if run_name.startswith('short'): mdu['output', 'MapInterval'] = 3600 ## mdu_fn = os.path.join(run_base_dir, run_name + ".mdu") mdu.write(mdu_fn)
return model ## run_coll_dir = os.path.join(local_config.run_dir_root, "roughsearch") run_log_file = os.path.join(run_coll_dir, "runlog") if os.path.exists(run_log_file): existing_runs = pd.read_csv(run_log_file) os.path.exists(run_coll_dir) or os.makedirs(run_coll_dir) ## 11 regions regions = wkb2shp.shp2geom('gis/roughness_regions.shp') ## # parameters for one run associate each region name with a roughness def baseline_settings(): settings = OrderedDict() for region in regions: settings[region['name']] = region['n_nominal'] return settings def settings_to_roughness_xyz(model, settings): xy = model.grid.nodes['x']
from shapely import geometry import matplotlib.tri as delaunay from stompy.spatial import wkb2shp, field from stompy.plot import plot_wkb from stompy.grid import (unstructured_grid, exact_delaunay) from stompy import utils ## #xyz=pd.read_csv('../../rma_bathy/SJSC-Bay-Delta_mNAVD88_6.xyz').values xyz = field.XYZText('../../rma_bathy/SJSC-Bay-Delta_mNAVD88_6.xyz', sep=',') sources = wkb2shp.shp2geom('sources_v01.shp') ## poly = sources['geom'][39] buff = 200 xyz_sub = xyz.clip_to_polygon(poly.buffer(buff)) ## dem1 = xyz_sub.to_grid(dx=5, dy=10, interp='linear') ## # That's okay, although not that great. # 1st step: build a triangulation, embed the polygon as
def test_load_shp(): feats = wkb2shp.shp2geom("data/dem_sources.shp") assert np.any(~utils.isnat(feats['start_date']))
from stompy.model.delft import io data_root = '/opt/data' ## dem = field.GdalGrid( os.path.join(data_root, 'bathy_interp/master2017/tiles_2m_20171024/merged_2m.tif')) ## # More details on the provenance of this on the Google drive in # Modeling/Lower South Bay/shoreline elevs inv_fn = "linear_features-v02.shp" inv = wkb2shp.shp2geom(inv_fn) ## # For the moment, focus on just the parts of the inventory which overlap with the DEM. bounds_xyxy = np.array(dem.extents)[[0, 2, 1, 3]] box = geometry.box(*bounds_xyxy) # There are some water control structures which came in from the original # shoreline inventory. Only structures which have been named are omitted here - # these are the ones which will be handled specifically as gates, while other # structures will be left as closed sel = [ box.intersects(geo) and ((klass != 'Water Control Structure') or (model_name == '')) for model_name, klass, geo in zip(inv['model_name'], inv['Class'],
is there anything to be gained from pulling in the ADCP data? """ from stompy.spatial import proj_utils, wkb2shp, field import matplotlib.pyplot as plt import xarray as xr ## dem = field.GdalGrid('junction-composite-20190117-no_adcp.tif') dem_adcp = field.GdalGrid('junction-composite-dem.tif') ## adcp_data = wkb2shp.shp2geom('derived/samples-depth.shp', target_srs='EPSG:26910') ## adcp_ds = xr.Dataset() adcp_ds['z_bed'] = ('sample', ), adcp_data['depth'] pnts = np.array([np.array(g) for g in adcp_data['geom']]) adcp_ds['x'] = ('sample', ), pnts[:, 0] adcp_ds['y'] = ('sample', ), pnts[:, 1] ## zoom = (647088.821657404, 647702.7390601198, 4185484.2206095303, 4185941.6880934904) plt.figure(10).clf()
from __future__ import print_function import sys import os import numpy as np import stompy.model.delft.io as dio from stompy.spatial import wkb2shp shp_fn, run_dir = sys.argv[1:] ## forcing = wkb2shp.shp2geom(shp_fn) print("shp_fn: %s" % shp_fn) print("run_dir: %s" % run_dir) for f in forcing: print(f['name']) pli_fn = os.path.join(run_dir, "%s.pli" % f['name']) with open(pli_fn, 'wt') as fp: dio.write_pli(fp, [[f['name'], np.array(f['geom'].coords)]])
parser = argparse.ArgumentParser( description='Plot model-data comparison figures for a run.') parser.add_argument("-d", '--run-dir', help="Run directory of model", required=True) args = parser.parse_args() run_dir = args.run_dir model = sun_driver.SuntansModel.load(run_dir) model.manual_z_offset = -4 # should be in sync with hor_snubby_oper.py tran_shp = os.path.join(base, "../../gis/model_transects.shp") tran_geoms = wkb2shp.shp2geom(tran_shp) fig_dir = os.path.join(model.run_dir, 'figs-20191121') os.path.exists(fig_dir) or os.makedirs(fig_dir) for ti, t in enumerate(tran_geoms): print(t['name']) if t['name'] == '2018_09': obs = xr.open_dataset( os.path.join(base, "../../field/adcp/040518_BT/040518_9BTref-avg.nc")) elif t['name'] == '2018_08': obs = xr.open_dataset( os.path.join(base, "../../field/adcp/040518_BT/040518_8BTref-avg.nc"))
return_type='cost', max_return=self.search_n // 2) results_rev = self.t_net.shortest_path(n, samples, edge_weight=rev_weight, directed=True, return_type='cost', max_return=self.search_n // 2) # results are lists of (n,cost) return results_fwd + results_rev # concatenate ## adcp_shp = wkb2shp.shp2geom('derived/samples-depth.shp') adcp_ll = np.array([np.array(pnt) for pnt in adcp_shp['geom']]) adcp_xy = proj_utils.mapper('WGS84', 'EPSG:26910')(adcp_ll) adcp_xyz = np.c_[adcp_xy, adcp_shp['depth']] ## src = 'dem' cluster = True xyz_input = adcp_xyz.copy() if src == 'dem': # Rather than use the ADCP data directly, during testing # use its horizontal distribution, but pull "truth" from the # DEM xyz_input[:, 2] = dem(xyz_input[:, :2])
ds.site_type.loc[s] = 'false' #for s in ['manteca','davis','tracy','stockton','sac_regional']: # Delta sources # ds.site_type.loc[s]='potw' #for s in ['sacramento','san_joaquin_at_vernalis']: # ds.site_type.loc[s]='river' for st in np.unique( ds.site_type.values ): count =np.sum(ds.site_type==st) print("Site type %s: %d"%(st,count)) ## # assign lat/lon from approx_discharge_locations.shp locs=wkb2shp.shp2geom('../sources/discharge_approx_locations.shp') ds['utm_x']=( ('site',), np.nan * np.ones(len(ds.site))) ds['utm_y']=1*ds.utm_x print("Discharges in discharge_approx_locations.shp, but not in sfbay_potw data") for rec in locs: if rec['short_name'] not in ds.site.values: print(" '%s'"%rec['short_name']) continue xy=np.array(rec['geom'].centroid) ds['utm_x'].loc[ dict(site=rec['short_name']) ]=xy[0] ds['utm_y'].loc[ dict(site=rec['short_name']) ]=xy[1] missing=ds['site'][ np.isnan(ds['utm_x'].values) ].values
""" First take at figuring out how sources get distributed during a tide """ import xarray as xr from shapely import geometry from stompy import utils from stompy.spatial import wkb2shp, field from stompy.grid import unstructured_grid from stompy.spatial import linestring_utils ## bounds = wkb2shp.shp2geom('region-bounds-v00.shp') ## from shapely import ops one_poly = ops.cascaded_union(bounds['geom']) bounds_xyxy = one_poly.bounds ## dem = field.GdalGrid( ('/media/idrive/BASELAYERS/Elevation_DerivedProducts/' 'LiDAR 2005-2012 entire Bay Area from AECOM/USGS_TopoBathy/' 'San_Francisco_TopoBathy_Elevation_2m.tif'), geo_bounds=[ bounds_xyxy[0], bounds_xyxy[2], bounds_xyxy[1], bounds_xyxy[3] ]) ##
if not os.path.exists(ugrid_file): g = ca_roms.extract_roms_subgrid() ca_roms.add_coastal_bathy(g) g.write_ugrid(ugrid_file) else: g = unstructured_grid.UnstructuredGrid.from_ugrid(ugrid_file) coastal_bc_coords = None # should get some coordinates if I return to this grid raise Exception( "Probably ought to fill in coastal_bc_coords for this grid") elif grid == 'ragged_coast': # ragged edge ugrid_file = 'derived/matched_grid_v01.nc' if not os.path.exists(ugrid_file): poly = wkb2shp.shp2geom('grid-poly-v00.shp')[0]['geom'] g = ca_roms.extract_roms_subgrid_poly(poly) ca_roms.add_coastal_bathy(g) g.write_ugrid(ugrid_file) else: g = unstructured_grid.UnstructuredGrid.from_ugrid(ugrid_file) g_shp = 'derived/matched_grid_v01.shp' if not os.path.exists(g_shp): g.write_edges_shp(g_shp) coastal_bc_coords = [ [450980., 4291405.], # northern [595426., 4037083.] ] # southern elif grid == 'ragged_splice': # Spliced grid generated in splice_grids.py ugrid_file = 'spliced_grids_01_bathy.nc' g = unstructured_grid.UnstructuredGrid.from_ugrid(ugrid_file)
Q *= -1 return dict(U=U, V=V, Q=Q, g=g) hydro = extract_global(model) ## # Sample datasets if 1: import bathy dem = bathy.dem() # fake, sparser tracks. adcp_shp = wkb2shp.shp2geom('sparse_fake_bathy_trackline.shp') xys = [] for feat in adcp_shp['geom']: feat_xy = np.array(feat) feat_xy = linestring_utils.resample_linearring(feat_xy, 1.0, closed_ring=0) feat_xy = filters.lowpass_fir(feat_xy, winsize=6, axis=0) xys.append(feat_xy) adcp_xy = np.concatenate(xys) source_ds = xr.Dataset() source_ds['x'] = ('sample', 'xy'), adcp_xy source_ds['z'] = ('sample', ), dem(adcp_xy) ##
from shapely import geometry from stompy.plot import plot_wkb ## # Load the Bay grid, with bathy #g_bay=unstructured_grid.UnstructuredGrid.from_ugrid('../grid-sfbay/sfbay-grid-20190301b-bathy.nc') # moving forward, use this cropped one: g_bay=unstructured_grid.UnstructuredGrid.from_ugrid('cropped-sfbay-20190301b.nc') ## # Generate a cartesian for the ocean portion. # shoreline.. states=wkb2shp.shp2geom("/home/rusty/svn/research/my_papers/gross-recruitment/figures/map/st99_d00.shp") ca=states[ states['NAME']=='California' ] ## project to UTM ll2utm=proj_utils.mapper('WGS84','EPSG:26910') p_geoms=[] for geom in ca['geom']: assert geom.type=='Polygon' p_ext=ll2utm(np.array(geom.exterior)) p_ints=[ ll2utm(np.array(r)) for r in geom.interiors] p_geoms.append( geometry.Polygon(p_ext,p_ints)) ##