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

Development #177

Merged
merged 9 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/source/data/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ An example parent JSON file is provided below:
"iFlag_global": 0,
"iFlag_multiple_outlet": 0,
"iFlag_rotation": 0,
"iFlag_mesh_boundary": 0,
"iCase_index": 1,
"iMesh_type": 1,
"dLongitude_left": -79,
Expand Down
15 changes: 15 additions & 0 deletions examples/susquehanna/run_simulation_mpas.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,22 @@
oPyflowline = pyflowline_read_model_configuration_file(sFilename_configuration_in,
iCase_index_in=iCase_index, sDate_in=sDate)

#take a look at the model parameters

oPyflowline.print()

#now we can change the following model parameters
#there are two ways to change the model parameters
#use a function or assign a value directly

oPyflowline.change_model_parameter(sVariable_in='sWorkspace_output', sValue_in=sWorkspace_output)

#if you need to change a parameter for a basin instead of the whole model domain, use the iFlag_basin_in option, this will change all the basins
oPyflowline.change_model_parameter(sVariable_in='dLatitude_outlet_degree', sValue_in=39.462000, iFlag_basin_in=1)
oPyflowline.change_model_parameter(sVariable_in='dLongitude_outlet_degree', sValue_in=-76.009300, iFlag_basin_in=1)


#the second way is to assign a value directly
oPyflowline.aBasin[0].dLatitude_outlet_degree=39.462000
oPyflowline.aBasin[0].dLongitude_outlet_degree=-76.009300

