
    tKgi                     X   d gZ ddlZddlZddlZddlZddlZddddddd	d	d
d	d	dddddZdddddddddddddddZi Z	d Z
d  Zd! Zd" Zd# Zd$ Zd% Zd& Zd' Zd( Zd) Zd* Zd+ Z G d, d-      Z G d. d/e      Zd0 Zd1 Zd2 Zd3 Zd4 Zd5 Zd6 Zd7 Z d8 Z!d9 Z"d: Z# G d; d<e$      Z%	 	 d>d=Z&y)?readsav    Nz>u1z>i2z>i4z>f4z>f8z>c8z|Oz>c16z>u2z>u4z>i8z>u8)                        	   
                  START_MARKERCOMMON_VARIABLEVARIABLESYSTEM_VARIABLE
END_MARKER	TIMESTAMPCOMPILEDIDENTIFICATIONVERSIONHEAP_HEADER	HEAP_DATA	PROMOTE64NOTICEDESCRIPTION)r   r   r   r   r	   r   r   r   r   r               c                 h    | j                         }|dz  dk7  r| j                  |dz   |dz  z
         y)z+Align to the next 32-bit position in a filer   r   N)tellseek)fposs     Q/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/scipy/io/_idl.py	_align_32r+   K   s6     &&(C
Qw!|	sQwq !
    c                 &    | j                  |       y)zSkip `n` bytesNreadr(   ns     r*   _skip_bytesr2   T   s    FF1I
r,   c                 $    | j                  |      S )zRead the next `n` bytesr.   r0   s     r*   _read_bytesr4   Z   s    66!9r,   c                 ~    t        j                  t        j                  d| j	                  d      dd       d         S )zRead a single bytez>Br   Nr   r   )npuint8structunpackr/   r(   s    r*   
_read_byter;   _   s/    88FMM$q	"16q9::r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )Read a signed 32-bit integer>lr   r   r6   int32r8   r9   r/   r:   s    r*   
_read_longrA   d   )    88FMM$q	21566r,   c                 ~    t        j                  t        j                  d| j	                  d      dd       d         S )zRead a signed 16-bit integerz>hr   r   r   )r6   int16r8   r9   r/   r:   s    r*   _read_int16rE   i   s/    88FMM$q	!A7:;;r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )r=   z>ir   r   r?   r:   s    r*   _read_int32rG   n   rB   r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )zRead a signed 64-bit integerz>qr   r   )r6   int64r8   r9   r/   r:   s    r*   _read_int64rJ   s   rB   r,   c                 ~    t        j                  t        j                  d| j	                  d      dd       d         S )zRead an unsigned 16-bit integerz>Hr   r   r   )r6   uint16r8   r9   r/   r:   s    r*   _read_uint16rM   x   s/    99V]]41Q8;<<r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )zRead an unsigned 32-bit integer>Ir   r   )r6   uint32r8   r9   r/   r:   s    r*   _read_uint32rQ   }   )    99V]]43A677r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )zRead an unsigned 64-bit integerz>Qr   r   )r6   uint64r8   r9   r/   r:   s    r*   _read_uint64rU      rR   r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )zRead a 32-bit floatz>fr   r   )r6   float32r8   r9   r/   r:   s    r*   _read_float32rX      )    ::fmmD!&&)4Q788r,   c                 x    t        j                  t        j                  d| j	                  d            d         S )zRead a 64-bit floatz>dr   r   )r6   float64r8   r9   r/   r:   s    r*   _read_float64r\      rY   r,   c                       e Zd ZdZd Zy)PointerzClass used to define pointersc                     || _         y N)index)selfra   s     r*   __init__zPointer.__init__   s    
r,   N)__name__
__module____qualname____doc__rc    r,   r*   r^   r^      s
    'r,   r^   c                       e Zd ZdZy)ObjectPointerz$Class used to define object pointersN)rd   re   rf   rg   rh   r,   r*   rj   rj      s    .r,   rj   c                 z    t        |       }|dkD  r(t        | |      j                  d      }t        |        |S d}|S )zRead a stringr   latin1 )rA   r4   decoder+   )r(   lengthcharss      r*   _read_stringrq      sC    ]FzAv&--h7! L Lr,   c                 r    t        |       }|dkD  r$t        |       }t        | |      }t        |        |S d}|S )z.Read a data string (length is specified twice)r   rm   )rA   r4   r+   )r(   ro   string_datas      r*   _read_string_datart      sC    ]FzA!!V,!  r,   c                    |dk(  r$t        |       dk7  rt        d      t        |       S |dk(  rt        |       S |dk(  rt        |       S |dk(  rt	        |       S |dk(  rt        |       S |dk(  r1t	        |       }t	        |       }t        j                  ||dz  z         S |d	k(  rt        |       S |d
