stompy.model.delft — Tools related Deltares models, D-Flow FM and D-WaQ¶
Submodules¶
stompy.model.delft.dflow_grid module¶
stompy.model.delft.dfm_grid module¶
stompy.model.delft.hydro_utils module¶
Tools for additional manipulations and data munging related to Hydro objects, but not central to typical D-WAQ hydro usage.
-
stompy.model.delft.hydro_utils.
extract_velocity
(hydro, xy, start_time, end_time)[source]¶ Not sure about the code location here… Try to pull together grid geometry to map point xy to an element, then to the set of segments, then exchanges, estimate a cell-centered velocity based on the exchange fluxes. sketchy.
-
stompy.model.delft.hydro_utils.
extract_water_level
(hydro, xy, start_time, end_time)[source]¶ Not sure about the code location here… Try to pull together grid geometry to map point xy to an element, then to the set of segments, evaluate their volumes within the given time range, normalize by planform area, and pack the resulting time series of free surface elevation into an xarray Dataset. too much going on…
stompy.model.delft.io module¶
-
class
stompy.model.delft.io.
MDUFile
(filename=None, text=None)[source]¶ Bases:
stompy.model.delft.io.SectionedConfig
Read/write MDU files, with an interface similar to python’s configparser, but better support for discerning and retaining comments
-
name
¶ base name of mdu filename, w/o extension, which is used in various other filenames.
-
-
class
stompy.model.delft.io.
SectionedConfig
(filename=None, text=None)[source]¶ Bases:
object
Handles reading and writing of config-file like formatted files. Follows some of the API of the standard python configparser
-
entries
()[source]¶ Generator which iterates over rows, parsing them into index, section, key, value, comment.
key is always present, but might indicate a section by including square brackets. value may be a string, or None. Strings will be trimmed comment may be a string, or None. It includes the leading comment character. Indices are not stable across set_value(), since new entries may get inserted into the middle of a section and shift other rows down.
-
filepath
(sec_key)[source]¶ Lookup a filename via a (‘section’,’name’) tuple, and return the full filename including base path. if the key does not exist or is empty, return None.
-
get_value
(sec_key)[source]¶ return the string-valued settings for a given key. if they key is not found, returns None. If the key is present but with no value, returns the empty string
-
inline_comment_prefixes
= ('#', ';')¶
-
set_filename
(fn)[source]¶ Updates self.filename and base_path, in anticipation of the file being written to a new location (this is so new file paths can be extrapolated before having to write this out)
-
write
(filename=None)[source]¶ Write this config out to a text file. filename: defaults to self.filename check_changed: if True, and the file already exists and is not materially different,
then do nothing. Good for avoiding unnecessary changes to mtimes.backup: if true, copy any existing file to <filename>.bak
-
-
stompy.model.delft.io.
add_suffix_to_feature
(feat, suffix)[source]¶ Utility method, takes a feature as returned by read_pli (name,
[ [x0,y0],[x1,y1],…], { [node_label0,node_label1,…] } # optional)
and adds a suffix to the name of the feature and the names of nodes if they exist
-
stompy.model.delft.io.
bal_his_file_xarray
(fn, region_exclude=None, region_include=None)¶ Read a delwaq balance file, return the result as an xarray. region_exclude: regular expression for region names to omit from the result region_include: regular expression for region names to include.
Defaults to returning all regions.
-
stompy.model.delft.io.
create_restart
(res_fn, map_fn, hyd, state_vars=None, map_net_cdf=False, extr_time=None, start_time=None)[source]¶ Create a restart file using an exisiting map file and a user defined time.
res_fn: path/file name of the restart file map_fn: provide either a binary or net CDF file. Default is the raw binary output. hyd: waq_scenario.Hydro() object state_vars: an array that provides the state variables used in the original model run. Name must match the information written to map file map_net_cdf: set this flag to True if providing net CDF output that has
already been created by read_map- extr_time: a date string that will be converted internally to a
- numpy datetime object.If not provided, will use the last time step of the mapfile
- start_time: a date string that will be converted internally to a numpy
- datetime object. If not provided will be set as hydro.
[author: Pradeep Mugunthan]
-
stompy.model.delft.io.
dataset_to_dfm_wind
(ds, period_start, period_stop, target_filename_base, extra_header=None, min_records=1, wind_u='wind_u', wind_v='wind_v', pres='pres')[source]¶ Write wind in an xarray dataset to a pair of gridded meteo files for DFM.
- ds:
- xarray dataset. Currently fairly brittle assumptions on the format of this dataset, already in the proper coordinates system, coordinates of x and y, and the wind variables named wind_u and wind_v.
- period_start,period_stop:
- include data from the dataset on or after period_start, and up to period_stop,
inclusive target_filename_base:
the path and filename for output, without the .amu and .amv extensions.- extra_header:
- extra text to place in the header. This is included as is, with the exception that a newline will be added if it’s missing
returns the number of available records overlapping the requested period. If that number is less than min_records, no output is written.
-
stompy.model.delft.io.
dfm_wind_to_nc
(wind_u_fn, wind_v_fn, nc_fn)[source]¶ Transcribe DFM ‘arcinfo’ style gridded wind to CF compliant netcdf file (ready for import to erddap)
Note that the order of rows in the DFM format is weird, and required bug fixes to this code 2017-12-21. While dy is specified positive, the rows of data are written from north to south. The DFM text file specifies coordinates for a llcorner and a dy, but that llcorner corresponds to the first column of the last row of data written out.
- wind_u_fn:
- path to the amu file for eastward wind
- wind_v_fn:
- path to the amv file for northward wind
- nc_fn:
- path to the netcdf file which will be created.
-
stompy.model.delft.io.
exp_z_layers
(mdu, zmin=None, zmax=None)[source]¶ This will probably change, not very flexible now. For singly exponential z-layers, return zslay, positive up, starting from the bed. first element is the bed itself.
- zmin: deepest depth, positive up. Defaults to ds.NetNode_z.min(),
- read from the net file specified in mdu.
zmax: top of water column. Defaults to WaterLevIni in mdu.
-
stompy.model.delft.io.
grid_to_pli_data
(g, node_fields, labeler=None)[source]¶ UnstructuredGrid => PLI translation translate the edges of g into a list of features as returned by read_pli() features are extracted as contiguous linestrings, as long as possible. node_fields is a list giving a subset of the grid’s node fields to be written out, in addition to x and y. labeler: leave as None to get Lnnn style labels. Otherwise, a function
which takes the index, and returns a string for the label.
-
stompy.model.delft.io.
his_file_xarray
(fn, region_exclude=None, region_include=None)[source]¶ Read a delwaq balance file, return the result as an xarray. region_exclude: regular expression for region names to omit from the result region_include: regular expression for region names to include.
Defaults to returning all regions.
-
stompy.model.delft.io.
inp_tok_include
(fp, fn, **kw)[source]¶ Wrap inp_tok and handle INCLUDE tokens transparently. Note that also requires the filename
-
stompy.model.delft.io.
map_add_z_coordinate
(map_ds, total_depth='TotalDepth', coord_type='sigma', layer_dim='layer')[source]¶ For an xarray representation of dwaq output, where the total depth has been recorded, add an inferred vertical coordinate in the dataset. This is necessary to allow the ugrid visit reader to understand the file. Currently only sigma coordinates, assumed to be evenly spaced, are supported.
total_depth: Name of the xarray variable in map_ds holding total water column depth for each segment. coord_type: type of coordinate, currently must be “sigma”. layer_dim: name of the vertical dimension in the data.
Makes an arbitrary assumption that the first output time step is roughly mean sea level. Obviously wrong, but a starting point.
Given the ordering of layers in dwaq output, the sigma coordinate created here is decreasing from 1 to 0.
Modifies map_ds in place, also returning it.
-
stompy.model.delft.io.
parse_boundary_conditions
(inp_file)[source]¶ Parse section 5 of DWAQ input file. Returns bcs,items bcs: BC links items: match data and bc links.
- strings are folded to lowercase
-
stompy.model.delft.io.
parse_his_file
(fn)[source]¶ you probably want mon_his_file_dataframe() or bal_his_file_dataframe() – parse mixed ascii/binary history files as output by delwaq. applies to both monitoring output and balance output.
- returns tuple:
- sim_descs - descriptive text from inp file. time0 - text line giving time origin and units regions - names of regions with numeric index (1-based as read from file) fields - names of fields, separate into substance and process frames - actual data, and timestamps
-
stompy.model.delft.io.
parse_inp_monitor_locations
(inp_file)[source]¶ returns areas[name]=>[seg1,…] , transects[name]=>[+-exch1, …] ONE-BASED return values.
-
stompy.model.delft.io.
parse_time0
(time0)[source]¶ return a np.datetime64 for the time stamp, and the time unit in seconds (almost always equal to 1 second) input format is: b’T0: 2012/08/07-00:00:00 (scu= 1s)’
-
stompy.model.delft.io.
pli_to_grid_edges
(g, levees)[source]¶ g: UnstructuredGrid levees: polylines in the format returned by stompy.model.delft.io.read_pli, i.e. a list of features [
- [ ‘name’,
- [ [x,y,z,…],…], [‘node0’,…]
], …
]
returns an array of length g.Nedges(), with z values from those features mapped onto edges. when multiple z values map to the same grid edge, the minimum value is used. grid edges which do not have a levee edge get nan.
-
stompy.model.delft.io.
read_bnd
(fn)[source]¶ Parse DWAQ-style boundary data file
Returns a list [ [‘boundary_name’,array([ boundary_link_idx,[[x0,y0],[x1,y1]] ])], …]
-
stompy.model.delft.io.
read_dfm_bc
(fn)[source]¶ Parse DFM new-style BC file, returning a hash of Name => xarray dataset.
-
stompy.model.delft.io.
read_dfm_tim
(fn, ref_time, time_unit='M', columns=['val1', 'val2', 'val3'])[source]¶ Parse a tim file to xarray Dataset. Must pass in the reference time (datetime64, or convertable to that via utils.to_dt64()) and the time_unit (‘s’ or ‘m’)
time_unit: ‘S’ for seconds, ‘M’ for minutes. Relative to model reference time. Probably ought to be ‘M’ always.
returns Dataset with ‘time’ dimension, and data columns labeled according to columns.
-
stompy.model.delft.io.
read_map
(fn, hyd=None, use_memmap=True, include_grid=True, return_grid=False)[source]¶ Read binary D-Water Quality map output, returning an xarray dataset.
fn: path to .map file hyd: waq_scenario.Hydro() object. In the past this could be a path,
but to avoid an apparent circular import, this must now be a Hydro object.- use_memmap: use memory mapping for file access. Currently
- this must be enabled.
- include_grid: the returned dataset also includes grid geometry, suitable
- for unstructured_grid.from_ugrid(ds). WARNING: there is currently a bug which causes this grid to have errors. probably a one-off error of some sort.
note that missing values at this time are not handled - they’ll remain as the delwaq standard -999.0.
-
stompy.model.delft.io.
read_pli
(fn, one_per_line=True)[source]¶ Parse a polyline file a la DFM inputs. Return a list of features: [ (feature_label, N*M values, N labels), … ] where the first two columns are typically x and y, but there may be more columns depending on the usage. If no labels are in the file, the list of labels will be all empty strings.
Generally assumes that the file is honest about the number of fields, but some files (like boundary condition pli) will add a text label for each node.
- one_per_line: for files which add a label to each node but say nothing of
- this in the number of fields, one_per_line=True will assume that each line of the text file has exactly one node, and any extra text becomes the label.
-
stompy.model.delft.io.
write_pli
(file_like, pli_data)[source]¶ Reverse of read_pli. file_like: a string giving the name of a file to be opened (clobbering an existing file), or a file-like object. pli_data: [ (label, N*M values, [optional N labels]), … ] typically first two values of each row are x and y, and the rest depend on intended usage of the file
stompy.model.delft.nefis module¶
stompy.model.delft.nefis_dev module¶
stompy.model.delft.nefis_nc module¶
Converting between nefis and netcdf In a separate module to separate dependencies
Relies on the qnc wrapper of netcdf4
-
stompy.model.delft.nefis_nc.
nefis_to_nc
(nef, squeeze_unl=True, squeeze_element=True, short_if_unique=True, to_lower=True, unl_name='time', element_map={}, nc_kwargs={}, nc=None)[source]¶ nef: an open Nefis object squeeze_unl: unit length unlimited dimensions in groups are dropped
groups can’t be dimensionless, so parameter values are often in a unit-length group.- short_if_unique: element names which appear in only one group get
- just the element name. others are group_element
to_lower: make names lower case squeeze_element: unit dimensions in elements are also removed unl_name: if there is a unique unlimited dimension length (ignoring
unit lengths if squeeze_unl is set) - use this name for it.- element_map: map original element names to new names. this matches
- against element names before to_lower (but after strip), and the results will not be subject to to_lower.
nc_kwargs: dict of argument to pass to qnc.empty nc: altenatively, an already open QDataset