
    1Vhb                        d Z ddlZddlZddlmZmZ ddlZddlm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&  e' e(d            Z) e	jT                         jV                  Z,	 	 	 	 	 	 	 d!dddZ-	 	 d"dZ. G d de      Z/ G d de/      Z0 G d de/      Z1 G d de/      Z2d Z3e,r
 G d d      Z4 G d d      Z5 G d d e      Z6y)#z7
    Implements support for high-level dataset access.
    N)ABCabstractmethod   )	h5h5sh5th5rh5dh5ph5fdh5ds	_selector   )array_for_new_objectcached_propertyEmptyfind_item_typeHLObjectphilproduct	with_phil)filters)
selections)selections2)Datatype)filename_decode)VDSmapvds_support
   	fill_timec                
   |t        |t              st        ||      }|'||t        d      t        |      }|j                  }nLt        |t
              r|fn
t        |      }|,t        |      t        |j                        k7  rt        d      t        |	t
              r|	f}	|	|	n|}t        |t
              rt        |t              s|f}t        |t              r9t        d t        ||      D              rdj                  ||      }t        |      t        |t              r|j                  }|j                  }nW||t!        j                  d      }n&|||j                  }nt!        j                  |      }t#        j$                  |d      }t        ||||	|f      r|d	u rt        d
      |du r|
d}
d}|t&        v r|
t        d      |}
d}t)        j*                  |xs# t-        j.                  t,        j0                        |||||
|||	||||      }|t        |t(        j2                        r|j4                  }t        |t
              rt)        j6                  |      }|t)        j8                  |      vr$t        d|dt)        j8                  |            |tt#        j:                  |      }|7t#        j<                  |j>                        }t!        j@                  ||      }nt!        j@                  |      }|jC                  |       |d	}|dv r|jE                  |       nt        d      |du r1|jG                  t,        jH                  t,        jJ                  z         n#|d	u r|jG                  d       n|t        d      |	t        d |	D              }	t        |||||g      r'|xs# t-        j.                  t,        jL                        }||jO                  |       ||jQ                  |       |s|s|r=tS        |jU                               }|||d<   |||d<   |||d<    |jV                  |  t        |t              r$tY        j.                  tX        jZ                        }ntY        j\                  ||	      }t_        j.                  | j                  |||||      }|?t        |t              s/|ja                  tX        jb                  tX        jb                  |       |S )z+ Return a new low-level dataset identifier )specified_dtypez-One of data, shape or dtype must be specifiedz%Shape tuple is incompatible with datac              3   2   K   | ]  \  }}|	||kD    y wN ).0dimchunks      @/home/dcms/DCMS/lib/python3.12/site-packages/h5py/_hl/dataset.py	<genexpr>z make_new_dset.<locals>.<genexpr>E   s!      )"U)s   

z^Chunk shape must not be greater than data shape in any dimension. {} is not compatible with {}z=f4r   )logicalFz1Chunked format required for given storage optionsT   gzipzConflict in compression optionsr    zcompression z not in filters dtype)TFz(track_times must be either True or Falser   z(track_order must be either True or Falsec              3   D   K   | ]  }||nt         j                    y wr%   r   	UNLIMITED)r'   ms     r*   r+   z make_new_dset.<locals>.<genexpr>   s     Q1am>Qs    r   )dcpldapl)2
isinstancer   r   	TypeErrorshapeinttupler   
ValueErrorboolanyzipformatr   idr0   numpyr   	py_create_LEGACY_GZIP_COMPRESSION_VALSr   	fill_dcplr   createDATASET_CREATEFilterRefBase	filter_idget_filter_nameget_filterscheck_string_dtypestring_dtypeencodingarrayset_fill_valueset_obj_track_timesset_attr_creation_orderCRT_ORDER_TRACKEDCRT_ORDER_INDEXEDDATASET_ACCESSset_efile_prefixset_virtual_prefixlistget_chunk_cacheset_chunk_cacher   NULLcreate_simpler
   writeALL) parentr9   r0   datanamechunkscompressionshuffle
fletcher32maxshapecompression_opts	fillvaluescaleoffsettrack_timesexternaltrack_orderr5   r6   efile_prefixvirtual_prefixallow_unknown_filterrdcc_nslotsrdcc_nbytesrdcc_w0r!   	tmp_shapeerrmsgtidstring_infocache_settingssiddset_ids                                    r*   make_new_dsetrz   $   s    
4 7#D%@ }<} OPP<D

