The SFRData Module

class sfrmaker.sfrdata.SFRData(reach_data=None, segment_data=None, grid=None, model=None, isfr=None, model_length_units='undefined', model_time_units='days', enforce_increasing_nsegs=True, default_slope=0.001, minimum_slope=0.0001, maximum_slope=1.0, package_name='model', **kwargs)[source]

Bases: DataPackage

Class for working with a streamflow routing (SFR) dataset, where the stream network is discretized into reaches contained within individual model cells. Reaches may be grouped into segments, with routing between segments specified, and routing between reaches within segments based on consecutive numbering (as in MODFLOW-2005). In this case, unique identifier numbers will be assigned to each reach (in the rno column of the reach_data table), and routing connections between rnos will be computed. Alternatively, reaches and their routing connections can be specified directly, as in MODFLOW-6. In this case, MODFLOW-2005 input will be written with one reach per segment.

Parameters:
reach_dataDataFrame

Table containing information on the SFR reaches.

segment_dataDataFrame

Table containing information on the segments (optional).

gridsfrmaker.grid class instance
model_length_unitsstr

‘meters’ or ‘feet’

model_time_unitsstr

‘s’: seconds ‘meters’: minutes ‘h’: hours ‘d’: days ‘y’: years

enforce_increasing_nsegsbool

If True, segment numbering is checked to ensure that it only increases downstream, and reset if it doesnt.

package_namestr

Base name for writing sfr output.

kwargskeyword arguments

Optional values to assign globally to SFR variables. For example icalc=1 would assign all segments an icalc value of 1. For default values see the sfrdata.defaults dictionary. Default values can be assigned using MODFLOW-2005 or MODFLOW-6 terminology.

add_observations(data, flowline_routing=None, obstype=None, sfrlines_shapefile=None, x_location_column=None, y_location_column=None, line_id_column=None, rno_column=None, obstype_column=None, obsname_column='site_no', gage_starting_unit_number=250)[source]
add_to_perioddata(data, flowline_routing=None, variable='inflow', line_id_column=None, rno_column=None, period_column='per', data_column='Q_avg', one_inflow_per_path=False, distribute_flows_to_reaches=False)[source]
add_to_segment_data(data, flowline_routing, variable='flow', line_id_column=None, segment_column='segment', period_column='per', data_column='Q_avg', one_inflow_per_path=False)[source]
assign_layers(adjusted_botm_output_path='.')[source]

Assign model layers to SFR reaches, using the discretzation package in the attached model. New botm elevations for the model will be written to a text array file, if any streambed bottoms are below the model bottom.

Parameters:
adjusted_botm_output_pathstr

Path for writing the text array of adjusted model bottom elevations, by default, ‘.’

property const
create_mf6sfr(model=None, unit_conversion=None, stage_filerecord=None, budget_filerecord=None, flopy_rno_input_is_zero_based=True, **kwargs)[source]
create_modflow_sfr2(model=None, const=None, isfropt=1, unit_number=None, ipakcb=None, istcb2=None, **kwargs)[source]
property crs
property crs_units

Length units of the coordinate reference system

defaults = {'gage_starting_unit_number': 250, 'icalc': 1, 'istcb2': 223, 'roughch': 0.037, 'strhc1': 1, 'strthick': 1}
dtypes = {'geometry': <class 'object'>, 'i': <class 'int'>, 'icalc': <class 'int'>, 'iprior': <class 'int'>, 'ireach': <class 'int'>, 'iseg': <class 'int'>, 'iupseg': <class 'int'>, 'j': <class 'int'>, 'k': <class 'int'>, 'line_id': 'object', 'name': <class 'object'>, 'node': <class 'int'>, 'nseg': <class 'int'>, 'nstrpts': <class 'int'>, 'outreach': <class 'int'>, 'outseg': <class 'int'>, 'per': <class 'int'>, 'rno': <class 'int'>}
export_observations(filename=None, geomtype='point')[source]
export_outlets(filename=None)[source]

Export shapefile of model cells with stream reaches.

export_routing(filename=None)[source]

Export linework shapefile showing all routing connections between SFR reaches. A length field containing the distance between connected reaches can be used to filter for the longest connections in a GIS.

