B
    &]\d                 @   s  d dl mZmZmZ 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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: ZG d;d< d<eZG d=d> d>eZd?d@ Z dAdB Z!dCdD Z"dEdF Z#dGdH Z$dIdJ Z%dKdL Z&dMdN Z'dOdP Z(dQdR Z)dSdT Z*G dUdV dVe+Z,dYdXdZ-dS )Z    )divisionprint_functionabsolute_importreadsavN)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.   |   }|d dkr*| |d |d   dS )z+Align to the next 32-bit position in a filer
   r   N)tellseek)fpos r'   +lib/python3.7/site-packages/scipy/io/idl.py	_align_32N   s    r)   c             C   s   |  | dS )zSkip `n` bytesN)read)r%   nr'   r'   r(   _skip_bytesW   s    
r,   c             C   s
   |  |S )zRead the next `n` bytes)r*   )r%   r+   r'   r'   r(   _read_bytes]   s    r-   c             C   s$   t td| ddd d S )zRead a single bytez>Br
   Nr   r   )npZuint8structunpackr*   )r%   r'   r'   r(   
_read_byteb   s    r1   c             C   s   t td| dd S )zRead a signed 32-bit integerz>lr
   r   )r.   int32r/   r0   r*   )r%   r'   r'   r(   
_read_longg   s    r3   c             C   s$   t td| ddd d S )zRead a signed 16-bit integerz>hr
   r   r   )r.   Zint16r/   r0   r*   )r%   r'   r'   r(   _read_int16l   s    r4   c             C   s   t td| dd S )zRead a signed 32-bit integerz>ir
   r   )r.   r2   r/   r0   r*   )r%   r'   r'   r(   _read_int32q   s    r5   c             C   s   t td| dd S )zRead a signed 64-bit integerz>qr   r   )r.   Zint64r/   r0   r*   )r%   r'   r'   r(   _read_int64v   s    r6   c             C   s$   t td| ddd d S )zRead an unsigned 16-bit integerz>Hr
   r   r   )r.   Zuint16r/   r0   r*   )r%   r'   r'   r(   _read_uint16{   s    r7   c             C   s   t td| dd S )zRead an unsigned 32-bit integerz>Ir
   r   )r.   Zuint32r/   r0   r*   )r%   r'   r'   r(   _read_uint32   s    r8   c             C   s   t td| dd S )zRead an unsigned 64-bit integerz>Qr   r   )r.   Zuint64r/   r0   r*   )r%   r'   r'   r(   _read_uint64   s    r9   c             C   s   t td| dd S )zRead a 32-bit floatz>fr
   r   )r.   Zfloat32r/   r0   r*   )r%   r'   r'   r(   _read_float32   s    r:   c             C   s   t td| dd S )zRead a 64-bit floatz>dr   r   )r.   Zfloat64r/   r0   r*   )r%   r'   r'   r(   _read_float64   s    r;   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(   r<      s   r<   c               @   s   e Zd ZdZdS )ObjectPointerz$Class used to define object pointersN)r@   rA   rB   rC   r'   r'   r'   r(   rD      s   rD   c             C   s4   t | }|dkr,t| |}t|  t|}nd}|S )zRead a stringr    )r3   r-   r)   r   )r%   lengthcharsr'   r'   r(   _read_string   s    

rH   c             C   s4   t | }|dkr,t | }t| |}t|  nd}|S )z.Read a data string (length is specified twice)r   rE   )r3   r-   r)   )r%   rF   Zstring_datar'   r'   r(   _read_string_data   s    

rI   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||d  S |d	krt| S |d
krtdn|dkrt| }t| }t	||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)r5   	Exceptionr1   r4   r:   r;   r.   Z	complex64rI   Z
complex128r<   rD   r7   r8   r6   r9   )r%   dtyperealimagr'   r'   r(   
_read_data   sJ    






rN   c       
      C   s  |d }|d }g }x|D ]z}|d s.|d rP| |d  |d ftjf q|d tkr| |d  |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  }	|	  ||	}|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)rK   arrtablestructtablendimsr   dimsN)appendlowerr.   object_
