Skip to content

Commit

Permalink
Merge pull request #183 from changliao1025/development
Browse files Browse the repository at this point in the history
add one key into change json
  • Loading branch information
changliao1025 authored Sep 28, 2023
2 parents 6c92d49 + 8ed5163 commit d93ef41
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 31 deletions.
8 changes: 3 additions & 5 deletions data/susquehanna/input/pyflowline_susquehanna_basins.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
[
{
"dLatitude_outlet_degree": 39.4620,
"dLongitude_outlet_degree": -76.0093,
"dLatitude_outlet_degree": 39.462,
"dLongitude_outlet_degree": -76.0093,
"dAccumulation_threshold": 100000,
"dThreshold_small_river": 10000,
"iFlag_dam": 0,
"iFlag_debug":1,
"iFlag_debug": 1,
"iFlag_disconnected": 0,
"lBasinID": 1,
"sFilename_dam": "/qfs/people/liao313/data/hexwatershed/susquehanna/auxiliary/ICoM_dams.csv",
"sFilename_flowline_filter": "/qfs/people/liao313/workspace/python/pyflowline/data/susquehanna/input/flowline.geojson",
"sFilename_flowline_raw": "/qfs/people/liao313/data/hexwatershed/susquehanna/vector/hydrology/allflowline.shp",
"sFilename_flowline_topo": "/qfs/people/liao313/data/hexwatershed/susquehanna/auxiliary/flowline.csv"


}
]
8 changes: 8 additions & 0 deletions examples/susquehanna/run_simulation_mpas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
sPath_parent = str(Path(__file__).parents[2]) # data is located two dir's up

sys.path.append(sPath_parent)
from pyflowline.change_json_key_value import change_json_key_value
from pyflowline.pyflowline_read_model_configuration_file import pyflowline_read_model_configuration_file

sPath_data = realpath( sPath_parent + '/data/susquehanna' )
Expand All @@ -32,6 +33,13 @@
sMesh = 'mpas'
sDate='20230701'

sFolder_data = os.path.join(sPath_parent, 'data')
sFolder_data_susquehanna = os.path.join(sFolder_data, 'susquehanna')
sFolder_input = os.path.join(sFolder_data_susquehanna, 'input')
sFilename_flowline = realpath( os.path.join(sFolder_input, 'flowline.geojson') )
sFilename_basins = realpath( os.path.join(sFolder_input , 'pyflowline_susquehanna_basins.json' ))
change_json_key_value(sFilename_basins, 'sFilename_flowline_filter', sFilename_flowline, iFlag_basin_in=1)

oPyflowline = pyflowline_read_model_configuration_file(sFilename_configuration_in,
iCase_index_in=iCase_index, sDate_in=sDate)