export_transient_variable(varname, filename=None)[source]

Export point shapefile showing locations with a given segment_data variable applied. For example, segments where streamflow is entering or leaving the upstream end of a stream segment (FLOW) or where RUNOFF is applied. Cell centroids of the first reach of segments with non-zero terms of varname are exported; values of varname are exported by stress period in the attribute fields (e.g. flow0, flow1, flow2… for FLOW in stress periods 0, 1, 2…

Parameters:
fstr, filename
varnamestr

Variable in SFR Package dataset 6a (see SFR package documentation)

classmethod from_package(sfrpackagefile, grid, namefile=None, sim_name=None, model_ws='.', version=None, model_name='model', package_name=None, linework=None)[source]

Read SFR package file

Parameters:
sfrpackagefilefile path

Modflow-2005 or MODFLOW6 SFR package

gridsfrmaker.grid instance
lineworkshapefile path or DataFrame

Contains linestrings for each reach; must have segment and reach, or reach number (rno in MODFLOW 6) information.

Returns:
sfrdatasfrmaker.sfrdata instance
classmethod from_tables(reach_data, segment_data, grid=None, isfr=None)[source]
classmethod from_yaml(config_file, package_name=None, output_path=None, write_output=True)[source]

Create an SFRData instance from a yaml-format configuration file.

Parameters:
config_filestr or mapping

Path to Sfrmaker configuration file in yaml format, or a dictionary-like mapping.

output_pathstr

Where output files will be saved. Default is ‘.’ (in default_config.yml).

package_namestr

Optional argument for naming the output SFR package; otherwise can be specified in configuration file.

write_outputbool

Whether or not to write output files, including package file, reach and segment data tables and shapefiles.

Returns:
sfrdatasfrmaker.SFRData instance
classmethod get_empty_reach_data(nreaches=0, default_value=0)[source]
classmethod get_empty_segment_data(nsegments=0, default_value=0)[source]
interpolate_to_reaches(segvar1, segvar2, per=0)[source]

Interpolate values in datasets 6b and 6c to each reach in stream segment

Parameters:
segvar1str

Column/variable name in segment_data array for representing start of segment (e.g. hcond1 for hydraulic conductivity) For segments with icalc=2 (specified channel geometry); if width1 is given, the eigth distance point (XCPT8) from dataset 6d will be used as the stream width. For icalc=3, an abitrary width of 5 is assigned. For icalc=4, the mean value for width given in item 6e is used.

segvar2str

Column/variable name in segment_data array for representing start of segment (e.g. hcond2 for hydraulic conductivity)

perint

Stress period with segment data to interpolate

Returns:
reach_values1D array

One dimmensional array of interpolated values of same length as reach_data array. For example, hcond1 and hcond2 could be entered as inputs to get values for the strhc1 (hydraulic conductivity) column in reach_data.

isfropt0_to_1()[source]

transfer isfropt=0 segment properties to reaches, using linear interpolation.

len_const = {0: 1.0, 1: 1.486, 2: 1.0, 3: 100.0}
mf5names = {'depth1': 'depth1', 'depth2': 'depth2', 'evaporation': 'etsw', 'inflow': 'flow', 'man': 'roughch', 'rainfall': 'pptsw', 'rbth': 'strthick', 'rgrd': 'slope', 'rhk': 'strhc1', 'rlen': 'rchlen', 'rno': 'rno', 'rtp': 'strtop', 'runoff': 'runoff', 'rwid': 'width'}
mf6names = {'depth1': 'depth1', 'depth2': 'depth2', 'etsw': 'evaporation', 'flow': 'inflow', 'pptsw': 'rainfall', 'rchlen': 'rlen', 'rno': 'rno', 'roughch': 'man', 'runoff': 'runoff', 'slope': 'rgrd', 'strhc1': 'rhk', 'strthick': 'rbth', 'strtop': 'rtp', 'width': 'rwid'}
property model
property modflow_sfr2

A flopy.modflow.mfsfr2.ModflowSfr2 represenation of the sfr dataset.

property observations
property observations_file
property package_name
package_type = 'sfr'
property paths

Dict listing routing sequence for each segment in SFR network.

property period_data
rdcols = ['rno', 'node', 'k', 'i', 'j', 'iseg', 'ireach', 'rchlen', 'width', 'slope', 'strtop', 'strthick', 'strhc1', 'thts', 'thti', 'eps', 'uhc', 'outreach', 'outseg', 'asum', 'line_id', 'name', 'geometry']
property reach_paths

Dict listing routing sequence for each segment in SFR network.

repair_outsegs()[source]

Set any outsegs that are not nsegs or lakes to 0 (outlet status)

reset_reaches()[source]

Ensure that the reaches in each segment are numbered consecutively starting at 1.

reset_segments()[source]

Reset the segment numbering so that is consecutive, starts at 1 and only increases downstream.

property rno_routing
run_diagnostics(checkfile=None, **kwargs)[source]

Run the Flopy SFR diagnostic suite.

Parameters:
checkfilestr

Path of file to write results to, by default, results are written to {}_SFR.chk, where {} is the SFRData.package_name attribute.

kwargskeyword arguments to flopy.modflow.ModflowSfr2.check()
sample_reach_elevations(dem, method='buffers', buffer_distance=100, smooth=True)[source]

Computes zonal statistics on a raster for SFR reaches, using either buffer polygons around the reach LineStrings, or the model grid cell polygons containing each reach.

Parameters:
dempath to valid raster dataset

Must be in same Coordinate Reference System as model grid.

methodstr; ‘buffers’ or ‘cell polygons’

If ‘buffers’, buffers (with flat caps; cap_style=2 in LineString.buffer()) will be created around the reach LineStrings (geometry column in reach_data).

buffer_distancefloat

Buffer distance to apply around reach LineStrings, in crs_units.

smoothbool

Run sfrmaker.elevations.smooth_elevations on sampled elevations to ensure that they decrease monotonically in the downstream direction (default=True).

Returns:
elevsdict of sampled elevations keyed by reach number
sdcols = ['per', 'nseg', 'icalc', 'outseg', 'iupseg', 'iprior', 'nstrpts', 'flow', 'runoff', 'etsw', 'pptsw', 'roughch', 'roughbk', 'cdpth', 'fdpth', 'awdth', 'bwdth', 'hcond1', 'thickm1', 'elevup', 'width1', 'depth1', 'thts1', 'thti1', 'eps1', 'uhc1', 'hcond2', 'thickm2', 'elevdn', 'width2', 'depth2', 'thts2', 'thti2', 'eps2', 'uhc2']
property segment_routing
set_outreaches()[source]

Determine the outreach for each SFR reach (requires a rno column in reach_data). Uses the segment routing specified for the first stress period to route reaches between segments.

set_streambed_top_elevations_from_dem(filename, elevation_units=None, dem=None, method='buffers', **kwargs)[source]

Set streambed top elevations from a DEM raster. Runs sfrdata.sample_reach_elevations

Parameters:
filenamepath to valid raster dataset

Must be in same Coordinate Reference System as model grid.

elevation_unitsstr

Elevation units for DEM (‘feet’ or ‘meters’). If None, units are assumed to be same as model (default).

methodstr; ‘buffers’ or ‘cell polygons’

If ‘buffers’, buffers (with flat caps; cap_style=2 in LineString.buffer()) will be created around the reach LineStrings (geometry column in reach_data).

kwargskeyword arguments to sfrdata.sample_reach_elevations
Returns:
updates strtop column of sfrdata.reach_data
property structured
time_const = {1: 1.0, 2: 60.0, 3: 3600.0, 4: 86400.0, 5: 31557600.0}
to_riv(segments=None, rno=None, line_ids=None, drop_in_sfr=True)[source]

Cast one or more reaches to a RivData instance, which can then be written as input to the MODFLOW RIV package.

Parameters:
segmentssequence of ints

Convert the listed segments, and all downstream segments, to the RIV package. If None, all segments are converted (default).

rnosequence of ints

Convert the listed reach numbers (nro), and all downstream reaches, to the RIV package. If None, all reaches are converted (default).

line_idssequence of ints or strings

Convert the segments corresponding to the line_ids (unique identifiers for LineString features in the source hydrography), and all downstream segments to the RIV package. If None, all reaches are converted (default).

drop_in_sfrbool

Whether or not to remove the converted segments from the SFR package (default True)

Returns:
rivSFRmaker.RivData instance
update_slopes(default_slope=None, minimum_slope=None, maximum_slope=None)[source]

Compute slopes by reach using values in strtop (streambed top) and rchlen (reach length) columns of reach_data. The slope for a reach n is computed as strtop(n) - strtop(n+1) / rchlen(n). Slopes for outlet reaches are set equal to a default value (default_slope). Populates the slope column in reach_data.

Parameters:
default_slopefloat

Slope value applied to outlet reaches (where water leaves the model). By default None, in which case ‘_default_slope’ attribute is used.

minimum_slopefloat

Assigned to reaches with computed slopes less than this value. This ensures that the Manning’s equation won’t produce unreasonable values of stage (in other words, that stage is consistent with assumption that streamflow is primarily drive by the streambed gradient). By default None, in which case ‘_minimum_slope’ attribute is used.

maximum_slopefloat

Assigned to reaches with computed slopes more than this value. By default None, in which case ‘_maximum_slope’ attribute is used.

Notes

If default_slope, minimum_slope or maximum_slope are not None, the respective attribute (for example ‘_default_slope’) will be set with the supplied value.

write_gage_package(filename=None, sitename_col='obsname', gage_package_unit=25, gage_starting_unit_number=None)[source]

Write observation input for the MODFLOW-2005 Gage Package.

Parameters:
filenamestr, optional

Gage package file, by default None, in which case SFRData.observations_file is used.

sitename_colstr

Unique name or number for each gage site. By default, ‘obsname’, which is the default field in the SFRData.observations table containing either site numbers or site number-variable pairs.

gage_package_unitint, optional

Unit number for Gage Package, by default 25

gage_starting_unit_numberint, optional

Starting unit number for gage output files, by default None

Returns:
gagflopy.modflow.mfgage.ModflowGage instance
write_mf6_sfr_obsfile(filename=None, sfr_output_filename=None)[source]
write_package(filename=None, version='mf2005', idomain=None, options=None, run_diagnostics=True, write_observations_input=True, external_files_path=None, gage_starting_unit_number=None, **kwargs)[source]

Write an SFR package input file.

Parameters:
filenamestr, optional

File name for SFR package. By default None, in which case the filename of the attached flopy.modflow.mfsfr2.ModflowSfr2 instance (SFRData.modflow_sfr2.fn_path) is used.

versionstr, optional, {‘mf2005’, ‘mfnwt’, ‘mf6’}

MODFLOW version for the SFR package, by default ‘mf2005’

idomainndarray, optional

3D numpy array designating active cells (idomain==1). SFR reaches in inactive cells will be written with ‘none’ in the cellid field. by default None

optionslist, optional

List of strings to write to the MODFLOW-6 SFR options block. For example:

options=['save_flows',
         'BUDGET FILEOUT model.sfr.cbc',
         'STAGE FILEOUT model.sfr.stage.bin']
  • An appropriate unit_conversion is written by default.

  • Cell-by-cell budget and stage output files are also added by default if not specified.

  • If the SFRData.observations is populated and write_observations_input=True, the observation input file is also added.

  • All files added by default are placed in the same path as the SFR package file. If another location is desired, the file(s) need to be specified explicitly as strings in the options list.

See MODFLOW-6 documentation for other options. By default None.

run_diagnosticsbool, optional

Option to run the diagnostic checks. by default True

write_observations_inputbool, optional

Option to write input to the the MODFLOW-2005 gage package or MODFLOW-6 Observations Process. Requires attached observations, as added through the SFRData.add_observations() method. by default True

external_files_pathstr, optional

Path for writing an external file for packagedata, relative to the location of the SFR package file. If specified, an open/close statement referencing the file is written to the packagedata block. By default, None (packagedata table is written to the SFR package file)

gage_starting_unit_numberint, optional

Starting unit number for gage output files, by default None

write_tables(basename=None)[source]

Write reach_data, segment_data, and period_data (if populated) to csv files.

Parameters:
basenamestr, optional

Base name for csv files, by default None, in which case SFRData.package_name is used.