-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathhelpers.py
More file actions
514 lines (437 loc) · 17.5 KB
/
helpers.py
File metadata and controls
514 lines (437 loc) · 17.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from __future__ import annotations
import datetime
from collections.abc import Hashable, Sequence
import numpy as np
import xarray as xr
from xarray import DataArray
def _guess_bounds_1d(da, dim):
"""
Guess bounds values given a 1D coordinate variable.
Assumes equal spacing on either side of the coordinate label.
This is an approximation only.
Output has an added "bounds" dimension at the end.
"""
if dim not in da.dims:
(dim,) = da.cf.axes[dim]
bound_position = 0.5
diff = da.diff(dim).pad({dim: (1, 1)}, mode="edge")
lower = da.copy(
deep=False,
data=da.data - bound_position * diff.isel({dim: slice(0, -1)}).data,
)
upper = da.copy(
deep=False,
data=da.data + bound_position * diff.isel({dim: slice(1, None)}).data,
)
return (
xr.concat([lower, upper], dim="bounds")
.transpose(..., "bounds")
.drop_attrs(deep=False)
)
def _guess_bounds_2d(da, dims):
"""
Guess bounds values given a 2D coordinate variable.
Assumes equal spacing on either side of the coordinate label.
This is a coarse approximation, especially for curvilinear grids.
Output has an added "bounds" dimension at the end.
"""
daX = _guess_bounds_1d(da, dims[0]).rename(bounds="Xbnds")
daXY = _guess_bounds_1d(daX, dims[1]).rename(bounds="Ybnds")
# At this point, we might have different corners for adjacent cells, we average them together to have a nice grid
# To make this vectorized and keep the edges, we'll pad with NaNs and ignore them in the averages
daXYp = (
daXY.pad({d: (1, 1) for d in dims}, mode="constant", constant_values=np.nan)
.transpose(*dims, "Xbnds", "Ybnds")
.values
) # Tranpose for an easier notation
# Mean of the corners that should be the same point.
daXYm = np.stack(
(
# Lower left corner (mean of : upper right of the lower left cell, lower right of the upper left cell, and so on, ccw)
np.nanmean(
np.stack(
(
daXYp[:-2, :-2, 1, 1],
daXYp[:-2, 1:-1, 1, 0],
daXYp[1:-1, 1:-1, 0, 0],
daXYp[1:-1, :-2, 0, 1],
)
),
axis=0,
),
# Upper left corner
np.nanmean(
np.stack(
(
daXYp[:-2, 1:-1, 1, 1],
daXYp[:-2, 2:, 1, 0],
daXYp[1:-1, 2:, 0, 0],
daXYp[1:-1, 1:-1, 0, 1],
)
),
axis=0,
),
# Upper right
np.nanmean(
np.stack(
(
daXYp[1:-1, 1:-1, 1, 1],
daXYp[1:-1, 2:, 1, 0],
daXYp[2:, 2:, 0, 0],
daXYp[2:, 1:-1, 0, 1],
)
),
axis=0,
),
# Lower right
np.nanmean(
np.stack(
(
daXYp[1:-1, :-2, 1, 1],
daXYp[1:-1, 1:-1, 1, 0],
daXYp[2:, 1:-1, 0, 0],
daXYp[2:, :-2, 0, 1],
)
),
axis=0,
),
),
axis=-1,
)
return xr.DataArray(daXYm, dims=(*dims, "bounds"), coords=da.coords)
def bounds_to_vertices(
bounds: DataArray,
bounds_dim: Hashable,
core_dims=None,
order: str | None = "counterclockwise",
) -> DataArray:
"""
Convert bounds variable to vertices.
There are 2 covered cases:
- 1D coordinates, with bounds of shape (N, 2),
converted to vertices of shape (N+1,)
- 2D coordinates, with bounds of shape (N, M, 4).
converted to vertices of shape (N+1, M+1).
Parameters
----------
bounds : DataArray
The bounds to convert.
bounds_dim : str
The name of the bounds dimension of `bounds` (the one of length 2 or 4).
core_dims : list, optional
List of core dimensions for apply_ufunc. This must not include bounds_dims.
The shape of ``(*core_dims, bounds_dim)`` must be (N, 2) or (N, M, 4).
order : {'counterclockwise', 'clockwise', None}
Valid for 2D coordinates only (i.e. bounds of shape (..., N, M, 4), ignored otherwise.
Order the bounds are given in, assuming that ax0-ax1-upward is a right handed
coordinate system, where ax0 and ax1 are the two first dimensions of `bounds`.
If None, the counterclockwise version is computed and then verified. If the
check fails the clockwise version is returned. See Notes for more details.
Returns
-------
DataArray
Either of shape (N+1,) or (N+1, M+1). New vertex dimensions are named
from the initial dimension and suffix "_vertices".
Notes
-----
Getting the correct axes "order" is tricky. There are no real standards for
dimension names or even axes order, even though the CF conventions mentions the
ax0-ax1-upward (counterclockwise bounds) as being the default. Moreover, xarray can
tranpose data without raising any warning or error, which make attributes
unreliable.
References
----------
Please refer to the CF conventions document : http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#cell-boundaries.
"""
if core_dims is None:
core_dims = [dim for dim in bounds.dims if dim != bounds_dim]
output_sizes = {f"{dim}_vertices": bounds.sizes[dim] + 1 for dim in core_dims}
output_core_dims = list(output_sizes.keys())
n_core_dims = len(core_dims)
nbounds = bounds[bounds_dim].size
if not (n_core_dims == 2 and nbounds == 4) and not (
n_core_dims == 1 and nbounds == 2
):
raise ValueError(
f"Bounds format not understood. Got {bounds.dims} with shape {bounds.shape}."
)
core_dim_coords = {
dim: bounds.coords[dim].values for dim in core_dims if dim in bounds.coords
}
core_dim_orders = _get_core_dim_orders(core_dim_coords)
return xr.apply_ufunc(
_bounds_helper,
bounds,
input_core_dims=[core_dims + [bounds_dim]],
dask="parallelized",
kwargs={
"n_core_dims": n_core_dims,
"nbounds": nbounds,
"order": order,
"core_dim_orders": core_dim_orders,
},
output_core_dims=[output_core_dims],
dask_gufunc_kwargs=dict(output_sizes=output_sizes),
output_dtypes=[bounds.dtype],
)
def _get_core_dim_orders(core_dim_coords: dict[str, np.ndarray]) -> dict[str, str]:
"""
Determine the order (ascending, descending, or mixed) of each core dimension
based on its coordinates.
Repeated (equal) coordinates are ignored when determining the order. If all
coordinates are equal, the order is treated as "ascending".
Parameters
----------
core_dim_coords : dict of str to np.ndarray
A dictionary mapping dimension names to their coordinate arrays.
Returns
-------
core_dim_orders : dict of str to str
A dictionary mapping each dimension name to a string indicating the order:
- "ascending": strictly increasing (ignoring repeated values)
- "descending": strictly decreasing (ignoring repeated values)
- "mixed": neither strictly increasing nor decreasing (ignoring repeated values)
"""
core_dim_orders = {}
for dim, coords in core_dim_coords.items():
if coords.size <= 1:
# A single value, same as all values are equal, treat as ascending
core_dim_orders[dim] = "ascending"
continue
diffs = np.diff(coords)
# Handle datetime64 and timedelta64 safely for both numpy 1.26.4 and numpy 2
if np.issubdtype(coords.dtype, np.datetime64) or np.issubdtype(
coords.dtype, np.timedelta64
):
# Cast to float64 for safe comparison
diffs_float = diffs.astype("float64")
nonzero_diffs = diffs_float[diffs_float != 0]
elif isinstance(diffs[0], datetime.timedelta):
# For datetime timedelta, we use the total_seconds method
diffs_float = np.array([x.total_seconds() for x in diffs])
nonzero_diffs = diffs_float[diffs_float != 0]
else:
zero = 0
nonzero_diffs = diffs[diffs != zero]
if nonzero_diffs.size == 0:
# All values are equal, treat as ascending
core_dim_orders[dim] = "ascending"
elif np.all(nonzero_diffs > 0):
core_dim_orders[dim] = "ascending"
elif np.all(nonzero_diffs < 0):
core_dim_orders[dim] = "descending"
else:
core_dim_orders[dim] = "mixed"
return core_dim_orders
def _bounds_helper(values, n_core_dims, nbounds, order, core_dim_orders):
if n_core_dims == 2 and nbounds == 4:
# Vertices case (2D lat/lon)
if order in ["counterclockwise", None]:
# Names assume we are drawing axis 1 upward et axis 2 rightward.
bot_left = values[..., :, :, 0]
bot_right = values[..., :, -1:, 1]
top_right = values[..., -1:, -1:, 2]
top_left = values[..., -1:, :, 3]
vertex_vals = np.block([[bot_left, bot_right], [top_left, top_right]])
if order is None: # We verify if the ccw version works.
calc_bnds = vertices_to_bounds(vertex_vals).values
order = (
"counterclockwise" if np.allclose(calc_bnds, values) else "clockwise"
)
if order == "clockwise":
bot_left = values[..., :, :, 0]
top_left = values[..., -1:, :, 1]
top_right = values[..., -1:, -1:, 2]
bot_right = values[..., :, -1:, 3]
# Our assumption was wrong, axis 1 is rightward and axis 2 is upward
vertex_vals = np.block([[bot_left, bot_right], [top_left, top_right]])
elif n_core_dims == 1 and nbounds == 2:
# Middle points case (1D lat/lon)
vertex_vals = _get_ordered_vertices(values, core_dim_orders)
return vertex_vals
def _get_ordered_vertices(
bounds: np.ndarray, core_dim_orders: dict[str, str]
) -> np.ndarray:
"""
Convert a bounds array of shape (..., N, 2) or (N, 2) into a 1D array of vertices.
This function reconstructs the vertices from a bounds array, handling both
monotonic and non-monotonic cases.
Monotonic bounds (all values strictly increase or decrease when flattened):
- Concatenate the left endpoints (bounds[..., :, 0]) with the last right
endpoint (bounds[..., -1, 1]) to form the vertices.
Non-monotonic bounds:
- Determine the order of the core dimension(s) ('ascending' or 'descending').
- For ascending order:
- Use the minimum of each interval as the vertex.
- Use the maximum of the last interval as the final vertex.
- For descending order:
- Use the maximum of each interval as the vertex.
- Use the minimum of the last interval as the final vertex.
- Vertices are then sorted to match the coordinate direction.
Features:
- Handles both ascending and descending bounds.
- Preserves repeated coordinates if present.
- Output shape is (..., N+1) or (N+1,).
Parameters
----------
bounds : np.ndarray
Array of bounds, typically with shape (N, 2) or (..., N, 2).
core_dim_orders : dict[str, str]
Dictionary mapping core dimension names to their order ('ascending' or
'descending'). Used for sorting the vertices.
Returns
-------
np.ndarray
Array of vertices with shape (..., N+1) or (N+1,).
"""
order = _get_order_of_core_dims(core_dim_orders)
if _is_bounds_monotonic(bounds):
vertices = np.concatenate((bounds[..., :, 0], bounds[..., -1:, 1]), axis=-1)
else:
if order == "ascending":
endpoints = np.minimum(bounds[..., :, 0], bounds[..., :, 1])
last_endpoint = np.maximum(bounds[..., -1, 0], bounds[..., -1, 1])
elif order == "descending":
endpoints = np.maximum(bounds[..., :, 0], bounds[..., :, 1])
last_endpoint = np.minimum(bounds[..., -1, 0], bounds[..., -1, 1])
else:
raise NotImplementedError(
f"Cannot determine vertices for non-monotonic bounds with {order} core "
"dimension orders. Try normalizing the coordinates to a monotonic "
"convention and try again."
)
vertices = np.concatenate(
[endpoints, np.expand_dims(last_endpoint, axis=-1)], axis=-1
)
vertices = _sort_vertices(vertices, order)
return vertices
def _is_bounds_monotonic(bounds: np.ndarray) -> bool:
"""Check if the bounds are monotonic.
Arrays are monotonic if all values are increasing or decreasing. This
functions ignores an intervals where consecutive values are equal, which
represent repeated coordinates.
Parameters
----------
arr : np.ndarray
Numpy array to check, typically with shape (..., N, 2).
Returns
-------
bool
True if the flattened array is increasing or decreasing, False otherwise.
"""
# NOTE: Python 3.10 uses numpy 1.26.4. If the input is a datetime64 array,
# numpy 1.26.4 may raise: numpy.core._exceptions._UFuncInputCastingError:
# Cannot cast ufunc 'greater' input 0 from dtype('<m8[ns]') to dtype('<m8')
# with casting rule 'same_kind' To avoid this, always cast to float64 before
# np.diff.
diffs = np.diff(bounds.flatten())
if isinstance(diffs[0], datetime.timedelta):
# For datetime timedelta, we use the total_seconds method
diffs_float = np.array([x.total_seconds() for x in diffs])
else:
diffs_float = diffs.astype("float64")
nonzero_diffs = diffs_float[diffs_float != 0]
# All values are equal, treat as monotonic
if nonzero_diffs.size == 0:
return True
return bool(np.all(nonzero_diffs > 0) or np.all(nonzero_diffs < 0))
def _get_order_of_core_dims(core_dim_orders: dict[str, str]) -> str:
"""
Determines the common order of core dimensions from a dictionary of
dimension orders.
Parameters
----------
core_dim_orders : dict of str
A dictionary mapping dimension names to their respective order strings.
Returns
-------
order : str
The common order string shared by all core dimensions.
Raises
------
ValueError
If the core dimension orders are not all aligned (i.e., not all values
are the same).
"""
orders = set(core_dim_orders.values())
if len(orders) != 1:
raise ValueError(
f"All core dimension orders must be aligned. Got orders: {core_dim_orders}"
)
order = next(iter(orders))
return order
def _sort_vertices(vertices: np.ndarray, order: str) -> np.ndarray:
"""
Sorts the vertices array along the last axis in ascending or descending order.
Parameters
----------
vertices : np.ndarray
An array of vertices to be sorted. Sorting is performed along the last
axis.
order : str
The order in which to sort the vertices. Must be either "ascending" or
any other value for descending order.
Returns
-------
np.ndarray
The sorted array of vertices, with the same shape as the input.
Examples
--------
>>> import numpy as np
>>> vertices = np.array([[3, 1, 2], [6, 5, 4]])
>>> _sort_vertices(vertices, "ascending")
array([[1, 2, 3],
[4, 5, 6]])
>>> _sort_vertices(vertices, "descending")
array([[3, 2, 1],
[6, 5, 4]])
"""
if order == "ascending":
new_vertices = np.sort(vertices, axis=-1)
else:
new_vertices = np.sort(vertices, axis=-1)[..., ::-1]
return new_vertices
def vertices_to_bounds(
vertices: DataArray, out_dims: Sequence[str] = ("bounds", "x", "y")
) -> DataArray:
"""
Convert vertices to CF-compliant bounds.
There are 2 covered cases:
- 1D coordinates, with vertices of shape (N+1,),
converted to bounds of shape (N, 2)
- 2D coordinates, with vertices of shape (N+1, M+1).
converted to bounds of shape (N, M, 4).
Parameters
----------
vertices : DataArray
The vertices to convert. Must be of shape (N + 1) or (N + 1, M + 1).
out_dims : Sequence[str],
The name of the dimension in the output. The first is the 'bounds'
dimension and the following are the coordinate dimensions.
Returns
-------
DataArray
Either of shape (2, N) or (4, N, M).
References
----------
Please refer to the CF conventions document : http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#cell-boundaries.
"""
if vertices.ndim == 1:
bnd_vals = np.stack((vertices[:-1], vertices[1:]), axis=0)
elif vertices.ndim == 2:
bnd_vals = np.stack(
(
vertices[:-1, :-1],
vertices[:-1, 1:],
vertices[1:, 1:],
vertices[1:, :-1],
),
axis=0,
)
else:
raise ValueError(
f"vertices format not understood. Got {vertices.dims} with shape {vertices.shape}."
)
return xr.DataArray(bnd_vals, dims=out_dims[: vertices.ndim + 1]).transpose(
..., out_dims[0]
)