Skip to content

Commit

Permalink
fix: trying to update the band names
Browse files Browse the repository at this point in the history
JanssenBrm committed Feb 19, 2024
1 parent 0547c6e commit 0fff8f0
Showing 2 changed files with 37 additions and 51 deletions.
22 changes: 12 additions & 10 deletions src/fusets/openeo/mogpr_udf.py
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
from pathlib import Path
from typing import Dict

from openeo.metadata import CollectionMetadata, Band
from openeo.udf import XarrayDataCube
from openeo.metadata import Band, CollectionMetadata
from openeo.udf import XarrayDataCube, inspect


def load_venv():
@@ -41,6 +41,14 @@ def write_gpy_cfg():
return home


def apply_metadata(metadata: CollectionMetadata, context: dict) -> CollectionMetadata:
extra_bands = [Band(f"{x}_STD", None, None) for x in metadata.bands]
inspect(data=metadata, message="MOGPR metadata")
for band in extra_bands:
metadata = metadata.append_band(band)
return metadata


def apply_datacube(cube: XarrayDataCube, context: Dict) -> XarrayDataCube:
"""
Apply mogpr integration to a datacube.
@@ -65,20 +73,14 @@ def apply_datacube(cube: XarrayDataCube, context: Dict) -> XarrayDataCube:
variables=variables,
time_dimension=time_dimension,
prediction_period=prediction_period,
include_uncertainties=include_uncertainties
include_uncertainties=include_uncertainties,
)
result_dc = XarrayDataCube(result.to_array(dim="bands").transpose(*dims))
inspect(data=result_dc, message="MOGPR result")
set_home(home)
return result_dc


def apply_metadata(metadata: CollectionMetadata, context: dict) -> CollectionMetadata:
extra_bands = [Band(f"{x}_STD", None, None) for x in metadata.bands]
for band in extra_bands:
metadata = metadata.append_band(band)
return metadata


def load_mogpr_udf() -> str:
"""
Loads an openEO udf that applies mogpr.
66 changes: 25 additions & 41 deletions src/fusets/openeo/services/publish_mogpr.py
Original file line number Diff line number Diff line change
@@ -4,10 +4,10 @@
import openeo
from openeo import DataCube
from openeo.api.process import Parameter
from openeo.processes import apply_neighborhood, ProcessBuilder
from openeo.processes import ProcessBuilder, apply_neighborhood

from fusets.openeo import load_mogpr_udf
from fusets.openeo.services.helpers import publish_service, read_description, get_context_value
from fusets.openeo.services.helpers import get_context_value, publish_service, read_description

NEIGHBORHOOD_SIZE = 32

@@ -18,46 +18,29 @@ def execute_udf():
"type": "Polygon",
"coordinates": [
[
[
5.170012098271149,
51.25062964728295
],
[
5.17085904378298,
51.24882567194015
],
[
5.17857421368097,
51.2468515482926
],
[
5.178972704726344,
51.24982704376254
],
[
5.170012098271149,
51.25062964728295
]
[5.170012098271149, 51.25062964728295],
[5.17085904378298, 51.24882567194015],
[5.17857421368097, 51.2468515482926],
[5.178972704726344, 51.24982704376254],
[5.170012098271149, 51.25062964728295],
]
]
],
}
temp_ext = ["2023-01-01", "2023-03-31"]

# Setup NDVI cube
base_s2 = connection.load_collection('SENTINEL2_L2A',
spatial_extent=spat_ext,
temporal_extent=temp_ext,
bands=["B04", "B08", "SCL"])
base_s2 = connection.load_collection(
"SENTINEL2_L2A", spatial_extent=spat_ext, temporal_extent=temp_ext, bands=["B04", "B08", "SCL"]
)
base_s2 = base_s2.process("mask_scl_dilation", data=base_s2, scl_band_name="SCL")
base_s2 = base_s2.ndvi(red="B04", nir="B08", target_band='NDVI')
base_s2 = base_s2.filter_bands(bands=['NDVI'])
base_s2 = base_s2.ndvi(red="B04", nir="B08", target_band="NDVI")
base_s2 = base_s2.filter_bands(bands=["NDVI"])
base_s2 = base_s2.mask_polygon(spat_ext)

# Setup RVI cube
base_s1 = connection.load_collection('SENTINEL1_GRD',
spatial_extent=spat_ext,
temporal_extent=temp_ext,
bands=["VH", "VV"])
base_s1 = connection.load_collection(
"SENTINEL1_GRD", spatial_extent=spat_ext, temporal_extent=temp_ext, bands=["VH", "VV"]
)

VH = base_s1.band("VH")
VV = base_s1.band("VV")
@@ -68,8 +51,7 @@ def execute_udf():
merged_datacube = base_s2.merge(base_s1)

# Execute MOGPR
mogpr = connection.datacube_from_flat_graph(
generate_mogpr_cube(merged_datacube, True).flat_graph())
mogpr = connection.datacube_from_flat_graph(generate_mogpr_cube(merged_datacube, True).flat_graph())
mogpr.execute_batch(
"./result_mogpr.nc",
title=f"FuseTS - MOGPR - Local",
@@ -84,14 +66,15 @@ def execute_udf():


def generate_mogpr_cube(
input_cube: Union[DataCube, ProcessBuilder, Parameter],
include_uncertainties: Union[bool, Parameter]
input_cube: Union[DataCube, ProcessBuilder, Parameter], include_uncertainties: Union[bool, Parameter]
):
return apply_neighborhood(
input_cube,
lambda data: data.run_udf(udf=load_mogpr_udf(), runtime="Python-Jep", context={
'include_uncertainties': get_context_value(include_uncertainties)
}),
lambda data: data.run_udf(
udf=load_mogpr_udf(),
runtime="Python-Jep",
context={"include_uncertainties": get_context_value(include_uncertainties)},
),
size=[
{"dimension": "x", "value": NEIGHBORHOOD_SIZE, "unit": "px"},
{"dimension": "y", "value": NEIGHBORHOOD_SIZE, "unit": "px"},
@@ -106,7 +89,8 @@ def generate_mogpr_udp():
input_cube = Parameter.raster_cube()

include_uncertainties = Parameter.boolean(
"include_uncertainties", "Flag to include the uncertainties in the output results", False)
"include_uncertainties", "Flag to include the uncertainties in the output results", False
)

mogpr = generate_mogpr_cube()

0 comments on commit 0fff8f0

Please sign in to comment.