a
    =_)                     @   sT   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 dZ
G dd	 d	eZdS )
z!Here is defined the CArray class.    N   )Atom)Array)correct_byteorderSizeTypez1.1c                       s>   e Zd ZdZdZd fdd	Zdd	 Zd
d Zdd Z  Z	S )CArraya  This class represents homogeneous datasets in an HDF5 file.

    The difference between a CArray and a normal Array (see
    :ref:`ArrayClassDescr`), from which it inherits, is that a CArray
    has a chunked layout and, as a consequence, it supports compression.
    You can use datasets of this class to easily save or load arrays to
    or from disk, with compression support included.

    CArray includes all the instance variables and methods of Array.
    Only those with different behavior are mentioned here.

    Parameters
    ----------
    parentnode
        The parent :class:`Group` object.

        .. versionchanged:: 3.0
           Renamed from *parentNode* to *parentnode*.

    name : str
        The name of this node in its parent group.
    atom
       An `Atom` instance representing the *type* and *shape* of
       the atomic objects to be saved.

    shape
       The shape of the new array.

    title
       A description for this node (it sets the ``TITLE`` HDF5
       attribute on disk).

    filters
       An instance of the `Filters` class that provides
       information about the desired I/O filters to be applied
       during the life of this object.

    chunkshape
       The shape of the data chunk to be read or written in a
       single HDF5 I/O operation.  Filters are applied to those
       chunks of data.  The dimensionality of `chunkshape` must
       be the same as that of `shape`.  If ``None``, a sensible
       value is calculated (which is recommended).

    byteorder
        The byteorder of the data *on disk*, specified as 'little'
        or 'big'.  If this is not specified, the byteorder is that
        of the platform.

    track_times
        Whether time data associated with the leaf are recorded (object
        access time, raw data modification time, metadata change time, object
        birth time); default True.  Semantics of these times depend on their
        implementation in the HDF5 library: refer to documentation of the
        H5O_info_t data structure.  As of HDF5 1.8.15, only ctime (metadata
        change time) is implemented.

        .. versionadded:: 3.4.3

    Examples
    --------

    See below a small example of the use of the `CArray` class.
    The code is available in ``examples/carray1.py``::

        import numpy
        import tables

        fileName = 'carray1.h5'
        shape = (200, 300)
        atom = tables.UInt8Atom()
        filters = tables.Filters(complevel=5, complib='zlib')

        h5f = tables.open_file(fileName, 'w')
        ca = h5f.create_carray(h5f.root, 'carray', atom, shape,
                               filters=filters)

        # Fill a hyperslab in ``ca``.
        ca[10:60, 20:70] = numpy.ones((50, 50))
        h5f.close()

        # Re-open a read another hyperslab
        h5f = tables.open_file(fileName)
        print(h5f)
        print(h5f.root.carray[8:12, 18:22])
        h5f.close()

    The output for the previous script is something like::

        carray1.h5 (File) ''
        Last modif.: 'Thu Apr 12 10:15:38 2007'
        Object Tree:
        / (RootGroup) ''
        /carray (CArray(200, 300), shuffle, zlib(5)) ''

        [[0 0 0 0]
         [0 0 0 0]
         [0 0 1 1]
         [0 0 1 1]]

    ZCARRAYN Tc              	      s  || _ d | _d| _d | _|d u | _}|| _d| _|| _d | _d | _	d | _
d | _d | _d | _d | _d| _d | _|rlt|tstdt| |d u rtdzt|}W n" ty   tdt| Y n0 tdd |D | _|d urlzt|}W n$ ty   td	t| Y n0 t|t|krBtd
||f nt|dk rXtdtdd |D | _tt| ||||||	|
 d S )NTFzHatom parameter should be an instance of tables.Atom and you passed a %s.z"you must specify a non-empty shapez8`shape` parameter must be a sequence and you passed a %sc                 s   s   | ]}t |V  qd S Nr   .0s r   ,lib/python3.9/site-packages/tables/carray.py	<genexpr>       z"CArray.__init__.<locals>.<genexpr>z=`chunkshape` parameter must be a sequence and you passed a %sz7the shape (%s) and chunkshape (%s) ranks must be equal.r   z1chunkshape parameter cannot have zero-dimensions.c                 s   s   | ]}t |V  qd S r
   r   r   r   r   r   r      r   )atomshapeZextdim
_v_versionZ_v_new_v_new_title
_v_convert_v_chunkshapeZ_startZ_stopZ_stepZ
_nrowsreadZ_startbZ_stopbZ_rowZ_initZlistarr
isinstancer   
ValueErrortypetuple	TypeErrorlenminsuperr   __init__)selfZ
parentnodenamer   r   titlefilters
chunkshape	byteorder_logZtrack_timesnew	__class__r   r   r!      sd    


zCArray.__init__c                 C   s"   t | jdk rtd| | jS )z+Create a new array in file (specific part).r   z,shape parameter cannot have zero-dimensions.)r   r   r   _g_create_commonZnrows)r"   r   r   r   	_g_create   s
    zCArray._g_createc                 C   s   t | _| jdu r&| || j| jj| _|  | _| j	du rLt
| jjtj	| _	z| | j| _W n   | jdd  Y n0 | jS )z)Create a new array in file (common part).Nr   )flush)	obversionr   r   Z_calc_chunkshapeZrowsizer   sizeZ_calc_nrowsinbuf
nrowsinbufr'   r   r   sysZ_create_carrayr   Z_v_objectidclose)r"   Zexpectedrowsr   r   r   r,      s    


zCArray._g_create_commonc
              
   K   s  |  |||\}}}| j}t| j}tt|||||< | j}dd | jD }d| _t||| j	|||||	d}t|||| D ]j}|||  }||kr|}t
|||||< || | }|| }||| kr|| }| t||||< q~d| _tj| jtd| j	j }||fS )z2Private part of Leaf.copy() for each kind of leaf.c                 S   s   g | ]}t d |dqS )r   r   )slice)r   Zdimr   r   r   
<listcomp>  r   z-CArray._g_copy_with_stats.<locals>.<listcomp>F)r   r   r$   r%   r&   r(   T)Zdtype)Z_process_range_readmaindimlistr   r   ranger1   r   r   r   r4   __getitem__r   numpyZprodr   r0   )r"   groupr#   startstopstepr$   r%   r&   r(   kwargsr6   r   r1   ZslicesobjectZstart2Zstop2Zstart3Zstop3nbytesr   r   r   _g_copy_with_stats  s0    
zCArray._g_copy_with_stats)NNr   NNNTT)
__name__
__module____qualname____doc__Z
_c_classidr!   r-   r,   rB   __classcell__r   r   r*   r   r      s   g    Q	r   )rF   r2   r:   r   r   Zarrayr   Zutilsr   r   r/   r   r   r   r   r   <module>   s   