a
    /§»a»N  ã                   @   s  d Z ddlZddlZddlZddlmZmZmZ ddl	m
Z
 G dd„ dƒZedd	ƒZed
dƒZeddƒZeddƒZeddƒZG dd„ dƒZeddƒZeddƒZeddƒZeddƒZeddƒZeddƒZeddƒZeddƒZeddƒZedd ƒZeeeeeeeeeed!œ
ZG d"d#„ d#ƒZedd$ƒZedd%ƒZed&d'ƒZ edd(ƒZ!ed)d*ƒZ"ed+d,ƒZ#ed-d.ƒZ$edd/ƒZ%ed0d1ƒZ&ed2d3ƒZ'ed4d5ƒZ(ed6d7ƒZ)ed8d9ƒZ*ed:d;ƒZ+ed<d=ƒZ,ed>d?ƒZ-ed@dAƒZ.edBdCƒZ/edDdEƒZ0dFdG„ Z1dHdI„ Z2dJdK„ Z3G dLdM„ dMƒZ4G dNdO„ dOƒZ5dS )Pz;Low level Python library to export data to binary VTK file.é    Né   )ÚwriteBlockSizeÚwriteArrayToFileÚwriteArraysToFile)Ú	XmlWriterc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚVtkFileTypez’
    Wrapper class for VTK file types.

    Parameters
    ----------
    name : str
        Data name.
    ext : str
        File extension.
    c                 C   s   || _ || _d S ©N©ÚnameÚext)Úselfr
   r   © r   úD/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/pyevtk/vtk.pyÚ__init__4   s    zVtkFileType.__init__c                 C   s   d| j | jf S )NzName: %s  Ext: %s 
r	   ©r   r   r   r   Ú__str__8   s    zVtkFileType.__str__N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   (   s   r   Z	ImageDataz.vtiZPolyDataz.vtpZRectilinearGridz.vtrZStructuredGridz.vtsZUnstructuredGridz.vtuc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚVtkDataTypez‘
    Wrapper class for VTK data types.

    Parameters
    ----------
    size : int
        Size in byte.
    name : str
        Type name.
    c                 C   s   || _ || _d S r   )Úsizer
   )r   r   r
   r   r   r   r   P   s    zVtkDataType.__init__c                 C   s   d| j | jf S )NzType: %s  Size: %d 
)r
   r   r   r   r   r   r   T   s    zVtkDataType.__str__Nr   r   r   r   r   r   D   s   r   ZInt8ZUInt8é   ZInt16ZUInt16é   ZInt32ZUInt32é   ZInt64ÚUInt64ZFloat32ZFloat64)
Zint8Zuint8Úint16Zuint16Úint32Zuint32Úint64Zuint64Úfloat32Úfloat64c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚVtkCellTypez�
    Wrapper class for VTK cell types.

    Parameters
    ----------
    tid : int
        Type ID.
    name : str
        Cell type name.
    c                 C   s   || _ || _d S r   )Útidr
   )r   r#   r
   r   r   r   r      s    zVtkCellType.__init__c                 C   s
   d| j  S )NzVtkCellType( %s ) 
)r
   r   r   r   r   r   ƒ   s    zVtkCellType.__str__Nr   r   r   r   r   r"   s   s   r"   ZVertexZ
PolyVertexé   ZLineZPolyLineé   ZTriangleé   ZTriangleStripé   ZPolygonZPixelé	   ZQuadé
   ZTetraé   ZVoxelé   Z
Hexahedroné   ZWedgeé   ZPyramidé   ZQuadratic_Edgeé   ZQuadratic_Triangleé   ZQuadratic_Quadé   ZQuadratic_Tetraé   ZQuadratic_Hexahedronc                 C   sR   t | ƒt |ƒ  krdks"n J ‚d| d |d | d |d | d |d f }|S )Nr$   z%d %d %d %d %d %dr   r   r   )Úlen)ÚstartÚendÚstringr   r   r   Ú_mix_extentsŸ   s    "úr7   c                 C   s   d  dd„ | D ƒ¡}|S )NÚ c                 S   s   g | ]}t |ƒd  ‘qS )ú )Úrepr)Ú.0Únumr   r   r   Ú
<listcomp>­   ó    z$_array_to_string.<locals>.<listcomp>)Újoin)ÚaÚsr   r   r   Ú_array_to_string¬   s    rB   c                   C   s   t jdkrdS dS )NÚlittleZLittleEndianZ	BigEndian)ÚsysÚ	byteorderr   r   r   r   Ú_get_byte_order±   s    
rF   c                   @   s*   e Zd ZdZdd„ Zdd„ Zddd	„Zd
S )ÚVtkGroupz“
    Creates a VtkGroup file that is stored in filepath.

    Parameters
    ----------
    filepath : str
        filename without extension.
    c                 C   sL   t |d ƒ| _| j d¡ | jjddtƒ d� | j d¡ tj |¡| _d S )Nz.pvdÚVTKFileÚ
