2525# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2626# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28-
2928"""
3029A pure Python implementation of the OPeNDAP server protocol.
3130
4342
4443import re
4544import numpy as np
45+ import dask .array as da
4646
4747INDENT = ' '
4848SLICE_CONSTRAINT_RE = r'\[([\d,\W]+)\]$'
@@ -55,7 +55,6 @@ class DAPError(Exception):
5555class DAPObject (object ):
5656 """A generic DAP object class.
5757 """
58-
5958 def __init__ (self , name = '' , parent = None , * args , ** kwargs ):
6059 try :
6160 self .name = '_' .join (name .split (' ' ))
@@ -132,12 +131,12 @@ def data_path(self):
132131 return '.' .join ([self .parent .data_path , self .name ])
133132
134133 def ddshead (self ):
135- return '{indent}{obj} {{\n ' .format (
136- indent = self . indent , obj = self .__class__ .__name__ )
134+ return '{indent}{obj} {{\n ' .format (indent = self . indent ,
135+ obj = self .__class__ .__name__ )
137136
138137 def ddstail (self ):
139- return '{indent}}} {name};\n ' .format (
140- indent = self . indent , name = self .name )
138+ return '{indent}}} {name};\n ' .format (indent = self . indent ,
139+ name = self .name )
141140
142141 def dashead (self ):
143142 name = self .name
@@ -211,12 +210,13 @@ def das(self, constraint=''):
211210
212211 def dds (self , constraint = '' ):
213212 if meets_constraint (constraint , self .data_path ):
214- yield '{indent}{dtype} {name};\n ' .format (
215- indent = self .indent , dtype = self .__str__ (), name = self .name )
213+ yield '{indent}{dtype} {name};\n ' .format (indent = self .indent ,
214+ dtype = self .__str__ (),
215+ name = self .name )
216216
217217 def dods_data (self , constraint = '' ):
218218 if meets_constraint (constraint , self .data_path ):
219- yield dods_encode (self ._val , self )
219+ yield from dods_encode (self ._val , self )
220220
221221
222222class Byte (DAPAtom ):
@@ -226,32 +226,32 @@ class Byte(DAPAtom):
226226
227227class Int16 (DAPAtom ):
228228 dtype = np .int16
229- str = '< i4'
229+ str = '> i4'
230230
231231
232232class UInt16 (DAPAtom ):
233233 dtype = np .uint16
234- str = '< u4'
234+ str = '> u4'
235235
236236
237237class Int32 (DAPAtom ):
238238 dtype = np .int32
239- str = '< i4'
239+ str = '> i4'
240240
241241
242242class UInt32 (DAPAtom ):
243243 dtype = np .uint32
244- str = '< u4'
244+ str = '> u4'
245245
246246
247247class Float32 (DAPAtom ):
248248 dtype = np .float32
249- str = '< f4'
249+ str = '> f4'
250250
251251
252252class Float64 (DAPAtom ):
253253 dtype = np .float64
254- str = '< f8'
254+ str = '> f8'
255255
256256
257257class String (DAPAtom ):
@@ -260,7 +260,7 @@ class String(DAPAtom):
260260
261261 def dods_data (self , constraint = '' ):
262262 if meets_constraint (constraint , self .data_path ):
263- yield dods_encode (self ._val .encode ('ascii' ), self )
263+ yield from dods_encode (self ._val .encode ('ascii' ), self )
264264
265265
266266class URL (String ):
@@ -277,7 +277,6 @@ class Structure(DAPObject):
277277class Dataset (Structure ):
278278 """Class representing a DAP dataset.
279279 """
280-
281280 def dods_data (self , constraint = '' ):
282281
283282 yield b'Data:\r \n '
@@ -342,7 +341,6 @@ def dods_data(self, constraint=''):
342341class SequenceInstance (DAPObject ):
343342 """Class representing a data item that will be added to a sequence.
344343 """
345-
346344 @property
347345 def data_path (self ):
348346 return self .parent .data_path
@@ -372,7 +370,6 @@ class DAPDataObject(DAPObject):
372370 """A generic class for typed non-atomic objects holding actual data (i.e.
373371 Array and Grid).
374372 """
375-
376373 def _parse_args (self , args , kwargs ):
377374
378375 self .data = kwargs .get ('data' , None )
@@ -394,11 +391,11 @@ def dods_data(self, constraint=''):
394391
395392 if meets_constraint (constraint , self .data_path ):
396393 slices = parse_slice_constraint (constraint )
397- yield dods_encode (self .data [slices ], self .dtype )
394+ yield from dods_encode (self .data [slices ], self .dtype )
398395 if self .dimensions is not None :
399396 for i , dim in enumerate (self .dimensions ):
400397 sl = slices [i ] if i < len (slices ) else ...
401- yield dods_encode (dim .data [sl ], dim .dtype )
398+ yield from dods_encode (dim .data [sl ], dim .dtype )
402399
403400
404401class Grid (DAPDataObject ):
@@ -482,9 +479,25 @@ def dods_encode(data, dtype):
482479 if not is_scalar :
483480 packed_length = length .astype ('<i4' ).byteswap ().tobytes () * 2
484481
485- packed_data = data .astype (dtype .str ).byteswap ().tobytes ()
486482
487- return packed_length + packed_data
483+ yield packed_length
484+
485+
486+ if isinstance (data , da .Array ):
487+ for x in range (0 , data .shape [0 ], data .chunks [0 ][0 ]):
488+ yield np .array (data [x :x + data .chunks [0 ][0 ],
489+ ...]).astype (dtype .str ).tobytes ()
490+ else :
491+ yield data .astype (dtype .str ).tobytes ()
492+ #yield data.astype(dtype.str).byteswap().tobytes()
493+
494+ ######
495+ #import pudb; pudb.set_trace()
496+ #if isinstance(data, dask.array.Array):
497+ # return packed_length + data.map_blocks(np.ndarray.tobytes, dtype=dtype.str)
498+ #else:
499+ # packed_data = data.astype(dtype.str).tobytes()
500+ # return packed_length + packed_data
488501
489502
490503def parse_slice_constraint (constraint ):
0 commit comments