3
hg                 @   s  d gZ ddlZddlZddlm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 d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 ZG d:d; d;eZG d<d= d=eZd>d? Zd@dA ZdBdC ZdDdE ZdFdG Z dHdI Z!dJdK Z"dLdM Z#dNdO Z$dPdQ Z%dRdS Z&G dTdU dUe'Z(dXdWd Z)dS )Yreadsav    N)asstrz>u1z>i2z>i4z>f4z>f8z>c8z|Oz>c16z>u2z>u4z>i8z>u8)                        	   
                  ZSTART_MARKERZCOMMON_VARIABLEVARIABLESYSTEM_VARIABLE
END_MARKER	TIMESTAMPZCOMPILEDZIDENTIFICATIONVERSIONHEAP_HEADER	HEAP_DATAZ	PROMOTE64NOTICEDESCRIPTION)r   r   r   r   r	   r   r   r   r   r               c             C   s.   | j  }|d dkr*| j|d |d   dS )z+Align to the next 32-bit position in a filer   r   N)tellseek)fpos r$   -/tmp/pip-build-riy7u7_k/scipy/scipy/io/idl.py	_align_32L   s    r&   c             C   s   | j | dS )zSkip `n` bytesN)read)r"   nr$   r$   r%   _skip_bytesU   s    
r)   c             C   s
   | j |S )zRead the next `n` bytes)r'   )r"   r(   r$   r$   r%   _read_bytes[   s    r*   c             C   s$   t jtjd| jddd d S )zRead a single bytez>Br   Nr   r   )npZuint8structunpackr'   )r"   r$   r$   r%   
_read_byte`   s    r.   c             C   s   t jtjd| jdd S )zRead a signed 32-bit integerz>lr   r   )r+   int32r,   r-   r'   )r"   r$   r$   r%   
_read_longe   s    r0   c             C   s$   t jtjd| jddd d S )zRead a signed 16-bit integerz>hr   r   r   )r+   Zint16r,   r-   r'   )r"   r$   r$   r%   _read_int16j   s    r1   c             C   s   t jtjd| jdd S )zRead a signed 32-bit integerz>ir   r   )r+   r/   r,   r-   r'   )r"   r$   r$   r%   _read_int32o   s    r2   c             C   s   t jtjd| jdd S )zRead a signed 64-bit integerz>qr   r   )r+   Zint64r,   r-   r'   )r"   r$   r$   r%   _read_int64t   s    r3   c             C   s$   t jtjd| jddd d S )zRead an unsigned 16-bit integerz>Hr   r   r   )r+   Zuint16r,   r-   r'   )r"   r$   r$   r%   _read_uint16y   s    r4   c             C   s   t jtjd| jdd S )zRead an unsigned 32-bit integerz>Ir   r   )r+   Zuint32r,   r-   r'   )r"   r$   r$   r%   _read_uint32~   s    r5   c             C   s   t jtjd| jdd S )zRead an unsigned 64-bit integerz>Qr   r   )r+   Zuint64r,   r-   r'   )r"   r$   r$   r%   _read_uint64   s    r6   c             C   s   t jtjd| jdd S )zRead a 32-bit floatz>fr   r   )r+   Zfloat32r,   r-   r'   )r"   r$   r$   r%   _read_float32   s    r7   c             C   s   t jtjd| jdd S )zRead a 64-bit floatz>dr   r   )r+   Zfloat64r,   r-   r'   )r"   r$   r$   r%   _read_float64   s    r8   c               @   s   e Zd ZdZdd ZdS )PointerzClass used to define pointersc             C   s
   || _ d S )N)index)selfr:   r$   r$   r%   __init__   s    zPointer.__init__N)__name__
__module____qualname____doc__r<   r$   r$   r$   r%   r9      s   r9   c               @   s   e Zd ZdZdS )ObjectPointerz$Class used to define object pointersN)r=   r>   r?   r@   r$   r$   r$   r%   rA      s   rA   c             C   s4   t | }|dkr,t| |}t|  t|}nd}|S )zRead a stringr    )r0   r*   r&   r   )r"   lengthcharsr$   r$   r%   _read_string   s    

rE   c             C   s4   t | }|dkr,t | }t| |}t|  nd}|S )z.Read a data string (length is specified twice)r   rB   )r0   r*   r&   )r"   rC   Zstring_datar$   r$   r%   _read_string_data   s    

rF   c             C   s^  |dkr$t | dkrtdt| S |dkr4t| S |dkrDt | S |dkrTt| S |dkrdt| S |dkrt| }t| }tj||d  S |d	krt| S |d
krtdn|dkrt| }t| }tj	||d  S |dk rt
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rNt| S td| dS )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 thisN)r2   	Exceptionr.   r1   r7   r8   r+   Z	complex64rF   Z
complex128r9   rA   r4   r5   r3   r6   )r"   dtyperealimagr$   r$   r%   
_read_data   sJ    






rK   c       
      C   s  |d }|d }g }x|D ]z}|d s.|d rP|j |d j |d ftjf q|d tkr|j |d j |d ft|d  f qtd|d  qW tj|f|d}xt|D ]}x|D ]}|d }|d rt| |d	 |d  |d
 |d  ||d  |< q|d r4t	| ||d	 |d  ||d  |< qt
| |||d  |< qW qW |d dkr|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)rH   arrtablestructtablendimsr   dimsN)appendlowerr+   object_
DTYPE_DICTrG   recarrayrange_read_structure_read_arrayrK   intreversereshape)
r"   
array_descstruct_descZnrowscolumnsrH   colrN   irU   r$   r$   r%   r\      s8    
"

 
 
r\   c       	   
   C   s  |dkrL|dkr.t | }||d
 kr.tjd tj| j|d
 t| d}nv|dkrtj| j|d
 d t| dddd }nBg }x,t|d D ]}|}t| |}|j	| qW tj
|tjd}|d dk r|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   nbytesz.Not able to verify number of bytes from header)rH   r   r   NrL   rT   rU   )	r   r   r   r   r	   r   r   r   r   )r   r   )r2   warningswarnr+   Z
frombufferr'   rY   r[   rK   rV   rO   rX   r^   r_   r`   r&   )	r"   rQ   ra   rf   rO   re   rH   datarU   r$   r$   r%   r]     s,    


