o
    ?Hhi                     @   sx  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d  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G d9d: d:ZG d;d< d<eZd=d> Zd?d@ ZdAdB ZdCdD ZdEdF ZdGdH ZdIdJ ZdKdL Z dMdN Z!dOdP Z"dQdR Z#G dSdT dTe$Z%	U	UdWdVd Z&dS )X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                 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(   M/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/io/_idl.py	_align_32K   s   r*   c                 C   s   |  | dS )zSkip `n` bytesNreadr&   nr(   r(   r)   _skip_bytesT   s   
r/   c                 C   s
   |  |S )zRead the next `n` bytesr+   r-   r(   r(   r)   _read_bytesZ   s   
r0   c                 C   s$   t td| ddd d S )zRead a single bytez>Br   Nr   r   )npuint8structunpackr,   r&   r(   r(   r)   
_read_byte_      $r6   c                 C      t td| dd S )Read a signed 32-bit integer>lr   r   r1   int32r3   r4   r,   r5   r(   r(   r)   
_read_longd      r=   c                 C   $   t td| ddd d S )zRead a signed 16-bit integerz>hr   r   r   )r1   int16r3   r4   r,   r5   r(   r(   r)   _read_int16i   r7   rA   c                 C   r8   )r9   z>ir   r   r;   r5   r(   r(   r)   _read_int32n   r>   rB   c                 C   r8   )zRead a signed 64-bit integerz>qr
   r   )r1   int64r3   r4   r,   r5   r(   r(   r)   _read_int64s   r>   rD   c                 C   r?   )zRead an unsigned 16-bit integerz>Hr   r   r   )r1   uint16r3   r4   r,   r5   r(   r(   r)   _read_uint16x   r7   rF   c                 C   r8   )zRead an unsigned 32-bit integer>Ir   r   )r1   uint32r3   r4   r,   r5   r(   r(   r)   _read_uint32}   r>   rI   c                 C   r8   )zRead an unsigned 64-bit integerz>Qr
   r   )r1   uint64r3   r4   r,   r5   r(   r(   r)   _read_uint64   r>   rK   c                 C   r8   )zRead a 32-bit floatz>fr   r   )r1   float32r3   r4   r,   r5   r(   r(   r)   _read_float32   r>   rM   c                 C   r8   )zRead a 64-bit floatz>dr
   r   )r1   float64r3   r4   r,   r5   r(   r(   r)   _read_float64   r>   rO   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)selfrR   r(   r(   r)   __init__   s   zPointer.__init__N)__name__
__module____qualname____doc__rT   r(   r(   r(   r)   rP      s    rP   c                   @   s   e Zd ZdZdS )ObjectPointerz$Class used to define object pointersN)rU   rV   rW   rX   r(   r(   r(   r)   rY      s    rY   c                 C   s4   t | }|dkrt| |d}t|  |S d}|S )zRead a stringr   latin1 )r=   r0   decoder*   )r&   lengthcharsr(   r(   r)   _read_string   s   r_   c                 C   s6   t | }|dkrt | }t| |}t|  |S d}|S )z.Read a data string (length is specified twice)r   r[   )r=   r0   r*   )r&   r]   string_datar(   r(   r)   _read_string_data   s   
ra   c                 C   sL  |dkrt | dkrtdt| S |dkrt| S |dkr"t | S |dkr*t| S |dkr2t| S |dkrGt| }t| }t||d  S |d	krOt| S |d
krWtd|dkrlt| }t| }t	||d  S |dkrvt
t | S |dkrtt | S |dkrt| S |dkrt| S |dkrt| S |dkrt| S td| )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)rB   	Exceptionr6   rA   rM   rO   r1   	complex64ra   
complex128rP   rY   rF   rI   rD   rK   )r&   dtyperealimagr(   r(   r)   
_read_data   sJ   rh   c           
      C   s|  |d }|d }g }|D ]<}|d s|d r'| |d  |d ftjf q|d tv rA| |d  |d ft|d  f qtd|d  tjj|f|d}t|D ]J}|D ]E}|d }|d r}t	| |d	 |d  |d
 |d  ||d  |< qZ|d rt
