@@ -79,7 +79,7 @@ def _quantize(data,least_significant_digit):
7979 return datout
8080
8181def _StartCountStride (elem , shape , dimensions = None , grp = None , datashape = None ,\
82- put = False , use_get_vars = True ):
82+ put = False , use_get_vars = True , return_selection = False ):
8383 """Return start, count, stride and indices needed to store/extract data
8484 into/from a netCDF variable.
8585
@@ -134,6 +134,9 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
134134 datashape : sequence
135135 The shape of the data that is being stored. Only needed by __setitem__
136136 put : True|False (default False). If called from __setitem__, put is True.
137+ return_selection : bool (default False)
138+ Also return the normalized selectors, for callers that need their exact
139+ indices after an empty selection has been represented by compact chunks.
137140
138141 Returns
139142 -------
@@ -148,6 +151,8 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
148151 indices : ndarray (..., n)
149152 An array storing the indices describing the location of the
150153 data chunk in the target/source array (__getitem__/__setitem__).
154+ selection : list, optional
155+ Normalized selectors, included only when return_selection is True.
151156
152157 Notes:
153158
@@ -322,6 +327,10 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
322327 newElem .append (e )
323328 elif np .iterable (e ) and len (e ) == 1 :
324329 newElem .append (slice (e [0 ], e [0 ] + 1 , 1 ))
330+ elif np .iterable (e ) and len (e ) == 0 :
331+ # Retain counts and scalar squeeze markers for the other axes.
332+ # A zero-length chunk axis would discard all of that metadata.
333+ newElem .append (slice (0 , 0 , 1 ))
325334 else :
326335 newElem .append (e )
327336 elem = newElem
@@ -358,8 +367,23 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
358367
359368 # pad datashape with zeros for dimensions not being sliced (issue #906)
360369 # only used when data covers slice over subset of dimensions
370+ padding_dims = sum (1 for e in elem if type (e ) == slice )
371+ if datashape and len (datashape ) != len (elem ):
372+ for i , e in enumerate (elem ):
373+ if type (e ) != slice :
374+ continue
375+ length = shape [i ]
376+ if hasunlim and unlimd [dimensions [i ]]:
377+ if e .stop is None :
378+ continue # an open-ended write may extend this dimension
379+ length = max (length , e .stop )
380+ if not range (* e .indices (length )):
381+ # Empty writes retain fancy axes too. Keep the historical
382+ # padding rules for nonempty writes outside this correction.
383+ padding_dims = sum (1 for index in elem if type (index ) == slice or np .iterable (index ))
384+ break
361385 if datashape and len (datashape ) != len (elem ) and \
362- len (datashape ) == sum ( 1 for e in elem if type ( e ) == slice ) :
386+ len (datashape ) == padding_dims :
363387 datashapenew = (); i = 0
364388 for e in elem :
365389 if type (e ) != slice and not np .iterable (e ): # scalar integer slice
@@ -369,13 +393,14 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
369393 i += 1
370394 datashape = datashapenew
371395
372- # Create the start, count, stride and indices arrays.
373-
396+ # First describe one chunk with the complete selected shape. Only expand
397+ # integer index arrays into their Cartesian chunk grid if it is nonempty.
374398 sdim .append (max (nDims , 1 ))
375- start = np .empty (sdim , dtype = np .intp )
376- count = np .empty (sdim , dtype = np .intp )
377- stride = np .empty (sdim , dtype = np .intp )
378- indices = np .empty (sdim , dtype = object )
399+ compact = [1 ] * nDims + [nDims ]
400+ start = np .empty (compact , dtype = np .intp )
401+ count = np .empty (compact , dtype = np .intp )
402+ stride = np .empty (compact , dtype = np .intp )
403+ indices = np .empty (compact , dtype = object )
379404
380405 for i , e in enumerate (elem ):
381406
@@ -426,11 +451,10 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
426451
427452 # ITERABLE #
428453 elif np .iterable (e ) and np .array (e ).dtype .kind in 'i' : # Sequence of integers
429- if start [...,i ].size :
430- start [...,i ] = np .apply_along_axis (lambda x : e * x , i , np .ones (sdim [:- 1 ]))
431- indices [...,i ] = np .apply_along_axis (lambda x : np .arange (sdim [i ])* x , i , np .ones (sdim [:- 1 ], int ))
432- count [...,i ] = 1
433- stride [...,i ] = 1
454+ start [...,i ] = e [0 ]
455+ count [...,i ] = len (e )
456+ stride [...,i ] = 1
457+ indices [...,i ] = 0 # preserve the distinction from a full slice
434458
435459 # all that's left is SCALAR INTEGER #
436460 else :
@@ -446,6 +470,25 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
446470 indices [...,i ] = - 1 # Use -1 instead of 0 to indicate that
447471 # this dimension shall be squeezed.
448472
473+ if 0 in count and not put :
474+ # Empty reads bypass native bounds checks, including those for vlens.
475+ for i , e in enumerate (elem ):
476+ if type (e ) != slice and not np .iterable (e ) and e >= shape [i ]:
477+ raise IndexError ("integer index exceeds dimension size" )
478+
479+ if 0 not in count and sdim != compact :
480+ start = np .broadcast_to (start , sdim ).copy ()
481+ count = np .broadcast_to (count , sdim ).copy ()
482+ stride = np .broadcast_to (stride , sdim ).copy ()
483+ indices = np .broadcast_to (indices , sdim ).copy ()
484+ for i , e in enumerate (elem ):
485+ if np .iterable (e ):
486+ start [...,i ] = np .apply_along_axis (lambda x : e * x , i , np .ones (sdim [:- 1 ]))
487+ indices [...,i ] = np .apply_along_axis (lambda x : np .arange (sdim [i ])* x , i , np .ones (sdim [:- 1 ], int ))
488+ count [...,i ] = 1
489+
490+ if return_selection :
491+ return start , count , stride , indices , elem
449492 return start , count , stride , indices #, out_shape
450493
451494def _out_array_shape (count ):
0 commit comments