³ò
T¶GJc        
   @   sû   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d e	 f d „  ƒ  YZ
 d e
 f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ e ƒ  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d S(   s_  
scopelog.py

A tool for parsing "Tlog.dat" created by scope programs and maintaining a cache of log files.

Classes: 'TraceInfo', 'ScopeLog', 'ScopeLogCache'
Functions: 'load', 'getinfo', 'getinfos', 'loadbinary', 'loadbinaries'

Please note, that 'ScopeLog' is not derived from 'TimeLog'
'ScopeLog' is not multi-thread safe!!!

Version: 26 May 2008
iÿÿÿÿNt	   TraceInfoc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sf  
    A record about a trace in the log

    Contains:
    dir - directory where both the log and the trace reside
    filename - trace file name
    Nav - number of averages
    smplrate - sampling rate [1/s]
    vrange - scope input range [+-V]
    delay - scope delay [s]
    starttime - time [sec since C Epoch] when the aquisition started
    finaltime - time [sec since C Epoch] when the aquisition finished
    T_MCTNS_s - starting MCTNS temperature [mK, Greywall scale]
    T_MCTNS_f - final MCTNS temperature [mK, Greywall scale]
    T_MCTNS - mean MCTNS temperature [mK, Greywall scale] - Mean object
    c
   
      C   sU   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ d  S(   N(	   t   patht   Navt   smplratet   delayt   vranget	   starttimet	   finaltimet	   T_MCTNS_st	   T_MCTNS_f(
   t   selfR   R   R   R   R   R   R   R   R	   (    (    s   c:\py\lib\scopelog.pyt   __init__6   s    								c         C   s   t  i i |  i ƒ S(   s   trace filename without path(   t   osR   t   basename(   R
   (    (    s   c:\py\lib\scopelog.pyR   A   s    c         C   s   t  i i |  i ƒ S(   s&   name of directory containing the trace(   R   R   t   dirname(   R
   (    (    s   c:\py\lib\scopelog.pyR   B   s    c         C   s   t  i |  i |  i ƒ S(   s=   return a string with timespan when the signal was taken (UTC)(   t   flibt   timespan2strR   R   (   R
   (    (    s   c:\py\lib\scopelog.pyt   timespanC   s    c         C   s   |  g S(   sE   return a list containing 'self' - see 'AveragedTraceInfo' for details(    (   R
   (    (    s   c:\py\lib\scopelog.pyt
   recordlistD   s    c         C   s   d |  i  ƒ  |  i ƒ  f S(   Ns   %s: %s(   R   R   (   R
   (    (    s   c:\py\lib\scopelog.pyt   __repr__E   s    c         C   s   |  i  d j o
 |  i  n d S(   Ni    i   (   R   (   R
   (    (    s   c:\py\lib\scopelog.pyt   safeNavF   s    (
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   (    (    (    s   c:\py\lib\scopelog.pyR    $   s   						t   AveragedTraceInfoc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s:   
    A combined record about a set of averaged traces
    c          C   s‡  t  i g  } | D] } | | i q ~ ƒ } t  i g  } | D] } | | i q> ~ ƒ } t  i g  }	 | D] } |	 | i qk ~	 ƒ }
 t | ƒ d j o7 t | ƒ t | ƒ j o t d ƒ ‚ qÚ | d } n
 t	 i
 } t | ƒ d j o7 t | ƒ t | ƒ j o t d ƒ ‚ q-| d } n
 t	 i
 } t |
 ƒ d j o$ t |
 ƒ t |
 ƒ j o |
 d n t	 i
 } t	 i g  } | D] } | | i ƒ  q~~ ƒ } t	 i g  } | D] } | | i q®~ ƒ } t	 i g  } | D] } | | i qÛ~ ƒ } t	 i g  } | D] } | | i q~ ƒ } t	 i g  } | D] } | | i q5~ ƒ } t | ƒ } t | ƒ } | | | j d } | | | j d } d | i d ƒ d } x0 t t | ƒ ƒ D] } | | i ƒ  o Pq»q»W| | d  } | i | d ƒ d t  i g  } | D] } | | | q~ d d	 d
 d ƒ} t i |  | | | | | | | | | ƒ
 | |  _ | |  _ | |  _ | |  _ d S(   s²   
        records - a set of TraceLog's of inidividual traces
        pathtemplate - filename template
        indices - indices used for making filenames out of template
        i    s4   Can not average traces with different sampling ratess7   Can not average traces with different aquisition delayst   %i   t   *s   , *=t   maxleni   t   sept   ,N(   R   t   finitesR   R   R   t   lent   maxt   mint
   ValueErrort   numpyt   NaNt   sumR   t   arrayR   R   R   R	   t   splitt   ranget   isalphat   replacet   a2strR    R   t   recordst   pathtemplatet   indicest   fmt(   R
   R,   R-   R.   t   _[1]t   it	   smplratest   _[2]t   delayst   _[3]t   vrangesR   R   R   t   _[4]R   t   _[5]t
   starttimest   _[6]t
   finaltimest   _[7]t
   TT_MCTNS_st   _[8]t
   TT_MCTNS_fR   R   R   R	   R/   t   _[9]R   (    (    s   c:\py\lib\scopelog.pyR   L   sF    --- 	 	@0----  	N(			c         C   s9   |  i  i d ƒ } t i  i | d ƒ | d <d i | ƒ S(   s.   trace filename without common path and indicesR   i    (   R   R'   R   R   t   join(   R
   t   parts(    (    s   c:\py\lib\scopelog.pyR   z   s    c         C   s    t  i i |  i i d ƒ d ƒ S(   s)   name of a common directory for the tracesR   i    (   R   R   R   R'   (   R
   (    (    s   c:\py\lib\scopelog.pyR   €   s    c         C   s   |  i  S(   s9   return a list of individual records contained in this set(   R,   (   R
   (    (    s   c:\py\lib\scopelog.pyR      s    (   R   R   R   R   R   R   R   (    (    (    s   c:\py\lib\scopelog.pyR   H   s
   	.		t   ScopeLogc           B   sD   e  Z d  Z g  d „ Z e d „  ƒ Z d „  Z d „  Z d „  Z RS(   sÇ   
    A representation of a scope log file. Contains a dictionary of entries
    for each trace and a timestamp of the log file.

    Please note, that 'ScopeLog' is not derived from 'TimeLog'!!!
    c         C   s;   t  i |  | ƒ g  |  _ | |  _ t i i | ƒ |  _ d  S(   N(   t   dictR   t   ordert   logfileR   R   t   getmtimet   logfile_modified(   R
   RF   t   list(    (    s   c:\py\lib\scopelog.pyR   ‹   s    		c         C   s†  t  i i |  ƒ }  t  i i |  ƒ oX x[ d d g D]3 } t  i i |  | ƒ } t  i i | ƒ o Pq2 q2 Wt d |  ƒ ‚ n |  } t  i i | ƒ }  t | ƒ } t	 | d ƒ } zÄd } x·| D]¯} | d 7} | i
 ƒ  } t | ƒ d j  p | d i ƒ  o qÀ n | i d ƒ d j o] g  } | i d ƒ \ } } }	 | | i d	 ƒ d
  7} | | g 7} | |	 i d	 ƒ d 7} n | i d	 ƒ } y³t | ƒ d j oˆ t t  i i |  | d ƒ t | d ƒ t i t i t i t i t | d ƒ ƒ t i t | d ƒ ƒ t | d ƒ t | d ƒ ƒ	 }
 nÁ t | ƒ d j o t t  i i |  | d ƒ t | d ƒ t | d ƒ t | d ƒ t | d ƒ t i t | d ƒ ƒ t i t | d ƒ ƒ t | d ƒ t | d ƒ ƒ	 }
 n t d | ƒ ‚ | i |
 i ƒ o! t i d |
 i ƒ  | f ƒ n |
 | |
 i <| i i |
 i ƒ WqÀ t j
 o# } t i d | | | f ƒ qÀ XqÀ WWd | i ƒ  X| S(   s]  
        Load a scope log file.
        If 'path' points to a directory, look for a logfile named 'logT.dat' or 'log.dat'
        in it, in this order. Otherwise load a logfile from a file pointed by 'path'
        itself.
        
        Return a dictionary with absolute trace filenames as keys and 'TraceInfo'
        objects as values.
        s   logT.dats   log.dats   No scope log file found in '%s't   ri    i   t   "i   s   	iÿÿÿÿi
   i   i   i   i   i   i   i   i   i   i   i   s   %s: uknown log file formats*   '%s' is featured twice in the logfile '%s's!   Can't process line %d in '%s': %sN(   R   R   t   abspatht   isdirRA   t   isfileR"   R   RC   t   opent   stripR   t   isdigitt   countR'   R    t   intR#   R$   R   t
   labview2tmt   floatt   has_keyt   loggingt   warnR   RE   t   appendt	   Exceptiont   close(   R   RF   t   scopelogt   handlet   linenot   linet   tokenst   pret   filenamet   postt   recordt   e(    (    s   c:\py\lib\scopelog.pyt   load‘   sb     	 
%&$&'$!'c         C   s   t  i i |  i ƒ |  i j S(   s:   Return True is a logfile on disk is newer than this object(   R   R   RG   RF   RH   (   R
   (    (    s   c:\py\lib\scopelog.pyt
   isoutdatedü   s    c         C   s   d |  i  t |  ƒ f S(   Ns   scope log '%s' %d entries(   RF   R   (   R
   (    (    s   c:\py\lib\scopelog.pyR      s    c         C   s   t  i i |  i ƒ S(   N(   R   R   R   RF   (   R
   (    (    s   c:\py\lib\scopelog.pyR     s    (	   R   R   R   R   t   staticmethodRf   Rg   R   R   (    (    (    s   c:\py\lib\scopelog.pyRC   ƒ   s   k		t   ScopeLogCachec           B   s;   e  Z d  Z d d „ Z d „  Z d „  Z e d „  ƒ Z RS(   s—  
    A cache for scope logs. The most recently demanded logs are kept in memory
    and a log is only read from the hard drive and parsed if it is not in the
    cache or if a cached version is outdated.
    
    Logs are kept in a list ordered from the most to the least recently
    demanded and if a critical number is exceeded, the log from the tail of the
    list is purged when adding a new one.
    i   c         C   s*   | d j  o t  d ƒ ‚ n | |  _ d S(   s@   
        Create a cache. Maximum size can be specified.
        i   s   Cache size should be positiveN(   R"   t	   cachesize(   R
   Rj   (    (    s   c:\py\lib\scopelog.pyR     s    c         C   sO   | d j  o t  d ƒ ‚ n | |  _ x" t |  ƒ | j o |  i ƒ  q) Wd  S(   Ni   s   Cache size should be positive(   R"   Rj   R   t   pop(   R
   Rj   (    (    s   c:\py\lib\scopelog.pyt
   setmaxsize  s    	 c         C   s_  t  i i | ƒ } t  i i | ƒ } x„ |  D]v } t  i i | i ƒ  ƒ p t  i i | i ƒ o |  i | ƒ q+ n |  i | i ƒ  | ƒ o |  i | ƒ Pq+ q+ Wd } | o | i	 ƒ  o ~ d } n | d j o1 y t
 i | ƒ } Wq
t j
 o d Sq
Xn t |  ƒ |  i j o |  i ƒ  n |  i d | ƒ | | j o d Sn	 | | Sd S(   sz   
        Return a 'TraceInfo' about a trace pointed by filename if available.
        Otherwise None is returned.
        i    N(   R   R   RL   R   t   existsRF   t   removet   samefilet   NoneRg   RC   Rf   R"   R   Rj   Rk   t   insert(   R
   t   tracet   dirt   log(    (    s   c:\py\lib\scopelog.pyt   getinfo  s2     1	
c         C   s\   d t  i i j o t  i i |  | ƒ Sn/ t  i i |  ƒ i ƒ  t  i i | ƒ i ƒ  j Sd  S(   NRo   (   R   R   t   __dict__Ro   RL   t   lower(   t   file1t   file2(    (    s   c:\py\lib\scopelog.pyRo   M  s    (   R   R   R   R   Rl   Ru   Rh   Ro   (    (    (    s   c:\py\lib\scopelog.pyRi     s
   			.c         C   s   t  i |  ƒ S(   sÇ   
    Return information about a trace refered by its path as a 'TraceInfo'
    object or 'None' if no appropriate log file was found. This function is
    a frontend to a static 'ScopeLogCache'.
    (   t   __logcache__Ru   (   Rr   (    (    s   c:\py\lib\scopelog.pyRu   W  s    c         C   s    g  } | D] } | |  | q ~ } t  | ƒ d j  o t d ƒ ‚ n g  } x? | D]7 } t | ƒ } | d j o ~ d Sn | i | ƒ qU Wt | |  | ƒ S(   s¥   
    Return information about a traces refered by path as a 'AveragedTraceInfo'
    object or 'None' if no appropriate log file was found for any of the traces.
    i   s"   No traces are going to be averagedN(   R   R"   Ru   Rp   RY   R   (   Rb   R.   R0   R1   t	   filenamest   infost   fnt   info(    (    s   c:\py\lib\scopelog.pyt   getinfos`  s    % c         C   s   t  i |  ƒ d S(   s   Modify static log cache sizeN(   Rz   Rl   (   t   size(    (    s   c:\py\lib\scopelog.pyt   setcachesizeu  s    c           C   s   t  i S(   s   Return static log cache size(   Rz   Rj   (    (    (    s   c:\py\lib\scopelog.pyt   getcachesizew  s    c      
   C   s=  t  i i |  ƒ p t d |  ƒ ‚ n t |  ƒ } | d j oI | d j o t d |  ƒ ‚ n | i d j p t d |  ƒ ‚ q‰ n | d j	 o | i d j o | i } n t i i	 |  | ƒ } | d j o[ t
 t  i i |  ƒ t i | t i t i t  i i |  ƒ t  i i |  ƒ t i t i ƒ	 } n | | _ | S(   sÜ   
    Loads signal from a binary file and fill all log information.
    'smplrate' argument is used if sampling rate could not be inferred from the log (1st version).
    It should be specified in samples per second.
    s   %s: no such filesF   Not scope log found for '%s'. Please specify sampling rate explicitelyi    sD   Sampling rate was not logged for '%s'. Please specify it explicitelyN(   R   R   Rm   R"   Ru   Rp   R   t   nmrt   Tracet
   loadbinaryR    RL   R#   R$   t   getctimeRG   R~   (   Rb   R   R~   Rr   (    (    s   c:\py\lib\scopelog.pyR…   y  s    [	c         C   sÔ   g  } | D] } | |  | q ~ } t  | ƒ d j  o t d ƒ ‚ n d } d } g  } xT | D]L }	 t |	 | ƒ }
 |
 i i ƒ  } | | |
 7} | | 7} | i |
 i ƒ qa W| | :} t | |  | ƒ | _ | S(   sê   
    Loads signals from binary files, average them and fill all log information.
    'smplrate' argument is used if sampling rate could not be inferred from the log (1st version).
    It should be specified in samples per second.
    i   s"   No traces are going to be averagedi    (   R   R"   R…   R~   R   RY   R   (   Rb   R.   R   R0   R1   R{   t   averaget   NR|   R}   Rr   R   (    (    s   c:\py\lib\scopelog.pyt   loadbinaries“  s     % 

(   R   R#   t   os.pathR   RW   t   globR   t   averRƒ   t   objectR    R   RD   RC   RI   Ri   Rz   Ru   R   R   R‚   Rp   R…   R‰   (    (    (    s   c:\py\lib\scopelog.pys   <module>   s   0$$;Q						