&uc2e74::3F!FDEE(C ;$0eI &#z&$'?&% S )&))V&<) &006vu0E 	  %"hh		 =T\KK&E]t/JJEKK&EmmE1- K*hDE&TY/LMM d#  33'=>>&.

3--.u-w
+x)=	D k7#8#89%//Kk3'!11+>Kg11$77|K?:J7K^K^_cKdJghii,,U3" $$[%9%9:EIU;II.II&m#  -BCCd$$!!C$9$99	;		$$Q'		 BCCQQQ
L.+{GLM5szz#"4"45l+!/kWd2245" +N1" +N1 'N1n-$jj"x0jjD#sDIG:dE#:cggsww-N    c                    t        |||||g      r'|xs# t        j                  t        j                        }||j	                  |       ||j                  |       |s|s|r=t        |j                               }	|||	d<   |||	d<   |||	d<    |j                  |	  t        j                  | j                  ||      }
|
S )z1 Return an existing low-level dataset identifier r   r   r   )r6   )r>   r   rF   rU   rV   rW   rX   rY   rZ   r
   openrA   )r_   ra   r6   rm   rn   rp   rq   rr   kwdsrw   ry   s              r*   	open_dsetr      s     L.+{GLM5szz#"4"45l+!/kWd2245" +N1" +N1 'N1n-hhvyy$T2GNr{   c                       e Zd ZU ded<   d Zd Zeed               Zed        Z	ed        Z
ed        Zed	        ZddZy
)AbstractViewDataset_dsetc                     || _         y r%   r   selfdsets     r*   __init__zAbstractView.__init__   s	    
r{   c                 ,    t        | j                        S r%   )lenr   r   s    r*   __len__zAbstractView.__len__   s    4::r{   c                      y r%   r&   r   s    r*   r0   zAbstractView.dtype   s     	r{   c                 .    | j                   j                  S r%   )r   ndimr   s    r*   r   zAbstractView.ndim       zzr{   c                 .    | j                   j                  S r%   )r   r9   r   s    r*   r9   zAbstractView.shape   s    zzr{   c                 .    | j                   j                  S r%   )r   sizer   s    r*   r   zAbstractView.size   r   r{   c                      y r%   r&   r   idxs     r*   __getitem__zAbstractView.__getitem__   s    r{   Nc                     |du r%t        | j                  j                   d|d      t        j                  | d   |xs | j
                        S )NFz.__array__ received copy=0 but memory allocation cannot be avoided on readr&   r/   )r<   	__class____name__rB   asarrayr0   r   r0   copys      r*   	__array__zAbstractView.__array__   sZ    5=>>**++Ew GB B  }}T"XU-@djjAAr{   NN)r   
__module____qualname____annotations__r   r   propertyr   r0   r   r9   r   r   r   r&   r{   r*   r   r      s                 Br{   r   c                   @     e Zd ZdZ fdZed        Zd ZddZ xZ	S )
AsTypeViewz7Wrapper to convert data on reading from a dataset.
    c                 2    t         |   |       || _        y r%   )superr   _dtype)r   r   r0   r   s      r*   r   zAsTypeView.__init__   s    r{   c                     | j                   S r%   )r   r   s    r*   r0   zAsTypeView.dtype  s    {{r{   c                 P    | j                   j                  || j                        S N)	new_dtype)r   r   r   r   s     r*   r   zAsTypeView.__getitem__  s     zz%%cT[[%AAr{   c                 V    | j                   j                  |xs | j                  |      S r%   )r   r   r   r   s      r*   r   zAsTypeView.__array__
  s"    zz##E$8T[[$??r{   r   )
r   r   r   __doc__r   r   r0   r   r   __classcell__r   s   @r*   r   r      s-      B@r{   r   c                   :     e Zd ZdZd fd	Zed        Zd Z xZS )	AsStrViewz0Wrapper to decode strings on reading the datasetc                 @    t         |   |       || _        || _        y r%   )r   r   rN   errors)r   r   rN   r   r   s       r*   r   zAsStrView.__init__  s     r{   c                 4    t        j                  t              S r%   )rB   r0   objectr   s    r*   r0   zAsStrView.dtype  s    {{6""r{   c           	         | j                   |   }t        j                  |      r&|j                  | j                  | j
                        S t        j                  |j                  D cg c](  }|j                  | j                  | j
                        * c}t              j                  |j                        S c c}w )Nr/   )r   rB   isscalardecoderN   r   rO   flatr   reshaper9   )r   r   	bytes_arrbs       r*   r   zAsStrView.__getitem__  s    JJsO	 >>)$##DMM4;;??{{:C..
56AHHT]]DKK0
 1	2 
s   (-B;)strict	r   r   r   r   r   r   r0   r   r   r   s   @r*   r   r     s$    :
 # #2r{   r   c                   8     e Zd ZdZ fdZed        Zd Z xZS )