| ||d	 |d  ||d  |< qZt| |||d  |< qZqV|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re   arrtablestructtablendimsr   dimsN)appendlowerr1   object_
DTYPE_DICTrb   recrecarrayrange_read_structure_read_arrayrh   intreversereshape)
r&   
array_descstruct_descnrowscolumnsre   colrk   irs   r(   r(   r)   r{      sB   "

r{   c           	      C   s  |dv r(|dkrt | }||d krtjddd tj| |d t| d}n9|dv rBtj| |d d	 t| ddd
d	 }ng }t|d D ]}|}t| |}|	| qJt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 headerr   
stacklevelro   )r   r   r   Nri   rr   rs   )rB   warningswarnr1   
frombufferr,   rw   rz   rh   rt   rl   rv   r}   r~   r   r*   )	r&   rn   r   r   rl   r   re   datars   r(   r(   r)   r|     s8   

r|   c                    s"  dt  i}t }|t tjd 7 }t d |d tvr)td|d  t|d  |d< |d dv r|d dkrDt |d< nt  |d< t d t	 }|d	 d
kri| 
 kred|d< n%tdt  }|dkrutd|d rt |d |d |d< n|d rt |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< n|d dkrt |d< t |d < t |d!< n|d d"krt |d#< n|d d$krt |d%< nt|d d&kr3t  |d'<  fd(d)t|d' D |d*< nW|d d+krVt  |d,< t |d-<  fd.d)t|d, D |d/< n4|d d0krbd1|d2< n(|d d3krqtjd4d5d6 n|d d7krtjd8d5d6 n
td9|d  d: | |S );z!Function to read in a full recordrectype        r   zUnknown RECTYPE: %i)r   r   r   varname
heap_indexrn   r   Nr   zUnexpected type code: 0r	   zVARSTART is not 7rk   r   r   rl   r   i   dateuserhostr   formatarchosreleaseIDENTIFICATONauthortitleidcoder   noticer   descriptionr   nvaluesc                       g | ]}t  qS r(   r=   .0_r5   r(   r)   
<listcomp>      z _read_record.<locals>.<listcomp>indicesCOMMONBLOCKnvarsrm   c                    r   r(   r_   r   r5   r(   r)   r     r   varnamesr   TendUNKNOWNzSkipping UNKNOWN recordr   r   r   zSkipping SYSTEM_VARIABLE recordzrecord['rectype']=z not implemented)r=   rI   astyper1   rC   r/   RECTYPE_DICTrb   r_   _read_typedescr$   
ValueErrorr{   r|   rh   ra   rz   r   r   r%   )r&   recordnextrecrectypedescvarstartre   r(   r5   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 r;t| |d	< t| |d
< |S |d rEt| |d	< |S )z%Function to read in a type descriptor)rn   varflagsr   r   z System variables not implementedr   rl       rk   r   r   )r=   rb   _read_arraydesc_read_structdesc)r&   typedescr(   r(   r)   r     s   r   c                    s0  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
< |S |d dkrtjddd t d t |d< t |d< t  |d< t d d|d< g |d
< t|d D ]}t  }|dkrtd|d
 t   qv|S td|d  )z'Function to read in an array descriptorarrstartr
   r   r   ri   rr   nmaxc                    r   r(   r   r   r5   r(   r)   r     r   z#_read_arraydesc.<locals>.<listcomp>rs      z$Using experimental 64-bit array readr   r   r   zExpected a zero in ARRAY_DESCzUnknown ARRSTART: %i)r=   r/   rz   r   r   rK   rb   rt   )r&   	arraydescdvr(   r5   r)   r     s4   



r   c                    sx  i }t  }|dkrtdt |d< t  }t  |d< t  |d< |d@ |d< |d@ |d	< |d
@ |d< |d s fddt|d D |d< |d D ]}t |d< qM fdd|d D |d<  fdd|d D |d< |d	 sx|d rt |d< t  |d<  fddt|d D |d<  fddt|d D |d< |t|d < |S |d tvrtdt|d  }|S )z*Function to read in a structure descriptorr   zSTRUCTSTART should be 9rm   ntagsr   r   predefr   inheritsr   is_superc                    r   r(   )_read_tagdescr   r5   r(   r)   r     s    z$_read_structdesc.<locals>.<listcomp>rj   c                    "   i | ]}|d  r|d t  qS )rl   rm   )r   r   tagr5   r(   r)   
<dictcomp>      z$_read_structdesc.<locals>.<dictcomp>rp   c                    r   )rk   rm   r   r   r5   r(   r)   r   	  r   rq   	classnamensupclassesc                    r   r(   r   r   r5   r(   r)   r         supclassnamesc                    r   r(   r   r   r5   r(   r)   r     r   supclasstablez"PREDEF=1 but can't find definition)r=   rb   r_   rz   STRUCT_DICT)r&   
structdescstructstartr   r   r(   r5   r)   r     sL   












	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v |d< |S )	z$Function to read in a tag descriptoroffsetrn   r   rl   r   rk   scalar)r=   rK   rw   )r&   tagdesctagflagsr(   r(   r)   r   !  s   r   c                 C   sV  t | tr:t | tr+| jdkrd } n| j|v r|| j } n	tjddd d } t | ts