k(  rt        d      |dk(  r1t        |       }t        |       }t        j                  ||dz  z         S |dk(  rt        t        |             S |dk(  rt        t        |             S |dk(  rt        |       S |dk(  rt        |       S |dk(  rt        |       S |dk(  rt        |       S t        d|z        )z*Read a variable with a specified data typer   z*Error occurred while reading byte variabler   r   r   r   r	   y              ?r
   r   z'Should not be here - please report thisr   r   r   r   r   r   r   z)Unknown IDL type: %i - please report this)rG   	Exceptionr;   rE   rX   r\   r6   	complex64rt   
complex128r^   rj   rM   rQ   rJ   rU   )r(   dtyperealimags       r*   
_read_datar|      s}   zq>QHII!}	!1~	!1~	!Q	!Q	!QQ||D4"9,--	! ##	!ABB	!QQ}}TD2I-..	"{1~&&	"[^,,	"A	"A	"1~	"ACeKLLr,   c           	         |d   }|d   }g }|D ]  }|d   s|d   r8|j                  |d   j                         |d   ft        j                  f       E|d   t        v r4|j                  |d   j                         |d   ft        |d      f       t        d|d   z         t        j                  j                  |f|      }t        |      D ]v  }|D ]o  }|d   }|d   r)t        | |d	   |d      |d
   |d            ||d      |<   6|d   r t        | ||d	   |d            ||d      |<   [t        | |      ||d      |<   q x |d   dkD  r5|d   dt        |d          }	|	j                          |j                  |	      }|S )z
    Read a structure, with the array and structure descriptors given as
    `array_desc` and `structure_desc` respectively.
    	nelementstagtable	structurearraynametypecodez Variable type %i not implementedry   arrtablestructtablendimsr   dimsN)appendlowerr6   object_
DTYPE_DICTrv   recrecarrayrange_read_structure_read_arrayr|   intreversereshape)
r(   
array_descstruct_descnrowscolumnsry   colr   ir   s
             r*   r   r      s    {#E*%GE{s7|LL3v;,,.F<bjjIJ:*,s6{002CK@$.s:$?A B   B<?
O!L M M  	7I5\C
OE;,;A&1*&=c&k&J&1-&@V&M-O	#f+&q) W,75&1*&=c&k&J-L	#f+&q) -7q%,@	#f+&q)   'Q&!";3z'':#;<%%d+	r,   c                    |dv r`|dk(  r*t        |       }||d   k7  rt        j                  dd       t        j                  | j                  |d         t        |         }n|dv r:t        j                  | j                  |d   d	z        t        |         dd
d	   }nYg }t        |d         D ]!  }|}t        | |      }|j                  |       # t        j                  |t        j                        }|d   dkD  r5|d   d
t        |d          }|j                          |j                  |      }t        |        |S )z`
    Read an array of type `typecode`, with the array descriptor given as
    `array_desc`.
    )	r   r   r   r   r	   r   r   r   r   r   nbytesz.Not able to verify number of bytes from headerr   
stacklevelr   )r   r   r   Nr~   r   r   )rG   warningswarnr6   
frombufferr/   r   r   r|   r   r   r   r   r   r   r+   )	r(   r   r   r   r   r   ry   datar   s	            r*   r   r     sH    11q= ^FH--N)*, affZ%9:$.x$8: 
W	 affZ%9!%;<$.x$8::;$Q$@ z+./AEa'DLL 0
 bjj1 'Q&!";3z'':#;<d# aLLr,   c                    dt        |       i}t        |       }|t        |       j                  t        j                        dz  z  }t        | d       |d   t        vrt        d|d   z        t        |d      |d<   |d   dv r|d   dk(  rt        |       |d<   nt        |       |d<   t        | d       t        |       }|d	   d
k(  r%|| j                         k(  rd|d<   nGt        d      t        |       }|dk7  rt        d      |d   rt        | |d   |d         |d<   n|d   rt        | |d	   |d         |d<   n|d	   }t        | |      |d<   n|d   dk(  r8t        | d       t        |       |d<   t        |       |d<   t        |       |d<   n|d   dk(  r:t        |       |d<   t        |       |d<   t        |       |d<   t        |       |d<   nO|d   dk(  r,t        |       |d<   t        |       |d <   t        |       |d!<   n|d   d"k(  rt        |       |d#<   n|d   d$k(  rt        |       |d%<   n|d   d&k(  r7t        |       |d'<   t!        |d'         D cg c]  }t        |        c}|d(<   n|d   d)k(  rEt        |       |d*<   t        |       |d+<   t!        |d*         D cg c]  }t        |        c}|d,<   n`|d   d-k(  rd.|d/<   nR|d   d0k(  rt#        j$                  d1d23       n2|d   d4k(  rt#        j$                  d5d23       nt        d6|d    d7      | j'                  |       |S c c}w c c}w )8z!Function to read in a full recordrectype        r   zUnknown RECTYPE: %i)r   r   r   varname
heap_indexr   r   Nr   zUnexpected type code: 0r
   zVARSTART is not 7r   r   r   r   r   i   dateuserhostr   formatarchosreleaseIDENTIFICATONauthortitleidcoder   noticer    descriptionr   nvaluesindicesCOMMONBLOCKnvarsr   varnamesr   TendUNKNOWNzSkipping UNKNOWN recordr   r   r   zSkipping SYSTEM_VARIABLE recordzrecord['rectype']=z not implemented)rA   rQ   astyper6   rI   r2   RECTYPE_DICTrv   rq   _read_typedescr&   
ValueErrorr   r   r|   rt   r   r   r   r'   )r(   recordnextrecrectypedescvarstartry   _s          r*   _read_recordr   <  s    A'F1oG|A%%bhh/%77G1i,-y0AABB$VI%67F9i55)
* ,QF9#-a=F< 1$Q'z"a'!&&("!%v !:;; "!}H1} 344;'!0K4M4?4N"PvW%!,QJ0G0;L0I"Kv $J/!+Au!5v			k	)Au%av%av%av			i	'%a=x%av#At(Oy			o	-'?x&q/w'?x			h	&'?x			m	+ 1! 4}			m	+&qMy49&:K4LM4LqZ]4LMy			m	+$Q-w%av7<VG_7MN7M!l1o7MNz			l	*u			i	'/A>			/	/7AF ,VI->,??OPQQFF7OM5 N Os   $L=1Mc                    t        |       t        |       d}|d   dz  dk(  rt        d      |d   dz  dk(  |d<   |d   dz  dk(  |d<   |d   rt        |       |d	<   t        |       |d
<   |S |d   rt        |       |d	<   |S )z%Function to read in a type descriptor)r   varflagsr   r   z System variables not implementedr   r       r   r   r   )rA   rv   _read_arraydesc_read_structdesc)r(   typedescs     r*   r   r     s     'qMz!}EH
a1$:;; ,q0A5HW$Z025;H[!0!3"21"5 O 
'	!0!3Or,   c                    dt        |       i}|d   dk(  rzt        | d       t        |       |d<   t        |       |d<   t        |       |d<   t        | d       t        |       |d<   t        |d         D cg c]  }t        |        c}|d<   |S |d   d	k(  rt        j                  d
d       t        | d       t        |       |d<   t        |       |d<   t        |       |d<   t        | d       d|d<   g |d<   t        |d         D ]:  }t        |       }|dk7  rt        d      |d   j                  t        |              < |S t        d|d   z        c c}w )z'Function to read in an array descriptorarrstartr   r   r   r~   r   nmaxr      z$Using experimental 64-bit array readr   r   r   zExpected a zero in ARRAY_DESCzUnknown ARRSTART: %i)rA   r2   r   r   r   rU   rv   r   )r(   	arraydescr   dvs        r*   r   r     sy    Z]+I!Aq(m	(!+A	+']	'Aq&qM	&49)F:K4LM4LqZ]4LM	&6 3 
:	"	$<KAq*1o	(!-a	+']	'Aq	&	&y()A1AAv ?@@f$$Z]3	 *  .:1FFGG3 Ns   6Ec                    i }t        |       }|dk7  rt        d      t        |       |d<   t        |       }t        |       |d<   t        |       |d<   |dz  |d<   |dz  |d	<   |d
z  |d<   |d   st        |d         D cg c]  }t	        |        c}|d<   |d   D ]  }t        |       |d<    |d   D ci c]  }|d   r|d   t        |        c}|d<   |d   D ci c]  }|d   r|d   t        |        c}|d<   |d	   s|d   rlt        |       |d<   t        |       |d<   t        |d         D cg c]  }t        |        c}|d<   t        |d         D cg c]  }t        |        c}|d<   |t        |d   <   |S |d   t        vrt        d      t        |d      }|S c c}w c c}w c c}w c c}w c c}w )z*Function to read in a structure descriptorr   zSTRUCTSTART should be 9r   ntagsr   r   predefr   inheritsr   is_superr   r   r   r   r   	classnamensupclassessupclassnamessupclasstablez"PREDEF=1 but can't find definition)rA   rv   rq   r   _read_tagdescr   r   STRUCT_DICT)r(   
structdescstructstartr   r   tags         r*   r   r     sP    JQ-Ka122%aJv]F$Q-Jw%a=Jx!A:Jx#aZJz#aZJzh ,1G1D+E"G+Ea #0"2+E"G
: j)C&q/CK * .8
-C"3-Cc%(\ #&f+q/A"A-C"3
:
 1;:0F%:0F(+K(8 &)[2B12E%E0F%:
=! j!Z
%;&21oJ{#(21J}%).z-/H)I+K)IAQ)I+KJ' .3:m3L-M+O-M #-M+OJ' +5Jv&'  f[0@AA F!34
?"G"3%:+K+Os   F)8F. F37F8F=c                     dt        |       i}|d   dk(  rt        |       |d<   t        |       |d<   t        |       }|dz  dk(  |d<   |dz  dk(  |d<   |d   t        v |d<   |S )	z$Function to read in a tag descriptoroffsetr   r   r   r   r   scalar)rA   rU   r   )r(   tagdesctagflagss      r*   r   r   !  s     A'GxB(O$Q-GJ!}H!|q(GG#b=B.GK
+z9GH Nr,   c                    t        | t              rt        | t              rZ| j                  dk(  rd } n7| j                  |v r|| j                     } nt        j                  dd       d } t        | t              rZt        | |      \  }}|r|} d| fS t        | t        j                  j                        r.t        |       D ]  \  }}t        ||      \  }}|s|| |<    d| fS t        | t        j                        r.t        |       D ]  \  }}t        ||      \  }}|s|| |<    d| fS t        | t        j                        ry| j                  j                  t        j                  u rOt        | j                         D ]7  }t        | j#                  |      |      \  }}|s$|| j%                  d      |<   9 d| fS d| fS )Nr   zNVariable referenced by pointer not found in heap: variable will be set to Noner   r   TFr   )
isinstancer^   ra   r   r   _replace_heapr6   r   r   	enumerater   ndarrayry   typer   r   sizeitemr   )variableheapreplacenewirr   ivvalues           r*   r   r   4  s   (G$7+~~">>T)#HNN3HMM #J-.0  $H 7+ %Xt4HX~	Hbffoo	. $H-JB(6LGS" . h	Hbii	( #8,IB(5LGS" - h	Hbjj	) >>"**,HMM*,X]]2->E/2H$$R(, + h hr,   c                   D     e Zd ZdZi fdZ fdZ fdZd ZeZeZ	 xZ
S )AttrDicta]  
    A case-insensitive dictionary with access via item, attribute, and call
    notations:

        >>> from scipy.io._idl import AttrDict
        >>> d = AttrDict()
        >>> d['Variable'] = 123
        >>> d['Variable']
        123
        >>> d.Variable
        123
        >>> d.variable
        123
        >>> d('VARIABLE')
        123
        >>> d['missing']
        Traceback (most recent error last):
        ...
        KeyError: 'missing'
        >>> d.missing
        Traceback (most recent error last):
        ...
        AttributeError: 'AttrDict' object has no attribute 'missing'
    c                 0    t         j                  | |       y r`   )dictrc   )rb   inits     r*   rc   zAttrDict.__init__  s    dD!r,   c                 >    t         |   |j                               S r`   )super__getitem__r   )rb   r   	__class__s     r*   r   zAttrDict.__getitem__  s    w"4::<00r,   c                 @    t         |   |j                         |      S r`   )r   __setitem__r   )rb   keyr   r   s      r*   r   zAttrDict.__setitem__  s    w"399;66r,   c                 x    	 | j                  |      S # t        $ r t        dt        |        d| d      d w xY w)N'z' object has no attribute ')r   KeyErrorAttributeErrorr   )rb   r   s     r*   __getattr__zAttrDict.__getattr__  sS    	N##D)) 	N DJ<:4&BDIMN	Ns    &9)rd   re   rf   rg   rc   r   r   r  __setattr____call____classcell__)r   s   @r*   r   r   w  s.    2  "17N KHr,   r   c           
      
   g }|s|ri }n
t               }t        | d      }t        |d      }|dk7  rt        d|z        t        |d      }	|	dk(  rn|	dk(  r|rt	        d       |rt        |d      }
nt        j                  d	
      }
|rt	        d|
j                  z         |
j                  d       	 t        |      }|
j                  t        j                  dt        |                   t        |      }|t        |      j                  t        j                         dz  z  }|j#                  d      }t$        |   dk(  rt        j                   d      }|
j                  t        j                  dt        |      |z               |
j                  t        j                  dt        |||z  z
  |z                     |
j                  |       n|j'                         }t)        j*                  |j#                  ||z
              }|
j'                         t-        |      z   dz   }|
j                  t        j                  dt        |dz                     |
j                  t        j                  dt        ||dz  z
  dz                     |
j                  |       |
j                  |        |j/                          |
}|j1                  d       nt        d|	z        	 t3        |      }|j5                  |       d|v r|d   rn'|j/                          i }|D ]  }|d   dk(  s|d   ||d   <    |D ]=  }|d   dk(  st7        |d   |      \  }}|r||d<   |d   ||d   j9                         <   ? |r|D ]J  }|d   dk(  st	        d       t	        d|d   z         t	        d|d    z         t	        d!|d"   z          n |D ][  }|d   d#k(  st	        d       t	        d$|d%   z         t	        d&|d'   z         t	        d(|d)   z         t	        d*|d+   z          n |D ]J  }|d   d,k(  st	        d       t	        d-|d.   z         t	        d/|d0   z         t	        d1|d2   z          n |D ](  }|d   d3k(  st	        d       t	        d4|d5   z          n t	        d       t	        d6t-        |      z         |D cg c]  }|d   	 }}t;        |      D ]'  }|dk7  s	t	        d7|j=                  |      |fz         ) t	        d       d|v r;t	        d8       |D ]   }t	        d9| d:t?        ||          d;       " t	        d       |r|D ]
  }||   ||<    |S |S c c}w )<a	  
    Read an IDL .sav file.

    Parameters
    ----------
    file_name : str
        Name of the IDL save file.
    idict : dict, optional
        Dictionary in which to insert .sav file variables.
    python_dict : bool, optional
        By default, the object return is not a Python dictionary, but a
        case-insensitive dictionary with item, attribute, and call access
        to variables. To get a standard Python dictionary, set this option
        to True.
    uncompressed_file_name : str, optional
        This option only has an effect for .sav files written with the
        /compress option. If a file name is specified, compressed .sav
        files are uncompressed to this file. Otherwise, readsav will use
        the `tempfile` module to determine a temporary filename
        automatically, and will remove the temporary file upon successfully
        reading it in.
    verbose : bool, optional
        Whether to print out information about the save file, including
        the records read, and available variables.

    Returns
    -------
    idl_dict : AttrDict or dict
        If `python_dict` is set to False (default), this function returns a
        case-insensitive dictionary with item, attribute, and call access
        to variables. If `python_dict` is set to True, this function
        returns a Python dictionary with all variable names in lowercase.
        If `idict` was specified, then variables are written to the
        dictionary specified, and the updated dictionary is returned.

    Examples
    --------
    >>> from os.path import dirname, join as pjoin
    >>> import scipy.io as sio
    >>> from scipy.io import readsav

    Get the filename for an example .sav file from the tests/data directory.

    >>> data_dir = pjoin(dirname(sio.__file__), 'tests', 'data')
    >>> sav_fname = pjoin(data_dir, 'array_float32_1d.sav')

    Load the .sav file contents.

    >>> sav_data = readsav(sav_fname)

    Get keys of the .sav file contents.

    >>> print(sav_data.keys())
    dict_keys(['array1d'])

    Access a content with a key.

    >>> print(sav_data['array1d'])
    [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0.]

    rbr   s   SRzInvalid SIGNATURE: %ss    s    zIDL Save file is compressedzw+bz.sav)suffixz -> expanding to %ss   SR r>   r   r   r   rO   r   zInvalid RECFMT: %sr   r   r   r   r   r   r   r   z2--------------------------------------------------zDate: %sr   zUser: %sr   zHost: %sr   r   z