FieldsViewzBWrapper to extract named fields from a dataset with a struct dtypec                     t         |   |       t        |t              r|| _        |g}nd | _        t        ||      | _        y r%   )r   r   r7   strextract_fieldreadtime_dtype
read_dtype)r   r   prior_dtypenamesr   s       r*   r   zFieldsView.__init__,  s?    eS!!&DGE!%D(e<r{   c                 T    | j                   }| j                  || j                     }|S r%   )r   r   )r   ts     r*   r0   zFieldsView.dtype5  s,    OO)$$$%Ar{   c                     | j                   j                  || j                        }| j                  || j                     }|S r   )r   r   r   r   )r   r   r`   s      r*   r   zFieldsView.__getitem__<  s@    zz%%cT__%E)**+Dr{   r   r   s   @r*   r   r   )  s$    L=  r{   r   c                     | j                   t        d      |D ]  }|| j                   vst        d|z         t        j                  |D cg c]  }|| j                  |   d   f c}      S c c}w )z=Make a NumPy compound dtype with a subset of available fieldsz+Field names only allowed for compound typesz&Field %s does not appear in this type.r   )r   r<   rB   r0   fields)basetyper   ra   s      r*   r   r   C  sx    ~~FGG Nx~~%ELMMN ;;UKTxt4Q78KLLKs   A0c                   "    e Zd ZdZd Zd Zd Zy)CollectiveContextz$ Manages collective I/O in MPI mode c                     || _         y r%   r   r   s     r*   r   zCollectiveContext.__init__V  s	    DJr{   c                 j    | j                   j                  j                  t        j                         y r%   )r   _dxplset_dxpl_mpior   MPIO_COLLECTIVEr   s    r*   	__enter__zCollectiveContext.__enter__Y  s     JJ**4+?+?@r{   c                 j    | j                   j                  j                  t        j                         y r%   )r   r   r   r   MPIO_INDEPENDENT)r   argss     r*   __exit__zCollectiveContext.__exit__]  s     JJ**4+@+@Ar{   N)r   r   r   r   r   r   r   r&   r{   r*   r   r   P  s    2		A	Br{   r   c                   $    e Zd ZdZddZd Zd Zy)ChunkIteratorzD
    Class to iterate through list of chunks of a given dataset
    Nc                     |j                    _        t        |j                         }|j                  st	        d      |j                   _        |#t         fdt        |      D               _        n t        |t              r	|f _        n| _        t         j                        |k7  rt        d      g  _        t        |      D ]  } j                  |   }|j                  dk  s5|j                   j                  |   kD  s|j                  |j                  k  rt        d      |j                   j
                  |   z  } j                  j                  |        y )NzChunked dataset requiredc              3   P   K   | ]  }t        d j                  |           yw)r   N)slice_shape)r'   r(   r   s     r*   r+   z)ChunkIterator.__init__.<locals>.<genexpr>q  s)       aS)*s   #&zCInvalid selection - selection region must have same rank as datasetr   zAInvalid selection - selection region must be within dataset space)r9   r   r   rb   r8   _layoutr;   range_selr7   r   r<   _chunk_indexstartstopappend)r   r   
source_selrankr(   sindexs   `      r*   r   zChunkIterator.__init__f  s!   jj4::{{677{{  ; DI
 *e,'M	&	tyy>T!bcc; 	,C		#Aww{afft{{3'77166QWW;L !deeGGt||C00E$$U+	,r{   c                     | S r%   r&   r   s    r*   __iter__zChunkIterator.__iter__  s    r{   c                    t        | j                        }g }|dk(  s9| j                  d   | j                  d   z  | j                  d   j
                  k\  r
t               t        |      D ]  }| j                  |   }| j                  |   | j                  |   z  }| j                  |   dz   | j                  |   z  }||j                  k  r|j                  }||j
                  kD  r|j
                  }t        ||d      }|j                  |        |dz
  }|dk\  r| j                  |   }| j                  |xx   dz  cc<   | j                  |   | j                  |   z  }||j
                  k  rt        |      S |dkD  r)|j                  | j                  |   z  | j                  |<   |dz  }|dk\  rt        |      S )Nr   r   )r   r   r   r   r   r   StopIterationr   r   r   r   r;   )r   r   slicesr(   r   r   r   	chunk_ends           r*   __next__zChunkIterator.__next__  s   4;;19))!,t||A>$))A,BSBSS/!; 
	C		#A%%c*T\\#->>E%%c*Q.$,,s2CCDqwwaff}vveT1%AMM!
	 QhQh		#Ac"a'"))#.c1BBI166!V}$Qw)*DLL4E)E!!#&1HC Qh V}r{   r%   )r   r   r   r   r   r   r   r&   r{   r*   r   r   b  s    ,<"r{   r   c                       e Zd ZdZd Zd5dZdddZeree	d               Z
ed        Zee	d	               Zed
        Zej                  e	d               Zed        Zed        Zed        Zed        Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zee	d               Zed        Zed        Z ed        Z!e	dd fd 
       Z"d6d!Z#e	d"        Z$d# Z%e	d$        Z&e	d6d%       Z'ed&        Z(e	d6d'       Z)e	d(        Z*d7d)Z+d7d*Z,e	d7d+       Z-e	d,        Z. e/e0jb                  d-      re	d.        Z2 e/e0jb                  d/      re	d0        Z3e4ree	d1               Z5e	d2        Z6e	d8d3       Z7ee	d4               Z8 xZ9S )9r   z(
        Represents an HDF5 dataset
    c                     t        j                  |      }|| j                  k(  r| S |j                  dk(  r0t        j                  | j                        }|t        d| d      t        | |      S )z Get a wrapper allowing you to perform reads to a
        different destination type, e.g.:

        >>> double_precision = dataset.astype('f8')[0:100:2]
        Tzdset.astype(z;) can only be used on datasets with an HDF5 string datatype)rB   r0   kindr   rL   r8   r   )r   r0   rv   s      r*   astypezDataset.astype  sv     E"DJJK::00<K""5' *. . 
 $&&r{   Nc                     t        j                  | j                        }|t        d      ||j                  }t        | ||      S )a  Get a wrapper to read string data as Python strings:

        >>> str_array = dataset.asstr()[:]

        The parameters have the same meaning as in ``bytes.decode()``.
        If ``encoding`` is unspecified, it will use the encoding in the HDF5
        datatype (either ascii or utf-8).

        .. note::
           On NumPy 2.0 and later, it is recommended to use native NumPy
           variable-width strings instead:

           >>> str_array = dataset.astype('T')[:]
        zFdset.asstr() can only be used on datasets with an HDF5 string datatype)r   )r   rL   r0   r8   rN   r   )r   rN   r   rv   s       r*   asstrzDataset.asstr  sR     ,,TZZ8*  "++Hx77r{   _prior_dtypec                8    || j                   }t        | ||      S )aQ  Get a wrapper to read a subset of fields from a compound data type:

        >>> 2d_coords = dataset.fields(['x', 'y'])[:]

        If names is a string, a single field is extracted, and the resulting
        arrays will have that dtype. Otherwise, it should be an iterable,
        and the read data will have a compound dtype.
        )r0   r   )r   r   r   s      r*   r   zDataset.fields  s"     ::L$e44r{   c                     t        |       S )z3 Context manager for MPI collective reads & writes )r   r   s    r*   
