Skip to content

Commit 5fdeab9

Browse files
committed
update PydapArrayWrapper to support backend batching
1 parent 69316e5 commit 5fdeab9

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

xarray/backends/pydap_.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535

3636

3737
class PydapArrayWrapper(BackendArray):
38-
def __init__(self, array):
38+
def __init__(self, array, batch=False, cache=None):
3939
self.array = array
40+
self._batch = batch
41+
self._cache = cache
4042

4143
@property
4244
def shape(self) -> tuple[int, ...]:
@@ -52,13 +54,29 @@ def __getitem__(self, key):
5254
)
5355

5456
def _getitem(self, key):
55-
result = robust_getitem(self.array, key, catch=ValueError)
56-
# in some cases, pydap doesn't squeeze axes automatically like numpy
57-
result = np.asarray(result)
57+
if self.array.id in self._cache.keys():
58+
# safely avoid re-downloading some coordinates
59+
result = self._cache[self.array.id]
60+
elif self._batch and hasattr(self.array, "dataset"):
61+
# this are both True only for pydap>3.5.5
62+
from pydap.lib import resolve_batch_for_all_variables
63+
64+
parent = self.array.parent # could be root ds | group
65+
variables = list(parent.variables())
66+
resolve_batch_for_all_variables(parent, variables, key)
67+
68+
result = np.asarray(
69+
parent.dataset._current_batch_promise.wait_for_result(self.array.id)
70+
)
71+
else:
72+
result = robust_getitem(self.array, key, catch=ValueError)
73+
try:
74+
result = np.asarray(result.data)
75+
except AttributeError:
76+
result = np.asarray(result)
5877
axis = tuple(n for n, k in enumerate(key) if isinstance(k, integer_types))
5978
if result.ndim + len(axis) != self.array.ndim and axis:
6079
result = np.squeeze(result, axis)
61-
6280
return result
6381

6482

0 commit comments

Comments
 (0)