r]   c                s   dt  i}t }|t d9 7 }t d |d tkrJtd|d  t|d  |d< |d d:krF|d dkrt |d< nt  |d	< t d t }|d
 dkr| j krd|d< ntdnvt  }|dkrtd|d rt	 |d |d |d< n:|d r,t
 |d
 |d |d< n|d
 }t ||d< n|d dkrt d; t |d< t |d< t |d< n|d dkrt  |d< t |d< t |d< t |d< nJ|d dkrt |d < t |d!< t |d"< n|d d#krt |d$< n|d d%kr6t |d&< n|d d'krpt  |d(<  fd)d*t|d( D |d+< n|d d,krt  |d-< t |d.<  fd/d*t|d- D |d0< n\|d d1krd2|d3< nD|d d4krtjd5 n*|d d6krtjd7 ntd8|d   j| |S )<z!Function to read in a full recordrectyper       r   zUnknown RECTYPE: %ir   r   varname
heap_indexrQ   r   Nri   zUnexpected type code: 0r
   zVARSTART is not 7rN   ra   rb   rO   r      dateuserhostr   formatarchosreleaseIDENTIFICATONauthortitleidcoder   noticer   descriptionr   Znvaluesc                s   g | ]}t  qS r$   )r0   ).0_)r"   r$   r%   
<listcomp>  s    z _read_record.<locals>.<listcomp>indicesZCOMMONBLOCKZnvarsrP   c                s   g | ]}t  qS r$   )rE   )r|   r}   )r"   r$   r%   r~     s    varnamesr   TendUNKNOWNzSkipping UNKNOWN recordr   zSkipping SYSTEM_VARIABLE recordz$record['rectype']=%s not implementedl        )r   r   i   )r0   r5   r)   RECTYPE_DICTrG   rE   _read_typedescr    
ValueErrorr\   r]   rK   rF   r[   rg   rh   r!   )r"   recordnextrecZrectypedescZvarstartrH   r$   )r"   r%   _read_record=  s~    








  