collectivezDataset.collective  s     %T**r{   c                 V    ddl m} t        5   ||       cddd       S # 1 sw Y   yxY w)z3 Access dimension scales attached to this dataset. r   )DimensionManagerN)dimsr  r   )r   r  s     r*   r  zDataset.dims  s'     	+ 	*#D)	* 	* 	*s   (c                 .    | j                   j                  S )z5Numpy-style attribute giving the number of dimensions)rA   r   r   s    r*   r   zDataset.ndim  s     ww||r{   c                     d| j                   v r| j                   d   S t        5  | j                  j                  }ddd       | j                  r| j                   d<   S # 1 sw Y   &xY w)z1Numpy-style shape tuple giving dataset dimensionsr9   N)_cache_propsr   rA   r9   	_readonlyr   r9   s     r*   r9   zDataset.shape   sf     d'''$$W-- 	"GGMME	"
 >>).Dg&	" 	"s   A  A)c                 &    | j                  |       y r%   )resizer  s     r*   r9   zDataset.shape  s     	Er{   c                     d| j                   v r| j                   d   S | j                  rd}nt        | j                        }| j                  r|| j                   d<   |S )z3Numpy-style attribute giving the total dataset sizer   N)r  	_is_emptyr   r9   r  r   r   s     r*   r   zDataset.size  s[     T&&&$$V,,>>D4::&D >>(,Df%r{   c                 R    | j                   }|y| j                  j                  |z  S )zHNumpy-style attribute giving the raw dataset size as the number of bytesr   )r   r0   itemsizer  s     r*   nbyteszDataset.nbytes&  s*     yy<zz""T))r{   c                     d| j                   v r| j                   d   S t        j                  | j                  j	                               }| j
                  r|| j                   d<   |S )z/Internal object for optimised selection of datar   )r  r   SelectorrA   	get_spacer  )r   slrs     r*   r   zDataset._selector.  s^     $+++$$[11  !2!2!45 >>-0Dk*
r{   c                     d| j                   v r| j                   d   S t        j                  | j                        }| j                  r|| j                   d<   |S )z-Internal object for optimised reading of data_fast_reader)r  r   ReaderrA   r  )r   rdrs     r*   r  zDataset._fast_reader<  sU     T...$$^44tww' >>03Dn-
r{   c                 .    | j                   j                  S )z%Numpy dtype representing the datatype)rA   r0   r   s    r*   r0   zDataset.dtypeJ  s     ww}}r{   c                 ~    | j                   }|j                         t        j                  k(  r|j	                         S y)zDataset chunks (or None)N)_dcpl
get_layoutr
   CHUNKED	get_chunk)r   r5   s     r*   rb   zDataset.chunksP  s0     zz??+>>##r{   c                 6    dD ]  }|| j                   v s|c S  y)zCompression strategy (or None))r.   lzfszipN_filters)r   xs     r*   rc   zDataset.compressionY  s)     ' 	ADMM!	 r{   c                 N    | j                   j                  | j                  d      S )z< Compression setting.  Int(0-9) for gzip, 2-tuple for szip. N)r#  getrc   r   s    r*   rg   zDataset.compression_optsb  s!     }}  !1!1488r{   c                     d| j                   v S )zShuffle filter present (T/F)rd   r"  r   s    r*   rd   zDataset.shuffleh  s     DMM))r{   c                     d| j                   v S )z"Fletcher32 filter is present (T/F)re   r"  r   s    r*   re   zDataset.fletcher32n  s     t}},,r{   c                 F    	 | j                   d   d   S # t        $ r Y yw xY w)a  Scale/offset filter settings. For integer data types, this is
        the number of bits stored, or 0 for auto-detected. For floating
        point data types, this is the number of decimal places retained.
        If the scale/offset filter is not in use, this is None.ri   r   N)r#  KeyErrorr   s    r*   ri   zDataset.scaleoffsett  s-    	==/22 		s    	  c                     | j                   j                         }|dk  ryt               }t        |      D ]>  }| j                   j	                  |      \  }}}|j                  t        |      ||f       @ |S )zExternal file settings. Returns a list of tuples of
        (name, offset, size) for each external file entry, or returns None
        if no external files are used.r   N)r  get_external_countrX   r   get_externalr   r   )r   countext_listr$  ra   offsetr   s          r*   rk   zDataset.external  sv     