Collectionz0.1)ÚtypeÚversionÚ
byte_order)	r   ÚxmlÚopenElementÚaddAttributesrF   ÚosÚpathÚdirnameÚroot)r   Úfilepathr   r   r   r   Ä   s    ÿzVtkGroup.__init__c                 C   s&   | j  d¡ | j  d¡ | j  ¡  dS )zClose this VtkGroup.rI   rH   N)rM   ÚcloseElementÚcloser   r   r   r   ÚsaveÍ   s    zVtkGroup.saver8   Ú0c                 C   sB   t jj|| jd�}| j d¡ | jj|||||d� | j ¡  dS )aW  
        Add a file to this VTK group.

        Parameters
        ----------
        filepath : str
            full path to VTK file.
        sim_time : float
            simulated time.
        group : str, optional
            This attribute is not required;
            it is only for informational purposes.
            The default is "".
        part : int, optional
            It is an integer value greater than or equal to 0.
            The default is "0".

        Notes
        -----
        See: http://www.paraview.org/Wiki/ParaView/Data_formats#PVD_File_Format for details.
        )r4   ZDataSet)ZtimestepÚgroupÚpartÚfiler
   N)rP   rQ   ÚrelpathrS   rM   rN   rO   rU   )r   rT   Zsim_timerY   rZ   r
   Úfilenamer   r   r   ÚaddFileÓ   s    
ÿzVtkGroup.addFileN)r8   rX   r8   )r   r   r   r   r   rW   r^   r   r   r   r   rG   º   s   		rG   c                   @   sž   e Zd ZdZdd„ Zdd„ Zd%dd„Zd	d
„ Zd&dd„Zdd„ Z	d'dd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ ZdS )(ÚVtkFilea3  
    Class for a VTK file.

    Parameters
    ----------
    filepath : str
        filename without extension.
    ftype : str
        file type, e.g. VtkImageData, etc.
    largeFile : bool, optional
        If size of the stored data cannot be represented by a UInt32.
        The default is False.
    c                 C   sL   || _ ||j | _t| jƒ| _d| _d| _| j d¡j|j	dt
