
     i(                     6    d Z ddlZddlZ G d de          ZdS )a  :mod:`mrc` --- the MRC/CCP4 volumetric data format
===================================================

.. versionadded:: 0.7.0

Reading of MRC/CCP4 volumetric files (`MRC2014 file format`_) using
the mrcfile_ library [Burnley2017]_.

.. _mrcfile: https://mrcfile.readthedocs.io/
.. _`MRC2014 file format`: http://www.ccpem.ac.uk/mrc_format/mrc2014.php


References
----------

.. [Burnley2017] Burnley T, Palmer C and Winn M (2017) Recent
                 developments in the CCP-EM software suite. *Acta
                 Cryst.* D73:469-477. doi: `10.1107/S2059798317007859`_


.. _`10.1107/S2059798317007859`: https://doi.org/10.1107/S2059798317007859

Classes
-------

.. autoclass:: MRC
   :members:


    Nc                   Z    e Zd ZdZd
dZddZed             Zed             Zd Z	d	 Z
dS )MRCa  Represent a MRC/CCP4 file.

    Load `MRC/CCP4 2014 <MRC2014 file format>`_ 3D volumetric data with
    the mrcfile_ library.

    Parameters
    ----------
    filename : str (optional)
       input file (or stream), can be compressed
    assume_volumetric : bool (optional)
      If ``False`` (default), check the file header to determine whether
      the data in `grid` is a 3D volume. If ``True``, assume `grid` is volumetric.

      .. versionadded:: 1.1.0
        

    Raises
    ------
    ValueError
       If the unit cell is not orthorhombic or if the data
       are not volumetric.


    Attributes
    ----------
    header : numpy.recarray
       Header data from the MRC file as a numpy record array.

    array : numpy.ndarray
       Data as a 3-dimensional array where axis 0 corresponds to X,
       axis 1 to Y, and axis 2 to Z. This order is always enforced,
       regardless of the order in the mrc file.

    delta : numpy.ndarray
       Diagonal matrix with the voxel size in X, Y, and Z direction
       (taken from the :attr:`mrcfile.mrcfile.voxel_size` attribute)

    origin : numpy.ndarray
       numpy array with coordinates of the coordinate system origin
       (computed from :attr:`header.origin`, the offsets
       :attr:`header.origin.nxstart`, :attr:`header.origin.nystart`,
       :attr:`header.origin.nzstart` and the spacing :attr:`delta`)

    rank : int
       The integer 3, denoting that only 3D maps are read.


    Notes
    -----
    * Only volumetric (3D) densities are read.
    * Only orthorhombic unitcells supported (other raise :exc:`ValueError`)
    * Reading and writing are supported.


    .. versionadded:: 0.7.0

    NFc                 J    || _         ||                     ||           d S d S )N)assume_volumetric)filenameread)selfr   r   s      V/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/gridData/mrc.py__init__zMRC.__init__c   s4     IIh2CIDDDDD      c                    ||| _         t          j        |          5 }|r'|j        duot	          |j        j                  dk    }n|                                }|s"t          d                    |                    |j	        
                                x| _	        }t          j        |j        j        |j        j        |j        j        gg d          sBt          d                    |j        j        |j        j        |j        j                            t          j        |j        |j        |j        g          }t          j        |          }t          j        |ddd                   }t          j        |j        |          | _        t          j        t          j        |j        j        |j        j        |j        j        g                    | _        t          j        |j        |j        |j         g          |         t          j        | j                  z  }	t          j        |j!        j        |j!        j        |j!        j        g          |	z   | _!        d| _"        ddd           dS # 1 swxY w Y   dS )z8Populate the instance from the MRC/CCP4 file *filename*.N   z(MRC file {} is not a volumetric density.)Z   r   r   zWOnly orthorhombic unitcells are currently supported, not alpha={0}, beta={1}, gamma={2}axes)#r   mrcfileopendatalenshape	is_volume
ValueErrorformatheadercopynpallclosecellbalphabetagammahstackmapcmaprmapsargsort	transposearraydiag
voxel_sizexyzdeltanxstartnystartnzstartoriginrank)
r	   r   r   mrcr   h
axes_orderaxes_c_ordertranspose_orderoffsetss
             r
   r   zMRC.readh   sk   $DM\(## %	s  ,HD0MS5H5HA5M		MMOO	 Q >EEhOOQ Q Q!joo///DK!;qw|QW]K+||- - R  "BBH&%&W]AGL!'-CQ CQR R R AFAFAF#;<<J:j11L jDDbD)9::Och_EEEDJ3>+;S^=Ms~O_*`!a!abbDJ iAIqy ABB<PSUSZ[_[eSfSffG)QXZQXZ$HIIGSDKDIK%	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	 %	s   I	I44I8;I8c                     | j         j        S )zShape of the :attr:`array`)r)   r   r	   s    r
   r   z	MRC.shape   s     zr   c                 D      fdt           j                  D             S )zzEdges of the grid cells, origin at centre of 0,0,0 grid cell.

        Only works for regular, orthonormal grids.
        c                     g | ]U}j         ||f         t          j        j        |         d z             z  j        |         z   dj         ||f         z  z
  VS )   g      ?)r/   r   aranger   r3   ).0dr	   s     r
   