--/!86u 	EA#'::#:#:1#= T64OOod3VTBD	E r{   c                     | j                   j                         }|j                  d      }|yt        d |D              S )zcShape up to which this dataset can be resized.  Axes with value
        None have no resize limit. TNc              3   J   K   | ]  }|t         j                  k7  r|nd   y wr%   r2   r'   r$  s     r*   r+   z#Dataset.maxshape.<locals>.<genexpr>  s      E1!s}},Q$6Es   !#)rA   r  get_simple_extent_dimsr;   )r   spacer  s      r*   rf   zDataset.maxshape  s?    
 !!#++D1<EEEEr{   c                     t        j                  d| j                        }| j                  j	                  |       |d   S )z*Fill value for this dataset (0 by default))r   r/   r   )rB   zerosr0   r  get_fill_value)r   arrs     r*   rh   zDataset.fillvalue  s3     kk$djj1

!!#&1vr{   c                 R    | j                   j                         j                         S )z9Get extent type for this dataset - SIMPLE, SCALAR or NULL)rA   r  get_simple_extent_typer   s    r*   _extent_typezDataset._extent_type  s      ww  "99;;r{   c                 <    | j                   t        j                  k(  S )zCheck if extent type is empty)r<  r   r[   r   s    r*   r  zDataset._is_empty  s       CHH,,r{   c                 6    | j                   j                         S )zX
        The dataset creation property list used when this dataset was created.
        )rA   get_create_plistr   s    r*   r  zDataset._dcpl  s    
 ww''))r{   c                 @    t        j                  | j                        S )z4
        The active filters of the dataset.
        )r   rK   r  r   s    r*   r#  zDataset._filters  s    
 ""4::..r{   F)readonlyc                    t        |t        j                        st        d|z        t        |   |       t        j                  t        j                        | _	        || _
        i | _        y)zJ Create a new Dataset object by binding to a low-level DatasetID.
        z%s is not a DatasetIDN)r7   r
   	DatasetIDr<   r   r   r   rF   DATASET_XFERr   r  r  )r   bindrA  r   s      r*   r   zDataset.__init__  sV     $.4t;<<ZZ 0 01
!r{   c                    t         5  | j                  t        d      |i|dk\  r|| j                  j                  k  s%t        d| j                  j                  dz
  z        	 t        |      }t        | j                        }|||<   t        |      }| j                  j                  |       ddd       y# t        $ r t        d      w xY w# 1 sw Y   yxY w)aE   Resize the dataset, or the specified axis.

        The dataset must be stored in chunked format; it can be resized up to
        the "maximum shape" (keyword maxshape) specified at creation time.
        The rank of the dataset cannot be changed.

        "Size" should be a shape tuple, or if an axis is specified, an integer.

        BEWARE: This functions differently than the NumPy resize() method!
        The data is not "reshuffled" to fit in the new shape; each axis is
        grown or shrunk independently.  The coordinates of existing data are
        fixed.
        Nz$Only chunked datasets can be resizedr   zInvalid axis (0 to %s allowed)r   z2Argument must be a single int if axis is specified)r   rb   r8   rA   r   r<   r:   rX   r9   r;   
set_extent)r   r   axisnewlens       r*   r
  zDataset.resize  s      	%{{" FGGqTDGGLL%8$%EVW%XYYZ YF DJJ'#T
;DGGt$	% 	% ! Z#$XYYZ	% 	%s%   AC%B90A C9CCCc                 b    | j                         }|t        j                  kD  rt        d      |S )z The size of the first axis.  TypeError if scalar.

        Limited to 2**32 on 32-bit systems; Dataset.len() is preferred.
        z>Value too big for Python's __len__; use Dataset.len() instead.)r   sysmaxsizeOverflowErrorr  s     r*   r   zDataset.__len__  s,     xxz#++ `aar{   c                     t         5  | j                  }t        |      dk(  rt        d      |d   cddd       S # 1 sw Y   yxY w)z The size of the first axis.  TypeError if scalar.

        Use of this method is preferred to len(dset), as Python's built-in
        len() cannot handle values greater then 2**32 on 32-bit systems.
        r   z'Attempt to take len() of scalar datasetN)r   r9   r   r8   r  s     r*   r   zDataset.len  sC      	JJE5zQ IJJ8		 	 	s	   *;Ac              #      K   | j                   }t        |      dk(  rt        d      t        |d         D ]	  }| |     yw)z Iterate over the first axis.  TypeError if scalar.

        BEWARE: Modifications to the yielded data are *NOT* written to file.
        r   z#Can't iterate over a scalar datasetN)r9   r   r8   r   )r   r9   is      r*   r   zDataset.__iter__  sH      

u:?ABBuQx 	Aq'M	s   AAc                     t        | |      S )a   Return chunk iterator.  If set, the sel argument is a slice or
        tuple of slices that defines the region to be used. If not set, the
        entire dataspace will be used for the iterator.

        For each chunk within the given region, the iterator yields a tuple of
        slices that gives the intersection of the given chunk with the
        selection area.

        A TypeError will be raised if the dataset is not chunked.

        A ValueError will be raised if the selection region is invalid.

        )r   )r   sels     r*   iter_chunkszDataset.iter_chunks  s     T3''r{   c                     | j                   t        j                  k(  xrB t        | j                  j                         t        j                  t        j                  f      S )z+Is this dataset suitable for simple reading)	r<  r   SIMPLEr7   rA   get_typer   TypeIntegerIDTypeFloatIDr   s    r*   _fast_read_okzDataset._fast_read_ok  sG     + U477++-0A0A3??/ST	
