API Reference¶
- class talsimpy.TalsimDataset(path, name)[source]¶
Class for handling and manipulating a Talsim ASCII dataset
Instantiates a new TalsimDataset instance
- Parameters:
- copy(destination, include_results=False)[source]¶
Copies the dataset to a destination directory, optionally including result files
- Parameters:
- Returns:
the destination dataset
- Return type:
- process_templates(variables)[source]¶
Processes template files in a Talsim dataset by replacing the appropriate parameter values
Notes
Template files must be named like the original file but with an additional extension
.template, e.g.dataset.ALL.template, and will replace the original file after variable substitution.Template files must contain placeholders for variables enclosed with curly braces, e.g.
{variable_name}Standard Python format specifiers (e.g.
{variable_name:formatspec}) are allowed but behave slightly differently than the default:floats: the given precision may be automatically reduced in order to fit the specified length
strings: strings may be cut to the specified length if too long
- Parameters:
variables (dict) – dict of variable names and values
{name: value, ...}- Return type:
None
- write_varfile(file_var, vars)[source]¶
Creates a Talsim VAR file containing the given variables
- Parameters:
file_var (str) – filename of VAR file to write
vars (dict[slice(<class 'str'>, talsimpy.timeseries.Timeseries | bool, None)]) – dictionary with identifiers as keys and variables as values. Values may be of type Timeseries or bool.
- Return type:
None
- property warnings: str | None¶
Returns the warnings from the last simulation run or None if none exist
- copy_result_files(dir_dest)[source]¶
Copy all result files to a destination directory
Copies all files with stem corresponding to the dataset name and one of the extensions in RESULT_EXTENSIONS
- calculate_average_soil_properties()[source]¶
Calculates the average soil properties for soil types over all layers
- Returns:
pandas Dataframe
- Return type:
- get_sim_options()[source]¶
Gets all options set in the ALL file
- Returns:
dictionary of keys and values as strings
- Return type:
- set_sim_options(options)[source]¶
Sets new options in the ALL file
- Parameters:
options (dict) – dictionary of keys and values to set. Keys must correspond to existing options in the ALL file
- Return type:
None
- set_calibration_parameters(parameters)[source]¶
Sets calibration parameters in the KAL file
- Parameters:
parameters (dict) – dictionary of keys and values to set. Keys must correspond to existing parameters in the KAL file
- Return type:
None
- class talsimpy.TalsimDatabase(path)[source]¶
Class for handling a Talsim 5 database
- Parameters:
path (Path | str)
- class talsimpy.TalsimScenario(path, scenario_id)[source]¶
Bases:
TalsimDatasetClass for handling and manipulating a scenario in a database
Instantiates a new TalsimScenario instance
- Parameters:
- db = None¶
Path to the database file
- id = None¶
Scenario id
- sim_id = None¶
Currently active simulation id
- simulations = {}¶
Dictionary of existing simulations
{id: name, ...}
- property active_simulation: int | None¶
Returns the currently active simulation
- Returns:
active simulation id or None if not set
- set_active_simulation(sim_id)[source]¶
Sets the active simulation id
- Parameters:
sim_id (int) – the simulation id to set
- Return type:
None
- copy(destination, include_results=False)[source]¶
Copies the scenario (i.e. the whole database) to a destination directory, optionally including result files
- Parameters:
- Returns:
the destination scenario
- Return type:
- set_sim_start(sim_start)[source]¶
Sets the simulation start date for the currently active simulation
- Parameters:
sim_start (datetime)
- Return type:
None
- class talsimpy.TalsimEngine(path)[source]¶
Class for carrying out simulations with Talsim
Instantiate a new TalsimEngine instance
- Parameters:
path (Path | str) – path to directory containing the engine executable
- simulate(dataset, name=None, variation_id=0, language='de', path_timeseries=None, sim_id=None, path_output=None)[source]¶
Carries out a simulation
- Parameters:
dataset (TalsimDataset | TalsimScenario) – TalsimDataset or TalsimScenario instance to simulate
name (str) – optional name for the output files (only relevant for TalsimScenario instances, defaults to scenario name)
variation_id (int) – optional Variation ID (default: 0)
language (str) – optional language (default: “de”)
path_timeseries (Path | str) – optional path to time series files (only relevant for TalsimScenario instances)
sim_id (int) – optional simulation id (only relevant for TalsimScenario instances, defaults to active simulation id)
path_output (Path | str) – optional output path (only relevant for TalsimScenario instances)
- Returns:
boolean success
- Return type:
- ktrcheck(dataset, language='de', path_output=None)[source]¶
Carries out a KTRCheck run
- Parameters:
dataset (TalsimDataset | TalsimScenario) – TalsimDataset or TalsimScenario instance to check
language (str) – optional language (default: “de”)
path_output – optional output path (only relevant for TalsimScenario instances)
- Returns:
boolean success
- Return type:
- class talsimpy.Timeseries(title='')[source]¶
Class for storing and manipulating time series
- nodes¶
dictionary of timestamps and corresponding values (is not automatically sorted by date!)
- Type:
Instantiates a new Timeseries object
- Parameters:
title (str) – Optional title
- class Interpretation(*values)[source]¶
Enum of interpretations as defined in sydrodomain.ini
- Instantaneous = 1¶
- BlockRight = 2¶
- BlockLeft = 3¶
- Cumulative = 4¶
- CumulativePerTimestep = 5¶
- Undefined = 99¶
- add_node(timestamp, value)[source]¶
Adds a new node consisting of timestamp, value to the timeseries
If a timestamp already exists, it is overwritten and a corresponding warning log message is emitted
- Parameters:
- Raises:
ValueError – if timestamp is not a valid datetime object
- Return type:
None
- plot(ax=None, **kwargs)[source]¶
Creates a simple line plot of the time series
- Parameters:
ax – an optional matplotlib.axes.Axes object to use
kwargs – keyword arguments passed to matplotlib.plot()
- Returns:
list of Line2D objects
- copy()[source]¶
create a copy of the time series
- Returns:
a copy of the Timeseries instance
- Return type:
- copy_metadata(ts)[source]¶
copies metadata from a second time series to this timeseries
- Parameters:
ts (Timeseries) – the time series from which to copy metadata
- Return type:
None
- property start: datetime¶
Returns the time series start date
- Returns:
start date or None if the time series is empty
- property end: datetime¶
Returns the time series end date
- Returns:
end date or None if the time series is empty
- write_to_file(filename, options={})[source]¶
Writes the timeseries to a file
Supported formats:
txt (with dateformat YYYYMMddHHmmss)
csv (with dateformat YYYY-MM-dd HH:mm:ss and comma as separator)
uvf
zrx
bin
- Parameters:
filename (Path | str) – path to file to write to. The file extension determines the format.
options (dict) –
optional dictionary with options, which can be format-specific
Supported options:
ZRX format:
”REXCHANGE”: a string that is used for setting the REXCHANGE header value
UVF format:
any key-value pair can be passed and will be written as an additional header entry in the format
key: value
- Return type:
None
- property dates: list[datetime]¶
returns a sorted list of the dates contained in the timeseries
- Returns:
list of datetime objects
- property values: list[float]¶
returns a list of the timeseries’ values sorted by date
- Returns:
list of values
- fill_gaps(dt='M')[source]¶
Fills date gaps with NaN values
- Parameters:
dt (str) – timestep, either “d” for day or “M” for month (default)
- Return type:
None
- count_value_nodes()[source]¶
Returns the number of nodes with non-NaN values in the time series
- Returns:
number of non-NaN values (int)
- Return type:
- delete_negative_nodes()[source]¶
Deletes nodes with negative values from the time series
- Return type:
None
- static read_file(filename, format=None)[source]¶
Reads a timeseries from a file
Supported formats:
uvf
zrx
bin
Note
title is not read from the file
- Parameters:
- Return type:
- static read_wbl(filename, series=[])[source]¶
Class method for reading one or more series from a WBL file
- static read_wel(filename, series=[])[source]¶
Class method for reading one or more series from a WEL file. Also supports WBL files.
- static read_wel_to_dataframe(filename, series=[])[source]¶
Class method for reading one or more series from a WEL file as a pandas Dataframe
- static read_fews(filename)[source]¶
Reads timeseries from a FEWS PI Timeseries XML file
Schema: http://fews.wldelft.nl/schemas/version1.0/pi-schemas/pi_timeseries.xsd
- static write_fews(filename, ts_dict)[source]¶
Writes timeseries to a FEWS PI Timeseries XML file
Schema: http://fews.wldelft.nl/schemas/version1.0/pi-schemas/pi_timeseries.xsd
- static synchronize(ts1, ts2)[source]¶
Synchronizes two time series by only keeping the nodes with identical dates/times.
- Parameters:
ts1 (Timeseries) – 1st Timeseries
ts2 (Timeseries) – 2nd Timeseries
- Returns:
tuple of two new synchronized Timeseries
- Return type:
- static to_dataframe(timeseries)[source]¶
Converts a list of Timeseries to a pandas.DataFrame
- Parameters:
timeseries (Timeseries | list[Timeseries]) – a Timeseries object (or list of Timeseries objects) to convert
- Returns:
the DataFrame
- Return type: