
     i(                     Z    d Z ddlZddlZddlZ G d de          Z G d de          ZdS )a;  
:mod:`gOpenMol` --- the gOpenMol plt format
===========================================

.. _gOpenMol: http://www.csc.fi/english/pages/g0penMol

The module provides a simple implementation of a reader for gOpenMol_
*plt* files. Plt files are binary files. The :class:`Plt` reader tries
to guess the endianess of the file, but this can fail (with a
:exc:`TypeError`); you are on your own in this case.

Only the reader is implemented. If you want to write gridded data use a format
that is more standard, such as OpenDX (see :mod:`OpenDX`).


Background
----------

gOpenMol http://www.csc.fi/english/pages/g0penMol plt format.

Used to be documented at http://www.csc.fi/gopenmol/developers/plt_format.phtml but currently this is only accessible through the internet archive at
http://web.archive.org/web/20061011125817/http://www.csc.fi/gopenmol/developers/plt_format.phtml



Grid data plt file format
-------------------------

Copyright CSC, 2005. Last modified: September 23, 2003 09:18:50

Plot file (plt) format The plot files are regular 3D grid files for plotting of
molecular orbitals, electron densities or other molecular properties. The plot
files are produced by several programs. It is also possible to format/unformat
plot files using the pltfile program in the utility directory. It is also
possible to produce plot files with external (own) programs. Produce first a
formatted text file and use then the pltfile program to unformat the file for
gOpenMol. The format for the plot files are very simple and a description of
the format can be found elsewhere in this manual. gOpenMol can read binary plot
files from different hardware platforms independent of the system type (little
or big endian machines).

Format of the binary ``*.plt`` file
...................................

The ``*.plt`` file binary and formatted file formats are very simple but please
observe that unformatted files written with a FORTRAN program are not pure
binary files because there are file records between the values while pure
binary files do not have any records between the values. gOpenMol should be
able to figure out if the file is pure binary or FORTRAN unformatted but it is
not very well tested.

Binary ``*.plt`` (grid) file format
...................................

Record number and meaning::

   #1: Integer, rank value must always be = 3
   #2: Integer, possible values are 1 ... 50. This value is not used but
   it can be used to define the type of surface!
   Values used (you can use your own value between 1... 50):

   1:   VSS surface
   2:   Orbital/density surface
   3:   Probe surface
   200: Gaussian 94/98
   201: Jaguar
   202: Gamess
   203: AutoDock
   204: Delphi/Insight
   205: Grid

   Value 100 is reserved for grid data coming from OpenMol!

   #3: Integer, number of points in z direction
   #4: Integer, number of points in y direction
   #5: Integer, number of points in x direction
   #6: Float, zmin value
   #7: Float, zmax value
   #8: Float, ymin value
   #9: Float, ymax value
   #10: Float, xmin value
   #11: Float, xmax value
   #12 ... Float, grid data values running (x is inner loop, then y and last z):

1.      Loop in the z direction
2.      Loop in the y direction
3.      Loop in the x direction

Example::

   nx=2  ny=1  nz=3

   0,0,0   1,0,0     y=0, z=0
   0,0,1   1,0,0     y=0, z=1
   0,0,2   1,0,2     y=0, z=2

The formatted (the first few lines) file can look like::

   3 2
   65 65 65
   -3.300000e+001 3.200000e+001 -3.300000e+001 3.200000e+001 -3.300000e+001 3.200000e+001
   -1.625609e+001 -1.644741e+001 -1.663923e+001 -1.683115e+001 -1.702274e+001 -1.721340e+001
   -1.740280e+001 -1.759018e+001 -1.777478e+001 -1.795639e+001 -1.813387e+001 -1.830635e+001
   ...

Formatted ``*.plt`` (grid) file format
......................................

Line numbers and variables on the line::

   line #1: Integer, Integer. Rank and type of surface (rank is always = 3)
   line #2: Integer, Integer, Integer. Zdim, Ydim, Xdim (number of points in the z,y,x directions)
   line #3: Float, Float, Float, Float, Float, Float. Zmin, Zmax, Ymin, Ymax, Xmin,Xmax (min and max values)
   line #4: ... Float. Grid data values running (x is inner loop, then y and last z) with one or several values per line:

    1. Loop in the z direction
    2. Loop in the y direction
    3. Loop in the x direction

Classes
-------

.. autoclass:: Plt
   :members:

    Nc                   (    e Zd ZddZd Zd Zd ZdS )RecordNc                 0    || _         || _        || _        d S N)keybintypevalues)selfr   r   r	   s       [/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/gridData/gOpenMol.py__init__zRecord.__init__   s        c                 &    | j         dS || j         v S )NT)r	   )r
   values     r   is_legalzRecord.is_legal   s    ;4##r   c                 B    |                      || j                           S r   )r   r   )r
   ds     r   is_legal_dictzRecord.is_legal_dict   s    }}Qtx[)))r   c                 &    dt          |           z  S )NzRecord(%(key)r,%(bintype)r,...))varsr
   s    r   __repr__zRecord.__repr__   s    04::==r   r   )__name__