Expand Down
157 changes: 125 additions & 32 deletions notebooks/mpas_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,49 @@
"import os\n",
"import sys\n",
"from pathlib import Path\n",
"from os.path import realpath"
"from os.path import realpath\n",
"import importlib\n",
"import requests"
]
},
{
"cell_type": "markdown",
"id": "bf81807a",
"metadata": {},
"source": [
"Then we will check whether some additional packages are installed."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a75e5100",
"metadata": {},
"outputs": [],
"source": [
"#check pyflowline\n",
"iFlag_pyflowline = importlib.util.find_spec(\"pyflowline\") \n",
"if iFlag_pyflowline is not None:\n",
" pass\n",
"else:\n",
" print('The pyflowline package is not installed. Please install it following the quickstart document.')\n",
"\n",
"#check optional packages \n",
"iFlag_cython = importlib.util.find_spec(\"cython\") \n",
"iFlag_cartopy = importlib.util.find_spec(\"cartopy\") \n",
"iFlag_geopandas = importlib.util.find_spec(\"geopandas\") \n",
"if iFlag_cartopy is not None:\n",
" iFlag_cartopy = 1\n",
" pass\n",
"else:\n",
" #if cartopy is not avaialble, we will use the geopanda for visualization\n",
" if iFlag_geopandas is not None:\n",
" pass\n",
" else:\n",
" print('We will install the geopandas package for visualization.')\n",
" !conda install --yes --prefix {sys.prefix} gepandas\n",
" iFlag_geopandas = 1\n",
" pass\n"
]
},
{
Expand Down Expand Up @@ -105,6 +147,43 @@
"sys.path.append(sPath_parent)"
]
},
{
"cell_type": "markdown",
"id": "9f2f1520",
"metadata": {},
"source": [
"We need to download an additional NetCDF file for this example.\n",
"This file is stored on the Github release:\n",
"https://github.com/changliao1025/pyflowline/releases/tag/0.2.0\n",
"https://github.com/changliao1025/pyflowline/releases/download/0.2.0/lnd_cull_mesh.nc"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c52960d0",
"metadata": {},
"outputs": [],
"source": [
"\n",
"sFilename_mpas = 'https://github.com/changliao1025/pyflowline/releases/download/0.2.0/lnd_cull_mesh.nc'\n",
"\n",
"#combind folder with filename to get the full path\n",
"sFilename_download = os.path.join(sPath_parent, 'mpas_mesh.nc')\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",
"else:\n",
" print(f\"Failed to download file from '{sFilename_mpas}'.\")"
]
},
{
"cell_type": "markdown",
"id": "25edfaeb",
Expand Down Expand Up @@ -274,6 +353,16 @@
" sMesh_type_in= sMesh_type, sDate_in=sDate)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17e011b8",
"metadata": {},
"outputs": [],
"source": [
"oPyflowline.change_model_parameter('sFilename_mesh_netcdf', sFilename_download)"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -419,25 +508,26 @@
}
],
"source": [
"iVisualization_method = 1\n",
"if iVisualization_method == 1: # you need to install the geopanda package and matplotlib package using conda or pip\n",
" import geopandas as gpd\n",
" import matplotlib.pyplot as plt\n",
" #use the geopanda package\n",
" #the raw/original geojson file \n",
" sFilename_geojson = oPyflowline.aBasin[0].sFilename_flowline_filter_geojson\n",
" gdf = gpd.read_file(sFilename_geojson)\n",
" gdf.plot()\n",
" plt.show()\n",
"\n",
" pass\n",
"\n",
"else: #use the default visualization method, only experimental\n",
"\n",
"if iFlag_cartopy == 1: # you need to install the geopanda package and matplotlib package using conda or pip\n",
" oPyflowline.plot(sVariable_in = 'flowline_filter' )\n",
" #if you provide a filename, a png file will be saved\n",
" oPyflowline.plot(sVariable_in = 'flowline_filter', sFilename_in = 'filter_flowline.png' )\n",
" pass"
" pass\n",
"else: #use the default visualization method, only experimental\n",
" if iFlag_geopandas = 1:\n",
" import geopandas as gpd\n",
" import matplotlib.pyplot as plt\n",
" #use the geopanda package\n",
" #the raw/original geojson file \n",
" sFilename_geojson = oPyflowline.aBasin[0].sFilename_flowline_filter_geojson\n",
" gdf = gpd.read_file(sFilename_geojson)\n",
" gdf.plot()\n",
" plt.show()\n",
" else:\n",
" print('The visulization packages are not installed.')\n",
" pass\n",
" "
]
},
{
Expand Down Expand Up @@ -491,7 +581,7 @@
"source": [
"\n",
"#the default visualization method has an option to set the extent you want to plot\n",
"if iVisualization_method ==2:\n",
"if iFlag_cartopy ==1:\n",
" #aExtent_full = [-78.5,-75.5, 39.2,42.5]\n",
" aExtent_meander = [-76.5,-76.2, 41.6,41.9] \n",
" oPyflowline.plot( sVariable_in = 'flowline_filter', aExtent_in = aExtent_meander )"
Expand All @@ -515,7 +605,7 @@
}
],
"source": [
"if iVisualization_method ==2:\n",
"if iFlag_cartopy ==1:\n",
" aExtent_braided = [-77.3,-76.5, 40.2,41.0] \n",
" oPyflowline.plot( sVariable_in='flowline_filter' , aExtent_in =aExtent_braided ) "
]
Expand Down Expand Up @@ -604,7 +694,11 @@
],
"source": [
"\n",
"if iVisualization_method == 1: # you need to install the geopanda package and matplotlib package using conda or pip\n",
"if iFlag_cartopy == 1: # use the default visualization method, only experimental\n",
" oPyflowline.plot( sVariable_in='flowline_simplified' ) \n",
"\n",
"else: \n",
" \n",
" import geopandas as gpd\n",
" import matplotlib.pyplot as plt\n",
" #use the geopanda package\n",
Expand All @@ -614,10 +708,7 @@
" gdf.plot()\n",
" plt.show()\n",
"\n",
" pass\n",
"\n",
"else: #use the default visualization method, only experimental\n",
" oPyflowline.plot( sVariable_in='flowline_simplified' ) "
" pass"
]
},
{
Expand Down Expand Up @@ -751,7 +842,11 @@
}
],
"source": [
"if iVisualization_method == 1: # you need to install the geopanda package and matplotlib package using conda or pip\n",
"if iFlag_cartopy == 1: # you need to install the geopanda package and matplotlib package using conda or pip\n",
" oPyflowline.plot( sVariable_in='mesh' ) \n",
"\n",
"else:\n",
" \n",
" import geopandas as gpd\n",
" import matplotlib.pyplot as plt\n",
" #use the geopanda package\n",
Expand All @@ -761,10 +856,7 @@
" gdf.plot()\n",
" plt.show()\n",
"\n",
" pass\n",
"\n",
"else:\n",
" oPyflowline.plot( sVariable_in='mesh' ) "
" pass"
]
},
{
Expand Down Expand Up @@ -879,7 +971,11 @@
],
"source": [
"\n",
"if iVisualization_method == 1: \n",
"if iFlag_cartopy == 1: \n",
" oPyflowline.plot( sVariable_in='overlap') \n",
" pass\n",
"else:\n",
" \n",
" #\n",
" file1_path = oPyflowline.aBasin[0].sFilename_mesh_geojson\n",
" file2_path = oPyflowline.aBasin[0].sFilename_flowline_conceptual_geojson\n",
Expand All @@ -889,9 +985,6 @@
" gdf1.plot(ax=ax, color='blue')\n",
" gdf2.plot(ax=ax, color='red')\n",
" plt.show()\n",
" pass\n",
"else:\n",
" oPyflowline.plot( sVariable_in='overlap') \n",
" pass"
]
},
Expand Down
16 changes: 8 additions & 8 deletions pyflowline/algorithms/intersect/intersect_flowline_with_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
pLayerOut = pDataset_out.CreateLayer('flowline', pSpatial_reference_flowline, ogr.wkbMultiLineString)
# Add one attribute
pLayerOut.CreateField(ogr.FieldDefn('lineid', ogr.OFTInteger64)) #long type for high resolution
pLayerOut.CreateField(ogr.FieldDefn('segment', ogr.OFTInteger)) #long type for high resolution
pLayerOut.CreateField(ogr.FieldDefn('order', ogr.OFTInteger)) #long type for high resolution
pLayerOut.CreateField(ogr.FieldDefn('stream_segment', ogr.OFTInteger)) #long type for high resolution
pLayerOut.CreateField(ogr.FieldDefn('stream_order', ogr.OFTInteger)) #long type for high resolution
pLayerDefn = pLayerOut.GetLayerDefn()
pFeatureOut = ogr.Feature(pLayerDefn)
lID_flowline = 0
Expand Down Expand Up @@ -84,8 +84,8 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
for j in range (nfeature_flowline):
pFeature_flowline = pLayer_flowline.GetFeature(j)
pGeometry_flowline = pFeature_flowline.GetGeometryRef()
iStream_segment = pFeature_flowline.GetField("segment")
iStream_order = pFeature_flowline.GetField("order")
iStream_segment = pFeature_flowline.GetField("stream_segment")
iStream_order = pFeature_flowline.GetField("stream_order")
if (pGeometry_flowline.IsValid()):
pass
else:
Expand All @@ -99,8 +99,8 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
if pGeometrytype_intersect == 'LINESTRING':
pFeatureOut.SetGeometry(pGeometry_intersect)
pFeatureOut.SetField("lineid", lID_flowline)
pFeatureOut.SetField("segment", iStream_segment)
pFeatureOut.SetField("order", iStream_order)
pFeatureOut.SetField("stream_segment", iStream_segment)
pFeatureOut.SetField("stream_order", iStream_order)
pLayerOut.CreateFeature(pFeatureOut)