<listcomp>zMRC.edges.<locals>.<listcomp>   st     + + + 
1a4 29TZ]Q->#?#??A!$tz!Q$'7!78 + + +r   )ranger4   r<   s   `r
   edgesz	MRC.edges   s8    + + + +ty))+ + + 	+r   c                     | j         | j        fS )z=Return array data as (edges,grid), i.e. a numpy nD histogram.)r)   rE   r<   s    r
   histogramddzMRC.histogramdd   s    
DJ''r   c                 
   ||| _         t          | d          rj| j        }t          j        |j        |j        |j        g          }t          |j                  t          |j                  t          |j                  }}}nt          j	        g d          }d\  }}}d}t          j
        |ddd                   }t          j
        |          }t          j        | j	        |          }	|	                    t          j                  }	t          j        |d          5 }
|
                    |	           t          j        | j                                      t          j                  }t'          |          |
_        ||
j        _        ||
j        _        ||
j        _        |t          |j                  }t          |j                  }t          |j                  }t          j	        |j        j        |j        j        |j        j        gt          j                  }t          |j                  |
j        _        t          |j                  |
j        _        t          |j                  |
j        _        t          |d	          rxt?          |j         j                  |
j        j         _        t?          |j         j                  |
j        j         _        t?          |j         j                  |
j        j         _        t          |d
          rxt?          |j!        j"                  |
j        j!        _"        t?          |j!        j#                  |
j        j!        _#        t?          |j!        j$                  |
j        j!        _$        t          |d          rt          |j%                  |
j        _%        nt          j&        |dk              rtO          d|           t          j(        dt          j                  }t          t          j)        | j        d         |d         z                      }t          t          j)        | j        d         |d         z                      }t          t          j)        | j        d         |d         z                      }||
j        _        ||
j        _        ||
j        _        t?          |d                   |
j        j        _        t?          |d                   |
j        j        _        t?          |d                   |
j        j        _        |
*                                 ddd           dS # 1 swxY w Y   dS )a8  Write grid data to MRC/CCP4 file format.
        
        Parameters
        ----------
        filename : str
            Output filename for the MRC file
        
        Notes
        -----
        The data array should be in xyz order (axis 0=X, axis 1=Y, axis 2=Z).
        
        If the MRC object was created by reading an existing file, the original
        header information (including mapc, mapr, maps ordering) is preserved.
        Otherwise, standard ordering (mapc=1, mapr=2, maps=3) is used.
        
        
        .. versionadded:: 1.1.0
        Nr   )r?      r   r   r   T)	overwrite)dtypecellar   ispgr   z!Voxel size must be positive, got r   r?   rI   )+r   hasattrr   r   r#   r$   r%   r&   intr)   r'   r(   astypefloat32r   newset_datar*   r/   tupler+   r0   r1   r2   r3   r,   r-   r.   mxmymzfloatrL   r   r    r!   r"   rM   anyr   zerosroundupdate_header_stats)r	   r   r6   r7   r$   r%   r&   r9   inverse_transpose_orderdata_for_filer5   r+   r0   r1   r2   header_origin_xyzs                   r
   writez	MRC.write   s{   & $DM 4"" 		AAFAFAF#;<<J"16{{CKKQV$DD ))),,J&D$A *Z"%566"$*_"="= TZ6MNNN &,,RZ88 [T222 :	&cLL''' ,,33BJ??J":..CN #CJO"CJO"CJO }ai..ai..ai..$&Hahj!(*ahj-QY[Yc$d$d$d! #AD		
 #AD		
 #AD		
 1g&& :).qwy)9)9CJ$&).qwy)9)9CJ$&).qwy)9)9CJ$&1g&& B-217=-A-ACJ$*,1!',,?,?CJ$)-217=-A-ACJ$*1f%% 2&)!&kkCJO 6*/** W$%U%U%UVVV %'HQbj$A$A$A!bht{1~
1'EFFGGbht{1~
1'EFFGGbht{1~
1'EFFGG ")CJ!(CJ!(CJ #((9!(<"="=CJ"'(9!(<"="=CJ"'(9!(<"="=CJ ##%%%u:	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	& :	&s   #P*UU!U)NF)F)__name__
__module____qualname____doc__r   r   propertyr   rE   rG   r`    r   r
   r   r   (   s        8 8tE E E E
) ) ) )V     X  + + X+( ( (g& g& g& g& g&r   r   )rd   numpyr   r   objectr   rf   r   r
   <module>ri      sd    <     f& f& f& f& f&& f& f& f& f& f&r   