__module____qualname__r   r   r   r    r   r   r   r      sU           $ $ $* * *> > > > >r   r   c                   x   e Zd ZdZ eddddi           eddddd	d
dddddddd           edd           edd           edd           edd           edd           edd           edd           edd           edd          fZdZd%dZd Ze	d              Z
e	d!             Zd" Zd# Zd$ ZdS )&PltaT  A class to represent a gOpenMol_ plt file.

    Only reading is implemented; either supply a filename to the constructor
      >>> G = Plt(filename)
    or load the file with the read method
      >>> G = Plt()
      >>> G.read(filename)

    The data is held in :attr:`GOpenMol.array` and all header information is in
    the dict :attr:`GOpenMol.header`.

    :attr:`Plt.shape`
         D-tuplet describing size in each dimension
    :attr:`Plt.origin`
         coordinates of the centre of the grid cell with index 0,0,...,0
    :attr:`Plt.delta`
         DxD array describing the deltas

    rankI   	dimensionsurfacezVSS surfacezOrbital/density surfacezProbe surface	gridcountOpenMolzGaussian 94/98JaguarGamessAutoDockzDelphi/InsightGrid)      r    *   d                     nznynxzminfzmaxyminymaxxminxmaxNc           	      ^   t          |          | _        d | j        D             d         }|j                            t          d t          dd          D                                  d                    d | j        D                       | _        || 	                    |           d S d S )Nc                 (    g | ]}|j         d k    |S )r"   r   .0rs     r   
<listcomp>z Plt.__init__.<locals>.<listcomp>   s$    III!aey6H6HA6H6H6Hr   r   c              3   *   K   | ]}|d k    |dfV  dS )r+   zuser-definedNr   )rA   ks     r   	<genexpr>zPlt.__init__.<locals>.<genexpr>   s,      #W#W1qTVwwQ~$6wwww#W#Wr      3    c                     g | ]	}|j         
S r   )r   r@   s     r   rC   z Plt.__init__.<locals>.<listcomp>   s    "J"J"J19"J"J"Jr   )
strfilename_header_structr	   updatedictrangejoin
_headerfmtread)r
   rL   rec_surfs      r   r   zPlt.__init__   s    HIIt2III!Lt#W#Wa#W#W#WWWXXX''"J"Jd6I"J"J"JKKIIh  r   c                 <   ddl m}m} |t          |          | _        t          | j        d          5 }|                     |          x}| _        |d         |d         z  |d         z  }|d         t          |          z   | j        z   }t          j
         |||                     ||                                        }ddd           n# 1 swxY w Y   | j        | j        d	<   |                    |d         |d         |d                                                   | _
        |                                 | _        t          j
        |d
         |d         |d         g          dt          j        | j                  z  z   | _        |d         | _        dS )z3Populate the instance from the plt file *filename*.r   )calcsizeunpackNrbr5   r4   r3   bsaflagrL   r;   r9   r6         ?r   )structrV   rW   rK   rL   open_read_headerheader_data_bintypenumpyarrayrS   reshape	transpose_deltadeltadiagonaloriginr   )	r
   rL   rV   rW   plthnentriesdatafmtas	            r   rS   zPlt.read   s   ++++++++MMDM$-&& 	J#"//444Aw4(1T72H	l3x==01CCGFF7CHHXXg5F5F,G,GHHIIA	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J #'-JYYqw$499CCEE
[[]]
k1V9ai6"CDDs5>Z^ZdKeKeGeefI			s   BCCCc                     | j         j        S r   )ra   shaper   s    r   rn   z	Plt.shape   s    zr   c                 D      fdt           j                  D             S )z}Edges 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 )r)   rZ   )re   r`   arangern   rg   )rA   r   r
   s     r   rC   zPlt.edges.<locals>.<listcomp>   st     E E E./ 
1Q3%,tz!}Q"?"??$+a.Pdj1o%& E E Er   )rP   r   r   s   `r   edgesz	Plt.edges   s@    E E E E383C3CE E E 	Er   c                    | j         }t          j        |d         |d         |d         g          }t          j        |d         |d         |d         g          }t          j        ||z
            | j        z  }t          j        |          S )Nr;   r9   r6   r<   r:   r8   )r^   r`   ra   absrn   diag)r
   ri   qminqmaxre   s        r   rd   z
Plt._delta   sw    K{AfIai&	:;;{AfIai&	:;;	$+&&3z%   r   c                     t          j         j                  }d  j        D             |                    |          d fd	}dD ]} ||          }|d         dk    r nd}|t          d           j        D ]A}|                    |          s*t          j        d	|j	        d
||j	                            B|S )zGRead header bytes, try all possibilities for byte order/size/alignment.c                     g | ]	}|j         
S r   r?   r@   s     r   rC   z$Plt._read_header.<locals>.<listcomp>   s    4441444r   @c           	          t          t          t          j        | j        z                                 }| |d<   |S )NrY   )rO   zipr[   rW   rR   )rY   ri   	binheadernamesr
   s     r   decode_headerz'Plt._read_header.<locals>.decode_header   s;    Sgdo.Ey Q QRRSSA"AiLHr   z@=<>r   r    Nz3Cannot decode header --- corrupted or wrong format?zKey z: Illegal value )rz   )
r[   rV   rR   rM   rS   	TypeErrorr   warningswarnr   )	r
   pltfilenheaderr   flagr^   recr}   r~   s	   `      @@r   r]   zPlt._read_header   s
   /$/2244 3444LL))		 	 	 	 	 	 	 	  	 	D"]4((Ff~""FF>QRRR& 	W 	WC$$V,, WCGGGVCG__UVVVr   c                     | j         | j        fS )z=Return array data as (edges,grid), i.e. a numpy nD histogram.)ra   rr   r   s    r   histogramddzPlt.histogramdd
  s    
DJ''r   r   )r   r   r   __doc__r   rM   r_   r   rS   propertyrn   rr   rd   r]   r   r   r   r   r   r      s        ( vfa_==vi1J1@2=3<3C3;3;3=3C39.0 .0 1 1 vd,,vd,,vd,,vf,,vf,,vf,,vf,,vf,,vf,,+.N, M	  	  	  	   "     X  E E XE! ! !  ,( ( ( ( (r   r   )r   r   r[   r`   objectr   r   r   r   r   <module>r      s   } }|   > > > > >V > > >u( u( u( u( u(& u( u( u( u( u(r   