r   c             C   s   t | t | d}|d d@ dkr*td|d d@ dk|d< |d d@ dk|d< |d rtt| |d	< t| |d
< n|d rt| |d	< |S )z%Function to read in a type descriptor)rQ   varflagsr   r   z System variables not implementedr   rO   rk   rN   ra   rb   )r0   rG   _read_arraydesc_read_structdesc)r"   Ztypedescr$   r$   r%   r     s    r   c                s4  dt  i}|d dkr|t d t  |d< t  |d< t  |d< t d t  |d<  fdd	t|d D |d
< n|d dkr tjd t d t |d< t |d< t  |d< t d d|d< g |d
< xRt|d D ]0}t  }|dkrtd|d
 jt   qW ntd|d  |S )z'Function to read in an array descriptorZarrstartr   r   rf   rL   rT   Znmaxc                s   g | ]}t  qS r$   )r0   )r|   r}   )r"   r$   r%   r~     s    z#_read_arraydesc.<locals>.<listcomp>rU      z$Using experimental 64-bit array readr   zExpected a zero in ARRAY_DESCzUnknown ARRSTART: %i)r0   r)   r[   rg   rh   r6   rG   rV   )r"   Z	arraydescdvr$   )r"   r%   r     s2    

 



r   c                s  i }t  }|dkrtdt |d< t  }t  |d< t  |d< |d@ |d< |d@ |d	< |d
@ |d< |d sZ fddt|d D |d< x|d D ]}t |d< qW  fdd|d D |d<  fdd|d D |d< |d	 s|d rLt |d< t  |d<  fddt|d D |d<  fddt|d D |d< |t|d < n"|d tkrptdt|d  }|S )z*Function to read in a structure descriptorr   zSTRUCTSTART should be 9rP   Zntagsrf   r   predefr   Zinheritsr   Zis_superc                s   g | ]}t  qS r$   )_read_tagdesc)r|   r}   )r"   r$   r%   r~     s   z$_read_structdesc.<locals>.<listcomp>rM   c                s"   i | ]}|d  rt  |d qS )rO   rP   )r   )r|   tag)r"   r$   r%   
<dictcomp>  s   z$_read_structdesc.<locals>.<dictcomp>rR   c                s"   i | ]}|d  rt  |d qS )rN   rP   )r   )r|   r   )r"   r$   r%   r     s   rS   	classnameZnsupclassesc                s   g | ]}t  qS r$   )rE   )r|   r}   )r"   r$   r%   r~     s    Zsupclassnamesc                s   g | ]}t  qS r$   )r   )r|   r}   )r"   r$   r%   r~     s    Zsupclasstablez"PREDEF=1 but can't find definition)r0   rG   rE   r[   STRUCT_DICT)r"   Z
structdescZstructstartr   r   r$   )r"   r%   r     s<    



r   c             C   sl   dt | i}|d d	kr$t| |d< t | |d< t | }|d@ dk|d< |d@ dk|d< |d tk|d< |S )
z$Function to read in a tag descriptoroffsetr   rQ   r   rO   rk   rN   Zscalar)r0   r6   rY   )r"   ZtagdescZtagflagsr$   r$   r%   r   #  s    r   c             C   sl  t | trlxBt | trL| jdkr&d } q| j|kr<|| j } qtjd d } qW t| |\}}|rd|} d| fS t | tjjj	rx.t
| D ]"\}}t||\}}|r|| |< qW d| fS t | tjjj rx.t
| D ]"\}}t||\}}|r|| |< qW d| fS t | tjr`| jjtjkrXx:t| jD ],}t| j||\}}|r(| j|| q(W d| fS d| fS d S )Nr   zNVariable referenced by pointer not found in heap: variable will be set to NoneTF)
isinstancer9   r:   rg   rh   _replace_heapr+   corerecordsrZ   	enumerater   ZndarrayrH   typerX   r[   sizeitemZitemset)variableheapreplacenewZirr   Zivvaluer$   r$   r%   r   6  s@    



