ó
î&]\c           @` 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 i d d 6d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z i d  d  6d! d 6d" d 6d# d
 6d$ d 6d% d 6d& d 6d' d 6d( d 6d) d 6d* d+ 6d, d- 6d. d/ 6d0 d1 6Z i  Z d2 „  Z d3 „  Z d4 „  Z d5 „  Z d6 „  Z d7 „  Z d8 „  Z d9 „  Z d: „  Z d; „  Z d< „  Z d= „  Z d> „  Z d? e f d@ „  ƒ  YZ dA e f dB „  ƒ  YZ dC „  Z  dD „  Z! dE „  Z" dF „  Z# dG „  Z$ dH „  Z% dI „  Z& dJ „  Z' dK „  Z( dL „  Z) dM „  Z* dN e+ f dO „  ƒ  YZ, d e. d e. dP „ Z/ d S(Q   i    (   t   divisiont   print_functiont   absolute_importt   readsavN(   t   asstrs   >u1i   s   >i2i   s   >i4i   s   >f4i   s   >f8i   s   >c8i   s   |Oi   i   s   >c16i	   i
   i   s   >u2i   s   >u4i   s   >i8i   s   >u8i   t   START_MARKERt   COMMON_VARIABLEt   VARIABLEt   SYSTEM_VARIABLEt
   END_MARKERt	   TIMESTAMPt   COMPILEDt   IDENTIFICATIONt   VERSIONt   HEAP_HEADERt	   HEAP_DATAi   t	   PROMOTE64i   t   NOTICEi   t   DESCRIPTIONi   c         C` s<   |  j  ƒ  } | d d k r8 |  j | d | d ƒ n  d S(   s+   Align to the next 32-bit position in a filei   i    N(   t   tellt   seek(   t   ft   pos(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt	   _align_32N   s    c         C` s   |  j  | ƒ d S(   s   Skip `n` bytesN(   t   read(   R   t   n(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _skip_bytesW   s    c         C` s   |  j  | ƒ S(   s   Read the next `n` bytes(   R   (   R   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_bytes]   s    c         C` s*   t  j t j d |  j d ƒ d  ƒ d ƒ S(   s   Read a single bytes   >Bi   i   i    (   t   npt   uint8t   structt   unpackR   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt
   _read_byteb   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read a signed 32-bit integers   >li   i    (   R   t   int32R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt
   _read_longg   s    c         C` s-   t  j t j d |  j d ƒ d d !ƒ d ƒ S(   s   Read a signed 16-bit integers   >hi   i   i    (   R   t   int16R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_int16l   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read a signed 32-bit integers   >ii   i    (   R   R!   R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_int32q   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read a signed 64-bit integers   >qi   i    (   R   t   int64R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_int64v   s    c         C` s-   t  j t j d |  j d ƒ d d !ƒ d ƒ S(   s   Read an unsigned 16-bit integers   >Hi   i   i    (   R   t   uint16R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_uint16{   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read an unsigned 32-bit integers   >Ii   i    (   R   t   uint32R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_uint32€   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read an unsigned 64-bit integers   >Qi   i    (   R   t   uint64R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_uint64…   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read a 32-bit floats   >fi   i    (   R   t   float32R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_float32Š   s    c         C` s&   t  j t j d |  j d ƒ ƒ d ƒ S(   s   Read a 64-bit floats   >di   i    (   R   t   float64R   R   R   (   R   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_float64   s    t   Pointerc           B` s   e  Z d  Z d „  Z RS(   s   Class used to define pointersc         C` s   | |  _  d  S(   N(   t   index(   t   selfR3   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   __init__—   s    	(   t   __name__t
   __module__t   __doc__R5   (    (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR2   ”   s   t   ObjectPointerc           B` s   e  Z d  Z RS(   s$   Class used to define object pointers(   R6   R7   R8   (    (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR9   œ   s   c         C` sJ   t  |  ƒ } | d k r@ t |  | ƒ } t |  ƒ t | ƒ } n d } | S(   s   Read a stringi    t    (   R"   R   R   R   (   R   t   lengtht   chars(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_string¡   s    
c         C` sJ   t  |  ƒ } | d k r@ t  |  ƒ } t |  | ƒ } t |  ƒ n d } | S(   s.   Read a data string (length is specified twice)i    R:   (   R"   R   R   (   R   R;   t   string_data(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_string_data­   s    c         C` sÖ  | d k r7 t  |  ƒ d k r- t d ƒ ‚ n  t |  ƒ S| d k rM t |  ƒ S| d k rc t  |  ƒ S| d k ry t |  ƒ S| d k r t |  ƒ S| d k rÈ t |  ƒ } t |  ƒ } t j | | d ƒ S| d	 k rÞ t |  ƒ S| d
 k rù t d ƒ ‚ nÙ | d k r2t |  ƒ } t |  ƒ } t j	 | | d ƒ S| d k rNt
 t  |  ƒ ƒ S| d k rjt t  |  ƒ ƒ S| d k r€t |  ƒ S| d k r–t |  ƒ S| d k r¬t |  ƒ S| d k rÂt |  ƒ St d | ƒ ‚ d S(   s*   Read a variable with a specified data typei   s*   Error occurred while reading byte variablei   i   i   i   i   y              ð?i   i   s'   Should not be here - please report thisi	   i
   i   i   i   i   i   s)   Unknown IDL type: %i - please report thisN(   R%   t	   ExceptionR    R$   R/   R1   R   t	   complex64R?   t
   complex128R2   R9   R)   R+   R'   R-   (   R   t   dtypet   realt   imag(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt
   _read_data¹   sJ    









c   
      C` sé  | d } | d } g  } x¥ | D] } | d s; | d rh | 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! Wt j | f d | ƒ} xÄ t | ƒ D]¶ } x­ | D]¥ } | d } | d rGt |  | d	 | d | d
 | d ƒ | | d | <qô | d r~t	 |  | | d	 | d ƒ | | d | <qô t
 |  | ƒ | | d | <qô Wqç W| d d k rå| d t | d ƒ  }	 |	 j ƒ  | j |	 ƒ } n  | S(   s   
    Read a structure, with the array and structure descriptors given as
    `array_desc` and `structure_desc` respectively.
    t	   nelementst   tagtablet	   structuret   arrayt   namet   typecodes    Variable type %i not implementedRC   t   arrtablet   structtablet   ndimsi   t   dims(   t   appendt   lowerR   t   object_t
   DTYPE_DICTR@   t   recarrayt   ranget   _read_structuret   _read_arrayRF   t   intt   reverset   reshape(
   R   t
   array_desct   struct_desct   nrowst   columnsRC   t   colRI   t   iRP   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyRW   ã   s8    

-

$
	$#
c   	   
   C` sc  | d k rp | d k rG t  |  ƒ } | | d
 k rG t j d ƒ qG n  t j |  j | d
 ƒ d t | ƒ} n¡ | d k r¶ t j |  j | d
 d ƒ d t | ƒd d d … } n[ g  } x: t | d ƒ D]( } | } t |  | ƒ } | j	 | ƒ qÍ Wt j
 | d t j ƒ} | d d k rU| d t | d ƒ  } | j ƒ  | j | ƒ } n  t |  ƒ | S(   s`   
    Read an array of type `typecode`, with the array descriptor given as
    `array_desc`.
    i   i   i   i   i   i	   i   i   i   t   nbytess.   Not able to verify number of bytes from headerRC   i   i   NRG   RO   RP   (	   i   i   i   i   i   i	   i   i   i   (   i   i   (   R%   t   warningst   warnR   t
   frombufferR   RT   RV   RF   RQ   RJ   RS   RY   RZ   R[   R   (	   R   RL   R\   Rb   RJ   Ra   RC   t   dataRP   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyRX     s,    

c         C` s,  i t  |  ƒ d 6} t |  ƒ } | t |  ƒ d4 7} t |  d ƒ | d t k rg t d | d ƒ ‚ n  t | d | d <| d d5 k r°| d d k r¬ t |  ƒ | d <n t  |  ƒ | d	 <t |  d ƒ t |  ƒ } | d
 d k r| |  j ƒ  k rd6 | d <q­t	 d ƒ ‚ qt  |  ƒ } | d k r:t d ƒ ‚ n  | d ret
 |  | d | d ƒ | d <q| d rt |  | d
 | d ƒ | d <q| d
 } t |  | ƒ | d <nk| d d k r t |  d7 ƒ t |  ƒ | d <t |  ƒ | d <t |  ƒ | d <n| d d k rSt  |  ƒ | d <t |  ƒ | d <t |  ƒ | d <t |  ƒ | d <nÈ| d d k r–t |  ƒ | d <t |  ƒ | d  <t |  ƒ | d! <n…| d d" k r¹t |  ƒ | d# <nb| d d$ k rÜt |  ƒ | d% <n?| d d& k r;t  |  ƒ | d' <g  | d( <xt | d' ƒ D] } | d( j t  |  ƒ ƒ qWnà | d d) k rªt  |  ƒ | d* <t |  ƒ | d+ <g  | d, <x£ t | d* ƒ D] } | d, j t |  ƒ ƒ q†Wnq | d d- k rÇt | d. <nT | d d/ k rçt j d0 ƒ n4 | d d1 k rt j d2 ƒ n t d3 | d ƒ ‚ |  j | ƒ | S(8   s!   Function to read in a full recordt   rectypei   i    i   s   Unknown RECTYPE: %iR   R   t   varnamet
   heap_indexRL   i    Rf   s   Unexpected type code: 0i   s   VARSTART is not 7RI   R\   R]   RJ   R
   i   t   datet   usert   hostR   t   formatt   archt   ost   releaset   IDENTIFICATONt   authort   titlet   idcodeR   t   noticeR   t   descriptionR   t   nvaluest   indicest   COMMONBLOCKt   nvarsRK   t   varnamesR	   t   endt   UNKNOWNs   Skipping UNKNOWN recordR   s   Skipping SYSTEM_VARIABLE records$   record['rectype']=%s not implementedI       (   R   R   Ni   (   R"   R+   R   t   RECTYPE_DICTR@   R=   t   _read_typedescR   t   Nonet
   ValueErrorRW   RX   RF   R?   RV   RQ   t   TrueRc   Rd   R   (   R   t   recordt   nextrect   rectypedesct   varstartRC   Ra   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   _read_record?  s†    




c         C` sÁ   i t  |  ƒ d 6t  |  ƒ d 6} | d d @d k rC t d ƒ ‚ n  | d d @d k | d <| d d @d k | d <| d r  t |  ƒ | d	 <t |  ƒ | d
 <n | d r½ t |  ƒ | d	 <n  | S(   s%   Function to read in a type descriptorRL   t   varflagsi   s    System variables not implementedi   RJ   i    RI   R\   R]   (   R"   R@   t   _read_arraydesct   _read_structdesc(   R   t   typedesc(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR   °  s     

c         C` s«  i t  |  ƒ d 6} | d d k r¼ t |  d ƒ t  |  ƒ | d <t  |  ƒ | d <t  |  ƒ | d <t |  d ƒ t  |  ƒ | d <g  | d <xt | d ƒ D] } | d j t  |  ƒ ƒ q˜ Wnë | d d	 k r“t j d
 ƒ t |  d ƒ t |  ƒ | d <t |  ƒ | d <t  |  ƒ | d <t |  d ƒ d | d <g  | d <xm t | d ƒ D]D } t  |  ƒ } | d k rut d ƒ ‚ n  | d j t  |  ƒ ƒ qHWn t d | d ƒ ‚ | S(   s'   Function to read in an array descriptort   arrstarti   i   Rb   RG   RO   t   nmaxRP   i   s$   Using experimental 64-bit array readi    s   Expected a zero in ARRAY_DESCs   Unknown ARRSTART: %i(   R"   R   RV   RQ   Rc   Rd   R-   R@   (   R   t	   arraydesct   dt   v(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR‰   Ä  s6    


c         C` sw  i  } t  |  ƒ } | d k r- t d ƒ ‚ n  t |  ƒ | d <t  |  ƒ } t  |  ƒ | d <t  |  ƒ | d <| d @| d <| d @| d	 <| d
 @| d <| d sFg  | d <x/ t | d ƒ D] } | d j t |  ƒ ƒ q¸ Wx" | d D] } t |  ƒ | d <qä Wi  | d <x7 | d D]+ } | d rt |  ƒ | d | d <qqWi  | d <x7 | d D]+ } | d rWt |  ƒ | d | d <qWqWW| d	 sš| d r5t |  ƒ | d <t  |  ƒ | d <g  | d <x/ t | d ƒ D] } | d j t |  ƒ ƒ qÕWg  | d <x2 t | d ƒ D] } | d j t |  ƒ ƒ qWn  | t | d <n- | d t k ret d ƒ ‚ n  t | d } | S(   s*   Function to read in a structure descriptori	   s   STRUCTSTART should be 9RK   t   ntagsRb   i   t   predefi   t   inheritsi   t   is_superRH   RM   RJ   RN   RI   t	   classnamet   nsupclassest   supclassnamest   supclasstables"   PREDEF=1 but can't find definition(	   R"   R@   R=   RV   RQ   t   _read_tagdescR‰   RŠ   t   STRUCT_DICT(   R   t
   structdesct   structstartR’   t   tt   tagt   s(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyRŠ   õ  sN    







c         C` s’   i t  |  ƒ d 6} | d d k r6 t |  ƒ | d <n  t  |  ƒ | d <t  |  ƒ } | d @d k | d <| d @d k | d <| d t k | d <| S(	   s$   Function to read in a tag descriptort   offsetiÿÿÿÿRL   i   RJ   i    RI   t   scalar(   R"   R-   RT   (   R   t   tagdesct   tagflags(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR™   0  s    c         C` sþ  t  |  t ƒ r x] t  |  t ƒ rn |  j d k r9 d  }  q |  j | k rX | |  j }  q t j d ƒ d  }  q Wt |  | ƒ \ } } | r“ | }  n  t |  f St  |  t j	 j
 j ƒ rxB t |  ƒ D]4 \ } } t | | ƒ \ } } | rÂ | |  | <qÂ qÂ Wt |  f St  |  t j	 j
 j ƒ rkxB t |  ƒ D]4 \ } } t | | ƒ \ } } | r)| |  | <q)q)Wt |  f St  |  t j ƒ rð|  j j t j k ræxQ t |  j ƒ D]= } t |  j | ƒ | ƒ \ } } | r¢|  j | | ƒ q¢q¢Wn  t |  f St |  f Sd  S(   Ni    sN   Variable referenced by pointer not found in heap: variable will be set to None(   t
   isinstanceR2   R3   R€   Rc   Rd   t   _replace_heapR‚   R   t   coret   recordsRU   t	   enumeratet   FalseRƒ   t   ndarrayRC   t   typeRS   RV   t   sizet   itemt   itemset(   t   variablet   heapt   replacet   newt   irRƒ   t   ivt   value(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR¥   C  s@    	
	



t   AttrDictc           B` s>   e  Z d  Z i  d „ Z d „  Z d „  Z e Z e Z e Z RS(   s.  
    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(   t   dictR5   (   R4   t   init(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR5   –  s    c         C` s   t  t |  ƒ j | j ƒ  ƒ S(   N(   t   superR¶   t   __getitem__RR   (   R4   RK   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyRº   ™  s    c         C` s   t  t |  ƒ j | j ƒ  | ƒ S(   N(   R¹   R¶   t   __setitem__RR   (   R4   t   keyRµ   (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR»   œ  s    (	   R6   R7   R8   R5   Rº   R»   t   __getattr__t   __setattr__t   __call__(    (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR¶   …  s   		c         C` sB  g  } | s | r i  } n	 t  ƒ  } t |  d ƒ } t | d ƒ } | d k ra t d | ƒ ‚ n  t | d ƒ }	 |	 d k r n |	 d k r| rž t d ƒ n  | r¶ t | d ƒ }
 n t j d	 d
 ƒ }
 | râ t d |
 j ƒ n  |
 j d ƒ x}t	 rn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> d? ƒ ƒ ƒ |
 j | ƒ Pn  | j ƒ  } t j | j | | ƒ ƒ } |
 j ƒ  t | ƒ d } |
 j t j d t | d@ ƒ ƒ ƒ |
 j t j d t | | dA dB ƒ ƒ ƒ |
 j | ƒ |
 j | ƒ qò W| j ƒ  |
 } | j d ƒ n t d |	 ƒ ‚ x@ t	 rát | ƒ } | j | ƒ d | k r¢| d rÞPqÞq¢q¢W| j ƒ  i  } x3 | D]+ } | d d k rù| d | | d <qùqùWxe | D]] } | d d k r/t | d | ƒ \ } } | rq| | d <n  | d | | d j ƒ  <q/q/W| rxf | D]^ } | d d k rt d d ƒ t d | d ƒ t d  | d! ƒ t d" | d# ƒ PqqWxx | D]p } | d d$ k rt d d ƒ t d% | d& ƒ t d' | d( ƒ t d) | d* ƒ t d+ | d, ƒ PqqWxf | D]^ } | d d- k rt d d ƒ t d. | d/ ƒ t d0 | d1 ƒ t d2 | d3 ƒ PqqWxB | D]: } | d d4 k rêt d d ƒ t d5 | d6 ƒ PqêqêWt d d ƒ t d7 t | ƒ ƒ g  | D] } | d ^ qQ} x@ t | ƒ D]2 } | d k rtt d8 | j | ƒ | f ƒ qtqtWt d d ƒ d | k rt d9 ƒ x, | D]$ } t d: | t | | ƒ f ƒ qÕWt d d ƒ qn  | r:x | D] } | | | | <qW| S| Sd; S(C   s  
    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.

    t   rbi   t   SRs   Invalid SIGNATURE: %ss    s    s   IDL Save file is compresseds   w+bt   suffixs   .savs    -> expanding to %ss   SR s   >li    i   R	   s   >Ii   s   Invalid RECFMT: %sR|   Rg   R   Rf   Ri   R   Rh   R
   t   -i2   s   Date: %sRj   s   User: %sRk   s   Host: %sRl   R   s
   Format: %sRm   s   Architecture: %sRn   s   Operating System: %sRo   s   IDL Version: %sRp   Rq   s
   Author: %sRr   s	   Title: %sRs   s   ID Code: %sRt   R   s   Description: %sRv   s&   Successfully read %i records of which:s    - %i are of type %ss   Available variables:s
    - %s [%s]NI       I       I       I       I       I       I       (   R¶   t   openR   R@   t   printt   tempfilet   NamedTemporaryFileRK   t   writeR‚   R"   R   t   packRY   R+   R   R~   R   t   zlibt
   decompresst   lent   closeR   R‡   RQ   R¥   RR   t   sett   countR«   (   t	   file_namet   idictt   python_dictt   uncompressed_file_namet   verboseR§   t	   variablesR   t	   signaturet   recfmtt   foutRg   R„   t   unknownR   t
   rec_stringt   rR°   R±   R²   Rƒ   t   rectypest   rtt   var(    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyR   ¤  sÒ    (			#+#+
	

$
"(0   t
   __future__R    R   R   t   __all__R   t   numpyR   t   numpy.compatR   RÆ   RÊ   Rc   RT   R~   Rš   R   R   R   R    R"   R$   R%   R'   R)   R+   R-   R/   R1   t   objectR2   R9   R=   R?   RF   RW   RX   R‡   R   R‰   RŠ   R™   R¥   R·   R¶   R€   R©   R   (    (    (    s+   lib/python2.7/site-packages/scipy/io/idl.pyt   <module>   s„   	



																	*	-	/	q		1	;		B