-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreadme_example.py
More file actions
68 lines (59 loc) · 2.88 KB
/
Copy pathreadme_example.py
File metadata and controls
68 lines (59 loc) · 2.88 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
#!/usr/bin/env -S uv run --script
#
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "omfiles[fsspec]>=1.2.1", # x-release-please-version
# ]
# ///
import datetime as dt
import fsspec
import numpy as np
from omfiles import OmFileReader
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}")
# Create and open filesystem, wrapping it in a blockcache
backend = fsspec.open(
f"blockcache::{S3_URI}",
mode="rb",
s3={"anon": True, "default_block_size": 65536}, # s3 settings
blockcache={"cache_storage": "cache"}, # blockcache settings
)
# Create reader from the fsspec file object using a context manager.
# This will automatically close the file when the block is exited.
with OmFileReader(backend) as root:
# We are at the root of the data hierarchy!
# What type of node is this?
print(f"root.is_array: {root.is_array}") # False
print(f"root.is_scalar: {root.is_scalar}") # False
print(f"root.is_group: {root.is_group}") # True
temperature_reader = root.get_child_by_name(VARIABLE)
print(f"temperature_reader.is_array: {temperature_reader.is_array}") # True
print(f"temperature_reader.is_scalar: {temperature_reader.is_scalar}") # False
print(f"temperature_reader.is_group: {temperature_reader.is_group}") # False
# What shape does the stored array have?
print(f"temperature_reader.shape: {temperature_reader.shape}") # (1441, 2879)
# Read all data from the array
temperature_data = temperature_reader.read_array((...))
print(f"temperature_data.shape: {temperature_data.shape}") # (1441, 2879)
# It's also possible to read any subset of the array
temperature_data_subset1 = temperature_reader.read_array((slice(0, 10), slice(0, 10)))
print(temperature_data_subset1)
print(f"temperature_data_subset1.shape: {temperature_data_subset1.shape}") # (10, 10)
# Numpy basic indexing is supported for direct access if the reader is an array.
temperature_data_subset2 = temperature_reader[0:10, 0:10]
print(temperature_data_subset2)
print(f"temperature_data_subset2.shape: {temperature_data_subset2.shape}") # (10, 10)
# Compare the two temperature subsets and verify that they are the same
are_equal = np.array_equal(temperature_data_subset1, temperature_data_subset2, equal_nan=True)
print(f"Are the two temperature subsets equal? {are_equal}")