35
35
36
36
37
37
class PydapArrayWrapper (BackendArray ):
38
- def __init__ (self , array ):
38
+ def __init__ (self , array , batch = False , cache = None ):
39
39
self .array = array
40
+ self ._batch = batch
41
+ self ._cache = cache
40
42
41
43
@property
42
44
def shape (self ) -> tuple [int , ...]:
@@ -52,13 +54,29 @@ def __getitem__(self, key):
52
54
)
53
55
54
56
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 )
58
77
axis = tuple (n for n , k in enumerate (key ) if isinstance (k , integer_types ))
59
78
if result .ndim + len (axis ) != self .array .ndim and axis :
60
79
result = np .squeeze (result , axis )
61
-
62
80
return result
63
81
64
82
0 commit comments