-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspatial_xarray.py
More file actions
78 lines (67 loc) · 2.31 KB
/
spatial_xarray.py
File metadata and controls
78 lines (67 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env -S uv run --script
#
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "omfiles[fsspec,grids,xarray]>=1.2.0", # x-release-please-version
# "matplotlib",
# "cartopy",
# ]
# ///
import datetime as dt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import fsspec
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
from omfiles.grids import OmGrid
MODEL_DOMAIN = "dwd_icon"
VARIABLE = "temperature_2m"
# Example: URI for a spatial data file in the `data_spatial` S3 bucket
# See data organization details: https://github.com/open-meteo/open-data?tab=readme-ov-file#data-organization
# Note: Spatial data is only retained for 7 days. The script uses one file within this period.
date_time = dt.datetime.now(dt.timezone.utc) - dt.timedelta(days=2)
S3_URI = (
f"s3://openmeteo/data_spatial/{MODEL_DOMAIN}/{date_time.year}/"
f"{date_time.month:02}/{date_time.day:02}/0000Z/"
f"{date_time.strftime('%Y-%m-%d')}T0000.om"
)
print(f"Using om file: {S3_URI}")
backend = fsspec.open(
f"blockcache::{S3_URI}",
mode="rb",
s3={"anon": True, "default_block_size": 65536},
blockcache={"cache_storage": "cache"},
)
ds = xr.open_dataset(backend, engine="om") # type: ignore
print(ds.attrs)
print(ds.variables.keys()) # any of these keys can be used for plotting
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE, linewidth=0.8)
ax.add_feature(cfeature.BORDERS, linewidth=0.5)
ax.add_feature(cfeature.OCEAN, alpha=0.3)
ax.add_feature(cfeature.LAND, alpha=0.3)
data = ds[VARIABLE] # shape: (lat, lon)
# Use OmGrid with the crs_wkt attribute to get the lat/lon grid
grid = OmGrid(ds.attrs["crs_wkt"], ds[VARIABLE].shape)
lon2d, lat2d = grid.get_meshgrid()
min = int(data.min().values)
max = int(data.max().values)
stepsize = int((max - min) / 30)
im = ax.contourf(
lon2d,
lat2d,
data,
levels=np.arange(min, max, stepsize),
cmap="Spectral_r",
vmin=min,
vmax=max,
extend="both",
)
ax.gridlines(draw_labels=True, alpha=0.3)
plt.colorbar(im, ax=ax, orientation="vertical", pad=0.05, aspect=40, shrink=0.55, label=VARIABLE)
plt.title(f"{MODEL_DOMAIN} {VARIABLE}", fontsize=12, fontweight="bold", pad=16)
plt.tight_layout()
plt.savefig("xarray_map.png", dpi=300, bbox_inches="tight")