Expand Down
147 changes: 123 additions & 24 deletions notebooks/mpas_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "d101804e",
"metadata": {},
"outputs": [],
Expand All @@ -52,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "4b2ca848",
"metadata": {},
"outputs": [],
Expand All @@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "b4ba011d",
"metadata": {},
"outputs": [],
Expand All @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "f5c78ad3",
"metadata": {},
"outputs": [],
Expand All @@ -91,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "4d2b0424",
"metadata": {},
"outputs": [],
Expand All @@ -104,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "21dd10ee",
"metadata": {},
"outputs": [],
Expand All @@ -127,10 +127,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "a75e5100",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C:\\workspace\\python\\pyflowline-main\n",
"The pyflowline package is not installed. We will use the current path to set it up.\n"
]
}
],
"source": [
"sPath_notebook = Path().resolve()\n",
"sPath_parent = str(Path().resolve().parents[0]) \n",
Expand Down Expand Up @@ -159,10 +168,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "c52960d0",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C:\\workspace\\python\\pyflowline-main\\data\\susquehanna\\input\\mpas_mesh.nc\n",
"File 'C:\\workspace\\python\\pyflowline-main\\data\\susquehanna\\input\\mpas_mesh.nc' downloaded successfully.\n"
]
}
],
"source": [
"\n",
"#download the MPAS mesh from the github release\n",
Expand Down Expand Up @@ -200,7 +218,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "9ad0e257",
"metadata": {},
"outputs": [],
Expand All @@ -223,12 +241,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "25e0e653",
"metadata": {},
"outputs": [],
"source": [
"sFilename_configuration_in = realpath( sPath_parent + '/examples/susquehanna/pyflowline_susquehanna_mpas.json' )\n",
"sFilename_configuration_in = realpath( sPath_parent + '/data/susquehanna/input/pyflowline_susquehanna_mpas.json' )\n",
"if os.path.isfile(sFilename_configuration_in):\n",
" pass\n",
"else:\n",
Expand All @@ -237,10 +255,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "394d0745",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"sFilename_model_configuration\": \"/qfs/people/liao313/workspace/python/pyflowline/pyflowline/config/hexwatershed_susquehanna_mpas.json\",\n",
" \"sWorkspace_data\": \"/people/liao313/data\",\n",
" \"sWorkspace_output\": \"/compyfs/liao313/04model/pyflowline/susquehanna\",\n",
" \"sWorkspace_project\": \"/hexwatershed/susquehanna\",\n",
" \"sWorkspace_bin\": \"/people/liao313/bin\",\n",
" \"sRegion\": \"susquehanna\",\n",
" \"sModel\": \"pyflowline\",\n",
" \"sJob\": \"hex\",\n",
" \"iFlag_standalone\": 1,\n",
" \"iFlag_create_mesh\": 1,\n",
" \"iFlag_mesh_boundary\": 1,\n",
" \"iFlag_save_mesh\": 1,\n",
" \"iFlag_simplification\": 1,\n",
" \"iFlag_intersect\": 1,\n",
" \"iFlag_flowline\": 1,\n",
" \"iFlag_use_mesh_dem\": 1,\n",
" \"iFlag_global\": 0,\n",
" \"iFlag_multiple_outlet\": 0,\n",
" \"iFlag_rotation\": 0,\n",
" \"iCase_index\": 1,\n",
" \"iMesh_type\": 4,\n",
" \"dLongitude_left\": -79,\n",
" \"dLongitude_right\": -74.5,\n",
" \"dLatitude_bot\": 39.2,\n",
" \"dLatitude_top\": 42.8,\n",
" \"dResolution_degree\": 5000,\n",
" \"dResolution_meter\": 5000,\n",
" \"sDate\": \"20220110\",\n",
" \"sMesh_type\": \"mpas\",\n",
" \"sFilename_spatial_reference\": \"/qfs/people/liao313/workspace/python/pyhexwatershed_icom/data/susquehanna/input/boundary_proj_buff.shp\",\n",
" \"sFilename_dem\": \"/qfs/people/liao313/workspace/python/pyhexwatershed_icom/data/susquehanna/input/dem_buff_ext.tif\",\n",
" \"sFilename_mesh_netcdf\": \"/qfs/people/liao313/workspace/python/pyflowline/data/susquehanna/input/lnd_cull_mesh.nc\",\n",
" \"sFilename_mesh_boundary\": \"/qfs/people/liao313/workspace/python/pyflowline/data/susquehanna/input/boundary_wgs.geojson\",\n",
" \"sFilename_basins\": \"/qfs/people/liao313/workspace/python/pyflowline/examples/susquehanna/pyflowline_susquehanna_basins.json\"\n",
"}\n"
]
}
],
"source": [
"#step 4B(optional)\n",
"#we can check what is the content of this json file\n",
Expand Down Expand Up @@ -276,7 +337,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "52820d49",
"metadata": {},
"outputs": [],
Expand All @@ -290,18 +351,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"id": "69f442ec",
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "list indices must be integers or slices, not str",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mc:\\workspace\\python\\pyflowline-main\\notebooks\\mpas_example.ipynb Cell 21\u001b[0m line \u001b[0;36m1\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X26sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m change_json_key_value(sFilename_configuration_in, \u001b[39m'\u001b[39m\u001b[39msFilename_basins\u001b[39m\u001b[39m'\u001b[39m, sFilename_basins)\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X26sZmlsZQ%3D%3D?line=8'>9</a>\u001b[0m sFilename_flowline \u001b[39m=\u001b[39m realpath( os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mjoin(sFolder_input, \u001b[39m'\u001b[39m\u001b[39mflowline.geojson\u001b[39m\u001b[39m'\u001b[39m) )\n\u001b[1;32m---> <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X26sZmlsZQ%3D%3D?line=9'>10</a>\u001b[0m change_json_key_value(sFilename_basins, \u001b[39m'\u001b[39;49m\u001b[39msFilename_flowline_filter\u001b[39;49m\u001b[39m'\u001b[39;49m, sFilename_flowline)\n",
"File \u001b[1;32mC:\\workspace\\python\\pyflowline-main\\pyflowline\\change_json_key_value.py:9\u001b[0m, in \u001b[0;36mchange_json_key_value\u001b[1;34m(sFilename_json_in, sKey, new_value)\u001b[0m\n\u001b[0;32m 6\u001b[0m data \u001b[39m=\u001b[39m json\u001b[39m.\u001b[39mload(file)\n\u001b[0;32m 8\u001b[0m \u001b[39m# Update the value associated with the specified sKey\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m data[sKey] \u001b[39m=\u001b[39m new_value\n\u001b[0;32m 11\u001b[0m \u001b[39m# Write the updated data back to the JSON file\u001b[39;00m\n\u001b[0;32m 12\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39m(sFilename_json_in, \u001b[39m'\u001b[39m\u001b[39mw\u001b[39m\u001b[39m'\u001b[39m) \u001b[39mas\u001b[39;00m file:\n",
"\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
]
}
],
"source": [
"#we need to update a few parameters in the configuration file before we can create the flowline object\n",
"sFilename_basins = realpath( os.path.join(sFolder_input , 'pyflowline_susquehanna_mpas_basins.json' ))\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"
"change_json_key_value(sFilename_configuration_in, 'sFilename_basins', sFilename_basins)\n",
"\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)"
]
},
{
Expand All @@ -315,10 +392,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "2a221825",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/compyfs/liao313/04model/pyflowline/susquehanna\n",
"The filtered flowline file does not exist!\n"
]
},
{
"ename": "NameError",
"evalue": "name 'exit' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mc:\\workspace\\python\\pyflowline-main\\notebooks\\mpas_example.ipynb Cell 23\u001b[0m line \u001b[0;36m6\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39m#the read function accepts several keyword arguments that can be used to change the default parameters.\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39m#the normal keyword arguments are:\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39m#iCase_index_in: this is an ID to identify the simulation case\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39m#sMesh_type_in: this specifies the mesh type ('mpas' in this example)\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m#sDate_in: this specifies the date of the simulation, the final output folder will have a pattern such as 'pyflowline20230901001', where pyflowline is model, 20230901 is the date, and 001 is the case index.\u001b[39;00m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m oPyflowline \u001b[39m=\u001b[39m pyflowline_read_model_configuration_file(sFilename_configuration_in, iCase_index_in\u001b[39m=\u001b[39;49miCase_index, \n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/workspace/python/pyflowline-main/notebooks/mpas_example.ipynb#X31sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m sMesh_type_in\u001b[39m=\u001b[39;49m sMesh_type, sDate_in\u001b[39m=\u001b[39;49msDate)\n",
"File \u001b[1;32mC:\\workspace\\python\\pyflowline-main\\pyflowline\\pyflowline_read_model_configuration_file.py:130\u001b[0m, in \u001b[0;36mpyflowline_read_model_configuration_file\u001b[1;34m(sFilename_configuration_in, iFlag_standalone_in, iFlag_use_mesh_dem_in, iCase_index_in, dResolution_degree_in, dResolution_meter_in, sMesh_type_in, sModel_in, sDate_in, sWorkspace_output_in)\u001b[0m\n\u001b[0;32m 116\u001b[0m aConfig[\u001b[39m\"\u001b[39m\u001b[39msFilename_model_configuration\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m sFilename_configuration_in\n\u001b[0;32m 120\u001b[0m \u001b[39m#based on global variable, a few variables are calculate once\u001b[39;00m\n\u001b[0;32m 121\u001b[0m \u001b[39m#calculate the modflow simulation period\u001b[39;00m\n\u001b[0;32m 122\u001b[0m \u001b[39m#https://docs.python.org/3/library/datetime.html#datetime-objects\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 127\u001b[0m \n\u001b[0;32m 128\u001b[0m \u001b[39m#simulation\u001b[39;00m\n\u001b[1;32m--> 130\u001b[0m oPyflowline \u001b[39m=\u001b[39m flowlinecase(aConfig)\n\u001b[0;32m 133\u001b[0m \u001b[39mreturn\u001b[39;00m oPyflowline\n",
"File \u001b[1;32mC:\\workspace\\python\\pyflowline-main\\pyflowline\\classes\\pycase.py:439\u001b[0m, in \u001b[0;36mflowlinecase.__init__\u001b[1;34m(self, aConfig_in, iFlag_standalone_in, sModel_in, sDate_in, sWorkspace_output_in)\u001b[0m\n\u001b[0;32m 437\u001b[0m dummy_basin \u001b[39m=\u001b[39m dummy_data[i]\n\u001b[0;32m 438\u001b[0m dummy_basin[\u001b[39m'\u001b[39m\u001b[39msWorkspace_output_basin\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m \u001b[39mstr\u001b[39m(Path(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39msWorkspace_output) \u001b[39m/\u001b[39m sBasin )\n\u001b[1;32m--> 439\u001b[0m pBasin \u001b[39m=\u001b[39m pybasin(dummy_basin)\n\u001b[0;32m 440\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39maBasin\u001b[39m.\u001b[39mappend(pBasin)\n\u001b[0;32m 441\u001b[0m \u001b[39melse\u001b[39;00m:\n",
"File \u001b[1;32mC:\\workspace\\python\\pyflowline-main\\pyflowline\\classes\\basin.py:288\u001b[0m, in \u001b[0;36mpybasin.__init__\u001b[1;34m(self, aParameter)\u001b[0m\n\u001b[0;32m 286\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39misfile(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39msFilename_flowline_filter):\n\u001b[0;32m 287\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mThe filtered flowline file does not exist!\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m--> 288\u001b[0m exit\n\u001b[0;32m 289\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39miFlag_dam\u001b[39m==\u001b[39m\u001b[39m1\u001b[39m:\n\u001b[0;32m 290\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39misfile(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39msFilename_flowline_raw):\n",
"\u001b[1;31mNameError\u001b[0m: name 'exit' is not defined"
]
}
],
"source": [
"#the read function accepts several keyword arguments that can be used to change the default parameters.\n",
"#the normal keyword arguments are:\n",
Expand Down Expand Up @@ -353,8 +453,7 @@
"oPyflowline.change_model_parameter('iFlag_dam', 0, iFlag_basin_in= 1)\n",
"\n",
"#we will set the original flowline file name\n",
"sFilename_flowline = os.path.join(sFolder_input, 'flowline.geojson')\n",
"oPyflowline.change_model_parameter('sFilename_flowline_filtered', sFilename_flowline, iFlag_basin_in= 1)\n",
"\n",
"\n"
]
},
Expand Down
7 changes: 5 additions & 2 deletions pyflowline/change_json_key_value.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json

def change_json_key_value(sFilename_json_in, sKey, new_value):
def change_json_key_value(sFilename_json_in, sKey, new_value, iFlag_basin_in= None):
# Read the JSON file
with open(sFilename_json_in, 'r') as file:
data = json.load(file)

# Update the value associated with the specified sKey
data[sKey] = new_value
if iFlag_basin_in is None:
data[sKey] = new_value
else:
data[0][sKey] = new_value

# Write the updated data back to the JSON file
with open(sFilename_json_in, 'w') as file:
Expand Down

0 comments on commit d93ef41

Please sign in to comment.