-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-generation.py
More file actions
299 lines (263 loc) · 8.98 KB
/
data-generation.py
File metadata and controls
299 lines (263 loc) · 8.98 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import numpy as np
import xarray as xr
import dask.array as da
from functools import reduce
import operator
from pathlib import Path
from zarr.storage import LocalStore
X = 1000
Y = 1000
Z = 50
T = 30
X_SMALL = 200
Y_SMALL = 200
TIME = xr.date_range("2000", "2001", T)
def prod(seq):
return reduce(operator.mul, seq, 1)
def _rotated_curvilinear_grid():
XG = np.arange(X)
YG = np.arange(Y)
LON, LAT = np.meshgrid(XG, YG)
angle = -np.pi / 24
rotation = np.array(
[[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]
)
# rotate the LON and LAT grids
LON, LAT = np.einsum("ji, mni -> jmn", rotation, np.dstack([LON, LAT]))
return xr.Dataset(
{
"data_g": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"data_c": (
["time", "ZC", "YC", "XC"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"U_A_grid": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"V_A_grid": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"U_C_grid": (
["time", "ZG", "YC", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"V_C_grid": (
["time", "ZG", "YG", "XC"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
},
coords={
"XG": (["XG"], XG, {"axis": "X", "c_grid_axis_shift": -0.5}),
"YG": (["YG"], YG, {"axis": "Y", "c_grid_axis_shift": -0.5}),
"XC": (["XC"], XG + 0.5, {"axis": "X"}),
"YC": (["YC"], YG + 0.5, {"axis": "Y"}),
"ZG": (
["ZG"],
np.arange(Z),
{"axis": "Z", "c_grid_axis_shift": -0.5},
),
"ZC": (
["ZC"],
np.arange(Z) + 0.5,
{"axis": "Z"},
),
"depth": (["ZG"], np.arange(Z), {"axis": "Z"}),
"time": (["time"], TIME, {"axis": "T"}),
"lon": (
["YG", "XG"],
LON,
{"axis": "X", "c_grid_axis_shift": -0.5}, # ? Needed?
),
"lat": (
["YG", "XG"],
LAT,
{"axis": "Y", "c_grid_axis_shift": -0.5}, # ? Needed?
),
},
)
def random_dask_array(shape, scaling=1):
return da.random.uniform(scaling, size=prod(shape)).reshape(shape)
def _cartesion_to_polar(x, y):
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y, x)
return r, theta
def _polar_to_cartesian(r, theta):
x = r * np.cos(theta)
y = r * np.sin(theta)
return x, y
def _unrolled_cone_curvilinear_grid():
# Not a great unrolled cone, but this is good enough for testing
# you can use matplotlib pcolormesh to plot
XG = np.arange(X)
YG = np.arange(Y) * 0.25
pivot = -10, 0
LON, LAT = np.meshgrid(XG, YG)
new_lon_lat = []
min_lon = np.min(XG)
for lon, lat in zip(LON.flatten(), LAT.flatten(), strict=True):
r, _ = _cartesion_to_polar(lon - pivot[0], lat - pivot[1])
_, theta = _cartesion_to_polar(min_lon - pivot[0], lat - pivot[1])
theta *= 1.2
r *= 1.2
lon, lat = _polar_to_cartesian(r, theta)
new_lon_lat.append((lon + pivot[0], lat + pivot[1]))
new_lon, new_lat = zip(*new_lon_lat, strict=True)
LON, LAT = (
np.array(new_lon).reshape(LON.shape),
np.array(new_lat).reshape(LAT.shape),
)
return xr.Dataset(
{
"data_g": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"data_c": (
["time", "ZC", "YC", "XC"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"U_A_grid": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"V_A_grid": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"U_C_grid": (
["time", "ZG", "YC", "XG"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
"V_C_grid": (
["time", "ZG", "YG", "XC"],
random_dask_array(scaling=5, shape=(T, Z, Y, X)),
),
},
coords={
"XG": (["XG"], XG, {"axis": "X", "c_grid_axis_shift": -0.5}),
"YG": (["YG"], YG, {"axis": "Y", "c_grid_axis_shift": -0.5}),
"XC": (["XC"], XG + 0.5, {"axis": "X"}),
"YC": (["YC"], YG + 0.5, {"axis": "Y"}),
"ZG": (
["ZG"],
np.arange(Z),
{"axis": "Z", "c_grid_axis_shift": -0.5},
),
"ZC": (
["ZC"],
np.arange(Z) + 0.5,
{"axis": "Z"},
),
"depth": (["ZG"], np.arange(Z), {"axis": "Z"}),
"time": (["time"], TIME, {"axis": "T"}),
"lon": (
["YG", "XG"],
LON,
{"axis": "X", "c_grid_axis_shift": -0.5}, # ? Needed?
),
"lat": (
["YG", "XG"],
LAT,
{"axis": "Y", "c_grid_axis_shift": -0.5}, # ? Needed?
),
},
)
def _ds_2d_left(x, y, z, t, time):
"""MITgcm indexing style dataset."""
return xr.Dataset(
{
"data_g": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(t, z, y, x)),
),
"data_c": (
["time", "ZC", "YC", "XC"],
random_dask_array(scaling=5, shape=(t, z, y, x)),
),
"U_A_grid": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(t, z, y, x)),
),
"V_A_grid": (
["time", "ZG", "YG", "XG"],
random_dask_array(scaling=5, shape=(t, z, y, x)),
),
"U_C_grid": (
["time", "ZG", "YC", "XG"],
random_dask_array(scaling=5, shape=(t, z, y, x)),
),
"V_C_grid": (
["time", "ZG", "YG", "XC"],
random_dask_array(scaling=5, shape=(t, z, y, x)),
),
},
coords={
"XG": (
["XG"],
2 * np.pi / x * np.arange(0, x),
{"axis": "X", "c_grid_axis_shift": -0.5},
),
"XC": (["XC"], 2 * np.pi / x * (np.arange(0, x) + 0.5), {"axis": "X"}),
"YG": (
["YG"],
2 * np.pi / y * np.arange(0, y),
{"axis": "Y", "c_grid_axis_shift": -0.5},
),
"YC": (
["YC"],
2 * np.pi / y * (np.arange(0, y) + 0.5),
{"axis": "Y"},
),
"ZG": (
["ZG"],
np.arange(z),
{"axis": "Z", "c_grid_axis_shift": -0.5},
),
"ZC": (
["ZC"],
np.arange(z) + 0.5,
{"axis": "Z"},
),
"lon": (["XG"], 2 * np.pi / x * np.arange(0, x)),
"lat": (["YG"], 2 * np.pi / y * np.arange(0, y)),
"depth": (["ZG"], np.arange(z)),
"time": (["time"], time, {"axis": "T"}),
},
)
datasets = {
"2d_left_rotated": _rotated_curvilinear_grid(),
"ds_2d_left": _ds_2d_left(X, Y, Z, T, TIME),
"2d_left_unrolled_cone": _unrolled_cone_curvilinear_grid(),
}
def save(ds: xr.Dataset, path: str, chunks: dict) -> None:
"""Save dataset to zarr with specified chunking."""
store = LocalStore(path)
ds.chunk(chunks).to_zarr(store, mode="w", encoding=None, consolidated=False)
size_mb = sum(ds[v].nbytes for v in ds.data_vars) / 1e6
print(f" {path} dims={dict(ds.sizes)} ~{size_mb:.0f} MB uncompressed")
if __name__ == "__main__":
dataset_path = "datasets/ds_2d_left_agrid.zarr"
print("Generating ds_2d_left...")
if Path(dataset_path).exists():
print(f"Dataset {dataset_path} already exists")
else:
save(
datasets["ds_2d_left"][["U_A_grid", "V_A_grid"]],
dataset_path,
{"time": 15, "XG": 40, "YG": 40, "ZG": 8},
)
dataset_path_small = "datasets/ds_2d_left_agrid_small.zarr"
print("Generating ds_2d_left_small...")
if Path(dataset_path_small).exists():
print(f"Dataset {dataset_path_small} already exists")
else:
save(
_ds_2d_left(X_SMALL, Y_SMALL, Z, T, TIME)[["U_A_grid", "V_A_grid"]],
dataset_path_small,
{"time": 15, "XG": 40, "YG": 40, "ZG": 8},
)