r   c                   sD   e Zd ZdZi fddZ fddZ fddZeZeZeZ	  Z
S )AttrDicta.  
    A case-insensitive dictionary with access via item, attribute, and call
    notations:

        >>> d = AttrDict()
        >>> d['Variable'] = 123
        >>> d['Variable']
        123
        >>> d.Variable
        123
        >>> d.variable
        123
        >>> d('VARIABLE')
        123
    c             C   s   t j| | d S )N)dictr<   )r;   initr$   r$   r%   r<     s    zAttrDict.__init__c                s   t t| j|j S )N)superr   __getitem__rW   )r;   rP   )	__class__r$   r%   r     s    zAttrDict.__getitem__c                s   t t| j|j |S )N)r   r   __setitem__rW   )r;   keyr   )r   r$   r%   r     s    zAttrDict.__setitem__)r=   r>   r?   r@   r<   r   r   __getattr____setattr____call____classcell__r$   r$   )r   r%   r   x  s   r   Fc             C   s  g }|s|ri }nt  }t| d}t|d}|dkr@td| t|d}	|	dkrVn|	dkr|rltd |r|t|d}
ntjd	d
}
|rtd|
j  |
jd xt	|}|
jt
jdt| t|}|t|d> 7 }|jd}t| dkr>|
jt
jdt|d?  |
jt
jdt||d@  dA  |
j| P |j }tj|j|| }|
j t| d }|
jt
jdt|dB  |
jt
jdt||dC  dD  |
j| |
j| qW |j  |
}|jd ntd|	 x.t|}|j| d|kr|d rP qW |j  i }x,|D ]$}|d dkr(|d ||d < q(W xP|D ]H}|d dkrVt|d |\}}|r||d< |d ||d j < qVW |rxZ|D ]R}|d dkrtdd  td|d   td |d!   td"|d#   P qW xj|D ]b}|d d$kr
tdd  td%|d&   td'|d(   td)|d*   td+|d,   P q
W xZ|D ]R}|d d-krvtdd  td.|d/   td0|d1   td2|d3   P qvW x:|D ]2}|d d4krtdd  td5|d6   P qW tdd  td7t|  d8d9 |D }x2t|D ]&}|dkr<td:|j||f  q<W tdd  d|krtd; x&|D ]}td<|t|| f  qW tdd  |rx|D ]}|| ||< qW |S |S d=S )Ea	  
    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 z>lrk   r   r   z>Ir   zInvalid RECFMT: %sr   rj   r   ri   rm   r   rl   r   -2   zDate: %sro   zUser: %srp   zHost: %srq   r   z
Format: %srr   zArchitecture: %srs   zOperating System: %srt   zIDL Version: %sru   rv   z
Author: %srw   z	Title: %srx   zID Code: %sry   r   zDescription: %sr{   z&Successfully read %i records of which:c             S   s   g | ]}|d  qS )rj   r$   )r|   rr$   r$   r%   r~   s  s    zreadsav.<locals>.<listcomp>z - %i are of type %szAvailable variables:z
 - %s [%s]Nl        l        l        l        l        l        l        )r   openr*   rG   printtempfileNamedTemporaryFilerP   writer0   r,   packr^   r5   r'   r   r    zlib
decompresslencloser!   r   rV   r   rW   setcountr   )	file_nameZidictZpython_dictZuncompressed_file_nameverboser   	variablesr"   	signatureZrecfmtZfoutrj   r   unknownr#   Z
rec_stringr   r   r   r   r   Zrectypesrtvarr$   r$   r%   r     s    F





"
"













)NFNF)*__all__r,   Znumpyr+   Znumpy.compatr   r   r   rg   rY   r   r   r&   r)   r*   r.   r0   r1   r2   r3   r4   r5   r6   r7   r8   objectr9   rA   rE   rF   rK   r\   r]   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s   	*-/m/6B 