Skip to content

Commit 5009d12

Browse files
committed
Zero out rather than NaN areas with no potential
1 parent 51010fa commit 5009d12

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

workflow/scripts/_script_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def random_categorical_cmap(n, base_cmap="tab20", seed=42):
2727
return mcolors.ListedColormap(colors)
2828

2929

30+
def plot_with_zero_separate(ax, da, cmap="viridis", zero_color="#e0e0e0"):
31+
"""Plot data array with zero values in a separate color."""
32+
da.where(da != 0).plot(ax=ax, cmap=cmap)
33+
da.where(da == 0).plot(
34+
ax=ax, cmap=mcolors.ListedColormap([zero_color]), add_colorbar=False
35+
)
36+
return ax
37+
38+
3039
def plot_all_dataset_variables(ds, ncols=2, savefig=None, categorical_vars=[]):
3140
"""Plot all variables in an xarray dataset on a grid of plots."""
3241
# If needed, resample `ds` to fit within a maximum of `max_pixels` pixels
@@ -63,7 +72,8 @@ def plot_all_dataset_variables(ds, ncols=2, savefig=None, categorical_vars=[]):
6372
cmap = random_categorical_cmap(len(ds[var].values))
6473
else:
6574
cmap = "viridis"
66-
ds[var].plot(ax=axes[i], cmap=cmap)
75+
76+
plot_with_zero_separate(ax=axes[i], da=ds[var], cmap=cmap)
6777
axes[i].set_title(var)
6878
# We want to save rasterized images also for e.g. PDF output
6979
# Any actor with a zorder below the value given here is rasterized

workflow/scripts/area_potential.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import matplotlib.pyplot as plt
88
import xarray as xr
99
import yaml
10+
from _script_utils import plot_with_zero_separate
1011

1112

1213
@click.command()
@@ -55,22 +56,23 @@ def get_area_potential(
5556
# Start with the configured pixel area as a base
5657
potential_da = ds[config["initial_area"]].squeeze(drop=True) # Drop `band`
5758

58-
# Drop pixels from binary layers with share 0 from potential_da
59+
# Zero out pixels from binary layers with share 0 from potential_da
5960
binary_layers = config.get("binary_layers", {})
6061
zero_binary_layers = [layer for layer, value in binary_layers.items() if value == 0]
6162
for layer in zero_binary_layers:
6263
if layer in ds:
63-
potential_da = potential_da.where(~(ds[layer] > 0))
64+
potential_da = potential_da.where(~(ds[layer] > 0), other=0)
6465
else:
6566
print(f"Warning: Layer '{layer}' not found in dataset. Skipping.")
6667

67-
# Apply the continuous_layers criteria to drop additional pixels
68+
# Apply the continuous_layers criteria to zero out additional pixels
6869
continuous_layers = config.get("continuous_layers", {})
6970
for layer, layer_config in continuous_layers.items():
7071
if layer in ds:
7172
# Apply the min-max criteria
7273
potential_da = potential_da.where(
73-
(ds[layer] <= layer_config["max"]) & (ds[layer] >= layer_config["min"])
74+
(ds[layer] <= layer_config["max"]) & (ds[layer] >= layer_config["min"]),
75+
other=0,
7476
)
7577
# If a share is defined, multiply the pixel area by the share
7678
if "share" in layer_config:
@@ -111,7 +113,8 @@ def get_area_potential(
111113
potential_da = potential_da.transpose("band", "y", "x")
112114
potential_da.rio.write_crs(ds.rio.crs, inplace=True)
113115

114-
potential_da.plot()
116+
fig, ax = plt.subplots(1, 1)
117+
ax = plot_with_zero_separate(ax=ax, da=potential_da)
115118
plt.savefig(plot_path, bbox_inches="tight")
116119

117120
# Fill NaN with a nodata value only after plotting

0 commit comments

Comments
 (0)