
     iL-                         d Z ddlZddlZddlZddlmZ ddlmZ d Z	 G d d	ej
                  Z G d
 dej                  Z G d dej                  ZdS )a	  
XVG auxiliary reader --- :mod:`MDAnalysis.auxiliary.XVG`
========================================================

xvg files are produced by Gromacs during simulation or analysis, formatted
for plotting data with Grace.

Data is column-formatted; time/data selection is enabled by providing column
indices.

Note
----
By default, the time of each step is assumed to be stored in the first column,
in units of ps.


.. autoclass:: XVGStep
   :members:


XVG Readers
-----------
The default :class:`XVGReader` reads and stores the full contents of the .xvg
file on initialisation, while a second reader (:class:`XVGFileReader`) that
reads steps one at a time as required is also provided for when a lower memory
footprint is desired.

Note
----
Data is assumed to be time-ordered.

Multiple datasets, separated in the .xvg file by '&', are currently not
supported (the readers will stop at the first line starting '&').


.. autoclass:: XVGReader
   :members:

.. autoclass:: XVGFileReader
   :members:


.. autofunction:: uncomment

    N   )base   )anyopenc              #      K   | D ]d}|                                 }|s|d         dk    r&|                    d          }|dk    r|d|         r|d|         V  X|dk     r|r|V  edS )zRemove comments from lines in an .xvg file

    Parameters
    ----------
    lines : list of str
        Lines as directly read from .xvg file

    Yields
    ------
    str
        The next non-comment line, with any trailing comments removed
    r   @#N)stripfind)lineslinestripped_linecomment_positions       b/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/auxiliary/XVG.py	uncommentr   L   s           

 	s""(--c22aM2C3C2C$D 1!1 122222!!m!       c                   0     e Zd ZdZd fd	Zd Zd Z xZS )XVGStepa  AuxStep class for .xvg file format.

    Extends the base AuxStep class to allow selection of time and
    data-of-interest fields (by column index) from the full set of data read
    each step.

    Parameters
    ----------
    time_selector : int | None, optional
        Index of column in .xvg file storing time, assumed to be in ps. Default
        value is 0 (i.e. first column).
    data_selector : list of int | None, optional
        List of indices of columns in .xvg file containing data of interest to
        be stored in ``data``. Default value is ``None``.
    **kwargs
        Other AuxStep options.

    See Also
    --------
    :class:`~MDAnalysis.auxiliary.base.AuxStep`
    r   Nc                 N     t          t          |           j        d||d| d S )N)time_selectordata_selector )superr   __init__)selfr   r   kwargs	__class__s       r   r   zXVGStep.__init__   sE    %gt% 	
'}	
 	