DTYPE_DICTrJ   recarrayrange_read_structure_read_arrayrN   intreversereshape)
r%   
array_descstruct_descZnrowscolumnsrK   colrQ   irX   r'   r'   r(   r_      s8    
"

 
 
r_   c       	      C   s  |dkrL|dkr.t | }||d kr.td tj| |d t| d}nv|dkrtj| |d d t| dddd }nBg }x,t|d	 D ]}|}t| |}|	| qW tj
|tjd}|d
 dkr|d dt|d
  }|  ||}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 header)rK   )r   r   r   NrO   rW   rX   )r5   warningswarnr.   Z
frombufferr*   r\   r^   rN   rY   rR   r[   ra   rb   rc   r)   )	r%   rT   rd   ri   rR   rh   rK   datarX   r'   r'   r(   r`     s,    


r`   c             C   sL  dt | i}t| }|t| d 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||  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< nv|d dkrt| |d< t| |d < t| |d!< n@|d d"krt| |d#< n"|d d$kr:t| |d%< n|d d&krt | |d'< g |d(< xt|d' D ]}|d( t |  qjW n|d d)krt | |d*< t| |d+< g |d,< xt|d* D ]}|d, t|  qW n\|d d-krd.|d/< nD|d d0krtd1 n*|d d2kr.td3 ntd4|d  | | |S )5z!Function to read in a full recordrectypel        r
   zUnknown RECTYPE: %i)r   r   r   varname
