forked from Open-EO/openeo-processes-dask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockdata.py
64 lines (53 loc) · 1.94 KB
/
mockdata.py
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
import logging
import warnings
import numpy as np
import pandas as pd
import xarray as xr
from openeo_pg_parser_networkx.pg_schema import BoundingBox, TemporalInterval
logger = logging.getLogger(__name__)
def create_fake_rastercube(
data,
spatial_extent: BoundingBox,
temporal_extent: TemporalInterval,
bands: list,
backend="numpy",
chunks=("auto", "auto", "auto", -1),
):
# Calculate the desired resolution based on how many samples we desire on the longest axis.
len_x = max(spatial_extent.west, spatial_extent.east) - min(
spatial_extent.west, spatial_extent.east
)
len_y = max(spatial_extent.south, spatial_extent.north) - min(
spatial_extent.south, spatial_extent.north
)
x_coords = np.arange(
min(spatial_extent.west, spatial_extent.east),
max(spatial_extent.west, spatial_extent.east),
step=len_x / data.shape[0],
)
y_coords = np.arange(
min(spatial_extent.south, spatial_extent.north),
max(spatial_extent.south, spatial_extent.north),
step=len_y / data.shape[1],
)
# This line raises a deprecation warning, which according to this thread
# will never actually be deprecated:
# https://github.com/numpy/numpy/issues/23904
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
t_coords = pd.date_range(
start=temporal_extent.start.to_numpy(),
end=temporal_extent.end.to_numpy(),
periods=data.shape[2],
).values
coords = {"x": x_coords, "y": y_coords, "t": t_coords, "bands": bands}
raster_cube = xr.DataArray(
data=data,
coords=coords,
attrs={"crs": spatial_extent.crs},
)
raster_cube.rio.write_crs(spatial_extent.crs, inplace=True)
if "dask" in backend:
import dask.array as da
raster_cube.data = da.from_array(raster_cube.data, chunks=chunks)
return raster_cube