t| |\}}|r6|} d| fS t | tjjr[t	| D ]\}}t||\}}|rV|| |< qEd| fS t | tj
r{t	| D ]\}}t||\}}|rv|| |< qed| fS t | tjr| jjtju rt| jD ]}t| ||\}}|r|| d|< qd| fS d| fS )Nr   zNVariable referenced by pointer not found in heap: variable will be set to Noner   r   TFr   )
isinstancerP   rR   r   r   _replace_heapr1   rx   ry   	enumerater   ndarrayre   typerv   rz   sizeitemr   )variableheapreplacenewirr   ivvaluer(   r(   r)   r   4  sL   




r   c                       sF   e Zd ZdZdddZ fddZ fddZd	d
 ZeZeZ	  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'
    Nc                 C   s   |d u ri }t | | d S rQ   )dictrT   )rS   initr(   r(   r)   rT     s   zAttrDict.__init__c                    s   t  | S rQ   )super__getitem__ru   rS   rm   	__class__r(   r)   r     s   zAttrDict.__getitem__c                    s   t  | |S rQ   )r   __setitem__ru   )rS   keyr   r   r(   r)   r     s   zAttrDict.__setitem__c              	   C   s8   z|  |W S  ty   tdt|  d| dd w )N'z' object has no attribute ')r   KeyErrorAttributeErrorr   r   r(   r(   r)   __getattr__  s   zAttrDict.__getattr__rQ   )rU   rV   rW   rX   rT   r   r   r   __setattr____call____classcell__r(   r(   r   r)   r   w  s    
r   Fc              	   C   s  g }|s|r	i }nt  }t| d}t|d}|dkr!td| t|d}	|	dkr+n|	dkr|r5td |r=t|d}
ntjd	d
}
|rMtd|
j  |
d 	 t	|}|
t
dt| t|}|t|tjd 7 }|d}t| dkrtd}|
t
dt||  |
t
dt|||  |  |
| nA| }t||| }|
 t| d }|
t
dt|d  |
t
dt||d  d  |
| |
| qS|  |
}|d ntd|	 	 t|}|| d|v r|d rnq|  i }|D ]}|d dkr)|d ||d < q|D ]$}|d dkrPt|d |\}}|rF||d< |d ||d  < q-|rW|D ]+}|d dkrtd td|d   td |d!   td"|d#    nqW|D ]4}|d d$krtd td%|d&   td'|d(   td)|d*   td+|d,    nq|D ]+}|d d-krtd td.|d/   td0|d1   td2|d3    nq|D ]}|d d4krtd td5|d6    nqtd td7t| d8 d9d: |D }t|D ]}|dkr/td;|||f  qtd d|v rWtd< |D ]}td=| d>t||  d? q@td |rh|D ]	}|| ||< q\|S |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.

    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: s    s    zIDL Save file is compressedzw+bz.sav)suffixz -> expanding to s   SR Tr:   r   r   r   rG   r   zInvalid RECFMT: r   r   r   r   r   r   r   r   z2--------------------------------------------------zDate: r   zUser: r   zHost: r   r   zFormat: r   zArchitecture: r   zOperating System: r   zIDL Version: r   r   zAuthor: r   zTitle: r   z	ID Code: r   r   zDescription: r   zSuccessfully read z records of which:c                 S   s   g | ]}|d  qS )r   r(   )r   rr(   r(   r)   r     r   zreadsav.<locals>.<listcomp>z - %i are of type %szAvailable variables:z - z []) r   openr0   rb   printtempfileNamedTemporaryFilerm   writer=   r3   packr}   rI   r   r1   rC   r,   r   r$   zlib
decompresslencloser%   r   rt   r   ru   setcountr   )	file_nameidictpython_dictuncompressed_file_nameverboserecords	variablesr&   	signaturerecfmtfoutr   r   unknownmodvalr'   
rec_stringr   r   r   r   r   rectypesrtvarr(   r(   r)   r     s   F






"

'


	

")NFNF)'__all__r3   numpyr1   r   r  r   rw   r   r   r*   r/   r0   r6   r=   rA   rB   rD   rF   rI   rK   rM   rO   rP   rY   r_   ra   rh   r{   r|   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   <module>   s   	*-0l/6C0