HN	
 	
 	
 	
 	
r   c                     |d S t          |t          j                  r|                     |          S t	          d          )Nz"Time selector must be single index)
isinstancenumbersIntegral_select_data
ValueError)r   keys     r   _select_timezXVGStep._select_time   sE    ;Fc7+,, 	C$$S)))ABBBr   c                     |d S t          |t          j                  rF	  j        |         S # t          $ r+ | dt           j                   d}t          |          d w xY wt          j         fd|D                       S )Nz! not a valid index for data with z columnsc                 :    g | ]}                     |          S r   )r"   ).0ir   s     r   
<listcomp>z(XVGStep._select_data.<locals>.<listcomp>   s'    ???aT..q11???r   )	r   r    r!   _data
IndexErrorlenr#   nparray)r   r$   errmsgs   `  r   r"   zXVGStep._select_data   s    ;Fc7+,, 
	A3z#& 3 3 3 1 14:1 1 1  !((d23 8????3???@@@s	   . 5A#)r   N)__name__
__module____qualname____doc__r   r%   r"   __classcell__r   s   @r   r   r   j   so         ,
 
 
 
 
 

C C CA A A A A A Ar   r   c                   B     e Zd ZdZdZeZ fdZd Zd Z	d Z
d Z xZS )	XVGReadera  Auxiliary reader to read data from an .xvg file.

    Default reader for .xvg files. All data from the file will be read and
    stored on initialisation.

    Parameters
    ----------
    filename : str
        Location of the file containing the auxiliary data.
    **kwargs
       Other AuxReader options.

    See Also
    --------
    :class:`~MDAnalysis.auxiliary.base.AuxReader`

    Note
    ----
    The file is assumed to be of a size such that reading and storing the full
    contents is practical.
    XVGc           	         t           j                            |          | _        t	          |          5 }|                                }d d d            n# 1 swxY w Y   g }t          t          |                    D ]\  }}|                                d         dk    r n|	                    d |
                                D                        t          ||                   t          |d                   k    r0t          d                    |||         |d                             t          j        |          | _        t          | j                  | _         t%          t&          |           j        di | d S )Nr   &c                 ,    g | ]}t          |          S r   float)r(   vals     r   r*   z&XVGReader.__init__.<locals>.<listcomp>   s    "F"F"F#5::"F"F"Fr   z'Step {0} has {1} columns instead of {2}r   )ospathabspath_auxdatar   	readlines	enumerater   lstripappendsplitr-   r#   formatr.   r/   _auxdata_values_n_stepsr   r8   r   )	r   filenamer   xvg_filer   auxdata_valuesr)   r   r   s	           r   r   zXVGReader.__init__   s   11X 	)(&&((E	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 5!1!122 
	 
	GAt{{}}Q3&&!!"F"F"F"F"FGGG>!$%%^A->)?)???  &N1$5~a7HII   @
  "x77D011'i'11&11111s   AAAc                     | j         j        S N)rJ   nbytesr   s    r   _memory_usagezXVGReader._memory_usage   s    #**r   c                     | j         }| j        dz   }|| j        k     r| j        |         |_        ||_        |S |                                  t          )a  Read next auxiliary step and update ``auxstep``.

        Returns
        -------
        AuxStep object
            Updated with the data for the new step.

        Raises
        ------
        StopIteration
            When end of auxiliary data set is reached.
        r   )auxstepstepn_stepsrJ   r+   rewindStopIteration)r   rU   new_steps      r   _read_next_stepzXVGReader._read_next_step   sQ     ,9q=dl"" 0:GM#GLNKKMMMr   c                     || j         k    s|dk     r(t          d                    || j                             |dz
  | j        _        |                                  | j        S )a-  Move to and read i-th auxiliary step.

        Parameters
        ----------
        i: int
            Step number (0-indexed) to move to

        Returns
        -------
        :class:`XVGStep`

        Raises
        ------
        ValueError
            If step index not in valid range.
        r   z:Step index {0} is not valid for auxiliary (num. steps {1})r   )rW   r#   rI   rU   rV   next)r   r)   s     r   _go_to_stepzXVGReader._go_to_step   sf    " A##)6!T\#:#:   E		|r   c                 .    | j         dd| j        f         S )zGet NumPy array of time at each step.

        Returns
        -------
        NumPy array of float
            Time at each step.
        N)rJ   r   rR   s    r   read_all_timeszXVGReader.read_all_times  s     #AAAt'9$9::r   )r1   r2   r3   r4   rI   r   _Auxstepr   rS   r[   r^   r`   r5   r6   s   @r   r8   r8      s         , FH2 2 2 2 2*+ + +     .  4; ; ; ; ; ; ;r   r8   c                   <     e Zd ZdZdZeZ fdZd Zd Z	d Z
 xZS )XVGFileReadera/  Auxiliary reader to read (one step at a time) from an .xvg file.

    An alternative XVG reader which reads each step from the .xvg file as
    needed (rather than reading and storing all from the start), for a lower
    memory footprint.

    Parameters
    ----------
    filename : str
       Location of the file containing the auxiliary data.
    **kwargs
       Other AuxReader options.

    See Also
    --------
    :class:`~MDAnalysis.auxiliary.base.AuxFileReader`


    Note
    ----
    The default reader for .xvg files is :class:`XVGReader`.
    zXVG-Fc                 J     t          t          |           j        |fi | d S rP   )r   rc   r   )r   rL   r   r   s      r   r   zXVGFileReader.__init__)  s.    +mT""+H???????r   c           	         t          | j                  }	 |r2|                                r9|                                d         dk    r|                                  t          t          |g          D ]}| j        }| j        dz   |_        d |                                D             |_	        	 |j
         n)# t          $ r t          |j	                  |_
        Y nw xY wt          |j	                  |j
        k    r4t          d| j         dt          |j	                   d|j
                   |c S t          | j                  }7)	a'  Read next recorded step in xvg file and update ``auxstep``.

        Returns
        -------
        AuxStep object
            Updated with the data for the new step.

        Raises
        ------
        StopIteration
            When end of file or end of first data set is reached.
        Tr   r;   r   c                 ,    g | ]}t          |          S r   r=   )r(   r)   s     r   r*   z1XVGFileReader._read_next_step.<locals>.<listcomp>E  s     G G Gaq G G Gr   zStep z has z columns instead of )r]   auxfiler
   rX   rY   r   rU   rV   rH   r+   _n_colsAttributeErrorr-   r#   )r   r   uncommentedrU   s       r   r[   zXVGFileReader._read_next_step,  si    DL!!	& $DJJLL $TZZ\\!_-C-C ##($00  ,#y1} G G;3D3D3F3F G G G9OOO% 9 9 9&)'-&8&8GOOO9 w}%%88$0	 0 0w}--0 0%o0 0  
 %%D5	&s   1B99#CCc                    | j         s_	 t          | j                  S # t          $ r= |                                 | _        t          |                                           cY S w xY w|                                  d}| D ]}|dz   }|S )zIterate through all steps to count total number.

        Returns
        -------
        int
            Total number of steps
        r   r   )constant_dtr-   _timesri   r`   _restart)r   countrV   s      r   _count_n_stepszXVGFileReader._count_n_stepsV  s      	24;'''! 2 2 2 #11334..0011111	2 MMOOOE " "	Ls    AA$#A$c                     |                                   g }| D ]}|                    | j                   t          j        |          S )zIterate through all steps to build times list.

        Returns
        -------
        NumPy array of float
            Time of each step
        )rn   rG   timer.   r/   )r   timesrV   s      r   r`   zXVGFileReader.read_all_timeso  sJ     	 	$ 	$DLL####xr   )r1   r2   r3   r4   rI   r   ra   r   r[   rp   r`   r5   r6   s   @r   rc   rc     s         . FH@ @ @ @ @(& (& (&T  2      r   rc   )r4   r    r@   numpyr.    r   lib.utilr   r   AuxStepr   	AuxReaderr8   AuxFileReaderrc   r   r   r   <module>rz      s  0, ,Z  				                     <3A 3A 3A 3A 3Adl 3A 3A 3Alk; k; k; k; k; k; k; k;\m m m m mD& m m m m mr   