Skip to content

Commit

Permalink
Adding minimal examples for MESHFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
kasra-keshavarz committed Feb 20, 2024
1 parent 60d2265 commit 28265b0
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 0 deletions.
205 changes: 205 additions & 0 deletions examples/example_setup.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "483fac00-27c6-4d77-bccb-81b2c1af3dea",
"metadata": {},
"outputs": [],
"source": [
"# import necessary libraries\n",
"import meshflow # version v0.1.0-dev1\n",
"\n",
"import os # python 3.10.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24c28351-852d-4b1f-b82b-ee94367af0de",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# main work path - modify\n",
"work_path = '/path/to/your/experiments/'\n",
"\n",
"# using meshflow==v0.1.0-dev1\n",
"# modify the following to match your settings\n",
"config = {\n",
" 'riv': os.path.join(work_path, 'your_rivers_file.shp'),\n",
" 'cat': os.path.join(work_path, 'your_catchments_file.shp'),\n",
" 'landcover': os.path.join(work_path, 'your_landcover_fractions_file.csv'),\n",
" 'forcing_files': os.path.join(work_path, 'easymore-outputs'),\n",
" 'forcing_vars': [ # MESH usuall needs 7 variables, list them below\n",
" \"var1\",\n",
" \"var2\",\n",
" \"var3\",\n",
" \"var4\",\n",
" \"var5\",\n",
" \"var6\",\n",
" \"var7\",\n",
" ],\n",
" 'forcing_units': { # Enter original units for each variable listed under 'forcing_vars'\n",
" \"var1\": 'millibar',\n",
" \"var2\": 'kg/kg',\n",
" \"var3\": 'celsius',\n",
" \"var4\": 'knot',\n",
" \"var5\": 'm/hr',\n",
" \"var6\": 'W/m^2',\n",
" \"var7\": 'W/m^2',\n",
" },\n",
" 'forcing_to_units': { # And here, the units that MESH needs to read\n",
" \"var1\": 'm/s',\n",
" \"var2\": 'W/m^2',\n",
" \"var3\": 'W/m^2',\n",
" \"var4\": 'mm/s',\n",
" \"var5\": 'pascal',\n",
" \"var6\": 'kelvin',\n",
" \"var7\": 'kg/kg',\n",
" },\n",
" 'main_id': 'main_riv_id', # what is the main ID of each river segment? Column name in the `cat` object\n",
" 'ds_main_id': 'ds_main_id', # what is the downstream segment ID for each river segment? ditto.\n",
" 'landcover_classes': { # these are the classes defined for NALCMS-Landsat 2015 dataset. Is this accurate?\n",
" 0: 'Class A', # \"key\" values are foudn in `landcover` object\n",
" 1: 'Class B',\n",
" 2: 'Class C',\n",
" 3: 'Class D',\n",
" 4: 'Class E',\n",
" 5: 'Class F',\n",
" },\n",
" 'ddb_vars': { # drainage database variables that MESH needs\n",
" # FIXME: in later versions, the positions of keys and values below will be switched\n",
" 'seg_slope': 'ChnlSlope',\n",
" 'Shape_Leng': 'ChnlLength',\n",
" 'Rank': 'Rank', # Rank variable - for WATROUTE routing\n",
" 'Next': 'Next', # Next variable - for WATROUTE routing\n",
" 'landcover': 'GRU', # GRU fractions variable\n",
" 'Shape_Area': 'GridArea', # Sub-basin area variable\n",
" 'landcover_names': 'LandUse', # LandUse names\n",
" },\n",
" 'ddb_units': { # units of variables in the drainage database\n",
" 'ChnlSlope': 'm/m',\n",
" 'ChnlLength': 'm',\n",
" 'Rank': 'dimensionless',\n",
" 'Next': 'dimensionless',\n",
" 'GRU': 'dimensionless',\n",
" 'GridArea': 'm^2',\n",
" 'LandUse': 'dimensionless',\n",
" },\n",
" 'ddb_to_units': { # units of variables in the drainage database the MESH needs\n",
" 'ChnlSlope': 'm/m',\n",
" 'ChnlLength': 'm',\n",
" 'Rank': 'dimensionless',\n",
" 'Next': 'dimensionless',\n",
" 'GRU': 'dimensionless',\n",
" 'GridArea': 'm^2',\n",
" 'LandUse': 'dimensionless',\n",
" },\n",
" 'ddb_min_values': { # minimum values in the drainage database\n",
" 'ChnlSlope': 1e-10, # in case there are 0s in the `rivers` Shapefile, we need minimums for certain variables\n",
" 'ChnlLength': 1e-3,\n",
" 'GridArea': 1e-3,\n",
" },\n",
" 'gru_dim': 'NGRU', # change to `NGRU` for 'MESH>=r1860', keep for 'MESH<=1860', for example for r1813.\n",
" 'hru_dim': 'subbasin', # consistent in various versions, no need to change\n",
" 'outlet_value': 0, # modify depending on the outlet values specific in `ds_main_id` object\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c58c6f35-532c-4477-a0d6-d3fdbfa78f2d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"exp1 = meshflow.MESHWorkflow(**config)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ac7f71b-2f09-460a-87f1-fa41b98aaa9b",
"metadata": {},
"outputs": [],
"source": [
"exp1.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c90482c-72af-46c8-a8bf-0d79c8560b39",
"metadata": {},
"outputs": [],
"source": [
"exp1.forcing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3924ad83-e16e-43c1-9ce3-967e363348b1",
"metadata": {},
"outputs": [],
"source": [
"exp1.ddb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0591dbd9-75f1-44bb-bd85-f0516be84b84",
"metadata": {},
"outputs": [],
"source": [
"# create a directory for MESH setup\n",
"os.makedirs('/path/to/your/experiments/MESH-model')\n",
"\n",
"# saving drainage database and forcing files\n",
"exp1.save('/path/to/your/experiments/MESH-model')"
]
},
{
"cell_type": "markdown",
"id": "6deca0e6-4195-4671-a66c-c625f43d2c1d",
"metadata": {},
"source": [
"____"
]
},
{
"cell_type": "markdown",
"id": "6e8d7bce-f674-4239-a0d6-b2816ef2dcd8",
"metadata": {},
"source": [
"If there is any issue, open a ticket on [MESHFlow's GitHub Webpage](https://github.com/kasra-keshavarz/meshflow/issues). Once this Notebook runs, then have the setting files along with a `MESH` executable all in the `MESH-smm` directory. Please be mindful of the settings, and assure the settings are all accurate to run `MESH` properly."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "scienv",
"language": "python",
"name": "scienv"
},
"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.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
138 changes: 138 additions & 0 deletions examples/example_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


# import necessary libraries
import meshflow # version v0.1.0-dev1

import os # python 3.10.2


# In[ ]:


# main work path - modify
work_path = '/path/to/your/experiments/'

# using meshflow==v0.1.0-dev1
# modify the following to match your settings
config = {
'riv': os.path.join(work_path, 'your_rivers_file.shp'),
'cat': os.path.join(work_path, 'your_catchments_file.shp'),
'landcover': os.path.join(work_path, 'your_landcover_fractions_file.csv'),
'forcing_files': os.path.join(work_path, 'easymore-outputs'),
'forcing_vars': [ # MESH usuall needs 7 variables, list them below
"var1",
"var2",
"var3",
"var4",
"var5",
"var6",
"var7",
],
'forcing_units': { # Enter original units for each variable listed under 'forcing_vars'
"var1": 'millibar',
"var2": 'kg/kg',
"var3": 'celsius',
"var4": 'knot',
"var5": 'm/hr',
"var6": 'W/m^2',
"var7": 'W/m^2',
},
'forcing_to_units': { # And here, the units that MESH needs to read
"var1": 'm/s',
"var2": 'W/m^2',
"var3": 'W/m^2',
"var4": 'mm/s',
"var5": 'pascal',
"var6": 'kelvin',
"var7": 'kg/kg',
},
'main_id': 'main_riv_id', # what is the main ID of each river segment? Column name in the `cat` object
'ds_main_id': 'ds_main_id', # what is the downstream segment ID for each river segment? ditto.
'landcover_classes': { # these are the classes defined for NALCMS-Landsat 2015 dataset. Is this accurate?
0: 'Class A', # "key" values are foudn in `landcover` object
1: 'Class B',
2: 'Class C',
3: 'Class D',
4: 'Class E',
5: 'Class F',
},
'ddb_vars': { # drainage database variables that MESH needs
# FIXME: in later versions, the positions of keys and values below will be switched
'seg_slope': 'ChnlSlope',
'Shape_Leng': 'ChnlLength',
'Rank': 'Rank', # Rank variable - for WATROUTE routing
'Next': 'Next', # Next variable - for WATROUTE routing
'landcover': 'GRU', # GRU fractions variable
'Shape_Area': 'GridArea', # Sub-basin area variable
'landcover_names': 'LandUse', # LandUse names
},
'ddb_units': { # units of variables in the drainage database
'ChnlSlope': 'm/m',
'ChnlLength': 'm',
'Rank': 'dimensionless',
'Next': 'dimensionless',
'GRU': 'dimensionless',
'GridArea': 'm^2',
'LandUse': 'dimensionless',
},
'ddb_to_units': { # units of variables in the drainage database the MESH needs
'ChnlSlope': 'm/m',
'ChnlLength': 'm',
'Rank': 'dimensionless',
'Next': 'dimensionless',
'GRU': 'dimensionless',
'GridArea': 'm^2',
'LandUse': 'dimensionless',
},
'ddb_min_values': { # minimum values in the drainage database
'ChnlSlope': 1e-10, # in case there are 0s in the `rivers` Shapefile, we need minimums for certain variables
'ChnlLength': 1e-3,
'GridArea': 1e-3,
},
'gru_dim': 'NGRU', # change to `NGRU` for 'MESH>=r1860', keep for 'MESH<=1860', for example for r1813.
'hru_dim': 'subbasin', # consistent in various versions, no need to change
'outlet_value': 0, # modify depending on the outlet values specific in `ds_main_id` object
}


# In[ ]:


exp1 = meshflow.MESHWorkflow(**config)


# In[ ]:


exp1.run()


# In[ ]:


exp1.forcing


# In[ ]:


exp1.ddb


# In[ ]:


# create a directory for MESH setup
os.makedirs('/path/to/your/experiments/MESH-model')

# saving drainage database and forcing files
exp1.save('/path/to/your/experiments/MESH-model')


# ____

# If there is any issue, open a ticket on [MESHFlow's GitHub Webpage](https://github.com/kasra-keshavarz/meshflow/issues). Once this Notebook runs, then have the setting files along with a `MESH` executable all in the `MESH-smm` directory. Please be mindful of the settings, and assure the settings are all accurate to run `MESH` properly.

0 comments on commit 28265b0

Please sign in to comment.