
     il                     v    d Z ddlmZmZmZmZ ddlZddlZddlZddl	Z
ddlmZ ddlmZ  G d de          ZdS )z
mrcmemmap
---------

Module which exports the :class:`MrcMemmap` class.

Classes:
    :class:`MrcMemmap`: An MrcFile subclass that uses a memory-mapped data
    array.

    )absolute_importdivisionprint_functionunicode_literalsN   )MrcFilec                   F     e Zd ZdZd Z fdZd Zd Zd Zd Z	d Z
 xZS )		MrcMemmapa  MrcFile subclass that uses a :class:`numpy memmap array <numpy.memmap>`
    for the data.
    
    Using a memmap means that the disk access is done lazily: the data array
    will only be read or written in small chunks when required. To access the
    contents of the array, use the array slice operator.
    
    Usage is the same as for :class:`~mrcfile.mrcfile.MrcFile`.
    
    Note that memmap arrays use a fairly small chunk size and so performance
    could be poor on file systems that are optimised for infrequent large I/O
    operations.
    
    If required, it is possible to create a very large empty file by creating a
    new MrcMemmap and then calling :meth:`_open_memmap` to create the memmap
    array, which can then be filled slice-by-slice. Be aware that the contents
    of a new, empty memmap array depend on your platform: the data values
    could be garbage or zeros.
    
    c                 L    d                     | j        j        | j                  S )NzMrcMemmap('{0}', mode='{1}'))format	_iostreamname_modeselfs    [/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/mrcfile/mrcmemmap.py__repr__zMrcMemmap.__repr__4   s)    -44T^5H59ZA A 	A    c                 >   | j         j        }t          t          |                               |           |j        |k    r| j                                        }|                                  || _         |j        | j        _	        | j        j        |j        z   }||j        z   }|j        dk    r|t          j        z  dk    r|dz  }| j                            |           |                     |j        |j                   t#          j        | j        |           dS dS )a[  Replace the file's extended header.
        
        Note that the file's entire data block must be moved if the extended
        header size changes. Setting a new extended header can therefore be
        very time consuming with large files, if the new extended header
        occupies a different number of bytes than the previous one.
        r   r   N)_extended_headernbytessuperr
   set_extended_header_datacopy_close_dataheadernsymbtmmapALLOCATIONGRANULARITYr   truncate_open_memmapdtypeshapenpcopyto)r   extended_headerold_ext_header_size	data_copyheader_nbytestotal_nbytes	__class__s         r   r   zMrcMemmap.set_extended_header8   s    #3:i22?CCC!%888
))I$3D!!0!7DK K.1GGM(9+;;L 1$$8R)RVW)W)W!N##L111ioy???Idj),,,,, 98r   c                    | j         s| j                            d           | j                            | j                   | j                            | j                   | j                                         | j                                         | j                                         | j                            | j        j        t          j
                   dS dS )z4Flush the header and data arrays to the file buffer.r   N)
_read_onlyr   seekwriter   r'   flushr   r   osSEEK_CURr   s    r   r1   zMrcMemmap.flushS   s     	@N"""N  ---N  !5666 N  """JN  """ N
 12;?????	@ 	@r   c                 B   	 t          j        | j                  }nT# t          $ rG}| j        r:t          j        d                    |          t                     d| _	        Y d}~dS  d}~ww xY wt          j
        | j                  }|                     ||           dS )zRead the data block from the file.
        
        This method first calculates the parameters needed to read the data
        (block start position, endian-ness, file mode, array shape) and then
        opens the data as a numpy memmap array.
        z{0} - data block not readN)utilsdata_dtype_from_headerr   
ValueError_permissivewarningswarnr   RuntimeWarningr   data_shape_from_headerr"   )r   r#   errr$   s       r   
_read_datazMrcMemmap._read_datac   s    		0==EE 	 	 	 9@@EE,. . .!
	 ,T[99%'''''s    
A-;A('A((A-c                 n   | j         rdnd}| j        j        t          | j        j                  z   }| j                                         	 t          j        | j        ||||          | _	        nB# t          $ r5}| j        r"t          j        dt                     d| _	        n|Y d}~nd}~ww xY w| j        z|                                 }||z
  }| j        j        }|dk    r|t"          j        z  dk    rd}||k     r6d                    ||z
            }	t          j        |	t                     dS dS dS )	z:Open a new memmap array pointing at the file's data block.rzr+)r#   modeoffsetr$   zError opening memmapNr   r   z*MRC file is {0} bytes larger than expected)r.   r   r   intr   r   r1   r%   memmapr   	Exceptionr8   r9   r:   r;   data_get_file_sizer   r    r   )
r   r#   r$   acc_moder*   ex	file_sizeremaining_file_size	data_sizemsgs
             r   r"   zMrcMemmap._open_memmapy   so   /333t *S1C-D-DD	4>).(0*7).	0 0 0DJJ
  	 	 	 4nEEE!

 



	 9 ++--I"+m";	(I A~~-$2L"LPQ"Q"Q	...C2Y>?? c>22222 !  /.s   #A0 0
B/:+B**B/c                 z    | j         3| j                                          d| j         j        _        d| _         dS dS )a  Delete the existing memmap array, if it exists.
        
        The array is flagged as read-only before deletion, so if a reference to
        it has been kept elsewhere, changes to it should no longer be able to
        change the file contents.
        NF)r   r1   flags	writeabler   s    r   r   zMrcMemmap._close_data   s@     :!J).DJ&DJJJ "!r   c                    | j         j        t          | j         j                  z   |j        z   }| j                            |           |                     |j        |j                   t          j
        | j        |d           dS )zbOverride of :meth:`_set_new_data` to handle opening a new memmap and
        copying data into it.no)castingN)r   r   rC   r   r   r!   r"   r#   r$   r%   r&   r   )r   rF   rJ   s      r   _set_new_datazMrcMemmap._set_new_data   sv     K&T[-?)@)@@4;N		***$*dj111
	$*dD111111r   )__name__
__module____qualname____doc__r   r   r1   r>   r"   r   rT   __classcell__)r,   s   @r   r
   r
      s         *A A A- - - - -6@ @ @ ( ( (,$3 $3 $3L
 
 
	2 	2 	2 	2 	2 	2 	2r   r
   )rX   
__future__r   r   r   r   r   r2   r9   numpyr%   mrcfile.utilsr5   mrcfiler   r
    r   r   <module>r_      s   
 
* * * * * * * * * * * *  				                 W2 W2 W2 W2 W2 W2 W2 W2 W2 W2r   