Database
Represents a MIKE+ model database.
Usage
Database(
model_path,
*,
auto_open=True,
)Attributes
| Name | Description |
|---|---|
| active_model | Get the name of the active model. |
| active_scenario | Active scenario. |
| active_simulation | Get the active simulation of the database. |
| alternative_groups | Access to MIKE+ alternative groups. |
| db_path | Get the path to the database file. |
| is_open | Check if the database is open. |
| mupp_path | Get the path to the MUPP file. |
| projection_string | Get the projection string of the database. |
| scenarios | Access to MIKE+ scenario management. |
| srid | Get the Spatial Reference ID (SRID) of the database. |
| tables | A collection of tables in the database. |
| unit_system | Get the unit system of the database in MIKE+ format. |
| version | Get the version of the database. |
active_model
Get the name of the active model.
active_model: str
active_scenario
Active scenario.
active_scenario: Scenario
Notes
This can be set to a Scenario (for example from scenarios.by_name()) to activate a different scenario. For more advanced scenario management, use the scenarios property.
Raises
ValueError- If the database is not open or its scenarios are not initialized
active_simulation
Get the active simulation of the database.
active_simulation: str
alternative_groups
Access to MIKE+ alternative groups.
alternative_groups: AlternativeGroupCollection
Raises
ValueError- If the database is not open
db_path
Get the path to the database file.
db_path: Path
is_open
Check if the database is open.
is_open: bool
mupp_path
Get the path to the MUPP file.
mupp_path: Path | None
projection_string
Get the projection string of the database.
projection_string: str
scenarios
Access to MIKE+ scenario management.
scenarios: ScenarioCollection
Raises
ValueError- If the database is not open
srid
Get the Spatial Reference ID (SRID) of the database.
srid: int
tables
A collection of tables in the database.
tables: TableCollection
This property provides access to all tables in the database through a fluent interface that allows for SQL-like operations on tables. It is the primary entry point for working with tables.
unit_system
Get the unit system of the database in MIKE+ format.
unit_system: str
version
Get the version of the database.
version: str
Methods
| Name | Description |
|---|---|
| __enter__() | Context manager entry. |
| __exit__() | Context manager exit. |
| __init__() | Initialize a new Database. |
| __repr__() | Get nice string representation. |
| begin_transaction() | Begin the data transaction. |
| close() | Close the model database. Closing a closed database does nothing. |
| create() | Create a new MIKE+ model database. |
| end_transaction() | End the data transaction. |
| ensure_mupp() | Get the model’s project file (.mupp), writing one if it has none. |
| import_from_epanet() | Import a model from an EPANET .inp file. |
| import_from_swmm() | Import a model from a SWMM .inp file. |
| open() | Open the model database. |
| run() | Run a simulation. |
__enter__()
Context manager entry.
Usage
__enter__()Returns
Database- The opened database.
__exit__()
Context manager exit.
Usage
__exit__(exc_type, exc_val, exc_tb)__init__()
Initialize a new Database.
Usage
__init__(model_path, *, auto_open=True)Parameters
model_path: str or Path-
Path to the model database file (e.g. “model.sqlite” or “model.mupp”)
auto_open: bool = True- If True, immediately open the database connection
Raises
FileNotFoundError- If the database file doesn’t exist
__repr__()
Get nice string representation.
Usage
__repr__()Returns
str- The database file name.
begin_transaction()
Begin the data transaction.
Usage
begin_transaction()Using a BEGIN/END transaction significantly improves batch commit performance.
Examples
>>> from mikeplus import Database
>>> db = Database("path/to/model.sqlite")
>>> db.begin_transaction()
>>> commit = True
>>> try:
>>> db.tables.msm_Node.update({"Diameter": 0.35}).by_muid("Node_1").execute()
>>> db.tables.msm_Node.update({"Diameter": 0.40}).by_muid("Node_2").execute()
>>> # ... update more rows
>>> except RuntimeError as e:
>>> print(f"An error occurred: {e}")
>>> commit = False
>>> finally:
>>> db.end_transaction(commit)Raises
ValueError- If the database is not open
close()
Close the model database. Closing a closed database does nothing.
Usage
close()Raises
DatabaseError- If MIKE+ fails to close the database
create()
Create a new MIKE+ model database.
Usage
create(
model_path,
*,
projection_string="",
srid=-1,
auto_open=True,
overwrite=False
)Writes the .sqlite database and a .mupp project file beside it, so the new model opens in MIKE+.
Parameters
model_path: str or Path-
Path where the new database will be created, as
.sqliteor.mupp projection_string: str = ""-
The projection string (WKT) for the database. The SRID is derived from it when MIKE+ recognises the projection.
srid: int = -1-
The EPSG code of a projected coordinate system, e.g. ETRS89 / UTM zone 32N is 25832. Geographic systems such as 4326 are not supported; use a projected one such as 3857.
auto_open: bool = True-
If True, immediately open the database connection
overwrite: (bool, optional(default is False)) = False- If True, delete an existing database and project file first. Settings MIKE+ stores in the project file, such as map layers, are lost.
Returns
Database- A Database object for the newly created database
Raises
FileExistsError-
If the database or project file already exists (except if overwrite is True)
ValueError-
If both projection_string and srid are given, or srid is not a projected coordinate system MIKE+ knows
DatabaseError- If MIKE+ fails to create the database
end_transaction()
End the data transaction.
Usage
end_transaction(commit=True)Parameters
commit: bool = True- true is to commit the data into database, false is to rollback the commit.
Raises
ValueError- If the database is not open
ensure_mupp()
Get the model’s project file (.mupp), writing one if it has none.
Usage
ensure_mupp()A missing project file is written beside the database with the same name, as MIKE+ does when it opens a bare .sqlite. It records the database’s model type and unit system and refers to the database by a path relative to itself. An existing project file is never changed.
Returns
Path- Path to the project file.
Raises
ValueError-
If the database is not open.
DatabaseError- If MIKE+ fails to write the project file.
Examples
>>> with mp.open("path/to/model.sqlite") as db:
... db.ensure_mupp()import_from_epanet()
Import a model from an EPANET .inp file.
Usage
import_from_epanet(file_path)Parameters
file_path: str or Path- Path to the EPANET .inp file. If not provided, a file dialog will open.
Examples
>>> with mp.create("path/to/model.sqlite") as db:
... db.import_from_epanet("path/to/model.inp")Raises
FileNotFoundError-
If the file doesn’t exist
ValueError-
If the file is not an .inp file
DatabaseError- If the import fails
import_from_swmm()
Import a model from a SWMM .inp file.
Usage
import_from_swmm(file_path)Parameters
file_path: str or Path- Path to the SWMM .inp file.
Examples
>>> with mp.create("path/to/model.sqlite") as db:
... db.import_from_swmm("path/to/model.inp")Raises
FileNotFoundError-
If the file doesn’t exist
ValueError-
If the file is not an .inp file
DatabaseError- If the import fails
open()
Open the model database.
Usage
open()Returns
Database- self, for method chaining
Raises
DatabaseError- If MIKE+ fails to open the database
run()
Run a simulation.
Usage
run(simulation_muid=None, *, sim_option=None)Parameters
simulation_muid: str = None-
Simulation MUID. Defaults to the active simulation.
sim_option: Literal[str] = None-
Simulation option. Defaults to None. Possible values are:
- “CS_MIKE_1D”: Rivers, collection systems, and overland flows.
- “CS_SWMM”: SWMM-based collection systems and overland flows.
- “WD_EPANET”: Water distribution systems.
- “CS_MIKE_1D_JobList”: Generate job list for LTS simulation (.MJL file).
Examples
>>> with mp.open("path/to/model.sqlite") as db:
... results = db.run()
>>> with mp.open("path/to/model.sqlite") as db:
... results = db.run("My Simulation")
>>> db = mp.open("path/to/model.sqlite")
>>> results = db.run("My Simulation")
>>> db.close()Returns
list[Path]- Paths to the result files the simulation wrote.
Raises
ValueError-
If
sim_optionis invalid, or not given and the active model does not determine one, or the model has no simulation with MUIDsimulation_muid. RuntimeError- If the simulation engine fails to start, exits with an error, or doesn’t write its result files. The message names the engine’s logs.