Beispiel #1
0
def bathy():
    # from stompy.spatial import field
    #return field.GdalGrid('../../bathy/OldRvr_at_SanJoaquinRvr2012_0104-utm-m_NAVD.tif')
    utils.path("../../bathy")
    from bathy import dem
    tile = dem().extract_tile(study_zoom)
    return tile
def bathy():
    # from stompy.spatial import field
    #return field.GdalGrid('../../bathy/OldRvr_at_SanJoaquinRvr2012_0104-utm-m_NAVD.tif')
    utils.path(os.path.join(os.path.dirname(__file__), "../../bathy"))
    from bathy import dem
    study_zoom = [646000, 649000, 4185000, 4186500]
    tile = dem().extract_tile(study_zoom)
    return tile
plt.figure(3).clf()
fig,ax=plt.subplots(num=3)
ax.plot(ds.time.values, c_mag)

##

# The 3D run is roughly at steady state by 4h, with a little
# bit of adjusting / sloshing evident.

# Extract some transects.
# First, read Ed's transects:
untrim_sections_fn="../../untrim/ed-steady/section_hydro.txt"

from stompy import utils
utils.path("../../../field/adcp/")

import read_untrim_section

untrim_sections=read_untrim_section.section_hydro_to_dss(untrim_sections_fn)

##

# Find 7B for a nice comparison.
sec=[ sec for sec in untrim_sections if sec.attrs['name']=='7B'][0]

##

import stompy.model.delft.dflow_model as dfm
from stompy import xr_utils
import six
Beispiel #4
0
Source datasets: 
  SFEI wind station compilation
  Megan Williams met station

Air temperature 
"""
import matplotlib.pyplot as plt
import xarray as xr
from scipy.io import loadmat
from stompy.model import data_comparison
import statsmodels.formula.api as smf

import numpy as np
import pandas as pd
from stompy import utils
utils.path("../../model")
import local_config

##
# Load the SFEI data, adjusting to UTC.
dss=[]

for year in [2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021]:
    # Downloaded from
    # https://drive.google.com/drive/folders/1MNFXUUs6CaDyacxN5oCmtK_OjMHVQI7f
    ds_year=xr.open_dataset(os.path.join(local_config.data_dir,'sfei_wind',f'SFB_hourly_wind_and_met_data_{year}.nc'))
    ds_year['julian']=('time',),ds_year['time'].values
    ds_year['time']=('time',),( np.timedelta64(8,'h')
                                + np.datetime64(f'{year}-01-01')+np.timedelta64(1,'h')*np.round(24*(ds_year['julian'].values-1)))
    dss.append(ds_year)
Beispiel #5
0
from stompy.model.suntans import sun_driver
from stompy import utils, filters
from stompy.spatial import wkb2shp
from stompy.plot import plot_utils
import matplotlib.pyplot as plt
import six
import numpy as np
import xarray as xr
import os

try:
    base = os.path.dirname(__file__)
except NameError:
    base = "/home/rusty/src/hor_flow_and_salmon/model/suntans"

utils.path(os.path.join(base, "../../field/adcp"))
from stompy import xr_transect
import summarize_xr_transects

##
six.moves.reload_module(summarize_xr_transects)

# plot a few a sections
# Run from the run directory
# run_dir="."
import argparse

parser = argparse.ArgumentParser(
    description='Plot model-data comparison figures for a run.')

parser.add_argument("-d",
Beispiel #6
0
import os
import pandas as pd
import xarray as xr
import six
import logging
import numpy as np
import stompy.model.delft.dflow_model as dfm
from stompy.plot import plot_wkb, plot_utils
from stompy import utils
import stompy.grid.unstructured_grid as ugrid

# kludge because I don't keep this in my PYTHONPATH
utils.path("/home/rusty/src/hydro")

import matplotlib.pyplot as plt
import hydro.plotting.hydro_cal_plotter as hcp

plot_dir="NOT_SET_YET" # gets set at the end.

try:
    base_dir=os.path.dirname(__file__)
except NameError:
    logging.info("Assuming current directory is the dflowfm base")
    base_dir="."
    
# Moving cal data into the repo, now reference relative directory
csv_dir=os.path.join(base_dir,"calibration_data")
def csv(fn): return os.path.join(csv_dir,fn)

defaults = {
    'corr_datum': True,
"""
Ingest ADCP data, eventually with concurrent RTK data, and
develop merged DEM.
"""
from stompy import utils
from stompy.plot import plot_utils
import stompy.plot.cmap as scmap
from stompy.spatial import proj_utils

import glob
import pandas as pd
import re
import bathy

utils.path("../field/adcp")
import read_sontek

##

patts = [
    "../field/adcp/bathy_data/*.riv",
    "../field/adcp/040518_BT/*/*.rivr",
    "../field/adcp/040518_BT/*/*.riv",
]

rivr_fns = []
for patt in patts:
    rivr_fns.extend(glob.glob(patt))

##
plt.scatter(sync_det.epo,
            sync_det.tag_idx,
            20,
            sync_det.rx.cat.codes,
            cmap='jet')

#
mat = sync_det.groupby(['tag_idx', 'rx']).size().unstack('tag_idx')

# so SM10 and SM11 hear each other, but don't hear others very well
# HERE: any indications of offsets greater than 30s?

##

from stompy import utils
utils.path("../tags")
import prepare_pings

##
# Back to what's in combine_for_yaps

# pretty slow...
pm = prepare_pings.ping_matcher_2020(split_on_clock_change=True)
pm.allow_dual_bad_pings = True
# full range that seems to have a chance of resolving tracks
# This almost certainly too early and too late.
breaks = [
    np.datetime64('2020-03-01T00:00:00'),
    # Make a nice manageable chunk in the middle for testing
    # But there were no fish in that chunk
    np.datetime64('2020-03-17T00:00:00'),
Beispiel #9
0
import numpy as np
import pandas as pd
import six
import matplotlib.pyplot as plt
from matplotlib import collections
import xarray as xr
from stompy import utils
utils.path("/home/rusty/src/hor_flow_and_salmon/bathy")
from stompy.model import stream_tracer
from stompy.plot import plot_utils

##

ds = xr.open_dataset("current-summary.nc")
g = unstructured_grid.UnstructuredGrid.from_ugrid(ds)
g.edge_to_cells()

##

plt.figure(1).clf()
fig, ax = plt.subplots(num=1)
zoom = (583157., 588467., 4226178, 4230135)

# This has some issues in places where the starting bathy is bad, as those
# places start dry.  In most places, it's a decent check
#ccoll=g.plot_cells(values=ds.min_waterdepth.values,cmap='viridis',clip=zoom)
#ccoll.set_clim([0,0.1])
ccoll = g.plot_cells(values=ds.max_waterdepth.values,
                     cmap='viridis',
                     clip=zoom)
ccoll.set_clim([0, 0.5])
"""
Add bathy data to grid.
Had been in the grid directory, but moved here for quicker edit-run cycles
"""
from stompy import utils
from stompy.grid import unstructured_grid
from stompy.spatial import wkb2shp
import numpy as np
import os

utils.path(os.path.join(os.path.dirname(__file__), "../../bathy"))
import bathy


def add_bathy(g, suffix=''):
    dem = bathy.dem(suffix=suffix)

    z = dem(g.cells_center())
    z[np.isnan(z)] = 5.0
    g.add_cell_field('z_bed', z, on_exists='overwrite')

    return g


def postprocess(g, suffix=""):
    if suffix == "":
        pass
    elif suffix == 'med0':
        L = 35

        # First cut at anisotropic median filter.
Beispiel #11
0
"""
For each of the transects, summarize the data in its raw form. 
"""
from __future__ import print_function

# Read the collection of csv files associated with a sontek transect
import numpy as np
import pandas as pd
import xarray as xr
import matplotlib.pyplot as plt

import glob
import six
from stompy import utils

utils.path("/home/rusty/src/ADCPy")

import read_sontek
# import read_untrim_section
import transect_to_adcpy
from stompy.memoize import memoize
from stompy.plot import plot_utils
from stompy.spatial import proj_utils
from stompy import xr_transect, xr_utils

from stompy.model.delft import dfm_grid
import stompy.model.delft.dflow_model as dfm

from adcpy import adcpy
from adcpy import adcpy_recipes
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import collections
import track_common
from scipy.interpolate import interp1d

from stompy import utils
from scipy.stats import gaussian_kde
from matplotlib import gridspec, collections, widgets
from stompy.spatial import field
import seaborn as sns
import stompy.plot.cmap as scmap
from stompy.plot import plot_utils
turbo = scmap.load_gradient('turbo.cpt')

utils.path("../../manuscripts/swimming/fig_common/")

##

input_path = "screen_final"

df = track_common.read_from_folder(input_path)

vel_suffix = '_top2m'

df['track'].apply(track_common.calc_velocities,
                  model_u='model_u' + vel_suffix,
                  model_v='model_v' + vel_suffix)
track_common.clip_to_analysis_polygon(df, 'track')

##
    ax_dens.plot(utils.unix_to_dt64(t), kernel(t), label='KDE (bw=%s)' % bw)

ax_dens.plot(times, 0 * df_start.t_mid, 'k+', label='Tag arrivals')

ax_dens.legend(loc='upper left')
plt.setp(ax_dens.get_yticklabels(), visible=0)

# ax.plot(times, df_start.mean_swim_urel, 'g.',label='Mean downstream swimming')
# ax_lat.plot(times, df_start.mean_swim_lateral, 'b.',label='Mean lateral swimming')
# ax.legend(loc='upper right')
# ax_lat.legend(loc='upper right')
# fig.autofmt_xdate()
# ax.axhline(0.0,color='0.6',lw=1.0)

## MSD flows
utils.path("../../model/suntans")
import common

dates = dict(start_date=np.datetime64("2018-03-01"),
             end_date=np.datetime64("2018-04-15"))

msd_flow = common.msd_flow(**dates)
msd_velo = common.msd_velocity(**dates)

# msd_flow=fetch_and_parse(local_file="env_data/msd/flow-2018.csv",
#                          url="http://wdl.water.ca.gov/waterdatalibrary/docs/Hydstra/docs/B95820Q/2018/FLOW_15-MINUTE_DATA_DATA.CSV",
#                          skiprows=3,parse_dates=['time'],names=['time','flow_cfs','quality','notes'])

ax2.plot(msd_flow.time, msd_flow.flow_m3s, label='MSD Flow (m$^3$s)')
ax_dens.axis(xmin=times.min(), xmax=times.max())
Beispiel #14
0
from stompy.spatial import field, wkb2shp
import stompy.plot.cmap as scmap
from stompy.plot import plot_wkb, plot_utils
from stompy.grid import unstructured_grid
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import os, glob
from stompy import memoize
from stompy.io.local import cdec
from scipy import signal

yaps_dir = "../../../field/hor_yaps"
utils.path(yaps_dir)

import track_common

utils.path("../../../model/suntans")
import common
##

df_start = track_common.read_from_folder(os.path.join(yaps_dir,
                                                      'screen_final'))
t_min = df_start.track.apply(lambda t: t.tnum.min())
t_max = df_start.track.apply(lambda t: t.tnum.min())
t_mid = 0.5 * (t_min + t_max)
df_start['t_mid'] = t_mid

Beispiel #15
0
    src_ec=g_src.edges_center()
    src_ec_kdt=KDTree(data=src_ec)
    
    dest_ec=g_dest.edges_center()
    
    for j in utils.progress(range(g_dest.Nedges())):
        dist,src_edge=src_ec_kdt.query(dest_ec[j],distance_upper_bound=eps)
        if not np.isfinite(dist): continue
        for fld in edge_fields:
            g_dest.edges[fld][j]=g_src.edges[fld][src_edge]
            
copy_matching_fields(g_src=g_bay,g_dest=g_complete,
                     cell_fields=['depth'],edge_fields=['edge_depth'])

##
utils.path("..")

import bathy

dem=bathy.dem()

sel_cells=np.nonzero(np.isnan(g_complete.cells['depth']))[0]
centr=g_complete.cells_centroid()

g_complete.cells['depth'][sel_cells]=dem(centr[sel_cells])

sel_edges=np.nonzero(np.isnan(g_complete.edges['edge_depth']))[0]

e2c=g_complete.edge_to_cells()

nc1=e2c[sel_edges,0].copy()
Beispiel #16
0
model.sun_verbose_flag = "-v"

# enable average output for ptm

model.config['ntout'] = int(15 * 60 / dt_secs)  # 15 minutes
model.config['ntaverage'] = int(15 * 60 / dt_secs)  # 15 minutes
model.config['calcaverage'] = 1
model.config['averageNetcdfFile'] = "average.nc"
# some kind of issue here where particles can't move between
# cells when it's 2D.  Run in 3D instead.
model.config['Nkmax'] = 10

model.write()
model.partition()
model.run_model()

##

# Convert to format for PTM
utils.path("/home/rusty/src")
import soda.dataio.ugrid.suntans2untrim
six.moves.reload_module(soda.dataio.ugrid.suntans2untrim)
from soda.dataio.ugrid.suntans2untrim import suntans2untrim

for avg_output in model.avg_outputs():
    ptm_output = os.path.join(os.path.dirname(avg_output),
                              "ptm-" + os.path.basename(avg_output))
    suntans2untrim(avg_output, ptm_output, tstart=None, tend=None)

##
import sys
from stompy import utils

from stompy.grid import unstructured_grid
utils.path("../../../bathy")
import bathy


def process(grid_in, grid_out):
    g = unstructured_grid.UnstructuredGrid.from_ugrid(grid_in)

    dem = bathy.dem()

    # By default, just put depths onto the nodes straight from DEM
    g.add_node_field('depth', dem(g.nodes['x']))

    g.write_ugrid(grid_out, overwrite=True)


if __name__ == '__main__':
    process(sys.argv[1], sys.argv[2])
else:
    process('derived/grid_ugrid.nc', 'derived/grid_bathy_ugrid.nc')
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()

import numpy as np
import xarray as xr
import pandas as pd
import shapely.wkt
from six import iteritems
from importlib import reload

from stompy import utils
from stompy.model.delft import waq_scenario
reload(waq_scenario)

##
utils.path("/hpcvol1/Mugunthan/Scripts/RunLaunchers")
import BayDynamoMin_v2_rr_141 as BayDynamo
reload(BayDynamo) # just in case I made some modification to dynamo
## 

# Specify locations of outside data sources:

# POTW data, checked out in subdirectory:
potw_fn='/hpcvol1/zhenlin/sfbay/common/sfbay_potw/outputs/sfbay_potw.nc'

# Selection of hydrodynamics data:
# this has spatially variable shear stresses output from the hydrorun
rel_hyd_path="/hpcvol1/Mugunthan/Inputs/hydro/WY2013/agg141_tau-lp-pass_params/com-agg141_tau-lp.hyd"

# Load those external datasets:
sfbay_potw=xr.open_dataset(potw_fn)
Beispiel #19
0
"""
Test flow over a bump. 
"""

import stompy.grid.unstructured_grid as ugrid
from stompy import utils
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

# Rather than adding swampy to sys.path, assume this script is run
# from this folder and add the relative path
utils.path("../")
from swampy import swampy
import numpy as np
import six

from test_common import calc_Fr_CFL_Bern
six.moves.reload_module(swampy)
#


class SwampyBump1D(swampy.SwampyCore):
    downstream_eta = 0.33
    upstream_flow = 0.12  # m2/s

    W = 1
    L = 20  # channel length
    nx = int(20 / 0.05)  # number of nodes in along-channel direction
    ny = 2  # >=2, number of nodes in cross-channel direction

    theta = 0.55
Beispiel #20
0
import pandas as pd

from stompy import utils

utils.path("../../../field/hor_yaps")
import track_common

##

master_in="../../../field/hor_yaps/screen_final"

df=track_common.read_from_folder(master_in)

# Make this look like Ed's input:
df_ptm=pd.DataFrame()
df_ptm['track']=df['basename'].str.replace('.csv','')
df_ptm['tag']=df.index.values

##

# Find the x,y for the time of entry:
df_ptm['entry_time']=np.nan # if 1st detection was above array
df_ptm['first_detection_time']=np.nan  # But not before array
df_ptm['exit_time']=np.nan
df_ptm['x']=np.nan
df_ptm['y']=np.nan
df_ptm['route']=""

for idx,rec in df.iterrows():
    entry_t=rec['top_of_array_first']
    if np.isnan(entry_t):
Beispiel #21
0
# from osgeo import ogr,osr # NO
from shapely import wkt, geometry, wkb, ops  # ok
import matplotlib.pyplot as plt  # ok
# from stompy.spatial import gen_spatial_index
# ?, proj_utils
# import netCDF4 # NO

##
import xarray as xr  # ok

# import local_config # no, specifically the dfm import
###

###

utils.path("../model")  # ok
# import pesca_base

import subprocess, shutil  # ok
# from numpy.ctypeslib import ndpointer  # nd arrays
from ctypes import (
    # Types
    c_double,
    c_int,
    c_char_p,
    c_bool,
    c_char,
    c_float,
    c_void_p,
    # Complex types
    # ARRAY, Structure,
"""
Try using ADCPy to merge multiple transects.
"""
import os
import glob

from stompy import utils

utils.path(os.path.join(os.environ['HOME'], "src/ADCPy"))

##

from adcpy import adcpy
import read_sontek


class ADCPSontekM9(adcpy.ADCPData):
    def __init__(self, rivr_file, **kwargs):
        super(ADCPSontekM9, self).__init__(**kwargs)

        self.rivr_file = rivr_file
        self.convert_sontek(self.rivr_file)

    def convert_sontek(self, rivr_file):
        self.ds = read_sontek.surveyor_to_xr(rivr_file)

        # Set ADCPData members from self.ds

        # Starts with z as a positive-down quantity
        # use round(...,4) to drop some FP roundoff trash
        z_in = self.ds.location.values