³ò
dÄ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 d k l Z d d k l Z d d k l Z d d k l Z d d	 k l Z d d
 k l Z d S(   s>   
Generic routines for manipulating log files.
5 Dec 2008, Lev
iÿÿÿÿNt   TimeLogc           B   sø   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d d d „ Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z e d d e d „ ƒ Z d d „ Z RS(   s_  
    A base class for various logs. Data is stored in a 2-dimensional array 'self.data':
    First column ([:,0]) is time (sec, since 00:00 1 January 1970 GMT), the rest is log-specific
    
    'self.device' contains the name of the logged device. The purpose is to separate logs
    of the same type, e.g. paroscientific and melting curve thermometry logs.

    Two logs can be concatenated with a '&' sign,
    one of the logs can be None - in this case the other is returned as a result of concatenation.

    The child classes should provide the following functionality:
    1. Methods to access custom 'self.data' fields.
    2. A 'fieldcount()' static method that returns number of columns in 'self.data' array.
    3. A 'load(filename)' static method that should load a log from a file and return an object of
       the appropriate child class.
    
    c         C   s‰   | d j o t i d |  i ƒ  f ƒ } n t i | ƒ } t | i ƒ d j p | i d |  i ƒ  j o t Sn | |  _ | |  _	 d S(   s„  
        TimeLog constructor.
        
        'log' is an N x fieldcount() array, containing the log. If the argument is None, the empty log is created.
        'device' is the name of the device that was used for the log.
        
        Columns have the following meaning:
        
        1  [:,0]  Time (sec, since 00:00 1 January 1970 GMT)
        ... rest is log-specific
        i    i   i   N(
   t   Nonet   numpyt   zerost
   fieldcountt   arrayt   lent   shapet
   ValueErrort   datat   device(   t   selft   logR
   (    (    s   c:\py\lib\timelog.pyt   __init__(   s    0	c         C   s   t  |  i ƒ S(   N(   R   R	   (   R   (    (    s   c:\py\lib\timelog.pyt   __len__>   s    c         C   s   |  i  d d … d f S(   s,   time, seconds since 00:00 1 January 1970 GMTNi    (   R	   (   R   (    (    s   c:\py\lib\timelog.pyt   t@   s    c         C   s   t  i i |  i ƒ  ƒ S(   s!   time for pylab.plot_date function(   t
   matplotlibt   datest	   epoch2numR   (   R   (    (    s   c:\py\lib\timelog.pyt   plottimeA   s    c         C   s1   t  i t  i g |  i ƒ  d |  i ƒ  d  f ƒ S(   sn   
        Return intervals between pulses, first element is NaN - the delay before the first pulse, so
        i   iÿÿÿÿ(   R   t   concatenatet   NaNR   (   R   (    (    s   c:\py\lib\timelog.pyt   delay_beforeC   s    c         C   s   t  |  ƒ d j  S(   s?   return True if there are no entries in the log, False otherwisei   (   R   (   R   (    (    s   c:\py\lib\timelog.pyt   isemptyI   s    c         C   s   t  |  ƒ |  i i ƒ  |  i ƒ S(   N(   t   typeR	   t   copyR
   (   R   (    (    s   c:\py\lib\timelog.pyR   K   s    c         C   sJ   t  |  ƒ t  | ƒ j o1 |  i i d | i i d j o |  i | i j S(   sj   Return True if the two TimeLogs are compatible (of the same type and for the same device), otherwise Falsei   (   R   R	   R   R
   (   R   t   other(    (    s   c:\py\lib\timelog.pyt
   compatibleM   s    c         C   sZ   | d  j o |  Sn |  i | ƒ p
 t ‚ n t |  ƒ t i |  i | i f ƒ |  i ƒ S(   N(   R   R   R   R   R   R   R	   R
   (   R   R   (    (    s   c:\py\lib\timelog.pyt   __and__S   s
    
c         C   s   | d  j o |  Sn t ‚ d  S(   N(   R   R   (   R   R   (    (    s   c:\py\lib\timelog.pyt   __rand__^   s    c         C   sN   |  i  ƒ  o d Sn |  i |  i d d … d f i ƒ  d d … f |  _ d S(   s7   
        Sort the log accending the timestamps
        Ni    (   R   R	   t   argsort(   R   (    (    s   c:\py\lib\timelog.pyt   sortd   s    c         C   sL   t  |  ƒ t |  ƒ d j o |  i | n t i d |  i ƒ  f ƒ |  i ƒ S(   sV   
        Return a subset of the log consisting of entries listed in 'indices'
        i    (   R   R   R	   R   R   R   R
   (   R   t   indices(    (    s   c:\py\lib\timelog.pyt   subsetl   s    c         C   s¢   |  i  ƒ  o |  Sn | d j o t |  i ƒ  ƒ } n t i | ƒ } | d j o t |  i ƒ  ƒ } n t i | ƒ } |  i |  i ƒ  | j |  i ƒ  | j @ƒ S(   sô   
        Return a subset of the log between the two dates, in C and UNIX format or free-form strings.
        If the start date is not specified, the earliest is assumed.
        If the end date is not specified, the latest is assumed.
        N(   R   R   t   minR   t   flibt   tmt   maxR!   (   R   t   startt   end(    (    s   c:\py\lib\timelog.pyt   periodr   s    c         C   sP   |  i  ƒ  o |  Sn |  i |  i ƒ  t i | ƒ j |  i ƒ  t i | ƒ j Bƒ S(   su   
        Return a subset of the log outside the specified period, in C and UNIX format or free-form strings.
        (   R   R!   R   R#   R$   (   R   R&   R'   (    (    s   c:\py\lib\timelog.pyt   noperiodˆ   s    c         C   s»   t  i | ƒ } | d j	 o t  i | ƒ n | } |  i ƒ  } t | | j ƒ o t i | | | j ƒ } n t | | j ƒ o t i | | | j ƒ } n |  i | | j | | j @ƒ S(   s¶   
        Return a subset of the log encompassing a period of time defined by
        'start'-'end' or a moment in time defined by 'start', if 'end' is not
        specified.
        N(	   R#   R$   R   R   t   anyR   R%   R"   R!   (   R   R&   R'   R   (    (    s   c:\py\lib\timelog.pyt   around’   s    #  c         C   sX   |  i  d  j o! d t |  ƒ i |  i ƒ  f Sn$ d |  i  t |  ƒ i |  i ƒ  f Sd  S(   Ns   %s %ss   %s %s %s(   R
   R   R   t   __name__t   datespan(   R   (    (    s   c:\py\lib\timelog.pyt   __repr__£   s    !c         C   sA   |  i  ƒ  o d Sn) t i t |  i ƒ  ƒ t |  i ƒ  ƒ ƒ Sd S(   sK   
        Return string representation of the date range of the log
        s   (empty)N(   R   R#   R-   R"   R   R%   (   R   (    (    s   c:\py\lib\timelog.pyR-   ©   s    c         C   s8   |  i  ƒ  } |  i | t | | t i | ƒ j ƒ j ƒ S(   s¬   
        Return a single point log with the regord at the moment 't', or just before it.
        Time is accepted in any format recognised by 'flib.tm()'.
        
        (   R   R!   R%   R#   R$   (   R   R   t   tt(    (    s   c:\py\lib\timelog.pyt
   justbefore²   s    c         C   s8   |  i  ƒ  } |  i | t | | t i | ƒ j ƒ j ƒ S(   s¬   
        Return a single point log with the regord at the moment 't', or just before it.
        Time is accepted in any format recognised by 'flib.tm()'.
        
        (   R   R!   R"   R#   R$   (   R   R   R/   (    (    s   c:\py\lib\timelog.pyt	   justafter»   s    c   
   	      s~  ˆ  d j o d „  } n ‡  f d †  } t | ƒ t |  ƒ j o t d ƒ ‚ n t i t |  ƒ g d t ƒ} |  i ƒ  } xû t t |  ƒ ƒ D]ç } xc t | d d d ƒ D]K }	 | |	 | | | j  o Pn | | |	 | | ƒ p t | | <Pq¬ q¬ W| | op xm t | d t |  ƒ d ƒ D]K }	 | |	 | | | j o Pn | | |	 | | ƒ p t | | <Pq#q#Wq q W| S(   s{  
        Return a boolean array of the length of the log with True in those
        cells only where 'condition' (a 1D array of the same length as the log)
        is the same as in every point within 'before' seconds before the cell
        in question and 'after' seconds after the it
        (time is the only information retained from the log itself).
        If 'allow_change_by' is 'None', the condition is considered steady if
        it does not change, otherwise changes not greater than the value of this
        argument are allowed. Both 'condition' and 'allow_change_by' are expected
        to be numerical then.
        c         S   s
   |  | j S(    (    (   t   at   b(    (    s   c:\py\lib\timelog.pyt   <lambda>Ò   s    c            s   t  | |  ƒ ˆ  j S(    (   t   abs(   R2   R3   (   t   allow_change_by(    s   c:\py\lib\timelog.pyR4   Ô   s    s   "condition" is wrong sizet   dtypei   iÿÿÿÿN(	   R   R   R   R   t   onest   boolR   t   xranget   False(
   R   t	   conditiont   beforet   afterR6   t   samet   maskR   t   it   j(    (   R6   s   c:\py\lib\timelog.pyt   steadyÄ   s2      
	 
c   
   
   C   sh  | d j	 o t i t i | ƒ ƒ } n d } | d j	 o t i t i | ƒ ƒ } n d } d } xÒ t i | ƒ D]Á } d }	 t | ƒ d j o‡ | d  | j ov | d  | j oe | o< y" |  i t i i	 | | ƒ ƒ }	 Wqt
 j
 o qXq|  i t i i	 | | ƒ ƒ }	 n |	 d j	 o | |	 M} qt qt W| d j o |  ƒ  Sn | i ƒ  | i | ƒ S(   s4  
        Load log files from a given directory.
        The function walkes through a directory and treats files named 'YYYYMMDD<suffix>.dat' as
        log files.
        
        If either boundary time (start, end) is specified, only files with relevant
        dates are read.    
        
        Selected files are read using the load() static method of a child class and appended to the log using '&'.
        
        If no files were read, an empty log is created using the child class default constructor.

        The resulting log is returned.
        t   00000000t   99999999i   N(   R   R#   t   dt2filenameR$   t   ost   listdirR   t   loadt   patht   joint	   ExceptionR   R(   (
   t   clst   dirnameR&   R'   t
   skipErrorst   startfnt   endfnR   t   filenamet   day(    (    s   c:\py\lib\timelog.pyt   loaddirð   s.     5"	"
i
   c         C   s(   |  i  ƒ  } t i |  i | ƒ | _ | S(   se   
        Average 'Nwindow' point long sections of the log to reduce size
        and scatter
        (   R   R#   t   averageR	   (   R   t   Nwindowt   l(    (    s   c:\py\lib\timelog.pyRU   !  s    N(   R,   t
   __module__t   __doc__R   R   R   R   R   R   R   R   R   R   R   R   R!   R(   R)   R+   R.   R-   R0   R1   RC   t   classmethodR;   RT   RU   (    (    (    s   c:\py\lib\timelog.pyR       s0   												
							,0(   t   PLMLog(   t   MCTLog(   t   ParoLog(   t   PIDLog(   t   KelvinoxLog(   t
   OneKPotLog(   RY   R   RG   t   os.patht   timet   datetimet   matplotlib.datesR   R#   t   objectR    t   plmlogR[   t   mctlogR\   t   parologR]   t   pidlogR^   t   kelvinoxlogR_   t
   oneKpotlogR`   (    (    (    s   c:\py\lib\timelog.pys   <module>   s   	ÿ 