-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathutils.py
More file actions
1361 lines (1115 loc) · 40.1 KB
/
utils.py
File metadata and controls
1361 lines (1115 loc) · 40.1 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from math import ceil
import warnings
import numpy as np
import xarray as xr
from numba import cuda, jit
try:
import cupy
except ImportError:
cupy = None
try:
import dask.array as da
except ImportError:
da = None
try:
import dask.dataframe as dd
except ImportError:
dd = None
ngjit = jit(nopython=True, nogil=True)
# ---------- Boundary mode utilities ----------
VALID_BOUNDARY_MODES = ('nan', 'nearest', 'reflect', 'wrap')
def _validate_boundary(boundary):
"""Raise ValueError if *boundary* is not a recognised mode."""
if boundary not in VALID_BOUNDARY_MODES:
raise ValueError(
f"boundary must be one of {VALID_BOUNDARY_MODES}, "
f"got {boundary!r}"
)
def _validate_raster(
agg,
*,
func_name: str,
name: str = 'raster',
ndim: int | tuple[int, ...] | None = 2,
numeric: bool = True,
integer_only: bool = False,
):
"""Validate that *agg* is an xarray.DataArray with expected properties.
Parameters
----------
agg : object
Value to validate.
func_name : str
Name of the calling function (for error messages).
name : str
Parameter name (for error messages).
ndim : int, tuple of int, or None
Allowed number of dimensions. ``None`` skips the check.
numeric : bool
If True, require a numeric dtype (int or float).
integer_only : bool
If True, require an integer dtype specifically.
Raises
------
TypeError
If *agg* is not an ``xr.DataArray``.
ValueError
If the dimensionality or dtype is wrong.
"""
if not isinstance(agg, xr.DataArray):
raise TypeError(
f"{func_name}(): `{name}` must be an xarray.DataArray, "
f"got {type(agg).__module__}.{type(agg).__qualname__}"
)
if ndim is not None:
allowed = (ndim,) if isinstance(ndim, int) else tuple(ndim)
if agg.ndim not in allowed:
expected = 'or '.join(f'{d}D' for d in allowed)
raise ValueError(
f"{func_name}(): `{name}` must be {expected}, "
f"got {agg.ndim}D"
)
if numeric:
if integer_only:
if not np.issubdtype(agg.dtype, np.integer):
raise ValueError(
f"{func_name}(): `{name}` must have an integer dtype, "
f"got {agg.dtype}"
)
else:
if not np.issubdtype(agg.dtype, np.number):
raise ValueError(
f"{func_name}(): `{name}` must have a numeric dtype "
f"(integer or float), got {agg.dtype}"
)
def _validate_scalar(
value,
*,
func_name: str,
name: str,
dtype: type | tuple = (int, float),
min_val=None,
max_val=None,
min_exclusive: bool = False,
):
"""Validate that *value* is a scalar of the expected type and range.
Parameters
----------
value : object
Value to validate.
func_name : str
Name of the calling function (for error messages).
name : str
Parameter name (for error messages).
dtype : type or tuple of types
Allowed Python types (checked with ``isinstance``).
min_val, max_val : numeric or None
Inclusive bounds (or exclusive lower bound if *min_exclusive*).
min_exclusive : bool
If True, the lower bound is exclusive (``>`` instead of ``>=``).
Raises
------
TypeError
If *value* is not an instance of *dtype*.
ValueError
If *value* is outside the allowed range.
"""
# Expand dtype to also accept numpy scalar equivalents so that
# e.g. np.int64(5) passes a check for dtype=int.
_dtype = dtype if isinstance(dtype, tuple) else (dtype,)
_expanded = list(_dtype)
if int in _dtype:
_expanded.append(np.integer)
if float in _dtype:
_expanded.append(np.floating)
_expanded = tuple(_expanded)
if not isinstance(value, _expanded):
expected = dtype.__name__ if isinstance(dtype, type) else \
' or '.join(t.__name__ for t in _dtype)
raise TypeError(
f"{func_name}(): `{name}` must be {expected}, "
f"got {type(value).__name__}"
)
if min_val is not None:
if min_exclusive:
if value <= min_val:
raise ValueError(
f"{func_name}(): `{name}` must be > {min_val}, "
f"got {value}"
)
else:
if value < min_val:
raise ValueError(
f"{func_name}(): `{name}` must be >= {min_val}, "
f"got {value}"
)
if max_val is not None:
if value > max_val:
raise ValueError(
f"{func_name}(): `{name}` must be <= {max_val}, "
f"got {value}"
)
def _boundary_to_dask(boundary, is_cupy=False):
"""Convert a boundary mode string to the value expected by
``dask.array.map_overlap``'s *boundary* parameter."""
if boundary == 'nan':
if is_cupy:
import cupy as _cp
return _cp.nan
return np.nan
_mode_map = {
'nearest': 'nearest',
'reflect': 'reflect',
'wrap': 'periodic',
}
return _mode_map[boundary]
def _pad_array(data, depth, boundary):
"""Pad a 2-D numpy or cupy array according to *boundary* mode.
Parameters
----------
data : array-like
2-D array to pad.
depth : int or tuple of int
Number of cells to pad on each side. An int pads all axes
equally; a tuple ``(d0, d1)`` pads each axis independently.
boundary : str
One of ``'nearest'``, ``'reflect'``, ``'wrap'``.
``'nan'`` should be handled before calling this function.
"""
# numpy.pad 'symmetric' matches dask map_overlap 'reflect'
# (both include the edge element in the reflection)
_np_mode_map = {
'nearest': 'edge',
'reflect': 'symmetric',
'wrap': 'wrap',
}
mode = _np_mode_map[boundary]
if isinstance(depth, int):
pad_width = ((depth, depth), (depth, depth))
else:
pad_width = tuple((d, d) for d in depth)
if is_cupy_array(data):
return cupy.pad(data, pad_width, mode=mode)
return np.pad(data, pad_width, mode=mode)
def has_cuda_and_cupy():
return _has_cuda() and _has_cupy()
def _has_cupy():
return cupy is not None
def is_cupy_array(arr):
return _has_cupy() and isinstance(arr, cupy.ndarray)
def has_dask_array():
return da is not None
def has_dask_dataframe():
return dd is not None
def _has_cuda():
"""Check for supported CUDA device. If none found, return False"""
local_cuda = False
try:
cuda.cudadrv.devices.gpus.current
local_cuda = True
except cuda.cudadrv.error.CudaSupportError:
local_cuda = False
return local_cuda
def cuda_args(shape):
"""
Compute the blocks-per-grid and threads-per-block parameters for
use when invoking cuda kernels
Parameters
----------
shape: int or tuple of ints
The shape of the input array that the kernel will parallelize
over.
Returns
-------
bpg, tpb : tuple
Tuple of (blocks_per_grid, threads_per_block).
"""
if isinstance(shape, int):
shape = (shape,)
max_threads = cuda.get_current_device().MAX_THREADS_PER_BLOCK
# Note: We divide max_threads by 2.0 to leave room for the registers
threads_per_block = int(ceil(max_threads / 2.0) ** (1.0 / len(shape)))
tpb = (threads_per_block,) * len(shape)
bpg = tuple(int(ceil(d / threads_per_block)) for d in shape)
return bpg, tpb
def calc_cuda_dims(shape):
threadsperblock = (32, 32)
blockspergrid = (
(shape[0] + (threadsperblock[0] - 1)) // threadsperblock[0],
(shape[1] + (threadsperblock[1] - 1)) // threadsperblock[1]
)
return blockspergrid, threadsperblock
def is_cupy_backed(agg: xr.DataArray):
try:
return type(agg.data._meta).__module__.split(".")[0] == "cupy"
except AttributeError:
return False
def is_dask_cupy(agg: xr.DataArray):
return isinstance(agg.data, da.Array) and is_cupy_backed(agg)
def not_implemented_func(agg, *args, messages='Not yet implemented.'):
raise NotImplementedError(messages)
class ArrayTypeFunctionMapping(object):
def __init__(self, numpy_func, cupy_func, dask_func, dask_cupy_func):
self.numpy_func = numpy_func
self.cupy_func = cupy_func
self.dask_func = dask_func
self.dask_cupy_func = dask_cupy_func
def __call__(self, arr):
# numpy case
if isinstance(arr.data, np.ndarray):
return self.numpy_func
# cupy case
elif has_cuda_and_cupy() and is_cupy_array(arr.data):
return self.cupy_func
# dask + cupy case
elif has_cuda_and_cupy() and is_dask_cupy(arr):
return self.dask_cupy_func
# dask + numpy case
elif has_dask_array() and isinstance(arr.data, da.Array):
return self.dask_func
else:
raise TypeError("Unsupported Array Type: {}".format(type(arr)))
def validate_arrays(*arrays):
if len(arrays) < 2:
raise ValueError(
"validate_arrays() input must contain 2 or more arrays"
)
first_array = arrays[0]
for i in range(1, len(arrays)):
if not first_array.data.shape == arrays[i].data.shape:
raise ValueError("input arrays must have equal shapes")
if not isinstance(first_array.data, type(arrays[i].data)):
raise ValueError("input arrays must have same type")
# ensure dask chunksizes of all arrays are the same
if has_dask_array() and isinstance(first_array.data, da.Array):
for i in range(1, len(arrays)):
if first_array.chunks != arrays[i].chunks:
arrays[i].data = arrays[i].data.rechunk(first_array.chunks)
def get_xy_range(raster, xdim=None, ydim=None):
"""
Compute xrange and yrange for input `raster`
Parameters
----------
raster: xarray.DataArray
xdim: str, default = None
Name of the x coordinate dimension in input `raster`.
If not provided, assume xdim is `raster.dims[-1]`
ydim: str, default = None
Name of the y coordinate dimension in input `raster`
If not provided, assume ydim is `raturns
----------
xrange, yrange
Tuple of tuples: (x, y-range).
xrange: tuple of (xmin, xmax)
yrange: tuple of (ymin, ymax)
"""
if ydim is None:
ydim = raster.dims[-2]
if xdim is None:
xdim = raster.dims[-1]
xmin = raster[xdim].min().item()
xmax = raster[xdim].max().item()
ymin = raster[ydim].min().item()
ymax = raster[ydim].max().item()
xrange = (xmin, xmax)
yrange = (ymin, ymax)
return xrange, yrange
def calc_res(raster, xdim=None, ydim=None):
"""
Calculate the resolution of xarray.DataArray raster and return it
as thetwo-tuple (xres, yres).
Parameters
----------
raster: xr.DataArray
Input raster.
xdim: str, default = None
Name of the x coordinate dimension in input `raster`.
If not provided, assume xdim is `raster.dims[-1]`
ydim: str, default = None
Name of the y coordinate dimension in input `raster`
If not provided, assume ydim is `raster.dims[-2]`
Returns
-------
xres, yres: tuple
Tuple of (x-resolution, y-resolution).
"""
h, w = raster.shape[-2:]
xrange, yrange = get_xy_range(raster, xdim, ydim)
xres = (xrange[-1] - xrange[0]) / (w - 1)
yres = (yrange[-1] - yrange[0]) / (h - 1)
return xres, yres
def get_dataarray_resolution(
agg: xr.DataArray,
xdim: str = None,
ydim: str = None,
):
"""
Calculate resolution of xarray.DataArray.
Parameters
----------
agg: xarray.DataArray
Input raster.
xdim: str, default = None
Name of the x coordinate dimension in input `raster`.
If not provided, assume xdim is `raster.dims[-1]`
ydim: str, default = None
Name of the y coordinate dimension in input `raster`
If not provided, assume ydim is `raster.dims[-2]`
Returns
-------
cellsize_x, cellsize_y: tuple
Tuple of (x cell size, y cell size).
"""
# get cellsize out from 'res' attribute
try:
cellsize = agg.attrs.get("res")
if (
isinstance(cellsize, (tuple, np.ndarray, list))
and len(cellsize) == 2
and isinstance(cellsize[0], (int, float))
and isinstance(cellsize[1], (int, float))
):
cellsize_x, cellsize_y = cellsize
elif isinstance(cellsize, (int, float)):
cellsize_x = cellsize
cellsize_y = cellsize
else:
cellsize_x, cellsize_y = calc_res(agg, xdim, ydim)
except Exception:
cellsize_x, cellsize_y = calc_res(agg, xdim, ydim)
return cellsize_x, cellsize_y
def lnglat_to_meters(longitude, latitude):
"""
Projects the given (longitude, latitude) values into Web Mercator
coordinates (meters East of Greenwich and meters North of the
Equator).
Longitude and latitude can be provided as scalars, Pandas columns,
or Numpy arrays, and will be returned in the same form. Lists
or tuples will be converted to Numpy arrays.
Parameters
----------
latitude: float
Input latitude.
longitude: float
Input longitude.
Returns
-------
easting, northing : tuple
Tuple of (easting, northing).
Examples
--------
.. sourcecode:: python
>>> easting, northing = lnglat_to_meters(-40.71,74)
>>> easting, northing = lnglat_to_meters(np.array([-74]),
>>> np.array([40.71]))
>>> df = pandas.DataFrame(dict(longitude=np.array([-74]),
>>> latitude=np.array([40.71])))
>>> df.loc[:, 'longitude'], df.loc[:, 'latitude'] = lnglat_to_meters(
>>> df.longitude, df.latitude)
"""
if isinstance(longitude, (list, tuple)):
longitude = np.array(longitude)
if isinstance(latitude, (list, tuple)):
latitude = np.array(latitude)
origin_shift = np.pi * 6378137
easting = longitude * origin_shift / 180.0
northing = np.log(
np.tan((90 + latitude) * np.pi / 360.0)
) * origin_shift / np.pi
return (easting, northing)
def height_implied_by_aspect_ratio(W, X, Y):
"""
Utility function for calculating height (in pixels) which is implied
by a width, x-range, and y-range. Simple ratios are used to maintain
aspect ratio.
Parameters
----------
W: int
Width in pixel.
X: tuple
X-range in data units.
Y: tuple
X-range in data units.
Returns
-------
height : int
height in pixels
Examples
--------
.. sourcecode:: python
>>> plot_width = 1000
>>> x_range = (0,35
>>> y_range = (0, 70)
>>> plot_height = height_implied_by_aspect_ratio(
plot_width,
x_range,
y_range,
)
"""
return int((W * (Y[1] - Y[0])) / (X[1] - X[0]))
def bands_to_img(r, g, b, nodata=1):
from PIL import Image
h, w = r.shape
data = np.zeros((h, w, 4), dtype=np.uint8)
data[:, :, 0] = (r).astype(np.uint8)
data[:, :, 1] = (g).astype(np.uint8)
data[:, :, 2] = (b).astype(np.uint8)
a = np.where(np.logical_or(np.isnan(r), r <= nodata), 0, 255)
data[:, :, 3] = a.astype(np.uint8)
return Image.fromarray(data, "RGBA")
def canvas_like(
raster,
width=512,
height=None,
x_range=None,
y_range=None,
**kwargs
):
"""
Resample a xarray.DataArray by canvas width and bounds.
Height of the resampled raster is implied from the canvas width
using aspect ratio of original raster.
This function uses of datashader.Canvas.raster internally.
Most of the docstrings are copied from Datashader.
Handles 2D or 3D xarray.DataArray, assuming that the last two
array dimensions are the y-axis and x-axis that are to be
resampled. If a 3D array is supplied a layer may be specified
to resample to select the layer along the first dimension to
resample.
Parameters
----------
raster : xarray.DataArray
2D or 3D labeled data array.
layer : float, optional
For a 3D array, value along the z dimension.
width : int, default=512
Width of the output aggregate in pixels.
height : int, default=None
Height of the output aggregate in pixels.
If not provided, height will be implied from `width`
using aspect ratio of input raster.
x_range : tuple of int, optional
A tuple representing the bounds inclusive space ``[min, max]``
along the x-axis.
y_range : tuple of int, optional
A tuple representing the bounds inclusive space ``[min, max]``
along the y-axis.
References
----------
- https://datashader.org/_modules/datashader/core.html#Canvas
"""
# get ranges
if x_range is None:
x_range = (
raster.coords["x"].min().item(),
raster.coords["x"].max().item()
)
if y_range is None:
y_range = (
raster.coords["y"].min().item(),
raster.coords["y"].max().item()
)
if height is None:
# set width and height
height = height_implied_by_aspect_ratio(width, x_range, y_range)
try:
import datashader as ds
except ImportError:
raise ImportError(
"canvas_like requires datashader: pip install datashader"
)
cvs = ds.Canvas(
plot_width=width, plot_height=height, x_range=x_range, y_range=y_range
)
out = cvs.raster(raster, **kwargs)
return out
def _hex_to_rgb(c):
"""Convert a hex color string (e.g. '#ff0000' or 'ff0000') to (r, g, b)."""
c = c.lstrip('#')
return int(c[0:2], 16), int(c[2:4], 16), int(c[4:6], 16)
def color_values(agg, color_key, alpha=255):
from PIL import Image
def _convert_color(c):
r, g, b = _hex_to_rgb(c)
return np.array([r, g, b, alpha]).astype(np.uint8).view(np.uint32)[0]
_converted_colors = {k: _convert_color(v) for k, v in color_key.items()}
f = np.vectorize(lambda v: _converted_colors.get(v, 0))
return Image.fromarray(f(agg.data).astype(np.uint32).view(np.uint8).reshape(
agg.data.shape + (4,)), "RGBA")
def _infer_coord_unit_type(coord: xr.DataArray, cellsize: float) -> str:
"""
Heuristic to classify a spatial coordinate axis as:
- 'degrees'
- 'linear' (meters/feet/etc)
- 'unknown'
Parameters
----------
coord : xr.DataArray
1D coordinate variable (x or y).
cellsize : float
Mean spacing along this coordinate.
Returns
-------
str
"""
units = str(coord.attrs.get("units", "")).lower()
# 1) Explicit units, if present
if "degree" in units or units in ("deg", "degrees"):
return "degrees"
if units in ("m", "meter", "metre", "meters", "metres",
"km", "kilometer", "kilometre", "kilometers", "kilometres",
"ft", "foot", "feet"):
return "linear"
# 2) Numeric heuristics (very conservative)
vals = coord.values
if vals.size < 2 or not np.issubdtype(vals.dtype, np.number):
return "unknown"
vmin = float(np.nanmin(vals))
vmax = float(np.nanmax(vals))
span = abs(vmax - vmin)
dx = abs(float(cellsize))
# Typical global geographic axes: span <= 360, spacing ~1e-5–0.5 deg
if -360.0 <= vmin <= 360.0 and -360.0 <= vmax <= 360.0:
if 1e-5 <= dx <= 0.5:
return "degrees"
# Typical projected axes in meters: span >> 1, spacing > ~0.1
# (e.g. UTM / national grids)
if span > 1000.0 and dx >= 0.1:
return "linear"
return "unknown"
def _infer_vertical_unit_type(agg):
units = str(agg.attrs.get("units", "")).lower()
# Cheap / reliable first
if any(k in units for k in ("degree", "deg")) or "rad" in units:
return "angle"
if units in ("m", "meter", "metre", "meters", "metres",
"km", "kilometer", "kilometre", "kilometers", "kilometres",
"ft", "foot", "feet"):
return "elevation"
# Numeric fallback: sample only (never full compute)
data = agg.data
try:
vmin, vmax = _sample_windows_min_max(data, max_window_elems=65536, windows=5)
except Exception:
return "unknown"
if not np.isfinite(vmin) or not np.isfinite(vmax):
return "unknown"
span = vmax - vmin
# Elevation-ish heuristic
if 10.0 <= span <= 20000.0 and vmin > -500.0:
return "elevation"
# Angle-ish heuristic
if -360.0 <= vmin <= 360.0 and -360.0 <= vmax <= 360.0 and span <= 720.0:
return "angle"
return "unknown"
def warn_if_unit_mismatch(agg: xr.DataArray) -> None:
"""
Heuristic check for horizontal vs vertical unit mismatch.
Intended to catch the common case of:
- coordinates in degrees (lon/lat)
- elevation values in meters/feet
Emits a UserWarning if a likely mismatch is detected.
"""
try:
cellsize_x, cellsize_y = get_dataarray_resolution(agg)
except Exception:
# If we can't even get a resolution, we also can't say much
return
# pick "x" and "y" coords in a generic way:
# - typically dims are ('y', 'x') or ('lat', 'lon')
# - fall back to last two dims
if len(agg.dims) < 2:
return
dim_y, dim_x = agg.dims[-2], agg.dims[-1]
coord_x = agg.coords.get(dim_x, None)
coord_y = agg.coords.get(dim_y, None)
if coord_x is None or coord_y is None:
# Can't infer spatial types without coords
return
horiz_x = _infer_coord_unit_type(coord_x, cellsize_x)
horiz_y = _infer_coord_unit_type(coord_y, cellsize_y)
vert = _infer_vertical_unit_type(agg)
horiz_types = {horiz_x, horiz_y} - {"unknown"}
# Only act if we have some signal about horizontal AND vertical
if not horiz_types or vert == "unknown":
return
# If any axis looks like degrees and vertical looks like elevation,
# it's almost certainly "lat/lon degrees + meter elevations"
if "degrees" in horiz_types and vert == "elevation":
warnings.warn(
"xrspatial: input DataArray appears to have coordinates in degrees "
"but elevation values in a linear unit (e.g. meters/feet). "
"Slope/aspect operations expect horizontal distances in the same "
"units as vertical. Consider reprojecting to a projected CRS with "
"meter-based coordinates before calling `slope`.",
UserWarning,
)
# ---------- Z-unit conversion for geodesic methods ----------
Z_UNITS = {
'meter': 1.0, 'meters': 1.0, 'm': 1.0,
'foot': 0.3048, 'feet': 0.3048, 'ft': 0.3048,
'kilometer': 1000.0, 'kilometers': 1000.0, 'km': 1000.0,
'mile': 1609.344, 'miles': 1609.344, 'mi': 1609.344,
}
# ---------- Lat/lon coordinate extraction for geodesic methods ----------
# Known dimension / coordinate names (lower-cased for matching)
_LAT_NAMES = {'lat', 'latitude', 'y'}
_LON_NAMES = {'lon', 'longitude', 'x'}
def _extract_latlon_coords(agg: xr.DataArray):
"""
Extract 2-D latitude and longitude arrays from a DataArray.
Supports:
- 1-D coordinates on the last two dims (regular geographic grid).
- 2-D coordinates that vary per cell (curvilinear grid).
Returns
-------
lat_2d, lon_2d : numpy.ndarray
Always 2-D float64 numpy arrays of shape ``(H, W)``.
Raises
------
ValueError
If coordinates are missing, non-numeric, or outside geographic
ranges (lat not in [-90, 90], lon not in [-180, 360]).
"""
if agg.ndim < 2:
raise ValueError(
"geodesic method requires a 2-D DataArray, "
f"got {agg.ndim}-D"
)
dim_y, dim_x = agg.dims[-2], agg.dims[-1]
# --- locate lat coordinate ---
lat_coord = _find_coord(agg, dim_y, _LAT_NAMES, 'latitude')
# --- locate lon coordinate ---
lon_coord = _find_coord(agg, dim_x, _LON_NAMES, 'longitude')
lat_vals = np.asarray(lat_coord.values, dtype=np.float64)
lon_vals = np.asarray(lon_coord.values, dtype=np.float64)
# Build 2-D arrays
if lat_vals.ndim == 1 and lon_vals.ndim == 1:
# Regular grid: broadcast to 2-D
lat_2d = np.broadcast_to(lat_vals[:, np.newaxis],
(agg.sizes[dim_y], agg.sizes[dim_x]))
lon_2d = np.broadcast_to(lon_vals[np.newaxis, :],
(agg.sizes[dim_y], agg.sizes[dim_x]))
elif lat_vals.ndim == 2 and lon_vals.ndim == 2:
lat_2d = lat_vals
lon_2d = lon_vals
else:
raise ValueError(
f"lat/lon coordinates must be both 1-D or both 2-D, "
f"got lat={lat_vals.ndim}-D and lon={lon_vals.ndim}-D"
)
# --- validate ranges ---
_validate_geographic_range(lat_2d, lon_2d)
return lat_2d, lon_2d
def _find_coord(agg, dim_name, known_names, label):
"""Find a coordinate matching *dim_name* or one of *known_names*."""
# 1) Try the dimension name directly
if dim_name in agg.coords:
coord = agg.coords[dim_name]
if np.issubdtype(coord.dtype, np.number):
return coord
# 2) Scan all coords for a known name
for name in agg.coords:
if str(name).lower() in known_names:
coord = agg.coords[name]
if np.issubdtype(coord.dtype, np.number):
return coord
raise ValueError(
f"geodesic method requires {label} coordinates on the DataArray. "
f"No numeric coordinate found for dim '{dim_name}' or any of "
f"{sorted(known_names)}."
)
def _validate_geographic_range(lat_2d, lon_2d):
"""Raise ValueError if lat/lon values look non-geographic."""
lat_min = np.nanmin(lat_2d)
lat_max = np.nanmax(lat_2d)
lon_min = np.nanmin(lon_2d)
lon_max = np.nanmax(lon_2d)
if lat_min < -90 or lat_max > 90:
raise ValueError(
f"Latitude values must be in [-90, 90], "
f"got [{lat_min}, {lat_max}]. "
f"Are your coordinates in a projected CRS?"
)
if lon_min < -180 or lon_max > 360:
raise ValueError(
f"Longitude values must be in [-180, 360], "
f"got [{lon_min}, {lon_max}]. "
f"Are your coordinates in a projected CRS?"
)
lat_span = lat_max - lat_min
lon_span = lon_max - lon_min
if lat_span > 180 or lon_span > 360:
raise ValueError(
f"Coordinate span too large for geographic coordinates "
f"(lat span={lat_span}, lon span={lon_span}). "
f"Are your coordinates in a projected CRS?"
)
def _to_float_scalar(x) -> float:
"""Convert numpy/cupy scalar or 0-d array to python float safely."""
if cupy is not None:
# cupy.ndarray scalar
if isinstance(x, cupy.ndarray):
return float(cupy.asnumpy(x).item())
# cupy scalar type
if x.__class__.__module__.startswith("cupy") and hasattr(x, "item"):
return float(x.item())
if hasattr(x, "item"):
return float(x.item())
return float(x)
def _sample_windows_min_max(
data,
*,
max_window_elems: int = 65536, # e.g. 256x256
windows: int = 5, # corners + center default
) -> tuple[float, float]:
"""
Estimate (nanmin, nanmax) from a small sample of windows.
Works for numpy, cupy, dask+numpy, dask+cupy. Only computes on the sampled
windows, not the full array.
"""
# Normalize to last-2D sampling (y,x). For higher dims, sample first index.
if hasattr(data, "ndim") and data.ndim >= 3:
prefix = (0,) * (data.ndim - 2)
else:
prefix = ()
# Determine y/x sizes
shape = data.shape
ny, nx = shape[-2], shape[-1]
if ny == 0 or nx == 0:
return np.nan, np.nan
# Choose a square-ish window size bounded by array shape
w = int(np.sqrt(max_window_elems))
w = max(1, min(w, ny, nx))
# Define window anchor positions: (top-left), (top-right), (bottom-left), (bottom-right), (center)
anchors = [
(0, 0),
(0, max(0, nx - w)),
(max(0, ny - w), 0),
(max(0, ny - w), max(0, nx - w)),
]
if windows >= 5:
anchors.append((max(0, ny // 2 - w // 2), max(0, nx // 2 - w // 2)))
# If windows > 5, sprinkle additional evenly-spaced anchors (optional)
if windows > 5:
extra = windows - 5
ys = np.linspace(0, max(0, ny - w), extra + 2, dtype=int)[1:-1]
xs = np.linspace(0, max(0, nx - w), extra + 2, dtype=int)[1:-1]
for y0, x0 in zip(ys, xs):
anchors.append((int(y0), int(x0)))
# Reduce min/max across sampled windows
mins = []
maxs = []
for y0, x0 in anchors:
sl = prefix + (slice(y0, y0 + w), slice(x0, x0 + w))
win = data[sl]
if da is not None and isinstance(win, da.Array):
# Compute scalars only on this window
mins.append(da.nanmin(win))