aCoords = list()
Expand All @@ -125,8 +125,8 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
Line = pGeometry_intersect.GetGeometryRef(i)
pFeatureOut.SetGeometry(Line)
pFeatureOut.SetField("lineid", lID_flowline)
pFeatureOut.SetField("segment", iStream_segment)
pFeatureOut.SetField("order", iStream_order)
pFeatureOut.SetField("stream_segment", iStream_segment)
pFeatureOut.SetField("stream_order", iStream_order)
pLayerOut.CreateFeature(pFeatureOut)
aCoords = list()
for i in range(0, Line.GetPointCount()):
Expand Down
8 changes: 4 additions & 4 deletions pyflowline/algorithms/merge/merge_flowline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

lID = 0

def merge_flowline(aFlowline_in, aVertex_in, \
pVertex_outlet_in, \
aIndex_headwater_in,\
aIndex_middle_in, \
def merge_flowline(aFlowline_in, aVertex_in,
pVertex_outlet_in,
aIndex_headwater_in,
aIndex_middle_in,
aIndex_confluence_in ):

nVertex=len(aVertex_in)
Expand Down
2 changes: 1 addition & 1 deletion pyflowline/classes/_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _plot_mesh_with_flowline(self,
sFilename_json = os.path.join(pBasin.sWorkspace_output_basin, dummy)
aFilename_in.append(sFilename_json)
aFlag_color.append(1)
aVariable_in.append('segment')
aVariable_in.append('stream_segment')

map_multiple_vector_data(aFiletype_in,
aFilename_in,
Expand Down
Loading
Loading