ƒ dd� d S )Nr   FrH   z1.0r   )rJ   rK   rL   Zheader_type)Úftyper   r]   r   rM   ÚoffsetÚappendedDataIsOpenrN   rO   r
   rF   )r   rT   r`   r   r   r   r     s    üzVtkFile.__init__c                 C   s   t j | j¡S )z"Return absolute path to this file.)rP   rQ   Úabspathr]   r   r   r   r   ÚgetFileName  s    zVtkFile.getFileNameNc	           
      C   sÐ   | j  d¡ |r.|r.t||ƒ}	| j j|	d� nž|rH|rH| j j||d� n„|s\|s\|s\|s\|rÄ|du rltdƒ}|du r|tdƒ}|du rŒtdƒ}|du rœtdƒ}|du r¬tdƒ}| j j|||||d� ndsÌJ ‚| S )ar  
        Open piece section.

        Parameters
        ----------
        start : array-like, optional
            array or list with start indexes in each direction.
            Must be given with end.
        end : array-like, optional
            array or list with end indexes in each direction.
            Must be given with start.
        npoints : int, optional
            number of points in piece
        ncells : int, optional
            number of cells in piece.
            If present, npoints must also be given.
        nverts : int, optional
            number of vertices.
        nlines : int, optional
            number of lines.
        nstrips : int, optional
            number of stripes.
        npolys : int, optional
            number of poly.

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.
        ÚPiece)ZExtent)ÚNumberOfPointsZNumberOfCellsNr   )rf   ZNumberOfVertsZNumberOfLinesZNumberOfStripsZNumberOfPolysF)rM   rN   r7   rO   Ústr)
r   r4   r5   ZnpointsZncellsZnvertsÚnlinesZnstripsZnpolysr   r   r   r   Ú	openPiece  s8    +
ÿûzVtkFile.openPiecec                 C   s   | j  d¡ dS )zClose Piece.re   N©rM   rU   r   r   r   r   Ú
closePiecea  s    zVtkFile.closePiecec                 C   sn   | j  |d ¡ |r"| j j|d� |r4| j j|d� |rF| j j|d� |rX| j j|d� |rj| j j|d� | S )aŸ  
        Open data section.

        Parameters
        ----------
        nodeType : str
            Either "Point", "Cell" or "Field".
        scalars : str, optional
            default data array name for scalar data.
        vectors : str, optional
            default data array name for vector data.
        normals : str, optional
            default data array name for normals data.
        tensors : str, optional
            default data array name for tensors data.
        tcoords : str, optional
            default data array name for tcoords data.

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.
        ÚData)ZScalars)ZVectors)ZNormals)ZTensors)ZTCoords)rM   rN   rO   )r   ÚnodeTypeZscalarsZvectorsZnormalsZtensorsZtcoordsr   r   r   ÚopenDatae  s     zVtkFile.openDatac                 C   s   | j  |d ¡ dS )zí
        Close data section.

        Parameters
        ----------
        nodeType : str
            "Point", "Cell" or "Field".

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.
        rl   Nrj   )r   rm   r   r   r   Ú	closeData“  s    zVtkFile.closeDatac                 C   s˜   | j j}| j |¡ |tjkr\|r.|r.|r.|s6ds6J ‚t||ƒ}| jj|t|ƒt|ƒd� n8|tjt	jfv r”|rt|s|ds|J ‚t||ƒ}| jj|d� | S )ae  
        Open grid section.

        Parameters
        ----------
        start : array-like, optional
            array or list of start indexes.
            Required for Structured, Rectilinear and ImageData grids.
            The default is None.
        end : array-like, optional
            array or list of end indexes.
            Required for Structured, Rectilinear and ImageData grids.
            The default is None.
        origin : array-like, optional
            3D array or list with grid origin.
            Only required for ImageData grids.
            The default is None.
        spacing : array-like, optional
            3D array or list with grid spacing.
            Only required for ImageData grids.
            The default is None.

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.
        F)ÚWholeExtentZOriginZSpacing)rp   )
r`   r
   rM   rN   ÚVtkImageDatar7   rO   rB   ÚVtkStructuredGridÚVtkRectilinearGrid)r   r4   r5   ÚoriginÚspacingZgTyper   r   r   r   ÚopenGrid£  s"    

