-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
241 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
14 changes: 13 additions & 1 deletion
14
App/functions/report-python-cloud-run/report/datasets/base_dataset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
from abc import ABC | ||
from typing import Optional | ||
import xarray as xr | ||
|
||
from .datasetcontent import DatasetContent | ||
from .esl import get_esl_content | ||
|
||
|
||
def get_dataset_content(dataset_id: str, xarr: xr.Dataset) -> Optional[DatasetContent]: | ||
match dataset_id: | ||
case "esl_gwl": | ||
return get_esl_content(xarr) | ||
case _: | ||
return None |
11 changes: 11 additions & 0 deletions
11
App/functions/report-python-cloud-run/report/datasets/datasetcontent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
|
||
@dataclass | ||
class DatasetContent: | ||
dataset_id: str | ||
title: str | ||
text: str | ||
image_base64: Optional[str] = None | ||
image_svg: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import base64 | ||
from io import BytesIO, StringIO | ||
import matplotlib | ||
|
||
matplotlib.use("Agg") | ||
from matplotlib import colors | ||
from matplotlib import pyplot as plt | ||
import numpy as np | ||
import xarray as xr | ||
|
||
import geopandas as gpd | ||
|
||
from .datasetcontent import DatasetContent | ||
|
||
|
||
def get_esl_content(xarr: xr.Dataset) -> DatasetContent: | ||
"""Get content for ESL dataset""" | ||
dataset_id = "esl" | ||
title = "Extreme Sea Level" | ||
text = "Here we generate some content based on the ESL dataset" | ||
|
||
image_base64 = create_esl_plot(xarr) | ||
return DatasetContent( | ||
dataset_id=dataset_id, | ||
title=title, | ||
text=text, | ||
image_base64=image_base64, | ||
) | ||
|
||
|
||
def create_esl_plot(xarr): | ||
GWL = 0 # look at ds.gwl.values for options | ||
GWLs = "present-day" | ||
# ens = 50 # look at ds.ensemble.values for options | ||
rp = 50.0 # look at ds.rp.values for options | ||
world = gpd.read_file( | ||
"""https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/world-administrative-boundaries/exports/shp?lang=en&timezone=Europe%2FBerlin""" | ||
) | ||
cmap = matplotlib.cm.RdYlGn_r | ||
norm = colors.BoundaryNorm(np.arange(0, 7.5, 0.5), cmap.N) | ||
ds_fil = xarr.sel(gwl=GWL, rp=rp) # filter the other params | ||
lonmin = min(ds_fil.lon.values) | ||
lonmax = max(ds_fil.lon.values) | ||
latmin = min(ds_fil.lat.values) | ||
latmax = max(ds_fil.lat.values) | ||
fig, ax = plt.subplots() | ||
fig.set_size_inches(15, 8) # fig.set_size_inches(15, 20) | ||
base = world.boundary.plot( | ||
ax=ax, edgecolor="grey", facecolor="grey", alpha=0.1, zorder=0 | ||
) | ||
im1 = ax.scatter( | ||
ds_fil.lon.values, | ||
ds_fil.lat.values, | ||
10 * ds_fil.sel(ensemble=5).esl.values, | ||
ds_fil.sel(ensemble=5).esl.values, | ||
cmap=cmap, | ||
norm=norm, | ||
zorder=1, | ||
) | ||
# plt.set_clim(0,5) | ||
im2 = ax.scatter( | ||
ds_fil.lon.values, | ||
ds_fil.lat.values + 0.1, | ||
10 * ds_fil.sel(ensemble=50).esl.values, | ||
ds_fil.sel(ensemble=50).esl.values, | ||
cmap=cmap, | ||
norm=norm, | ||
zorder=1, | ||
) | ||
im3 = ax.scatter( | ||
ds_fil.lon.values, | ||
ds_fil.lat.values + 0.2, | ||
10 * ds_fil.sel(ensemble=95).esl.values, | ||
ds_fil.sel(ensemble=95).esl.values, | ||
cmap=cmap, | ||
norm=norm, | ||
zorder=1, | ||
) | ||
ax.set_title("%s-year extreme sea level for %s global warming level" % (rp, GWLs)) | ||
ax.axis("square") | ||
ax.set( | ||
xlabel="lon", | ||
ylabel="lat", | ||
xlim=[lonmin - 2, lonmax + 2], | ||
ylim=[latmin - 2, latmax + 2], | ||
) | ||
# fig.colorbar(im1, ax=ax) | ||
im1.set_clim(0, 7) | ||
|
||
cax = fig.add_axes( | ||
[ | ||
ax.get_position().x1 + 0.01, | ||
ax.get_position().y0, | ||
0.02, | ||
ax.get_position().height, | ||
] | ||
) # to give colorbar own axes | ||
plt.colorbar(im1, cax=cax) # Similar to fig.colorbar(im, cax = cax) | ||
cax.set_title("ESL in meters") | ||
# | ||
imgdata = BytesIO() | ||
fig.savefig(imgdata, format="png") | ||
|
||
return base64.b64encode(imgdata.getbuffer()).decode("ascii") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
App/functions/report-python-cloud-run/report/template.html.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
<h1>Coastal report</h1> | ||
<p>In this report some coastal characteristics of the selected area are explained.</p> | ||
|
||
{% for dataset in datasets %} | ||
{% for dataset in data.datasets %} | ||
<h2>{{ dataset.title }}</h2> | ||
{% if dataset.image_svg %} | ||
<img src="data:image/svg+xml;base64,{{ dataset.image_svg }}" /> | ||
{% endif %} | ||
<p>{{ dataset.text }}</p> | ||
{% if dataset.image_base64 %} | ||
<img src="data:image/png;base64,{{ dataset.image_base64 }}" /> | ||
{% endif %} | ||
{% endfor %} |
Empty file.
20 changes: 15 additions & 5 deletions
20
App/functions/report-python-cloud-run/report/utils/stac.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
from collections import namedtuple | ||
from dataclasses import dataclass | ||
from pystac_client import Client | ||
|
||
Zarr_dataset = namedtuple("Zarr_dataset", ["dataset_id", "zarr_uri"]) | ||
|
||
@dataclass | ||
class ZarrDataset: | ||
dataset_id: str | ||
zarr_uri: str | ||
|
||
|
||
class STACClientGCA(Client): | ||
def get_all_zarr_uris(self) -> list[Zarr_dataset]: | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
def get_all_zarr_uris(self) -> list[ZarrDataset]: | ||
collections = self.get_collections() | ||
zarr_datasets = [] | ||
|
||
for collection in collections: | ||
# we only look at collections that have a child links | ||
if collection.get_child_links(): | ||
if collection.get_item_links(): | ||
zarr_datasets.append( | ||
Zarr_dataset(collection.id, collection.assets["data"].href) | ||
ZarrDataset( | ||
dataset_id=collection.id, | ||
zarr_uri=collection.assets["data"].href, | ||
) | ||
) | ||
return zarr_datasets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters