Skip to content

Commit adf61a6

Browse files
committed
DuraMAT pref maps
1 parent 1b113c3 commit adf61a6

File tree

3 files changed

+98
-17
lines changed

3 files changed

+98
-17
lines changed

pvdeg/geospatial.py

+68-3
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ def plot_sparse_analysis(
970970
method="nearest",
971971
resolution:complex=100j,
972972
figsize:tuple=(10,8),
973+
show_plot:bool=False,
973974
) -> None:
974975
"""
975976
Plot the output of a sparse geospatial analysis using interpolation.
@@ -999,7 +1000,7 @@ def plot_sparse_analysis(
9991000
)
10001001

10011002
fig = plt.figure(figsize=figsize)
1002-
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal(), frameon=False)
1003+
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal(), frameon=False) # these should be the same ccrs
10031004
ax.patch.set_visible(False)
10041005

10051006
extent = [lon.min(), lon.max(), lat.min(), lat.max()]
@@ -1009,7 +1010,7 @@ def plot_sparse_analysis(
10091010
extent=extent,
10101011
origin="lower",
10111012
cmap="viridis",
1012-
transform=ccrs.PlateCarree(),
1013+
transform=ccrs.PlateCarree(), # why are ccrs different
10131014
)
10141015

10151016
shapename = "admin_1_states_provinces_lakes"
@@ -1030,6 +1031,70 @@ def plot_sparse_analysis(
10301031
plt.title(f"Interpolated Sparse Analysis, {data_var}")
10311032
plt.xlabel("Longitude")
10321033
plt.ylabel("Latitude")
1033-
plt.show()
1034+
1035+
if show_plot:
1036+
plt.show()
1037+
1038+
return fig, ax
1039+
1040+
def plot_sparse_analysis_land(
1041+
result: xr.Dataset,
1042+
data_var: str,
1043+
method="nearest",
1044+
resolution:complex=100j,
1045+
figsize:tuple=(10,8),
1046+
show_plot:bool=False,
1047+
proj=ccrs.PlateCarree(),
1048+
):
1049+
1050+
import matplotlib.path as mpath
1051+
from cartopy.mpl.patch import geos_to_path
1052+
1053+
grid_values, lat, lon = interpolate_analysis(
1054+
result=result, data_var=data_var, method=method, resolution=resolution
1055+
)
1056+
1057+
fig = plt.figure(figsize=figsize)
1058+
ax = fig.add_axes([0, 0, 1, 1], projection=proj, frameon=False)
1059+
ax.patch.set_visible(False)
1060+
1061+
extent = [lon.min(), lon.max(), lat.min(), lat.max()]
1062+
ax.set_extent(extent, crs=proj)
1063+
1064+
mesh = ax.pcolormesh(lon, lat, grid_values, transform=proj, cmap='viridis')
1065+
1066+
land_path = geos_to_path(list(cfeature.LAND.geometries()))
1067+
land_path = mpath.Path.make_compound_path(*land_path)
1068+
plate_carre_data_transform = proj._as_mpl_transform(ax)
1069+
mesh.set_clip_path(land_path, plate_carre_data_transform)
1070+
1071+
shapename = "admin_1_states_provinces_lakes"
1072+
states_shp = shpreader.natural_earth(
1073+
resolution="110m", category="cultural", name=shapename
1074+
)
1075+
1076+
ax.add_geometries(
1077+
shpreader.Reader(states_shp).geometries(),
1078+
proj,
1079+
facecolor="none",
1080+
edgecolor="black",
1081+
linestyle=':'
1082+
)
1083+
1084+
cbar = plt.colorbar(mesh, ax=ax, orientation="vertical", fraction=0.02, pad=0.04)
1085+
cbar.set_label("Value")
1086+
1087+
utilities._add_cartopy_features(
1088+
ax=ax,
1089+
features = [
1090+
cfeature.BORDERS,
1091+
cfeature.COASTLINE,
1092+
cfeature.LAND,
1093+
cfeature.OCEAN,
1094+
],
1095+
)
1096+
1097+
if show_plot:
1098+
plt.show()
10341099

10351100
return fig, ax

pvdeg/scenario.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def plot_coords(
17671767
coord_2: Optional[tuple[float]] = None,
17681768
coords: Optional[np.ndarray[float]] = None,
17691769
size: Union[int, float] = 1,
1770-
) -> None:
1770+
):
17711771
"""
17721772
Plot lat-long coordinate pairs on blank map. Quickly view
17731773
geospatial datapoints before your analysis.
@@ -1789,6 +1789,11 @@ def plot_coords(
17891789
size : float
17901790
matplotlib scatter point size. Without any downsampling NSRDB
17911791
points will siginficantly overlap.
1792+
1793+
Returns:
1794+
--------
1795+
fig, ax
1796+
matplotlib figure and axis
17921797
"""
17931798
fig = plt.figure(figsize=(15, 10))
17941799
ax = plt.axes(projection=ccrs.PlateCarree())
@@ -1811,6 +1816,9 @@ def plot_coords(
18111816
plt.title(f"Coordinate Pairs from '{self.name}' Meta Data")
18121817
plt.show()
18131818

1819+
return fig, ax
1820+
1821+
18141822
def plot_meta_classification(
18151823
self,
18161824
col_name: str = None,
@@ -1844,6 +1852,11 @@ def plot_meta_classification(
18441852
size : float
18451853
matplotlib scatter point size. Without any downsampling NSRDB
18461854
points will siginficantly overlap.
1855+
1856+
Returns:
1857+
--------
1858+
fig, ax
1859+
matplotlib figure and axis
18471860
"""
18481861
if not col_name:
18491862
raise ValueError("col_name cannot be none")
@@ -1892,6 +1905,8 @@ def plot_meta_classification(
18921905
plt.legend()
18931906
plt.show()
18941907

1908+
return fig, ax
1909+
18951910
def plot_world(
18961911
self,
18971912
data_variable: str,

pvdeg/utilities.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections import OrderedDict
1212
import xarray as xr
1313
from subprocess import run
14-
14+
import cartopy.feature as cfeature
1515

1616
def gid_downsampling(meta, n):
1717
"""
@@ -1012,7 +1012,7 @@ def _find_bbox_corners(coord_1=None, coord_2=None, coords=None):
10121012
min and max latitude and longitudes. Minimum latitude at lats[0].
10131013
Maximum latitude at lats[1]. Same pattern for longs.
10141014
"""
1015-
if coord_1 and coord_2:
1015+
if coord_1 is not None and coord_2 is not None:
10161016
lats = [coord_1[0], coord_2[0]]
10171017
longs = [coord_1[1], coord_2[1]]
10181018
elif coords.any():
@@ -1043,20 +1043,21 @@ def _plot_bbox_corners(ax, coord_1=None, coord_2=None, coords=None):
10431043
return
10441044

10451045

1046-
def _add_cartopy_features(ax):
1046+
def _add_cartopy_features(
1047+
ax,
1048+
features = [
1049+
cfeature.BORDERS,
1050+
cfeature.COASTLINE,
1051+
cfeature.LAND,
1052+
cfeature.OCEAN,
1053+
cfeature.LAKES,
1054+
cfeature.RIVERS,
1055+
],
1056+
):
10471057
"""
10481058
Add cartopy features to an existing matplotlib.pyplot axis.
10491059
"""
1050-
import cartopy.feature as cfeature
1051-
1052-
features = [
1053-
cfeature.BORDERS,
1054-
cfeature.COASTLINE,
1055-
cfeature.LAND,
1056-
cfeature.OCEAN,
1057-
cfeature.LAKES,
1058-
cfeature.RIVERS,
1059-
]
1060+
10601061

10611062
for i in features:
10621063
if i == cfeature.BORDERS:

0 commit comments

Comments
 (0)