Skip to content

Commit 4f94ef3

Browse files
committed
Added udf to compute temporal score and combine with distance score for
weighted composites. The computation still expects a target date to be provided explicitly, I am not sure if that is possible with the moving window approach that I was targeting.
1 parent c343d64 commit 4f94ef3

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

efast/openeo/udf/__init__.py

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import numpy as np
2+
import xarray as xr
3+
from openeo.udf import XarrayDataCube
4+
from openeo.udf.debug import inspect
5+
from datetime import datetime
6+
7+
DATE_FORMAT = "%Y-%m-%d"
8+
CONTEXT = {
9+
"target_date": "2018-06-18",
10+
"sigma_doy": 10,
11+
}
12+
13+
def apply_datacube(cube: XarrayDataCube, context: dict=CONTEXT) -> XarrayDataCube:
14+
distance_score = cube.get_array()
15+
inspect(distance_score["t"], level="INFO", message="inspecting time dimension")
16+
target_date = datetime.strptime(get_from_context(context, "target_date"), DATE_FORMAT)
17+
18+
# TODO see if this is the correct order
19+
time_differences = distance_score.indexes["t"] - target_date
20+
sigma_doy = get_from_context(context, "sigma_doy")
21+
22+
doy_score = np.exp(
23+
-1
24+
/ 2
25+
* (np.array(time_differences.days) ** 2 / sigma_doy**2)
26+
)
27+
# TODO this division is likely redundant
28+
#doy_score = doy_score / np.max(doy_score)
29+
30+
inspect(distance_score.dims, message="distance score dims")
31+
inspect(doy_score.shape, message="doy score dims")
32+
score = distance_score * doy_score.reshape((-1, 1, 1, 1))
33+
score = score / (np.sum(score, axis=0) + 1e-5)
34+
inspect(score.shape)
35+
36+
score = xr.DataArray(
37+
score,
38+
#dims=["t", "x", "y", "bands"],
39+
dims=["t", "bands", "y", "x"],
40+
coords={
41+
"t": distance_score["t"],
42+
"x": distance_score["x"],
43+
"y": distance_score["y"],
44+
"bands": ["weight"]
45+
}
46+
)
47+
48+
return XarrayDataCube(score)
49+
50+
51+
def get_from_context(context, key):
52+
return context.get(key, CONTEXT[key])

tests/test_temporal_score_udf.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from openeo import UDF
2+
from openeo.udf import XarrayDataCube
3+
from openeo.udf.run_code import execute_local_udf
4+
import xarray as xr
5+
import pandas as pd
6+
import numpy as np
7+
8+
from efast.openeo.udf.compute_s3_composite_weights import apply_datacube
9+
10+
11+
DATE_FORMAT = "%Y-%m-%d"
12+
13+
14+
def create_dummy_data():
15+
time = pd.date_range("2018-06-01", periods=30, freq="D")
16+
x = np.arange(7)
17+
y = np.arange(6)
18+
bands = ["red", "green", "blue"]
19+
data = np.random.rand(len(time), len(x), len(y), len(bands))
20+
21+
ds = xr.DataArray(
22+
data,
23+
dims=["t", "x", "y", "bands"],
24+
coords={
25+
"t": time,
26+
"x": x,
27+
"y": y,
28+
"bands": bands,
29+
}
30+
)
31+
return ds
32+
33+
34+
def create_dummy_dtc():
35+
time = pd.date_range("2018-06-01", periods=30, freq="D")
36+
x = np.arange(7)
37+
y = np.arange(6)
38+
bands = ["distance_to_cloud"]
39+
#data = np.random.rand(len(time), len(x), len(y), len(bands))
40+
data = np.random.rand(len(time), len(bands), len(y), len(x))
41+
42+
da = xr.DataArray(
43+
data,
44+
#dims=["t", "x", "y", "bands"],
45+
dims=["t", "bands", "y", "x"],
46+
coords={
47+
"t": time,
48+
"x": x,
49+
"y": y,
50+
"bands": bands,
51+
}
52+
)
53+
return da
54+
55+
56+
def test_time_difference_score():
57+
data = create_dummy_dtc()
58+
udf = UDF.from_file("efast/openeo/udf/compute_s3_composite_weights.py")
59+
target_date = "2018-06-18"
60+
context = {
61+
"target_date": target_date,
62+
"sigma_doy": 10,
63+
}
64+
apply_datacube(XarrayDataCube(data), context)
65+
res = execute_local_udf(udf, data, fmt="netcdf")
66+
67+
return True
68+
69+
70+

0 commit comments

Comments
 (0)