11from __future__ import annotations
22
33import os
4- import warnings
54from collections .abc import Iterable
65from typing import TYPE_CHECKING , Any
76
3736
3837
3938class PydapArrayWrapper (BackendArray ):
40- def __init__ (self , array , batch = False , cache = None , checksums = True ):
39+ def __init__ (self , array , batch = None , checksums = True ):
4140 self .array = array
4241 self ._batch = batch
43- self ._cache = cache
4442 self ._checksums = checksums
4543
4644 @property
@@ -57,27 +55,19 @@ def __getitem__(self, key):
5755 )
5856
5957 def _getitem (self , key ):
60- if self .array .id in self ._cache .keys ():
61- # safely avoid re-downloading some coordinates
62- result = self ._cache [self .array .id ]
63- elif self ._batch and hasattr (self .array , "dataset" ):
58+ if self ._batch and hasattr (self .array , "dataset" ):
6459 # this are both True only for pydap>3.5.5
65- from pydap .lib import resolve_batch_for_all_variables
60+ from pydap .client import data_check , get_batch_data
6661
6762 dataset = self .array .dataset
68- resolve_batch_for_all_variables (self .array , key , checksums = self ._checksums )
69- result = np .asarray (
70- dataset ._current_batch_promise .wait_for_result (self .array .id )
71- )
63+ get_batch_data (self .array , checksums = self ._checksums , key = key )
64+ result = data_check (np .asarray (dataset [self .array .id ].data ), key )
7265 else :
7366 result = robust_getitem (self .array , key , catch = ValueError )
74- try :
75- result = np .asarray (result .data )
76- except AttributeError :
77- result = np .asarray (result )
78- axis = tuple (n for n , k in enumerate (key ) if isinstance (k , integer_types ))
79- if result .ndim + len (axis ) != self .array .ndim and axis :
80- result = np .squeeze (result , axis )
67+ result = np .asarray (result .data )
68+ axis = tuple (n for n , k in enumerate (key ) if isinstance (k , integer_types ))
69+ if result .ndim + len (axis ) != self .array .ndim and axis :
70+ result = np .squeeze (result , axis )
8171 return result
8272
8373
@@ -105,7 +95,7 @@ def __init__(
10595 dataset ,
10696 group = None ,
10797 session = None ,
108- batch = False ,
98+ batch = None ,
10999 protocol = None ,
110100 checksums = True ,
111101 ):
@@ -119,8 +109,6 @@ def __init__(
119109 self .dataset = dataset
120110 self .group = group
121111 self ._batch = batch
122- self ._batch_done = False
123- self ._array_cache = {} # holds 1D dimension data
124112 self ._protocol = protocol
125113 self ._checksums = checksums # true by default
126114
@@ -135,7 +123,7 @@ def open(
135123 timeout = None ,
136124 verify = None ,
137125 user_charset = None ,
138- batch = False ,
126+ batch = None ,
139127 checksums = True ,
140128 ):
141129 from pydap .client import open_url
@@ -167,34 +155,23 @@ def open(
167155 elif hasattr (url , "ds" ):
168156 # pydap dataset
169157 dataset = url .ds
170- args = {"dataset" : dataset }
171- args ["checksums" ] = checksums
158+ args = {"dataset" : dataset , "checksums" : checksums }
172159 if group :
173160 args ["group" ] = group
174161 if url .startswith (("http" , "dap2" )):
175162 args ["protocol" ] = "dap2"
176163 elif url .startswith ("dap4" ):
177164 args ["protocol" ] = "dap4"
178165 if batch :
179- if args ["protocol" ] == "dap2" :
180- warnings .warn (
181- f"`batch={ batch } ` is currently only compatible with the `DAP4` "
182- "protocol. Make sue the OPeNDAP server implements the `DAP4` "
183- "protocol and then replace the scheme of the url with `dap4` "
184- "to make use of it. Setting `batch=False`." ,
185- stacklevel = 2 ,
186- )
187- else :
188- # only update if dap4
189- args ["batch" ] = batch
166+ args ["batch" ] = batch
190167 return cls (** args )
191168
192169 def open_store_variable (self , var ):
193- try :
170+ if hasattr ( var , "dims" ) :
194171 dimensions = [
195172 dim .split ("/" )[- 1 ] if dim .startswith ("/" ) else dim for dim in var .dims
196173 ]
197- except AttributeError :
174+ else :
198175 # GridType does not have a dims attribute - instead get `dimensions`
199176 # see https://github.com/pydap/pydap/issues/485
200177 dimensions = var .dimensions
@@ -214,7 +191,7 @@ def open_store_variable(self, var):
214191 else :
215192 # all non-dimension variables
216193 data = indexing .LazilyIndexedArray (
217- PydapArrayWrapper (var , self ._batch , self ._array_cache , self . _checksums )
194+ PydapArrayWrapper (var , self ._batch , self ._checksums )
218195 )
219196
220197 return Variable (dimensions , data , var .attributes )
@@ -264,16 +241,14 @@ def _get_data_array(self, var):
264241 """gets dimension data all at once, storing the numpy
265242 arrays within a cached dictionary
266243 """
267- from pydap .lib import get_batch_data
244+ from pydap .client import get_batch_data
268245
269- if not self ._batch_done or var .id not in self ._array_cache :
270- # store all dim data into a dict for reuse
271- self ._array_cache = get_batch_data (
272- var .parent , self ._array_cache , self ._checksums
273- )
274- self ._batch_done = True
246+ if not var ._is_data_loaded ():
247+ # data has not been deserialized yet
248+ # runs only once per store/hierarchy
249+ get_batch_data (var , checksums = self ._checksums )
275250
276- return self ._array_cache [var .id ]
251+ return self .dataset [var .id ]. data
277252
278253
279254class PydapBackendEntrypoint (BackendEntrypoint ):
@@ -336,7 +311,7 @@ def open_dataset(
336311 timeout = None ,
337312 verify = None ,
338313 user_charset = None ,
339- batch = False ,
314+ batch = None ,
340315 checksums = True ,
341316 ) -> Dataset :
342317 store = PydapDataStore .open (
@@ -382,7 +357,7 @@ def open_datatree(
382357 timeout = None ,
383358 verify = None ,
384359 user_charset = None ,
385- batch = False ,
360+ batch = None ,
386361 checksums = True ,
387362 ) -> DataTree :
388363 groups_dict = self .open_groups_as_dict (
@@ -423,7 +398,7 @@ def open_groups_as_dict(
423398 timeout = None ,
424399 verify = None ,
425400 user_charset = None ,
426- batch = False ,
401+ batch = None ,
427402 checksums = True ,
428403 ) -> dict [str , Dataset ]:
429404 from xarray .core .treenode import NodePath
0 commit comments