r{   c                 &   t        |t              r|n|f}| j                  r|	 | j                  j	                  |      S | j                  r>|dk(  st        |      dk(  r |d   t        u rt        | j                        S t        d      t        d |D              }|r;t        |      dk(  r|d   }t        d |D              }| j                  ||      |   S || j                  }t        j                  |      }t        |      dk(  rt        |d   t        j                         rt        j"                  |d   | j$                        }|| j$                  k7  rt        d      t        j&                  |d   | j$                        }t)        j*                  |      }|t        |      S t-        j.                  ||	      }|j0                  dk(  r|S t3        j4                  |      }	|	j7                          | j$                  j	                  |	|||       |S | j0                  dk(  r?|dk(  st        |      dk(  r,|d   t        u r!t-        j.                  | j8                  |	      S | j8                  dk(  r| j$                  j;                         }
t=        j>                  |
|      }|j@                  t-        j.                  d|	      }n!t-        j.                  |j@                  |	      }|D ]#  \  }}
| j$                  j	                  ||
||       % |j@                  |d   S |S t)        jB                  | j8                  || 
      }|jD                  dk(  r!t-        j.                  |jF                  |	      S t-        j.                  |jF                  |d      }t3        j4                  |j@                        }|j$                  }
| j$                  j	                  ||
||| jH                         |j8                  dk(  r|d   S |S # t
        $ r Y w xY w)a   Read a slice from the HDF5 dataset.

        Takes slices and recarray-style field names (more than one is
        allowed!) in any order.  Obeys basic NumPy rules, including
        broadcasting.

        Also supports:

        * Boolean "mask" array indexing
        r&   r   r   zEmpty datasets cannot be slicedc              3   B   K   | ]  }t        |t              s|  y wr%   r7   r   r3  s     r*   r+   z&Dataset.__getitem__.<locals>.<genexpr>@       <AAs);a<   c              3   B   K   | ]  }t        |t              r|  y wr%   r\  r3  s     r*   r+   z&Dataset.__getitem__.<locals>.<genexpr>F  s     Cq
1c0BCr^  r   z+Region reference must point to this datasetr/   datasetC)orderdxpl)%r7   r;   rY  r  readr8   r  r   Ellipsisr   r0   r<   r   r   rC   r	   RegionReferencedereferencerA   
get_regionrR  guess_shaperB   r7  r   r   r\   
select_allr9   r  sel2select_readmshapeselectnselectarray_shaper   )r   r   r   r   mtypeobjrx   ro  outsid_outfspace	selectionr9  mspaces                 r*   r   zDataset.__getitem__$  sw    "$.tTG9#4((--d33 >> rzc$i1naH1DTZZ((>?? <<<5zQaCDCCD;;u9;=dCC

Ii( t9>ja#2E2EF//$q'4773Cdgg~ !NOO..a$''2C__S)F~Y''++fI6Cxx1}
''/G GGLL#sE2J 99> rzc$i1naH1D{{4::Y?? ::WW&&(F((6I'kk"I6kk)"2"2)D"+ 9VVS%89'2wJ
 JJtzz4>	!;;y44IFFkk)//#F ""9#3#34VVS%djjA 99?r7N
o  s   P 	PPc           
         t        |t              r|n|f}t        d |D              }t        d |D              }t        j                  | j                        }||t
        t        fvr	 t        j                  ||      }||j                  k(  rf|j                  dkD  r~t        j                  |j                  dd t              }|j!                  t#        |j                  dd       |j                  d   f      D cg c]  }| c}|j%                         dd n!t        j                  dgt              }||d<   |}n| j                  j&                  d	k(  se| j                  j&                  d
