
     iu:                         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
mZ ddlmZ ddl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ZddZd ZddZddZddZddZdS )zz
load_functions
--------------

Module for top-level functions that open MRC files and form the main API of
the package.

    )absolute_importdivisionprint_functionunicode_literalsN   )utils)Bzip2MrcFile)MAP_IDMAP_ID_OFFSET_BYTES)FutureMrcFile)GzipMrcFile)MrcFile	MrcMemmapFc                     |dk    rt           }n9|dk    rt          }n+|"t          d                    |                    t          } || d|          }||                    |           |S )a  Create a new MRC file.
    
    Args:
        name: The file name to use, as a string or :class:`~pathlib.Path`.
        data: Data to put in the file, as a :class:`numpy array
            <numpy.ndarray>`. The default is :data:`None`, to create an empty
            file.
        compression: The compression format to use. Acceptable values are:
            :data:`None` (the default; for no compression), ``'gzip'`` or
            ``'bzip2'``.
            It's good practice to name compressed files with an appropriate
            extension (for example, ``.mrc.gz`` for gzip) but this is not
            enforced.
        overwrite: Flag to force overwriting of an existing file. If
            :data:`False` and a file of the same name already exists, the file
            is not overwritten and an exception is raised.
    
    Returns:
        An :class:`~mrcfile.mrcfile.MrcFile` object (or a
        subclass of it if ``compression`` is specified).
    
    Raises:
        :exc:`ValueError`: If the file already exists and overwrite is
            :data:`False`.
        :exc:`ValueError`: If the compression format is not recognised.

    Warns:
        RuntimeWarning: If the data array contains Inf or NaN values.
    gzipbzip2Nz Unknown compression format '{0}'w+mode	overwrite)r   r	   
