Skip to content
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
239 changes: 35 additions & 204 deletions notebooks/part0_python_intro/08_pandas.ipynb

Large diffs are not rendered by default.

1,068 changes: 624 additions & 444 deletions notebooks/part0_python_intro/solutions/08_pandas.ipynb

Large diffs are not rendered by default.

78 changes: 32 additions & 46 deletions notebooks/part1_flopy/04_Modelgrid_and_intersection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source": [
"# Modelgrid and intersection\n",
"\n",
"The first part of this notebook is focused on the `flopy.discretization.Grid` (modelgrid) object(s). The modelgrid object(s) are a relatively new addition to FloPy's capabilities and are the backbone of plotting, exporting, and GIS data processing within FloPy. These objects are automatically created when a model is loaded. Alternatively they can also be created as a stand alone object. \n",
"The first part of this notebook is focused on the `flopy.discretization.Grid` (modelgrid) object(s). The modelgrid object(s) are core to FloPy's capabilities and are the backbone of plotting, exporting, and GIS data processing within FloPy. These objects are automatically created when a model is loaded. Alternatively they can also be created as a stand alone object. \n",
"\n",
"There are three types of modelgrids:\n",
"\n",
Expand Down Expand Up @@ -426,26 +426,6 @@
"shape"
]
},
{
"cell_type": "markdown",
"id": "49458e73",
"metadata": {},
"source": [
"## Class Exercise 2: \n",
"\n",
"Find and plot all of the nearest neighbors at node 79 on the vertex modelgrid.\n",
"\n",
"**Hint**: Use `flopy.plot.PlotMapView` for your plotting, and plot a boolean (1, 0) array with the cells that are not neighbors masked out.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fad34d11",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "0426f4ab",
Expand Down Expand Up @@ -479,24 +459,6 @@
"ugrid.plot();"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5575ecf9",
"metadata": {},
"outputs": [],
"source": [
"neighbors = ugrid.neighbors(79)\n",
"arr = np.zeros(ugrid.shape, dtype=int)\n",
"arr[neighbors] = 1\n",
"\n",
"fig, ax = plt.subplots(figsize=(8, 8))\n",
"\n",
"pmv = flopy.plot.PlotMapView(ax=ax, modelgrid=ugrid)\n",
"pmv.plot_grid()\n",
"pmv.plot_array(arr, masked_values=[0,]);"
]
},
{
"cell_type": "markdown",
"id": "32fb78b6",
Expand Down Expand Up @@ -677,7 +639,7 @@
"source": [
"gx = GridIntersect(modelgrid)\n",
"\n",
"result = gx.intersect(basin.loc[station_id, \"geometry\"])\n",
"result = gx.intersect(basin.loc[station_id, \"geometry\"], geo_dataframe=False)\n",
"result.cellids"
]
},
Expand Down Expand Up @@ -979,7 +941,7 @@
"outputs": [],
"source": [
"arr = np.genfromtxt(data_path / \"pet.txt\")\n",
"arr.shape = (12, modelgrid.nrow, modelgrid.ncol)\n",
"arr = arr.reshape(12, modelgrid.nrow, modelgrid.ncol)\n",
"pet_monthly = {i: a for i, a in enumerate(arr)}\n",
"pet_monthly"
]
Expand Down Expand Up @@ -1269,10 +1231,14 @@
"metadata": {},
"outputs": [],
"source": [
"import dataretrieval.nwis as nwis\n",
"from dataretrieval import waterdata\n",
"try:\n",
" gage = nwis.get_record(sites=station_id, service='iv', start=\"2024-01-01\", end=\"2024-12-31\", parameterCd=\"00065\")\n",
" gage.reset_index(inplace=True)\n",
" import dataretrieval.nwis as nwis\n",
" gage, metadata = waterdata.get_continuous(\n",
" f\"USGS-{station_id}\",\n",
" parameter_code=\"00065\",\n",
" time=\"2024-01-01/2024-12-31\"\n",
" )\n",
" gage.to_csv(data_path / \"sagehen_gage_data.csv\")\n",
"except (ValueError, ConnectionError, NameError):\n",
" gage = pd.read_csv(data_path / \"sagehen_gage_data.csv\")\n",
Expand All @@ -1287,10 +1253,10 @@
"metadata": {},
"outputs": [],
"source": [
"gage[\"datetime\"] = pd.to_datetime(gage[\"datetime\"])\n",
"gage[\"datetime\"] = pd.to_datetime(gage[\"time\"])\n",
"gage = gage.set_index(\"datetime\")\n",
"\n",
"gage.rename(columns={\"00065\": \"stage\"}, inplace=True)\n",
"gage.rename(columns={\"value\": \"stage\"}, inplace=True)\n",
"gage.stage *= 0.3284\n",
"ax = gage.stage.plot()\n",
"ax.set_ylabel(\"stage, in feet\")\n",
Expand Down Expand Up @@ -1488,13 +1454,33 @@
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f9dadaa-53e6-45c2-a3ff-130b1305c8f5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.14.6"
}
},
"nbformat": 4,
Expand Down
72 changes: 42 additions & 30 deletions notebooks/part1_flopy/05_Unstructured_Grid_generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"import numpy as np\n",
"import pandas as pd\n",
"import contextily as ctx\n",
"from dataretrieval import nldi, nwis\n",
"from dataretrieval import nldi, waterdata\n",
"from flopy.discretization import StructuredGrid, VertexGrid\n",
"from flopy.plot import PlotMapView\n",
"from flopy.utils.gridgen import Gridgen\n",
Expand Down Expand Up @@ -120,8 +120,7 @@
"metadata": {},
"outputs": [],
"source": [
"# live code modelgrid creation\n",
"cellsize = 200\n"
"# live code modelgrid creation\n"
]
},
{
Expand Down Expand Up @@ -257,14 +256,16 @@
"griddf = sgrid.geo_dataframe.set_crs(epsg=26911)\n",
"grid_wgs = griddf.to_crs(epsg=4326)\n",
"\n",
"wxmin, wxmax, wymin, wymax = grid_wgs.total_bounds\n",
"wxmin, wymin, wxmax, wymax = grid_wgs.total_bounds\n",
"wgs_bounds = [wxmin, wymin, wxmax, wymax]\n",
"\n",
"try:\n",
" info, metadata = nwis.get_info(bBox=[f\"{i :.2f}\" for i in wgs_bounds])\n",
" info, metadata = waterdata.get_monitoring_locations(bbox=wgs_bounds)\n",
" # info.to_file(geospatial_ws / \"gage_info.geojson\", driver=\"GeoJSON\")\n",
"except (ValueError, ConnectionError, NameError):\n",
" info = gpd.read_file(geospatial_ws / \"gage_info.shp\")\n",
"\n",
" info = gpd.read_file(geospatial_ws / \"gage_info.geojson\")\n",
" \n",
"info = info.set_crs(grid_wgs.crs)\n",
"info.head()"
]
},
Expand Down Expand Up @@ -305,7 +306,7 @@
"\n",
"rstr.plot(ax=ax)\n",
"info.plot(\n",
" column=\"site_no\",\n",
" column=\"monitoring_location_number\",\n",
" ax=ax,\n",
" cmap=\"tab20\",\n",
" legend=True,\n",
Expand All @@ -328,7 +329,7 @@
"metadata": {},
"outputs": [],
"source": [
"sitedf = info.loc[info.site_no == \"10343500\"]\n",
"sitedf = info.loc[info.monitoring_location_number == \"10343500\"]\n",
"sitedf"
]
},
Expand All @@ -350,7 +351,7 @@
"try:\n",
" basindf = nldi.get_basin(\n",
" feature_source=\"nwissite\",\n",
" feature_id=f\"USGS-{sitedf.site_no.values[0]}\",\n",
" feature_id=sitedf.monitoring_location_id.values[0],\n",
" )\n",
" basindf = basindf.to_crs(epsg=epsg)\n",
"except (ValueError, ConnectionError, NameError):\n",
Expand All @@ -376,7 +377,7 @@
"rstr.plot(ax=ax)\n",
"basindf.plot(ax=ax, facecolor=\"None\", edgecolor=\"k\", lw=1.5)\n",
"info.plot(\n",
" column=\"site_no\",\n",
" column=\"monitoring_location_number\",\n",
" ax=ax,\n",
" cmap=\"tab20\",\n",
" legend=True,\n",
Expand Down Expand Up @@ -488,7 +489,7 @@
"\n",
"basindf.plot(ax=ax, facecolor=\"None\", edgecolor=\"r\", lw=1.5)\n",
"sitedf.plot(\n",
" column=\"site_no\",\n",
" column=\"monitoring_location_number\",\n",
" ax=ax,\n",
" cmap=\"tab20\",\n",
" legend=True,\n",
Expand Down Expand Up @@ -525,7 +526,7 @@
" navigation_mode=\"UT\",\n",
" distance=999,\n",
" feature_source=\"nwissite\",\n",
" feature_id=f\"USGS-{sitedf.site_no.values[0]}\",\n",
" feature_id=sitedf.monitoring_location_id.values[0],\n",
" )\n",
" nhddf = nhddf.to_crs(epsg=epsg)\n",
"except (ValueError, ConnectionError, NameError):\n",
Expand Down Expand Up @@ -732,7 +733,7 @@
"\n",
"basindf.plot(ax=ax, facecolor=\"None\", edgecolor=\"r\", lw=1.5)\n",
"sitedf.plot(\n",
" column=\"site_no\",\n",
" column=\"monitoring_location_number\",\n",
" ax=ax,\n",
" cmap=\"tab20\",\n",
" legend=True,\n",
Expand Down Expand Up @@ -1677,11 +1678,11 @@
"source": [
"## Activity (If time permits); Build a Voronoi mesh/Vertex Grid:\n",
"\n",
"Define a voronoi grid for the basin upstream of the Black Earth Creek streamgauge near Cross Plains, WI. The upstream basin information is supplied by NLDI and has been saved to the class repo in case of internet connectivity or NLDI service issues.\n",
"Define a voronoi grid for the basin upstream of the Little Sandy River streamgauge near Bull Run, OR. The upstream basin information is supplied by NLDI and has been saved to the class repo in case of internet connectivity or NLDI service issues.\n",
"\n",
"Some of the previous code can be re-used to create a voronoi mesh and subsequent `VertexGrid` representation of the basin. See the Triangular and Voronoi grid sections of the notebook.\n",
"\n",
"**Optional**, use the Upstream Main segment of Black Earth Creek to refine the voronoi mesh around the stream location. Refinement control points have been pre-defined below."
"**Optional**, use the Upstream Main segment of the Little Sandy River to refine the voronoi mesh around the stream location. Refinement control points have been pre-defined below."
]
},
{
Expand All @@ -1692,16 +1693,15 @@
"outputs": [],
"source": [
"epsg = 5070 # NAD83 CONUS Albers\n",
"# try:\n",
" # todo: update this for black earth creek near cross plains WI site-no 05406479\n",
"be_cr_nr_cp = \"05406479\"\n",
"basindf = nldi.get_basin(\n",
" feature_source=\"nwissite\", feature_id=f\"USGS-{be_cr_nr_cp}\"\n",
")\n",
"basindf = basindf.to_crs(epsg=epsg)\n",
"basindf.to_file(geospatial_ws / \"black_earth_basin.shp\")\n",
"# except (ValueError, ConnectionError, NameError):\n",
"# basindf = gpd.read_file(geospatial_ws / \"black_earth_basin.shp\")"
"try:\n",
" lsr_gauge_id = \"USGS-14141500\"\n",
" basindf = nldi.get_basin(\n",
" feature_source=\"nwissite\", feature_id=lsr_gauge_id\n",
" )\n",
" basindf = basindf.to_crs(epsg=epsg)\n",
" basindf.to_file(geospatial_ws / \"little_sandy_basin.shp\")\n",
"except (ValueError, ConnectionError, NameError):\n",
" basindf = gpd.read_file(geospatial_ws / \"little_sandy_basin.shp\")"
]
},
{
Expand All @@ -1716,12 +1716,12 @@
" navigation_mode=\"UM\",\n",
" distance=9999,\n",
" feature_source=\"nwissite\",\n",
" feature_id=f\"USGS-{be_cr_nr_cp}\",\n",
" feature_id=lsr_gauge_id,\n",
" )\n",
" nhddf = nhddf.to_crs(epsg=epsg)\n",
" nhddf.to_file(geospatial_ws / \"black_earth_creek_main.shp\")\n",
" nhddf.to_file(geospatial_ws / \"little_sandy_river_main.shp\")\n",
"except (ValueError, ConnectionError, NameError):\n",
" nhddf = gpd.read_file(geospatial_ws / \"black_earth_creek_main.shp\")"
" nhddf = gpd.read_file(geospatial_ws / \"little_sandy_river_main.shp\")"
]
},
{
Expand Down Expand Up @@ -1766,7 +1766,7 @@
"outputs": [],
"source": [
"wsloc = basindf.centroid.values[0]\n",
"stloc = list(zip(*nhddf.geometry.values[10].xy))[0]"
"stloc = list(zip(*nhddf.geometry.values[5].xy))[0]"
]
},
{
Expand Down Expand Up @@ -1799,6 +1799,18 @@
"display_name": "Python 3 (ipykernel)",
"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.14.6"
}
},
"nbformat": 4,
Expand Down
Loading
Loading