Skip to content

Commit

Permalink
Initial demo code for viewer extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
forman committed Nov 7, 2024
1 parent 5b7cd65 commit d5003d6
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
64 changes: 64 additions & 0 deletions examples/serve/demo3/compute_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import xarray as xr


def compute_indexes(dataset):
b04 = dataset.B04
b05 = dataset.B05
b06 = dataset.B06
b08 = dataset.B08
b11 = dataset.B11
scl = dataset.SCL

# See https://gisgeography.com/sentinel-2-bands-combinations/

# moisture_index
#
moisture_index = (b08 - b11) / (b08 + b11)
moisture_index.attrs.update(
color_value_min=0.0,
color_value_max=0.5,
color_bar_name="Blues",
units="-",
description="Simple moisture index: (B08-B11) / (B08+B11)",
)
# where valid and not cloud
moisture_index = moisture_index.where((scl >= 2) & (scl <= 7))

# vegetation_index
#
vegetation_index = (b08 - b04) / (b08 + b04)
vegetation_index.attrs.update(
color_value_min=0.0,
color_value_max=0.25,
color_bar_name="Greens",
units="-",
description="Simple vegetation index or NDVI: (B08-B04) / (B08+B04)",
)
# where water
vegetation_index = vegetation_index.where(scl == 6)

# chlorophyll_index
#
b_from, b_peek, b_to = b04, b05, b06
wlen_from = b04.attrs["wavelength"]
wlen_peek = b05.attrs["wavelength"]
wlen_to = b06.attrs["wavelength"]
f = (wlen_peek - wlen_from) / (wlen_to - wlen_from)
chlorophyll_index = (b_peek - b_from) - f * (b_to - b_from)
chlorophyll_index.attrs.update(
color_value_min=0.0,
color_value_max=0.025,
color_bar_name="viridis",
units="-",
description="Maximum chlorophyll index: (B05-B04) - f * (B06-B04)",
)
# where water
chlorophyll_index = chlorophyll_index.where(scl == 6)

return xr.Dataset(
dict(
moisture_index=moisture_index,
vegetation_index=vegetation_index,
chlorophyll_index=chlorophyll_index,
)
)
96 changes: 96 additions & 0 deletions examples/serve/demo3/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
DatasetAttribution:
- "© by Brockmann Consult GmbH 2024, contains modified Copernicus Data 2019, processed by ESA"

DatasetChunkCacheSize: 100M

DataStores:
- Identifier: opensr-test
StoreId: s3
StoreParams:
root: opensr-test
Datasets:
- Path: openSR_musselbeds_use_case_waddensea_S2L2A_2018-2022.levels
Identifier: waddensea
Title: Wadden Sea
Style: S2L2A
Augmentation:
Path: compute_indexes.py
Function: compute_indexes

Styles:
- Identifier: S2L2A
ColorMappings:
B01:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B02:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B03:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B04:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B05:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B06:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B07:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B08:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B8A:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B09:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B10:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B11:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
B12:
ColorBar: "Greys_r"
ValueRange: [0., 0.25]
SCL:
ColorBar: "Accent"
ValueRange: [ 0, 255 ]
rgb:
Red:
Variable: B04
ValueRange: [0., 0.25]
Green:
Variable: B03
ValueRange: [0., 0.25]
Blue:
Variable: B02
ValueRange: [0., 0.25]

Viewer:
Extensions:
- Path: my_viewer_ext.ext

ServiceProvider:
ProviderName: "Brockmann Consult GmbH"
ProviderSite: "https://www.brockmann-consult.de"
ServiceContact:
IndividualName: "Norman Fomferra"
PositionName: "Senior Software Engineer"
ContactInfo:
Phone:
Voice: "+49 4152 889 303"
Facsimile: "+49 4152 889 330"
Address:
DeliveryPoint: "HZG / GITZ"
City: "Geesthacht"
AdministrativeArea: "Herzogtum Lauenburg"
PostalCode: "21502"
Country: "Germany"
ElectronicMailAddress: "[email protected]"
5 changes: 5 additions & 0 deletions examples/serve/demo3/my_viewer_ext/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dashipy import Extension
from .my_panel_1 import panel as my_panel_1

ext = Extension(__name__)
ext.add(my_panel_1)
77 changes: 77 additions & 0 deletions examples/serve/demo3/my_viewer_ext/my_panel_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from dashipy import Component, Input, State, Output
from dashipy.components import Box, Dropdown, Checkbox, Typography
from dashipy.demo.contribs import Panel
from dashipy.demo.context import Context


panel = Panel(__name__, title="Panel B")


COLORS = [("red", 0), ("green", 1), ("blue", 2), ("yellow", 3)]


@panel.layout(
Input(source="app", property="control.selectedDatasetId"),
)
def render_panel(
ctx: Context,
dataset_id: str = "",
) -> Component:

opaque = False
color = 0

opaque_checkbox = Checkbox(
id="opaque",
value=opaque,
label="Opaque",
)

color_dropdown = Dropdown(
id="color",
value=color,
label="Color",
options=COLORS,
style={"flexGrow": 0, "minWidth": 80},
)

info_text = Typography(
id="info_text", text=update_info_text(ctx, dataset_id, opaque, color)
)

return Box(
style={
"display": "flex",
"flexDirection": "column",
"width": "100%",
"height": "100%",
"gap": "6px",
},
children=[opaque_checkbox, color_dropdown, info_text],
)


# noinspection PyUnusedLocal
@panel.callback(
Input(source="app", property="control.selectedDatasetId"),
Input("opaque"),
Input("color"),
State("info_text", "text"),
Output("info_text", "text"),
)
def update_info_text(
ctx: Context,
dataset_id: str = "",
opaque: bool = False,
color: int = 0,
info_text: str = "",
) -> str:
opaque = opaque or False
color = color if color is not None else 0
return (
f"The dataset is {dataset_id},"
f" the color is {COLORS[color][0]} and"
f" it {'is' if opaque else 'is not'} opaque."
f" The length of the last info text"
f" was {len(info_text or "")}."
)

0 comments on commit d5003d6

Please sign in to comment.