Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notebook example of how to create a MIKE+ sqlite database #25

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 270 additions & 0 deletions notebooks/create_empty_mikeplus_sqlite_database.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create an emtpty MIKE+ sqlite database\n",
"\n",
"This example shows to create an empty MIKE+ sqlite database. Basic workflow is:\n",
"\n",
"1. Specify the coordinate system.\n",
"2. Create the empty database.\n",
"3. Configure the model type."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Required imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import clr\n",
"from System import Nullable\n",
"\n",
"import mikeplus\n",
"from mikeplus import DataTableAccess\n",
"from DHI.Amelia.DataModule.Services.DataSource import BaseDataSource"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. Specify the coordinate system"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A MIKE+ compatible projection string (e.g. readable WKT of the coordinate system from MIKE+). Refer to the MIKE+ documentation for more information.\n",
"projection_string = (\n",
"\"\"\"\n",
"PROJCS[\"ETRS89 / UTM zone 32N\",\n",
" GEOGCS[\"ETRS89\",\n",
" DATUM[\"European_Terrestrial_Reference_System_1989\",\n",
" SPHEROID[\"GRS 1980\",6378137,298.257222101,\n",
" AUTHORITY[\"EPSG\",\"7019\"]],\n",
" TOWGS84[0,0,0,0,0,0,0],\n",
" AUTHORITY[\"EPSG\",\"6258\"]],\n",
" PRIMEM[\"Greenwich\",0,\n",
" AUTHORITY[\"EPSG\",\"8901\"]],\n",
" UNIT[\"degree\",0.0174532925199433,\n",
" AUTHORITY[\"EPSG\",\"9122\"]],\n",
" AUTHORITY[\"EPSG\",\"4258\"]],\n",
" PROJECTION[\"Transverse_Mercator\"],\n",
" PARAMETER[\"latitude_of_origin\",0],\n",
" PARAMETER[\"central_meridian\",9],\n",
" PARAMETER[\"scale_factor\",0.9996],\n",
" PARAMETER[\"false_easting\",500000],\n",
" PARAMETER[\"false_northing\",0],\n",
" UNIT[\"metre\",1,\n",
" AUTHORITY[\"EPSG\",\"9001\"]],\n",
" AXIS[\"Easting\",EAST],\n",
" AXIS[\"Northing\",NORTH],\n",
" AUTHORITY[\"EPSG\",\"25832\"]]\n",
"\"\"\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Alternatively, define CRS using an SRID (e.g. EPSG).\n",
"srid = 25832"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If the projection string is known, then set SRID to -1. Otherwise, if using SRID, set projection_string to an empty string.\n",
"if projection_string:\n",
" srid = -1\n",
"else:\n",
" projection_string = \"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Create the database"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Path to model database\n",
"database_path = \"my_model.sqlite\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a new database\n",
"data_source = BaseDataSource.Create(database_path)\n",
"data_source.CreateDatabase();\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set the coordinate system\n",
"data_source.OpenDatabase()\n",
"data_source.CreateModelTables(srid, projection_string)\n",
"data_source.CloseDatabase();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Configure the model type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a DataTableAccess object to access the database\n",
"dta = DataTableAccess(database_path)\n",
"dta.open_database()\n",
"dta"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set the active model\n",
"\n",
"# CS_MIKE1D = River, collection systems and overland flow\n",
"# CS_SWMM = SWMM collection systems and overland flow\n",
"# WD_EPANET = Water distribution\n",
"\n",
"dta.datatables.SetActiveModel(dta.datatables.ActiveModel.CS_MIKE1D)\n",
"dta.datatables.ActiveModel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get the default model type settings as a dictionary\n",
"fields = [c.Field for c in dta.datatables[\"m_ModelSetting\"].Columns]\n",
"values = dta.get_field_values(\"m_ModelSetting\", \"MuModel\", fields)\n",
"settings = {field:value for field, value in zip(fields, values)}\n",
"settings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Disable all features and modules, then enable 2D Overland and Hydrodynamic.\n",
"settings_to_update = {}\n",
"for key, value in settings.items():\n",
" if \"Enable\" in key:\n",
" settings_to_update[key] = Nullable[int](0)\n",
"settings_to_update[\"Enable_2DOverland\"] = Nullable[int](1)\n",
"settings_to_update[\"Enable_HD\"] = Nullable[int](1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Update the sqlite database with the new settings\n",
"dta.set_values(\"m_ModelSetting\", \"MuModel\", settings_to_update)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Close the database\n",
"dta.close_database()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Clean up"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"db_sqlite = Path(database_path)\n",
"db_mupp = db_sqlite.with_suffix(\".mupp\")\n",
"\n",
"for file in [db_sqlite, db_mupp]:\n",
" if file.exists():\n",
" file.unlink()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "mikeplus",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading