Skip to content

Commit

Permalink
process_plot from future
Browse files Browse the repository at this point in the history
  • Loading branch information
jmccreight committed Nov 20, 2024
1 parent 6b5a515 commit 6f85215
Showing 1 changed file with 66 additions and 37 deletions.
103 changes: 66 additions & 37 deletions pywatershed/analysis/process_plot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pathlib as pl
from textwrap import wrap
from typing import Callable
from typing import Callable, Tuple, Union

import contextily as cx
import matplotlib.pyplot as plt
Expand All @@ -19,37 +19,62 @@
class ProcessPlot:
def __init__(
self,
gis_dir: pl.Path,
gis_dir: Union[str, pl.Path],
hru_shp_file_name: str = "HRU_subset.shp",
seg_shp_file_name: str = "Segments_subset.shp",
):
gpd = import_optional_dependency("geopandas")

self.hru_shapefile = gis_dir / hru_shp_file_name
self.seg_shapefile = gis_dir / seg_shp_file_name
self.gis_dir = pl.Path(gis_dir)
if hru_shp_file_name is not None:
self.hru_shapefile = self.gis_dir / hru_shp_file_name
else:
self.hru_shapefile = None

if seg_shp_file_name is not None:
self.seg_shapefile = self.gis_dir / seg_shp_file_name
else:
self.seg_shapefile = None

# HRU one-time setups
self.hru_gdf = (
gpd.read_file(self.hru_shapefile)
.drop("nhm_id", axis=1)
.rename(columns={"nhru_v1_1": "nhm_id"})
.set_index("nhm_id")
)
if self.hru_shapefile is not None:
self.hru_gdf = gpd.read_file(self.hru_shapefile)

# standardization manipulations based on a variety of different
# conventions which have been found for the shp files
if ("nhm_id" in self.hru_gdf.columns) and (
"nhru_v1_1" in self.hru_gdf.columns
):
# This is borderline non-sense
self.hru_gdf = (
self.hru_gdf.drop("nhm_id", axis=1)
.rename(columns={"nhru_v1_1": "nhm_id"})
.set_index("nhm_id")
)
elif "GRID_CODE" in self.hru_gdf.columns:
self.hru_gdf = self.hru_gdf.rename(
columns={"GRID_CODE": "nhm_id"}
).set_index("nhm_id")

else:
msg = "Unidentified shp file convention, work needed"
raise ValueError(msg)

# segment one-time setup
self.seg_gdf = gpd.read_file(self.seg_shapefile)
# if (self.__seg_poly.crs.name
# == "USA_Contiguous_Albers_Equal_Area_Conic_USGS_version"):
# print("Overriding USGS aea crs with EPSG:5070")
self.seg_gdf.set_crs("EPSG:5070")

self.seg_geoms_exploded = (
self.seg_gdf.explode(index_parts=True)
.reset_index(level=1, drop=True)
.drop("model_idx", axis=1)
.rename(columns={"nsegment_v": "nhm_seg"})
.set_index("nhm_seg")
)
if self.seg_shapefile is not None:
self.seg_gdf = gpd.read_file(self.seg_shapefile)
# if (self.__seg_poly.crs.name
# == "USA_Contiguous_Albers_Equal_Area_Conic_USGS_version"):
# print("Overriding USGS aea crs with EPSG:5070")
self.seg_gdf.set_crs("EPSG:5070")

self.seg_geoms_exploded = (
self.seg_gdf.explode(index_parts=True)
.reset_index(level=1, drop=True)
.drop("model_idx", axis=1)
.rename(columns={"nsegment_v": "nhm_seg"})
.set_index("nhm_seg")
)

return

Expand Down Expand Up @@ -147,6 +172,7 @@ def plot_hru_var(
data: np.ndarray = None,
data_units: str = None,
nhm_id: np.ndarray = None,
clim: Tuple[float] = None,
):
_ = import_optional_dependency("hvplot.pandas")

Expand Down Expand Up @@ -195,20 +221,23 @@ def plot_hru_var(
)
clabel = f'{metadata["units"]}'

plot = plot_df.hvplot(
tiles=True,
crs=ccrs.epsg(5070),
frame_height=frame_height,
c=var_name,
line_width=0,
alpha=0.75,
# clim=stat_lims[stat],
hover_cols=["nhm_id"],
title=title,
clabel=clabel,
xlabel="Longitude (degrees East)",
ylabel="Latitude (degrees North)",
)
args = {
"tiles": True,
"crs": ccrs.epsg(5070),
"frame_height": frame_height,
"c": var_name,
"line_width": 0,
"alpha": 0.75,
"hover_cols": ["nhm_id"],
"title": title,
"clabel": clabel,
"xlabel": "Longitude (degrees East)",
"ylabel": "Latitude (degrees North)",
}
if clim is not None:
args["clim"] = clim

plot = plot_df.hvplot(**args)
return plot


Expand Down

0 comments on commit 6f85215

Please sign in to comment.