k(  rct        |t        j(                        r|j                  j&                  d
k7  r/| j                  j*                  t-        |      dk(  rd| j                  j.                  N|d   | j                  j.                  vrt        d|d   z        | j                  j.                  |d      d   }d}	n| j                  }d}	t        j                  ||j0                  d      }|	r|j3                  t        j                  |d   |fg            }|j!                  |j                  dt-        |j                        t-        |j                        z
         }n| j                  j&                  dk(  rt        j4                  | j                        j6                  dk(  rt9        |      t        u r~t        j                  |dt              }
t        j                  |
j:                  D cg c]  }|j=                  d       c}| j                        j!                  |
j                        }nJt        |t        j(                        rdn| j                  j0                  }t        j                  |d|      }| j                  j*                  | j                  j*                  d   }|j                  t-        |       d }||k7  rt        d|d|d      t        j>                  t        j                  |j                  |f            }|j                  dt-        |j                        t-        |      z
   }nt-        |      dk7  r|j                  }| j                  j.                  t        d      |D cg c]  }|| j                  j.                  vs| }}t-        |      dk7  r&djA                  d |D              }t        d|z        t-        |      dk(  r|j                  j.                  wt        j>                  |j                        }t        jB                  t        jD                  |jG                               }|jI                  | jK                  |d         d|       n|j                  jL                  D cg c]	  }||v s| }}t        jB                  t        jD                  |j                  jN                        }|D ]o  }t        j>                  |j                  j.                  |   d         }|j                  j.                  |   d   }|jI                  | jK                  |      ||       q n|j                  }d}tQ        jR                  | j                  ||       }|jT                  dk(  ry|dk(  r|jV                  dk7  r| j                  j*                  t        d      | jX                  rWt#        | jX                        t#        |jV                        k\  r,t        j                  |jV                  |j                        }n.t        j                  |jV                  d   |j                        }||d<   |}|j                  }t[        j\                  |j_                  |            }|ja                  |      D ],  }| jb                  je                  ||||| jf                         . y# t        t        f$ re 	 t        j                  |D cg c]  }t        j                  ||       nc c}w c}| j                        }n# t        t        f$ r Y nw xY wY 2w xY wc c}w c c}w c c}w c c}w )z Write to the HDF5 dataset from a Numpy array.

        NumPy's broadcasting rules are honored, for "simple" indexing
        (slices and integers).  For advanced indexing, the shapes must
        match.
        c              3   B   K   | ]  }t        |t              s|  y wr%   r\  r3  s     r*   r+   z&Dataset.__setitem__.<locals>.<genexpr>  r]  r^  c              3   B   K   | ]  }t        |t              r|  y wr%   r\  r3  s     r*   r+   z&Dataset.__setitem__.<locals>.<genexpr>  s     ?1Jq#,>Q?r^  Nr/   r   )r9   r0   r   OVzNo such field for indexing: %sTFrb  )r0   rc  Szutf-8)rc  r0   zBWhen writing to array types, last N dimensions have to match (got z, but should be )z1Illegal slicing argument (not a compound dataset)z, c              3   &   K   | ]	  }d |z    yw)z"%s"Nr&   r3  s     r*   r+   z&Dataset.__setitem__.<locals>.<genexpr>  s     $@!VAX$@s   z8Illegal slicing argument (fields %s not in dataset type)r`  r&   z5Scalar broadcasting is not supported for array dtypes.rd  )4r7   r;   r   check_vlen_dtyper0   bytesr   rB   r   r<   r8   rO   r   emptyr9   r   r   r   ravelr   ndarraysubdtyper   r   baseviewrL   rN   r   r   encoderC   joinrF   COMPOUNDget_sizeinsert_er   r  rR  rp  rq  rr  rb   r   r\   expand_shape	broadcastrA   r]   r   )r   r   valr   vlenr$  tmprP  r0   cast_compound	str_arrayr   dtshpvalshprs  ro  mismatchsubtype
fieldnames	fieldnamer0  rx  val2ry  rw  s                             r*   __setitem__zDataset.__setitem__  s    "$.tTG <<<??? ##DJJ/UCL 8mmCt4 syy 88a<++CIIcrN&IC14 3B0#))B-@2 &Aa &CIIKN  ++tfF;C CFZZ__#::??c!#u}}-31F::&5zQ4::#4#4#@Qx4::#4#44$%Ea%PQQ

))%(3A6 $

 %--5::SAChhu{{U1Xu,=+>?@kk#)),NS^c%++>N-N"OPjjoo$))$**5>>'I!#&#- cFCI+++4>>&'!ZZ!!(!9  $C7TZZ__B--3b9C ::***%%a(CYYCyz*F}  ~D  FI  !K  L  LMM%++syy#.>"?@EYYqSYYC!89F Z1_YYF zz  ( STT#(GaATZZ5F5F,FGHG8}!99$@x$@@ ![^f!fgg 5zQ399#3#3#;--		2

3<<1A1A1CDTWWU1X.7; *-GAAJaG
G

3<<1C1CD!+ FI!mmCII,<,<Y,G,JKG YY--i8;FLL!3VWEF YYFE JJtzz4>	!" R<I11R7zz"". WXX{{ 4	@U@U8V V{{9#8#8		J{{9#8#8#<CIINDICYYF ""9#9#9&#AB))&1 	GFGGMM&&#u4::MF	GE 	* ++03'5+, (-{{1D'A '5 '5<@JJHC"I. 	&H4 H Hsf   *a   	b7(b<:cc	cc b4b#b
bb4b-*b4,b--b43b4c                    t         5  | j                  rt        d      | t        j                  | j
                        }n!t        j                  | j
                  ||       }|j                  }| t        j                  |j
                        }n t        j                  |j
                  |      }|j                  |j                        D ]+  }| j                  j                  |||| j                         - 	 ddd       y# 1 sw Y   yxY w)z Read data directly from HDF5 into an existing NumPy array.

        The destination array must be C-contiguous and writable.
        Selections must be the output of numpy.s_[<args>].

        Broadcasting is supported for simple indexing.
        z+Empty datasets have no numpy representationNrd  )r   r  r8   rR  SimpleSelectionr9   rp  rA   r  rr  rf  r   )r   destr   dest_selrw  ry  s         r*   read_directzDataset.read_direct'  s      	D~~ MNN! 00<
 ZZ