ValueErrorformatr   set_data)namedatacompressionr   NewMrcmrcs         `/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/mrcfile/load_functions.pynewr!      s    < f					 ; &--/ / 	/ 
&DI
6
6
6CTJ    rc                 0   t           }t          |           } t          j                            |           rd|v r"t          d                    |                     t          j        | d          5 }|	                    t          t          t                    z             }ddd           n# 1 swxY w Y   |t          t                     d         t          k    r+|dd         dk    rt          }n|dd         dk    rt          } || |||          S )	a  Open an MRC file.
    
    This function opens both normal and compressed MRC files. Supported
    compression formats are: gzip, bzip2.
    
    It is possible to use this function to create new MRC files (using mode
    ``w+``) but the :func:`new` function is more flexible.
    
    This function offers a permissive read mode for attempting to open corrupt
    or invalid files. In permissive mode, :mod:`warnings` are issued instead of
    exceptions if problems with the file are encountered. See
    :class:`~mrcfile.mrcinterpreter.MrcInterpreter` or the
    :doc:`usage guide <../usage_guide>` for more information.
    
    Args:
        name: The file name to open, as a string or :class:`~pathlib.Path`.
        mode: The file mode to use. This should be one of the following: ``r``
            for read-only, ``r+`` for read and write, or ``w+`` for a new empty
            file. The default is ``r``.
        permissive: Read the file in permissive mode. The default is
            :data:`False`.
        header_only: Only read the header (and extended header) from the file.
            The default is :data:`False`.
    
    Returns:
        An :class:`~mrcfile.mrcfile.MrcFile` object (or a
        :class:`~mrcfile.gzipmrcfile.GzipMrcFile` object if the file is
        gzipped).
    
    Raises:
        :exc:`ValueError`: If the mode is not one of ``r``, ``r+`` or ``w+``.
        :exc:`ValueError`: If the file is not a valid MRC file and
            ``permissive`` is :data:`False`.
        :exc:`ValueError`: If the mode is ``w+`` and the file already exists.
            (Call :func:`new` with ``overwrite=True`` to deliberately overwrite
            an existing file.)
        :exc:`OSError`: If the mode is ``r`` or ``r+`` and the file does not
            exist.
    
    Warns:
        RuntimeWarning: If the file appears to be a valid MRC file but the data
            block is longer than expected from the dimensions in the header.
        RuntimeWarning: If the file is not a valid MRC file and ``permissive``
            is :data:`True`.
        RuntimeWarning: If the header's ``exttyp`` field is set to a known
            value but the extended header's size is not a multiple of the
            number of bytes in the corresponding dtype.
    wzFile '{0}' already exists; use a different name, or delete it first or call 'mrcfile.new()' with 'overwrite=True' to overwrite itrbN   s   s   BZ)r   
permissiveheader_only)r   strospathexistsr   r   ioopenreadr   lenr
   r   r	   )r   r   r(   r)   r   fstarts          r    r/   r/   L   sO   b Ft99D	w~~d &$;; AAGO O O WT4   	>AFF.V<==E	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> #f++6))RaRyK''$rre##%6$Tj)+ + + +s   10B--B14B1c                     t          | dd          5 }|j                                        }ddd           n# 1 swxY w Y   |S )a  Read an MRC file's data into a numpy array.

    This is a convenience function to read the data from an MRC file when there is no
    need for the file's header information. To read the headers as well, or if you need
    access to an :class:`~mrcfile.mrcfile.MrcFile` object representing the file, use
    :func:`mrcfile.open` instead.

    Args:
        name: The file name to read, as a string or :class:`~pathlib.Path`.

    Returns:
        A :class:`numpy array<numpy.ndarray>` containing the data from the file.
    r#   Tr   r(   N)r/   r   copy)r   r   r   s      r    r0   r0      s     
d	.	.	. #x}}              Ks   9= =c                     t          |           } d}|                     d          rd}n|                     d          rd}t          | |||          5 }|||_        ddd           dS # 1 swxY w Y   dS )a  Write a new MRC file.

    This is a convenience function to allow data to be quickly written to a file (with
    optional compression) using just a single function call. However, there is no
    control over the file's metadata except for optionally setting the voxel size. For
    more control, or if you need access to an :class:`~mrcfile.mrcfile.MrcFile` object
    representing the new file, use :func:`mrcfile.new` instead.

    Args:
        name: The file name to use, as a string or :class:`~pathlib.Path`. If the name
            ends with ``.gz`` or ``.bz2``, the file will be compressed using gzip or
            bzip2 respectively.
        data: Data to put in the file, as a :class:`numpy array
            <numpy.ndarray>`. The default is :data:`None`, to create an empty
            file.
        overwrite: Flag to force overwriting of an existing file. If
            :data:`False` and a file of the same name already exists, the file
            is not overwritten and an exception is raised.
        voxel_size: float | 3-tuple
            The voxel size to be written in the file header.

    Raises:
        :exc:`ValueError`: If the file already exists and overwrite is
            :data:`False`.

    Warns:
        RuntimeWarning: If the data array contains Inf or NaN values.
    Nz.gzr   z.bz2r   )r*   endswithr!   
voxel_size)r   r   r   r9   r   r   s         r    writer:      s    : t99DK}}U 	v		 	T4i	0	0 (C!'CN( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   
A**A.1A.c                 N    t          t          | ft          ||                    S )a  Open an MRC file asynchronously in a separate thread.

    This allows a file to be opened in the background while the main thread
    continues with other work. This can be a good way to improve performance if
    the main thread is busy with intensive computation, but will be less
    effective if the main thread is itself busy with disk I/O.

    Multiple files can be opened in the background simultaneously. However,
    this implementation is relatively crude; each call to this function will
    start a new thread and immediately use it to start opening a file. If you
    try to open many large files at the same time, performance will decrease as
    all of the threads attempt to access the disk at once. You'll also risk
    running out of memory to store the data from all the files.

    This function returns a :class:`~mrcfile.future_mrcfile.FutureMrcFile`
    object, which deliberately mimics the API of the
    :class:`~concurrent.futures.Future` object from Python 3's
    :mod:`concurrent.futures` module. (Future versions of this library might
    return genuine :class:`~concurrent.futures.Future` objects instead.)

    To get the real :class:`~mrcfile.mrcfile.MrcFile` object from a
    :class:`~mrcfile.future_mrcfile.FutureMrcFile`, call
    :meth:`~mrcfile.future_mrcfile.FutureMrcFile.result`. This will block until
    the file has been read and the :class:`~mrcfile.mrcfile.MrcFile` object is
    ready. To check if the :class:`~mrcfile.mrcfile.MrcFile` is ready without
    blocking, call :meth:`~mrcfile.future_mrcfile.FutureMrcFile.running` or
    :meth:`~mrcfile.future_mrcfile.FutureMrcFile.done`.

    Args:
        name: The file name to open, as a string or :class:`~pathlib.Path`.
        mode: The file mode (one of ``r``, ``r+`` or ``w+``).
        permissive: Read the file in permissive mode. The default is
            :data:`False`.

    Returns:
        A :class:`~mrcfile.future_mrcfile.FutureMrcFile` object.

    r5   )r   r/   dictr   r   r(   s      r    
open_asyncr>      s&    N w$:(N(N(NOOOr"   c                 &    t          | ||          S )a  Open a memory-mapped MRC file.
    
    This allows much faster opening of large files, because the data is only
    accessed on disk when a slice is read or written from the data array. See
    the :class:`~mrcfile.mrcmemmap.MrcMemmap` class documentation for more
    information.
    
    Because the memory-mapped data array accesses the disk directly, compressed
    files cannot be opened with this function. In all other ways, :func:`mmap`
    behaves in exactly the same way as :func:`open`. The
    :class:`~mrcfile.mrcmemmap.MrcMemmap` object returned by this function can
    be used in exactly the same way as a normal
    :class:`~mrcfile.mrcfile.MrcFile` object.
    
    Args:
        name: The file name to open, as a string or :class:`~pathlib.Path`.
        mode: The file mode (one of ``r``, ``r+`` or ``w+``).
        permissive: Read the file in permissive mode. The default is
            :data:`False`.
    
    Returns:
        An :class:`~mrcfile.mrcmemmap.MrcMemmap` object.
    r5   r   r=   s      r    mmapr@      s    0 T<<<<r"   c           	         |D ]m}|t          j        t           j                  j        k    rDt	          d                    |t          j        t           j                  j                            nt          | d|          }||                    |           |||j        _	        t          j        |          }	|                    |	|           |                                 |
||j        d<   |                                 |S )ar	  Create a new, empty memory-mapped MRC file.

    This function is useful for creating very large files. The initial contents
    of the data array can be set with the ``fill`` parameter if needed, but be
    aware that filling a large array can take a long time.

    If ``fill`` is not set, the new data array's contents are unspecified and
    system-dependent. (Some systems fill a new empty mmap with zeros, others
    fill it with the bytes from the disk at the newly-mapped location.) If you
    are definitely going to fill the entire array with new data anyway you can
    safely leave ``fill`` as :data:`None`, otherwise it is advised to use a
    sensible fill value (or ensure you are on a system that fills new mmaps
    with a reasonable default value).

    Args:
        name: The file name to use, as a string or :class:`~pathlib.Path`.
        shape: The shape of the data array to open, as a 2-, 3- or 4-tuple of
            ints. For example, ``(nz, ny, nx)`` for a new 3D volume, or
            ``(ny, nx)`` for a new 2D image.
        mrc_mode: The MRC mode to use for the new file. One of 0, 1, 2, 4 or 6,
            which correspond to numpy dtypes as follows:

            * mode 0 -> int8
            * mode 1 -> int16
            * mode 2 -> float32
            * mode 4 -> complex64
            * mode 6 -> uint16

            The default is 0.
        fill: An optional value to use to fill the new data array. If
            :data:`None`, the data array will not be filled and its contents
            are unspecified. Numpy's usual rules for rounding or rejecting
            values apply, according to the dtype of the array.
        overwrite: Flag to force overwriting of an existing file. If
            :data:`False` and a file of the same name already exists, the file
            is not overwritten and an exception is raised.
        extended_header: The extended header object
        exttyp: The extended header type

    Returns:
        A new :class:`~mrcfile.mrcmemmap.MrcMemmap` object.

    Raises:
        :exc:`ValueError`: If the MRC mode is invalid.
        :exc:`ValueError`: If the file already exists and overwrite is
            :data:`False`.
        :exc:`ValueError`: If the extended header or any of the dimensions of
                the shape have a size greater than 2,147,483,647 (and
                therefore the size cannot be stored in the header).
    zPNew shape is too large! Found a dimension of size {}. The maximum allowed is {}.r   r   N.)npiinfoint32maxr   r   r   set_extended_headerheaderexttypr   dtype_from_mode_open_memmapupdate_header_from_datar   flush)
r   shapemrc_modefillr   extended_headerrH   dimr   dtypes
             r    new_mmaprS     s   f  C C"(##''' D$fS"(28*<*<*@AAC C C (
 Dty
9
9
9C "000"
!(++EUE"""!!!IIKKKJr"   )NNF)r#   FF)NFN)r#   F)r   NFNN) __doc__
__future__r   r   r   r   r.   r+   numpyrB    r   bzip2mrcfiler	   	constantsr
   r   future_mrcfiler   gzipmrcfiler   mrcfiler   	mrcmemmapr   r!   r/   r0   r:   r>   r@   rS    r"   r    <module>r_      s   * * * * * * * * * * * * 
			 				           & & & & & & 2 2 2 2 2 2 2 2 ) ) ) ) ) ) $ $ $ $ $ $                  * * * *ZF+ F+ F+ F+R  &%( %( %( %(P'P 'P 'P 'PT= = = =6H H H H H Hr"   