Skip to content

Commit

Permalink
Merge pull request #188 from changliao1025/development
Browse files Browse the repository at this point in the history
cleanup notebook
  • Loading branch information
changliao1025 committed Sep 28, 2023
2 parents a2a82be + 72727ba commit e2145fa
Showing 1 changed file with 36 additions and 75 deletions.
111 changes: 36 additions & 75 deletions notebooks/mpas_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"from pathlib import Path\n",
"from os.path import realpath\n",
"import importlib\n",
"#install dependencies\n",
"#check dependencies\n",
"\n",
"iFlag_numpy = importlib.util.find_spec(\"numpy\")\n",
"iFlag_gdal = importlib.util.find_spec(\"osgeo\")\n",
Expand Down Expand Up @@ -122,7 +122,7 @@
"id": "bf81807a",
"metadata": {},
"source": [
"Then we will check whether some additional packages are installed."
"If any dependency is missing, please install it using conda."
]
},
{
Expand All @@ -141,6 +141,7 @@
}
],
"source": [
"#now add the pyflowline into the Python path.\n",
"sPath_notebook = Path().resolve()\n",
"sPath_parent = str(Path().resolve().parents[0]) \n",
"print(sPath_parent)\n",
Expand Down Expand Up @@ -192,19 +193,22 @@
"sFolder_input = os.path.join(sFolder_data_susquehanna, 'input')\n",
"\n",
"sFilename_download = os.path.join(sFolder_input, 'mpas_mesh.nc')\n",
"print(sFilename_download)\n",
"\n",
"# Send an HTTP GET request to the URL\n",
"response = requests.get(sFilename_mpas)\n",
"\n",
"# Check if the request was successful\n",
"if response.status_code == 200:\n",
" # Save the content of the response to the local file\n",
" with open(sFilename_download, 'wb') as file:\n",
" file.write(response.content)\n",
" print(f\"File '{sFilename_download}' downloaded successfully.\")\n",
"#check whether the file exists\n",
"if os.path.exists(sFilename_download):\n",
" print(f\"File '{sFilename_download}' exists. Skip downloading.\")\n",
"else:\n",
" print(f\"Failed to download file from '{sFilename_mpas}'.\")"
" print(sFilename_download)\n",
" # Send an HTTP GET request to the URL\n",
" response = requests.get(sFilename_mpas)\n",
"\n",
" # Check if the request was successful\n",
" if response.status_code == 200:\n",
" # Save the content of the response to the local file\n",
" with open(sFilename_download, 'wb') as file:\n",
" file.write(response.content)\n",
" print(f\"File '{sFilename_download}' downloaded successfully.\")\n",
" else:\n",
" print(f\"Failed to download file from '{sFilename_mpas}'.\")"
]
},
{
Expand Down Expand Up @@ -303,7 +307,7 @@
}
],
"source": [
"#step 4B(optional)\n",
"\n",
"#we can check what is the content of this json file\n",
"import json\n",
"with open(sFilename_configuration_in, 'r') as pJSON:\n",
Expand Down Expand Up @@ -342,7 +346,7 @@
"metadata": {},
"outputs": [],
"source": [
"#step 5, set up some parameters\n",
"#set up some parameters\n",
"sMesh_type = 'mpas'\n",
"iCase_index = 1\n",
"dResolution_meter=5000\n",
Expand All @@ -357,17 +361,18 @@
"outputs": [],
"source": [
"#we need to update a few parameters in the configuration file before we can create the flowline object\n",
"#the json file will be overwritten, you may want to make a copy of it first\n",
"sFilename_basins = realpath( os.path.join(sFolder_input , 'pyflowline_susquehanna_basins.json' ))\n",
"sFilename_mesh_boundary = realpath(os.path.join(sFolder_input, 'boundary_wgs.geojson'))\n",
"\n",
"change_json_key_value(sFilename_configuration_in, 'sFilename_mesh_netcdf', sFilename_download)\n",
"change_json_key_value(sFilename_configuration_in, 'sFilename_mesh_boundary', sFilename_mesh_boundary)\n",
"change_json_key_value(sFilename_configuration_in, 'sFilename_basins', sFilename_basins)\n",
"sFilename_mesh_boundary = realpath(os.path.join(sFolder_input, 'boundary_wgs.geojson')) #boundary to clip mesh\n",
"sWorkspace_output = os.path.join(sFolder_data_susquehanna, 'output')\n",
"change_json_key_value(sFilename_configuration_in, 'sFilename_mesh_netcdf', sFilename_download) #mpas file we just downloaded\n",
"change_json_key_value(sFilename_configuration_in, 'sFilename_mesh_boundary', sFilename_mesh_boundary) \n",
"change_json_key_value(sFilename_configuration_in, 'sFilename_basins', sFilename_basins) #individual basin configuration file\n",
"change_json_key_value(sFilename_configuration_in, 'sWorkspace_output', sWorkspace_output) #output folder\n",
"\n",
"#now change basin configuration file\n",
"sFilename_flowline = realpath( os.path.join(sFolder_input, 'flowline.geojson') )\n",
"change_json_key_value(sFilename_basins, 'sFilename_flowline_filter', sFilename_flowline, iFlag_basin_in=1)\n",
"sWorkspace_output = os.path.join(sFolder_data_susquehanna, 'output')\n",
"change_json_key_value(sFilename_configuration_in, 'sWorkspace_output', sWorkspace_output)"
"change_json_key_value(sFilename_basins, 'sFilename_flowline_filter', sFilename_flowline, iFlag_basin_in=1) #user provided flowline\n"
]
},
{
Expand Down Expand Up @@ -410,25 +415,18 @@
"metadata": {},
"outputs": [],
"source": [
"#other than setting the paraemeter using the read model configuration fucntion, user can also change model parameters after creating the model object\n",
"#rememere that, it is recommended to set output folder using the read model configuration function since the change_model_parameter function will not update output folder\n",
"#other than setting the parameter using the read model configuration fucntion, user can also change model parameters after creating the model object\n",
"#only a list of parameters can be changed, for full list, please check the documentation\n",
"#in this example, we will change the mesh file name\n",
"#the function will check the data type, if incorrect data type is provided, it will raise an error\n",
"oPyflowline.change_model_parameter('sFilename_mesh_netcdf', sFilename_download) #because the mpas mesh already contains elevation, we do not need to set the elevation file name\n",
"#we will set the boundary file name\n",
"#this file should be located in the input folder\n",
"oPyflowline.change_model_parameter('sFilename_mesh_boundary', sFilename_mesh_boundary)\n",
"\n",
"#we can also set for individual basin in the domain, in this example, we only has one basin.\n",
"#remember that, each basin can have different parameters, so if you want to set them different (for example, basin 1 has no dam, but basin 2 has dam), you should edit the basin json instead using this function.\n",
"#because change_model_parameter will set all the basin using the same parameter in current version\n",
"#must set iFlag_basin_in = 1 for basin parameter\n",
"oPyflowline.change_model_parameter('iFlag_dam', 0, iFlag_basin_in= 1)\n",
"\n",
"#we will set the original flowline file name\n",
"\n",
"\n"
"oPyflowline.change_model_parameter('iFlag_dam', 0, iFlag_basin_in= 1)\n"
]
},
{
Expand Down Expand Up @@ -516,9 +514,7 @@
"outputs": [],
"source": [
"#another important setting for basin is the approximate outlet location\n",
"oPyflowline.aBasin[0].dLatitude_outlet_degree=39.4620\n",
"oPyflowline.aBasin[0].dLongitude_outlet_degree=-76.0093\n",
"#you can also set it using the change_model_parameter function\n",
"#you can set it using the change_model_parameter function\n",
"oPyflowline.change_model_parameter('dLongitude_outlet_degree', -76.0093, iFlag_basin_in = 1)\n",
"oPyflowline.change_model_parameter('dLatitude_outlet_degree', 39.4620, iFlag_basin_in = 1)"
]
Expand Down Expand Up @@ -628,13 +624,7 @@
"id": "39507b33",
"metadata": {},
"source": [
"Before any operation, we can visualize the original or raw flowline dataset. \n",
"\n",
"You can either use your own visualization method to view or map the model output.\n",
"\n",
"For example, if you want to use the built-in function, all you need to provide is a keyword (a look up table is provided in the documentation).\n",
"Be default, all the dataset will be converted to the WGS84 system to maintain consistency.\n",
"We can take a look at the user-provided flowline."
"Before any operation, we can visualize the original or raw flowline dataset. "
]
},
{
Expand Down Expand Up @@ -678,18 +668,7 @@
"id": "8d3b8d00",
"metadata": {},
"source": [
"You can also use QGIS or other Python libraries such as GeoPandas."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "b6d5e2e0",
"metadata": {},
"outputs": [],
"source": [
"#the raw/original geojson file \n",
"sFilename_geojson = oPyflowline.aBasin[0].sFilename_flowline_filter_geojson\n"
"You can also use QGIS."
]
},
{
Expand Down Expand Up @@ -1447,8 +1426,7 @@
],
"source": [
"\n",
"#use the geopanda package\n",
"#the raw/original geojson file \n",
"#visualize the mesh\n",
"sFilename_geojson = oPyflowline.sFilename_mesh\n",
"gdf = gpd.read_file(sFilename_geojson)\n",
"gdf.plot()\n",
Expand Down Expand Up @@ -1599,7 +1577,7 @@
"source": [
"\n",
"\n",
" #\n",
"#plot both the mesh and the flowline\n",
"file1_path = oPyflowline.sFilename_mesh\n",
"file2_path = oPyflowline.aBasin[0].sFilename_flowline_conceptual\n",
"gdf1 = gpd.read_file(file1_path)\n",
Expand All @@ -1611,23 +1589,6 @@
"pass"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "47ea8562",
"metadata": {},
"source": [
"and take a close look at the outlet region"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b93fb6b1",
"metadata": {},
"outputs": [],
"source": []
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down

0 comments on commit e2145fa

Please sign in to comment.