Format: %sr   zArchitecture: %sr   zOperating System: %sr   zIDL Version: %sr   r   z
Author: %sr   z	Title: %sr   zID Code: %sr   r    zDescription: %sr   z&Successfully read %i records of which:z - %i are of type %szAvailable variables:z - z []) r   openr4   rv   printtempfileNamedTemporaryFiler   writerA   r8   packr   rQ   r   r6   rI   r/   r   r&   zlib
decompresslencloser'   r   r   r   r   setcountr   )	file_nameidictpython_dictuncompressed_file_nameverboserecords	variablesr(   	signaturerecfmtfoutr   r   unknownmodvalr)   
rec_stringrr   r   r   r   rectypesrtvars                            r*   r   r     sv   L Ge	J	 	YA Aq!IE/);<< AF	;	/0!.6D..f=D'$))34 	

=!  !mGJJv{{4W67 #1oG|A--bhh7%??G ffQiG G$4%

6;;tS\F-BCD

KKc7g6F+G6*Q&RS 

7# &&(C )<=J iikC
O3b8G JJv{{4Wu_)=>?JJv{{4g5.IU-R)STUJJwJJz"I N 	
	 	q	 ,v566 OqA:x  GGI DY<;&$%fID<! 
 Y<:%(6D9LGS&	./iIa	l((*+   Fi K/fj6&>12j6&>12j6&>12  Fi I-flVH%556(6&>9:,vd|;<'&*;;<  Fi O3flVH%556kF7O34mfX&667  Fi M1f'&*??@	  	f6-0\; 	< +22'QAiL'2h-B\!,r0BB/GGH   	f!() C54	##7"8:; !&MC"3E#J % 3s   U5)NFNF)'__all__r8   numpyr6   r  r  r   r   r   r   r+   r2   r4   r;   rA   rE   rG   rJ   rM   rQ   rU   rX   r\   r^   rj   rq   rt   r|   r   r   r   r   r   r   r   r   r   r   r   rh   r,   r*   <module>r-     s@  < +      
" "$$$!!#  
;
7
<
7
7
=
8
8
9
9
 	G 	
	'MT*Z-`iX(,^3l&@F+t +\ 0516qr,   