-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathconstrain.py
More file actions
43 lines (34 loc) · 1.03 KB
/
constrain.py
File metadata and controls
43 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Constraint masking for MCDA suitability surfaces."""
from __future__ import annotations
import numpy as np
import xarray as xr
def constrain(
suitability: xr.DataArray,
exclude: list[xr.DataArray],
fill: float = np.nan,
name: str | None = None,
) -> xr.DataArray:
"""Mask out areas that are categorically unsuitable.
Pixels where any exclusion mask is True (nonzero) are set to *fill*.
Parameters
----------
suitability : xr.DataArray
Input suitability surface.
exclude : list of xr.DataArray
Boolean or binary masks. True/nonzero marks excluded areas.
fill : float
Value to assign to excluded pixels. Default ``np.nan``.
name : str, optional
Name of the output DataArray.
Returns
-------
xr.DataArray
Constrained suitability surface.
"""
if name is None:
name = suitability.name
result = suitability.copy()
for mask in exclude:
result = xr.where(mask, fill, result)
result.name = name
return result