JE
]]F..tzz:::djj(;",,Z-C-CD DVVT

CD	D 	D 	D   C2DDc                    t         5  | j                  rt        d      | t        j                  |j
                        }n t        j                  |j
                  |      }|j                  }| t        j                  | j
                        }n!t        j                  | j
                  ||       }|j                  |j                        D ]+  }| j                  j                  |||| j                         - 	 ddd       y# 1 sw Y   yxY w)z Write data directly to HDF5 from a NumPy array.

        The source array must be C-contiguous.  Selections must be
        the output of numpy.s_[<args>].

        Broadcasting is supported for simple indexing.
        z#Empty datasets cannot be written toNrd  )r   r  r8   rR  r  r9   rp  rA   r  rr  r]   r   )r   sourcer   r  ry  rw  s         r*   write_directzDataset.write_direct@  s      	G~~ EFF! 00>
 ZZjA
]]F..tzz:::djj(DA",,Z-C-CD Gfff4::FG	G 	G 	Gr  c                     |du rt        d|d      t        j                  | j                  || j                  n|      }| j
                  dk(  r|S | j                  |       |S )z Create a Numpy array containing the whole dataset.  DON'T THINK
        THIS MEANS DATASETS ARE INTERCHANGEABLE WITH ARRAYS.  For one thing,
        you have to read the whole dataset every time this method is called.
        Fz Dataset.__array__ received copy=r   r/   r   )r<   rB   r7  r9   r0   r   r  )r   r0   r   r9  s       r*   r   zDataset.__array__Y  sp     5=3dW 5B B  kk$**%-DJJUS 99>J
r{   c                     | sy| j                   d}n8t        j                  t        j                  | j                               }d| d}d| d| j                   d| j
                  j                   dS )Nz<Closed HDF5 dataset>z(anonymous)"z<HDF5 dataset z: shape z, type "z">)ra   ppbasenamenormpathr9   r0   r   r   ra   s     r*   __repr__zDataset.__repr__m  sh    *99 D;;r{{49956DtfA;DvXdjj\$**..AQQSTTr{   refreshc                 l    | j                   j                          | j                  j                          y)z Refresh the dataset metadata by reloading from the file.

            This is part of the SWMR features and only exist when the HDF5
            library version >=1.9.178
            N)_idr  r  clearr   s    r*   r  zDataset.refresh{  s&     HH##%r{   flushc                 8    | j                   j                          y)a    Flush the dataset data and metadata to the file.
            If the dataset is chunked, raw data chunks are written to the file.

            This is part of the SWMR features and only exist when the HDF5
            library version >=1.9.178
            N)r  r  r   s    r*   r  zDataset.flush  s     HHNNr{   c                 X    | j                   j                         t        j                  k(  S )z"Check if this is a virtual dataset)r  r  r
   VIRTUALr   s    r*   
is_virtualzDataset.is_virtual  s      ::((*ckk99r{   c                 0   | j                   st        d      | j                  }t        |j	                               D cg c]L  }t        |j                  |      |j                  |      |j                  |      |j                  |            N c}S c c}w )z5Get a list of the data mappings for a virtual datasetzNot a virtual dataset)
r  RuntimeErrorr  r   get_virtual_countr   get_virtual_vspaceget_virtual_filenameget_virtual_dsetnameget_virtual_srcspace)r   r5   js      r*   virtual_sourceszDataset.virtual_sources  s     ??"#:;;::D t5578:
 	 t..q10030030035: : :s   ABc                 b    t        j                  | j                  | j                  |             y)zMake this dataset an HDF5 dimension scale.

        You can then attach it to dimensions of other datasets like this::

            other_ds.dims[0].attach_scale(ds)

        You can optionally pass a name to associate with this scale.
        N)r   	set_scaler  r  r  s     r*   
make_scalezDataset.make_scale  s     	txx/r{   c                 @    t        j                  | j                        S )zhReturn ``True`` if this dataset is also a dimension scale.

        Return ``False`` otherwise.
        )r   is_scaler  r   s    r*   r  zDataset.is_scale  s     }}TXX&&r{   )Nr   r%   r   ) ):r   r   r   r   r   r   r   MPIr   r   r   r  r   r9   setterr   r  r   r  r0   rb   rc   rg   rd   re   ri   rk   rf   rh   r   r<  r  r  r#  r   r
  r   r   r   rS  rY  r   r  r  r  r   r  hasattrr
   rC  r  r  r   r  r  r  r  r   r   s   @r*   r   r     s   '(82 -1 5 			+ 
 
	+ * *      \\      * *              9  9 *  * -  -       F  F    <  < - - * * / / ). 	 	%@  
 	 	 ( (  
 
 g gR VG VGpD2G2  & 
U 
U s}}i(		& 
	& s}}g&		 
	 			: 
 
	: 

	: 

	: 	0 	0 '  'r{   r   )NNNNNNNNNNNNFNNNNNNFNNN)NNNNNN)7r   	posixpathr  rK  abcr   r   rB   r  r   r   r   r	   r
   r   r   r   r   r  r   r   r   r   r   r   r   r   r   r   rR  r   rm  datatyper   compatr   vdsr   r   	frozensetr   rD   
get_configmpir  rz   r   r   r   r   r   r   r   r   r   r&   r{   r*   <module>r     s    
 #  A A A     !  # $ )%) 4 bmmo CG9=CG@ECGOT>BP !Pf JN:>:&B3 &BP@ @$2 26 4	M B B$G GTK'h K'r{   