Skip to content

Commit a599727

Browse files
committed
Extracted s2 preprocessing implementation from test.
Renamed efast_openeo to s2_processing_openeo
1 parent 448b65d commit a599727

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,39 @@ def get_s2_cube(self, connection):
3636
)
3737

3838

39-
def extract_mask(cube):
40-
scl = cube.band("SCL")
41-
# TODO what are the meanings of these values?
39+
def extract_cloud_mask(cube: openeo.DataCube):
40+
scl = cube.filter_bands(["SCL"])
41+
# 0: No data
42+
# 3: Cloud shadow
43+
# 8-10: Clouds
44+
# 11: Snow or ice
45+
4246
mask = (scl == 0) | (scl == 3) | (scl > 7)
4347
return mask
4448

4549

50+
def calculate_large_grid_cloud_mask(cube: openeo.DataCube, tolerance_percentage: float = 0.05, grid_side_length: int=300):
51+
cloud_mask = extract_cloud_mask(cube)
52+
# FIXME check also if there is negative or zero data, otherwise results will differ
53+
54+
# TODO this could better be resample_cube_spatial, because we are matching to a sentinel-3 cube
55+
cloud_mask = cloud_mask * 1.0 # convert to float
56+
cloud_mask_resampled = cloud_mask.resample_spatial(grid_side_length, method="average") # resample to sentinel-3 size
57+
58+
# TODO extract UDF to file
59+
# UDF to apply an element-wise less than operation. Normal "<" does not properly work on openEO datacubes
60+
udf = openeo.UDF(f"""
61+
import numpy as np
62+
import xarray as xr
63+
def apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:
64+
array = cube.get_array()
65+
array = array < {tolerance_percentage}
66+
#return XarrayDataCube(xr.DataArray(array, dims=["t", "x", "y", "bands"]))
67+
return XarrayDataCube(xr.DataArray(array, dims=["bands", "x", "y"]))
68+
""")
69+
return cloud_mask_resampled.apply(process=udf)
70+
71+
4672
def distance_to_clouds(
4773
cube: openeo.DataCube, tolerance_percentage=0.05, ratio=30, max_distance=255
4874
):

tests/test_preprocessing_openeo.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import openeo
1313
from openeo.udf import execute_local_udf
1414

15-
from efast import efast_openeo, s2_processing
15+
from efast import s2_processing_openeo, s2_processing
1616

1717
TEST_DATA_ROOT = Path(__file__).parent.parent / "test_data"
1818
TEST_DATA_S2 = TEST_DATA_ROOT / "S2"
@@ -68,45 +68,28 @@ def test_distance_to_cloud():
6868

6969
# openeo
7070

71-
conn = efast_openeo.connect()
71+
conn = s2_processing_openeo.connect()
7272
conn.authenticate_oidc()
7373

74-
test_area = efast_openeo.TestArea(bbox=bounds, s2_bands=["SCL"], temporal_extent=(TEST_DATE_DASH, TEST_DATE_DASH))
74+
test_area = s2_processing_openeo.TestArea(bbox=bounds, s2_bands=["SCL"], temporal_extent=(TEST_DATE_DASH, TEST_DATE_DASH))
7575
cube = test_area.get_s2_cube(conn)
7676

77-
scl = cube.filter_bands(["SCL"])
78-
cloud_mask = (scl == 0) | (scl == 3) | (scl > 7)
79-
# FIXME check also if there is negative or zero data, otherwise results will differ
80-
81-
# TODO this could better be resample_cube_spatial, because we are matching to a sentinel-3 cube
82-
cloud_mask = cloud_mask * 1.0 # convert to float
83-
cloud_mask_resampled = cloud_mask.resample_spatial(300, method="average") # resample to sentinel-3 size
84-
85-
# UDF to apply an element-wise less than operation. Normal "<" does not properly work on openEO datacubes
86-
udf = openeo.UDF("""
87-
import numpy as np
88-
import xarray as xr
89-
def apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:
90-
array = cube.get_array()
91-
array = array < 0.05
92-
#return XarrayDataCube(xr.DataArray(array, dims=["t", "x", "y", "bands"]))
93-
return XarrayDataCube(xr.DataArray(array, dims=["bands", "x", "y"]))
94-
""")
95-
dtc_input = cloud_mask_resampled.apply(process=udf)
96-
dtc = efast_openeo.distance_to_clouds(dtc_input, tolerance_percentage=0.05, ratio=30)
97-
download_path = tmp_path / "test_distance_to_cloud.nc"
77+
dtc_input = s2_processing_openeo.calculate_large_grid_cloud_mask(cube, tolerance_percentage=0.05, grid_side_length=300)
78+
dtc = s2_processing_openeo.distance_to_clouds(dtc_input, tolerance_percentage=0.05, ratio=30)
79+
download_path = tmp_path / "test_distance_to_cloud.tif"
9880

9981
print("openEO execution")
10082

10183
# intermediate results for debugging
102-
#BASE_DIR = Path("openeo_results")
84+
BASE_DIR = Path("openeo_results")
10385
#BASE_DIR.mkdir(exist_ok=True)
10486
#cloud_mask.download(BASE_DIR / "cloud_mask.tif")
10587
#cloud_mask_resampled.download(BASE_DIR / "cloud_mask_resampled.tif")
10688
#dtc_input.download(BASE_DIR / "dtc_input.tif")
10789

10890
before = time.perf_counter()
10991
dtc.download(download_path)
92+
shutil.copy(download_path, BASE_DIR)
11093
elapsed = time.perf_counter() - before
11194
print(f"executed and downloaded in {elapsed:.2f}s")
11295

0 commit comments

Comments
 (0)