ý
zVtkFile.openGridc                 C   s   | j  | jj¡ dS )z‡
        Close grid element.

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.
        N)rM   rU   r`   r
   r   r   r   r   Ú	closeGridÓ  s    	zVtkFile.closeGridc                 C   sX   t | }| j d¡ | jj|||jd| jd� | j ¡  |  j|| |j d 7  _| S )a0  
        Add data array description to xml header section.

        Parameters
        ----------
        name : str
            data array name.
        dtype : str
            data type.
        nelem : int
            number of elements in array.
        ncomp : int
            number of components, 1 (=scalar) and 3 (=vector).

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.

        Notes
        -----
        This is a low level function.
        Use addData if you want to add a numpy array.
        Z	DataArrayZappended)ÚNameZNumberOfComponentsrJ   Úformatra   r   )Ú	np_to_vtkrM   rN   rO   r
   ra   rU   r   )r   r
   ÚdtypeÚnelemÚncompr   r   r   Ú	addHeaderÞ  s    û
ÿzVtkFile.addHeaderc                 C   sš   t |tƒr:t|ƒdksJ ‚|d }|  ||jj|jd¡ n\t |tjƒrŠ|j	dksZ|j	dkrr|  ||jj|jd¡ q–ds–J dt
|jƒ ƒ‚nds–J dƒ‚dS )a›  
        Add array description to xml header section.

        Parameters
        ----------
        name : str
            data array name.
        data : array-like
            one numpy array or a tuple with 3 numpy arrays.
            If a tuple, the individual arrays must represent the components
            of a vector field.
            All arrays must be one dimensional or three-dimensional.
        r$   r   r   FzBad array shape: zArgument must be a Numpy arrayN)Ú
isinstanceÚtupler3   r~   r{   r
   r   ÚnpÚndarrayÚndimrg   Úshape)r   r
   ÚdataÚxr   r   r   ÚaddData  s    
zVtkFile.addDatac                 C   s0   |   ¡  t| j}|| | }t| jj|ƒ dS )a  
        Append size of data block to header.

        This function only writes the size of the data block
        that will be appended.
        The data itself must be written immediately after
        calling this function.

        Parameters
        ----------
        dtype : str
            string with data type representation (same as numpy).
            For example, 'float64'.
        nelem : int
            number of elements.
        ncomp : int
            number of components, 1 (=scalar) or 3 (=vector)..
        N)ÚopenAppendedDatarz   r   r   rM   Ústream)r   r{   r|   r}   ÚdsizeÚ
block_sizer   r   r   ÚappendHeader"  s    
zVtkFile.appendHeaderc           	      C   sî   |   ¡  t|tƒr†t|ƒ}|dks&J ‚|d jj}|d j}|| | }t| jj	|ƒ |d |d |d   }}}t
| jj	|||ƒ ndt|tjƒrâ|jdks¦|jdkrâd}|jj}|j}|| | }t| jj	|ƒ t| jj	|ƒ ndsêJ ‚| S )a~  
        Append data to binary section.

        This function writes the header section
        and the data to the binary file.

        Parameters
        ----------
        data : array-like
            one numpy array or a tuple with 3 numpy arrays.
            If a tuple, the individual
            arrays must represent the components of a vector field.
            All arrays must be one dimensional or three-dimensional.
            The order of the arrays must coincide with
            the numbering scheme of the grid.

        Returns
        -------
        VtkFile
            This VtkFile to allow chained calls.
        r$   r   r   r   F)rˆ   r   r€   r3   r{   Úitemsizer   r   rM   r‰   r   r�   r‚   rƒ   r   )	r   r…   r}   rŠ   r|   r‹   r†   ÚyÚzr   r   r   Ú
appendData<  s.    

ÿÿzVtkFile.appendDatac                 C   s*   | j s&| j d¡jdd� d¡ d| _ dS )z†
        Open binary section.

        It is not necessary to explicitly call this function
        from an external library.
        ÚAppendedDataÚraw)ÚencodingÚ_TN)rb   rM   rN   rO   ZaddTextr   r   r   r   rˆ   t  s    ÿþzVtkFile.openAppendedDatac                 C   s   | j  d¡ dS )z‡
        Close binary section.

        It is not necessary to explicitly call this function
        from an external library.
        r‘   Nrj   r   r   r   r   ÚcloseAppendedData�  s    zVtkFile.closeAppendedDatac                 C   s   | j  |¡ dS )zl
        Open an element.

        Useful to add elements such as: Coordinates, Points, Verts, etc.
        N)rM   rN   ©r   ZtagNamer   r   r   rN   Š  s    zVtkFile.openElementc                 C   s   | j  |¡ dS )zClose an element.Nrj   r–   r   r   r   rU   ’  s    zVtkFile.closeElementc                 C   s,   | j r| j d¡ | j d¡ | j ¡  dS )zClose file.r‘   rH   N)rb   rM   rU   rV   r   r   r   r   rW   –  s    zVtkFile.save)NNNNNNNN)NNNNN)NNNN)r   r   r   r   r   rd   ri   rk   rn   ro   rv   rw   r~   r‡   rŒ   r�   rˆ   r•   rN   rU   rW   r   r   r   r   r_   õ   s>           ÷
L     ù
.
0*8	r_   )6r   rD   rP   Únumpyr�   Zevtkr   r   r   rM   r   r   rq   ZVtkPolyDatars   rr   ZVtkUnstructuredGridr   ZVtkInt8ZVtkUInt8ZVtkInt16Z	VtkUInt16ZVtkInt32Z	VtkUInt32ZVtkInt64Z	VtkUInt64Z
VtkFloat32Z
VtkFloat64rz   r"   Z	VtkVertexZVtkPolyVertexZVtkLineZVtkPolyLineZVtkTriangleZVtkTriangleStripZ
VtkPolygonZVtkPixelZVtkQuadZVtkTetraZVtkVoxelZVtkHexahedronZVtkWedgeZ
VtkPyramidZVtkQuadraticEdgeZVtkQuadraticTriangleZVtkQuadraticQuadZVtkQuadraticTetraZVtkQuadraticHexahedronr7   rB   rF   rG   r_   r   r   r   r   Ú<module>   st   














ö


















	;