heap_indexrT   r   Nrl   zUnexpected type code: 0r   zVARSTART is not 7rQ   rd   re   rR   r   i   dateuserhostr   formatarchosreleaseIDENTIFICATONauthortitleidcoder   Znoticer   descriptionr   ZnvaluesindicesZCOMMONBLOCKZnvarsrS   Zvarnamesr   TendZUNKNOWNzSkipping UNKNOWN recordr   zSkipping SYSTEM_VARIABLE recordz$record['rectype']=%s not implemented)r3   r8   r,   RECTYPE_DICTrJ   rH   _read_typedescr#   
ValueErrorr_   r`   rN   rI   r^   rY   rj   rk   r$   )r%   recordnextrecZrectypedescZvarstartrK   rh   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)rT   varflagsr   r   z System variables not implementedr
   rR       rQ   rd   re   )r3   rJ   _read_arraydesc_read_structdesc)r%   Ztypedescr'   r'   r(   r     s    r   c             C   sF  dt | i}|d dkrt| d t | |d< t | |d< t | |d< t| d t | |d< g |d< xt|d D ]}|d t |  qrW n|d d	kr2t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 t |  qW ntd|d  |S )z'Function to read in an array descriptorZarrstartr   r
   ri   rO   rW   ZnmaxrX      z$Using experimental 64-bit array readr   zExpected a zero in ARRAY_DESCzUnknown ARRSTART: %i)r3   r,   r^   rY   rj   rk   r9   rJ   )r%   Z	arraydescdvr'   r'   r(   r     s6    





r   c             C   s  i }t | }|dkrtdt| |d< t | }t | |d< t | |d< |d@ |d< |d@ |d	< |d
@ |d< |d sg |d< x&t|d D ]}|d t|  qW x|d D ]}t| |d< qW i |d< x,|d D ] }|d rt| |d |d < qW i |d< x0|d D ]$}|d rt| |d |d < qW |d	 sH|d rt| |d< t | |d< g |d< x(t|d D ]}|d t|  qvW g |d< x(t|d D ]}|d t|  qW |t|d < n"|d tkrtdt|d  }|S )z*Function to read in a structure descriptorr   zSTRUCTSTART should be 9rS   Zntagsri   r   predefr   Zinheritsr
   Zis_superrP   rU   rR   rV   rQ   Z	classnameZnsupclassesZsupclassnamesZsupclasstablez"PREDEF=1 but can't find definition)	r3   rJ   rH   r^   rY   _read_tagdescr   r   STRUCT_DICT)r%   Z
structdescZstructstartr   ttagsr'   r'   r(   r     sN    

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offsetrT   r
   rR   r   rQ   Zscalar)r3   r9   r\   )r%   ZtagdescZtagflagsr'   r'   r(   r   0  s    r   c             C   sl  t | trlxBt | trL| jdkr&d } q| j|kr<|| j } qt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| ||\}}|r(| || 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)
isinstancer<   r=   rj   rk   _replace_heapr.   Zcorerecordsr]   	enumerater   ZndarrayrK   typer[   r^   sizeitemZitemset)ZvariableheapreplacenewZirr   Zivvaluer'   r'   r(   r   C  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 | | d S )N)dictr?   )r>   Zinitr'   r'   r(   r?     s    zAttrDict.__init__c                s   t t| | S )N)superr   __getitem__rZ   )r>   rS   )	__class__r'   r(   r     s    zAttrDict.__getitem__c                s   t t| | |S )N)r   r   __setitem__rZ   )r>   keyr   )r   r'   r(   r     s    zAttrDict.__setitem__)r@   rA   rB   rC   r?   r   r   __getattr____setattr____call____classcell__r'   r'   )r   r(   r     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  |
d xt	|}|
t
dt| t|}|t|d 7 }|d}t| dkr>|
t
dt|d  |
t
dt||d  d  |
| P | }t||| }|
 t| d }|
t
dt|d  |
t
dt||d  d  |
| |
| qW |  |
}|d ntd|	 x.t|}|| d|kr|d rP qW |  i }x,|D ]$}|d dkr(|d ||d < q(W xP|D ]H}|d dkrVt|d |\}}|r||d< |d ||d  < qVW |rxV|D ]N}|d dkrtd td|d   td|d    td!|d"   P qW xf|D ]^}|d d#krtd td$|d%   td&|d'   td(|d)   td*|d+   P qW xV|D ]N}|d d,krntd td-|d.   td/|d0   td1|d2   P qnW x6|D ].}|d d3krtd td4|d5   P qW td td6t|  d7d8 |D }x2t|D ]&}|dkr(td9|||f  q(W td d|krtd: x&|D ]}td;|t|| f  qrW td |rx|D ]}|| ||< qW |S |S d<S )=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.

    rbr   s   SRzInvalid SIGNATURE: %ss    s    zIDL Save file is compressedzw+bz.sav)suffixz -> expanding to %ss   SR z>ll        r
   r   z>Ir   zInvalid RECFMT: %sr}   rm   r   rl   ro   r   rn   r   z2--------------------------------------------------zDate: %srp   zUser: %srq   zHost: %srr   r   z
Format: %srs   zArchitecture: %srt   zOperating System: %sru   zIDL Version: %srv   rw   z
Author: %srx   z	Title: %sry   zID Code: %srz   r   zDescription: %sr{   z&Successfully read %i records of which:c             S   s   g | ]}|d  qS )rm   r'   ).0rr'   r'   r(   
<listcomp>b  s    zreadsav.<locals>.<listcomp>z - %i are of type %szAvailable variables:z
 - %s [%s]N)r   openr-   rJ   printtempfileZNamedTemporaryFilerS   writer3   r/   Zpackra   r8   r*   r~   r#   zlibZ
decompresslencloser$   r   rY   r   rZ   setcountr   )	file_nameZidictZpython_dictZuncompressed_file_nameverboser   Z	variablesr%   Z	signatureZrecfmtZfoutrm   r   unknownr&   Z
rec_stringr   r   r   r   r   ZrectypesZrtvarr'   r'   r(   r     s    (





"
"













)NFNF).Z
__future__r   r   r   __all__r/   Znumpyr.   Znumpy.compatr   r   r   rj   r\   r~   r   r)   r,   r-   r1   r3   r4   r5   r6   r7   r8   r9   r:   r;   objectr<   rD   rH   rI   rN   r_   r`   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s   	*-/q1;B 