
:SKc           @   sW  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 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 Z e i Z d Z e i d  Z e i d  Z d   Z d   Z d   Z e d  Z  e d  Z! d d  Z" d   Z# d d d d d e e d  Z$ d d d e d d  Z% d d d   Z& d d d!  Z' d" d d# d$  Z( d%   Z) d&   Z* e+ d'  Z, d( e e+ d)  Z- d( e e+ d*  Z. d( e e+ d+  Z/ d,   Z0 d- e1 f d.     YZ2 d/   Z3 d0   Z4 d d1  Z5 e+ d2  Z6 d d d3  Z7 d S(4   s  
flib.py
A collection of useful routines.

Date and time: 'str2tm', 'tm', 'tm2str', 'dt2str', 'dt2filename', 'labview2tm' and 'tm2labview'
Saving/loading data files: 'loadbinary', 'savebinary', 'loadascii', 'saveascii',
    'swapendian', 'savenum', 'loadnum', 'savestr', 'loadstr'
String formatting: 'a2str'
Array Handling: 'average', 'absdiff', 'finites'
Complex numbers: 'phase'
Number comparision: 'floatcmp'

Plotting: 'plotdate'

7 Jul 2009, Lev Levitin
iNc         K   sC   | i  d  p t | d <n t i t i i |  | |  i    S(   s   
    Convert a string into seconds from UNIX Epoch
    using the intelligent dateutil.parser.parse() routine
    with dayfirst=True argument, unless specified.
    t   dayfirst(   t   has_keyt   Truet   timet   mktimet   dateutilt   parsert   parset	   timetuple(   t   stringt
   parserinfot   kwargs(    (    s   c:\py\lib\flib.pyt   str2tm8   s    c         C   s   |  d j o |  Sn t i |   p2 t i g  } |  D] } | t |  q6 ~  Sn) t |  t t f  o t |   Sn |  Sd S(   sE  
    Convert a timestamp from a range of formats into seconds from UNIX Epoch.
    Handle an array of dates.
    
    Call flib.str2tm if the argument is a string, returns the raw value if the argument is a scalar.
    If an array is supplied, every element is handled as above, and a numpy.array of numbers is returned.
    N(	   t   Nonet   numpyt   isscalart   arrayt   tmt
   isinstancet   strt   unicodeR   (   t   tt   _[1]t   e(    (    s   c:\py\lib\flib.pyR   D   s    2c         C   sV   t  i |   p) g  } |  D] } | t |  q ~ Sn t i d t i |    Sd S(   s   
    Convert a timestamp or an array of timestamps in
    C and UNIX format (seconds since 00:00 1 Jan 1970 UTC)
    into 'dd mmm yyyy HH:MM:SS' human readable strings according to locale
    s   %d %b %Y %H:%M:%SN(   R   R   t   tm2strR   t   strftimet	   localtime(   R   R   R   (    (    s   c:\py\lib\flib.pyR   U   s    )c         C   sV   t  i |   p) g  } |  D] } | t |  q ~ Sn t i d t i |    Sd S(   s   
    Convert a timestamp or an array of timestamps in
    C and UNIX format (seconds since 00:00 1 Jan 1970 UTC)
    into 'HH:MM:SS' human readable strings according to locale
    s   %H:%M:%SN(   R   R   R   R   R   R   (   R   R   R   (    (    s   c:\py\lib\flib.pyt	   tmHMS2str`   s    )c         C   sV   t  i |   p) g  } |  D] } | t |  q ~ Sn t i d t i |    Sd S(   s   
    Convert date in a timestamp or an array of timestamps in
    C and UNIX format (seconds since 00:00 1 Jan 1970 UTC)
    into 'dd mmm yyyy' human readable string(s) according to locale
    s   %d %b %YN(   R   R   t   dt2strR   R   R   (   R   R   R   (    (    s   c:\py\lib\flib.pyR   k   s    )c         C   sV   t  i |   p) g  } |  D] } | t |  q ~ Sn t i d t i |    Sd S(   s   
    Convert date in a timestamp or an array of timestamps in
    C and UNIX format (seconds since 00:00 1 Jan 1970 UTC)
    into 'yyyymmdd' string(s) used for log file names
    s   %Y%m%dN(   R   R   t   dt2filenameR   R   R   (   R   R   R   (    (    s   c:\py\lib\flib.pyR   v   s    )c         C   sU   t  |   t  |  j o d t |   t |  f Sn d t |   t |  f Sd S(   s   
    Convert a pair of times in any format understood by 'flib.tm()' into a
    string of form '<start time>-<end time>' where the date is omitted from the
    <end> time if both <start> and <end> have the same date.
    s   %s-%sN(   R   R   R   (   t   startt   end(    (    s   c:\py\lib\flib.pyt   timespan2str   s    c         C   s+   t  i |   p t  i |   }  n |  d S(   s   
    Convert a timestamp, or an array of timestamps from
    Labview format (seconds since 00:00 1 Jan 1904 UTC) to
    C and UNIX format (seconds since 00:00 1 Jan 1970 UTC)
    i%|(   R   R   R   (   R   (    (    s   c:\py\lib\flib.pyt
   labview2tm   s    c         C   s+   t  i |   p t  i |   }  n |  d S(   s   
    Convert a timestamp, or an array of timestamps from
    C and UNIX format (seconds since 00:00 1 Jan 1970 UTC)
    to Labview format (seconds since 00:00 1 Jan 1904 UTC) 
    i%|(   R   R   R   (   R   (    (    s   c:\py\lib\flib.pyt
   tm2labview   s    c         C   sI   t  t |    }  t  t |   } |  | j o |  Sn |  d | Sd S(   sR   
    Return a string representating of date span of given two moments in time
    t   -N(   R   R   (   R   R   (    (    s   c:\py\lib\flib.pyt   datespan   s
     c           C   s   t  S(   s>   
    Determines byte order for loading/saving binary data
    (   R   (    (    (    s   c:\py\lib\flib.pyt
   swapendian   s    s   i   c         C   sD   | |  } | i  i d d j o | i   } n | i |   d S(   s   
    Save a number 'n' to a binary file pointed by a handle 'file'
    optional 'dtype' argument specify a format for a number to be stored in,
    defaults to unsigned 64 bit integer. Use big endian format.
    i    t   <N(   t   dtypeR   t   byteswapt   tofile(   t   filet   nR'   (    (    s   c:\py\lib\flib.pyt   savenum   s    c         C   sJ   t  i |  d | d d } | i i d d j o | i   } n | d S(   s   
    Load a number from a binary file pointed by a handle 'file'.
    Optional 'dtype' argument specifies a format for a number to be loaded from,
    defaults to unsigned 64 bit integer. Use big endian format.
    R'   t   counti   i    R&   (   R   t   fromfileR'   R   R(   (   R*   R'   R+   (    (    s   c:\py\lib\flib.pyt   loadnum   s    c         C   s*   t  |  t |  d t |  i |  d S(   s{   
    Save a string into a binary file, preceeded with its length encoded into
    a big endian 64 bit unsigned integer
    R'   N(   R,   t   lent   __bin_size_type__t   write(   R*   R	   (    (    s   c:\py\lib\flib.pyt   savestr   s    c         C   s   |  i  t |  d t  S(   s   
    Load a string from a binary file. First length is read as
    a big endian 64 bit unsigned integer, then the contents.
    R'   (   t   readR/   R1   (   R*   (    (    s   c:\py\lib\flib.pyt   loadstr   s    c         C   s:  t  |  d  } z| i t  | i d  t | t  p7 t i |  } t |  } h  | d <| d <} n t | t	 |  d t
 x | i   D] } t | |  | | } t | t  o t | d  t | |  q t i |  } t | | i i  t | | i  | i |  q WWd | i   Xd S(   s   
    Save numeric data into a binary file.
    
    Argument formats:
        'data' - dictionary of sections to be stored.
        keys should be strings
        values should be numpy 1D arrays or strings.
    t   wbt   ver00002t   headert   channel0R'   R	   N(   t   openR2   t   __bin_magic__R   t   dictR   t   asarrayR   R,   R0   R1   t   keysR3   R'   t   nbytesR)   t   close(   t   filenamet   dataR*   R8   t   namet   section(    (    s   c:\py\lib\flib.pyt
   savebinary   s*    	 
c      
   C   sK  t  |  d  } z*| i t t   } | t j o t i t i | d t i d d t i | d t i g  } t	   o | i
 t  n d } d } h  | d <| d <} nc| i t  } | d	 j o d } t | d t } | i |  } t | d t } | i t  i d
  d } t i | d t i |  d | } t	   o | i
 t  n h  | d <| d <} n | d j oy d } t | d t }	 h  } x0 t |	  D]" }
 t |  } t |  | | <qWd | i   j o | d n d } n t d |   | o | | | f Sn	 | d SWd | i   Xd S(   s  
    Load a numeric dataset from a binary file. Accepts a bare 64 bit floating
    point array or a file with a __bin_magic__ number (floating point NaN) and a header.
    If 'full' optional argument is set to True, a tuple is retured containing the
    file format version, header and data array. Version is 0 and header is an
    empty string, if a bare binary datafile was encountered.
    t   rbR'   R-   i   i    t    R8   R9   t   ver00001t    R7   i   s   '%s' format is not implementedN(   R:   R4   R0   R;   R   t   concatenatet
   frombuffert   float64R.   R%   R(   R   t   __bin_version_len__R/   R1   t   __bin_dtype_len__t   splitR'   t   rangeR5   t   loadbinarysectionR>   t   NotImplementedErrorR@   (   RA   t   fullR*   t   sRB   t   versionR8   t   lt   typet	   nsectionsR+   RC   (    (    s   c:\py\lib\flib.pyt
   loadbinary   sJ    
$
 (c         C   s   t  |   } t |  d t } | o |  i | d  d  Sn | i   d j o |  i |  Snc y t i |  } Wn# t	 j
 o t
 d |   n Xt i |  d | d t i | | i  Sd  S(   NR'   i   R	   s+   unsupported data format '%s' in binary fileR-   (   R5   R/   R1   t   seekR   t   lowerR4   R   R'   t	   TypeErrort
   ValueErrorR.   t   uint64t   itemsize(   R*   t   skipt   formatt   lengthR'   (    (    s   c:\py\lib\flib.pyRQ   4  s    s   %gc         C   sL   t  i |   o | |  Sn t  i |   o |  d j o d n d Sn d S(   s   
    Convert a number into string.
    Handle special cases:
        "NaN" for not-a-number
        "Inf" for positive infinity
        "-Inf" for negative infinity    
    i    t   Infs   -Inft   NaN(   R   t   isfinitet   isinf(   t   numbert   fmt(    (    s   c:\py\lib\flib.pyt   n2str  s
    c         C   s  t  |  t t f  o |  i   i   } | d j p | d  d j o t i Sn | d  d j o t i Sn | d j p/ | d j p" | d  d j p | d  d	 j o t i Sn | d
 j p | d  d j o t i Sq n t  |  t  o t	 |  i
  Sn t	 |   S(   s  
    Converts into float from different formats:
    
    1. if arg is a string, treat as a decimal floating point number.
       Handle special cases:
            "NaN", "-1.#IND..." is Not-a-Number
            "1.#QNAN" is minus Not-a-Number (???)
            "Inf", "+Inf", "1.#INF...", "+1.#INF..." is Positive Infinity
            "-Inf","-1.#INF..." is Negative Infinity
                        
    2. a real part is taken from a complex number
    3. float(arg) is called for anything else.
    t   NANi   s   -1.#INDs   1.#QNANt   INFs   +INFi   s   1.#INFs   +1.#INFs   -INFs   -1.#INF(   R   R   R   t   stript   upperR   Rd   Rc   t   complext   floatt   real(   t   argRT   (    (    s   c:\py\lib\flib.pyt
   smartfloat  s    <t   #i    c            s  d d k  l } | d j o
 h  } n | i |   }	 zwg  }
   d j o d   } n   f d   } d } xSt |	  D]E\ } } | | j  o q{ n | i | d  d i   } t |  p q{ n | d j oC g  } t | |   D] \ } } | | i | t	  q ~ } n | d j	 oB | i    } g  } | D] } | | | | |  qB~ } n> g  } t | |   D] \ } } | | | |  q~ } t |  } |
 i
 |  q{ Wt i |
 t i  }
 t |
  d j o3 | d j o d	 |
 _ qd t |  f |
 _ n | pm |
 i \ } } | d j p | d j o t | |  f |
 _ q| d j p | d j o d
 |
 _ qn | o |
 i   Sn |
 SWd |	 |  j	 o |	 i   n Xd S(   sA  
    A CUSTOMISED VERSION OF pylab.load

    CHANGES FROM PYLAB:
    if keep2D is set to True:
        1. 2D arrays are preserved even if an empty/single line/single column is read.
        2a. if en empty file was read and "usecols" is specified, a shape is set to 0,len(usecols)
        2.b if an empty file was read and "usecols" is not specified, a shape is set to 0,0
    if keep2D is set to False:
        1. A single line/single column data is converted into a 1D array (standard pylab behaviour)
        2. An empty file is converted into an empty array, does not generate an error (pylab generates an error)

    Load ASCII data from filename into an array and return the array.

    The data must be regular, same number of values in every row

    filename can be a filename or a file handle.  Support for gzipped files is
    automatic, if the filename ends in .gz

    matfile data is not supported; use scipy.io.mio module

    Example usage:

      X = load('test.dat')  # data in two columns
      t = X[:,0]
      y = X[:,1]

    Alternatively, you can do the same with "unpack"; see below

    comments - the character used to indicate the start of a comment
    in the file

    delimiter is a string-like character used to seperate values in the
    file. If delimiter is unspecified or none, any whitespace string is
    a separator.

    converters, if not None, is a dictionary mapping column number to
    a function that will convert that column to a float.  Eg, if
    column 0 is a date string: converters={0:datestr2num}

    skiprows is the number of rows from the top to skip

    usecols, if not None, is a sequence of integer column indexes to
    extract where 0 is the first column, eg usecols=(1,4,5) to extract
    just the 2nd, 5th and 6th columns

    unpack, if True, will transpose the matrix allowing you to unpack
    into named arguments on the left hand side

        t,y = load('test.dat', unpack=True) # for  two column data
        x,y,z = load('somefile.dat', usecols=(3,5,7), unpack=True)

    See examples/load_demo.py which exeercises many of these options.
    i(   t   cbookt    c         S   s
   |  i    S(   N(   RO   (   t   x(    (    s   c:\py\lib\flib.pyt	   splitfunc  s    c            s   |  i     S(   N(   RO   (   Rv   (   t	   delimiter(    s   c:\py\lib\flib.pyRw     s    i   i    N(   i    i    (   i    (   t
   matplotlibRt   R   t   to_filehandlet	   enumerateRO   Rl   R0   t   getRr   t   appendR   R   RL   t   shapet   maxt	   transposeR@   (   RA   t   commentsRx   t
   converterst   skiprowst   usecolst   unpackt   keep2DRt   t   fht   XRw   t   converterseqt   it   lineR   t   jt   valt   valst   _[2]t   rowt   _[3]t   thisLent   rt   c(    (   Rx   s   c:\py\lib\flib.pyt	   loadascii  sR    7 
   C3= s   %.8gs   	c         C   s@  d d k  l } | i |   or | o
 d } n d } t i i |   }	 |  i d  o& d d k }
 |
 i |  | d  } q t	 |  |  } n3 t
 |  d  o |  } t } t }	 n t d	   zY| d j	 o$ | o |	 o | i | d
  n t |  t j } t i |  } d } | i d j o" | i } t |  d f | _ n | t j p | d j o | o | i   } n xL | D]D } | i | i g  } | D] } | t | |  q~  d
  qW| t j p | d j o | o | i   } n | d j	 o | | _ n Wd | |  j	 o | i   n Xd S(   s  
    A CUSTOMISED VERSION OF pylab.save
    CHANGES:
        1. Allows to add a header to the file.
        2. Allows append data to an existing file. It is totally a resposibility of the user to make sure
           data appended is in the same format as in the initial part of the file.
           The header is not written down if appending to an existing file.
        2. Default number format is '%g'
        3. Default delimiter is a tab character '	'
        4. If 'pack' is True, 'X' is transposed before saving (compatible with 'flib.loadascii' and 'pylab.load' with 'unpack=True')
           If 'pack' is None (default), 'X' is transposed if it is a tuple, and preserved otherwise
           If 'pack' is False, 'X', transposition is not carried (the default pylab behaviour)
    
    Save the data in X to file filename using fmt string to convert the
    data to strings

    filename can be a filename or a file handle.  If the filename ends in .gz,
    the file is automatically saved in compressed gzip format.  The load()
    command understands gzipped files transparently.

    Example usage:

    save('test.out', X)         # X is an array
    save('test1.out', (x,y,z))  # x,y,z equal sized 1D arrays
    save('test2.out', x)        # x is 1D
    save('test3.out', x, fmt='%1.4e')  # use exponential notation

    delimiter is used to separate the fields, eg delimiter ',' for
    comma-separated values
    i(   Rt   t   at   ws   .gzNt   bRZ   s(   filename must be a string or file handles   
i   (   Ry   Rt   t   is_string_liket   ost   patht   existst   endswitht   gzipR:   R*   t   hasattrR   R]   R   R2   RW   t   tupleR   R=   t   ndimR~   R0   R   t   joinRi   R@   (   RA   R   Rh   Rx   R8   R}   t   packRt   t   modet   already_existsR   R   t   isXtuplet	   origShapeR   R   R   (    (    s   c:\py\lib\flib.pyt	   saveasciiE  sF     

	! B!c   	   	   C   s   d d k  l } | i |   } d } zZ g  } xM t |  D]? \ } } | | j  o q; n | i |  o | | 7} q; Pq; WWd  | |  j	 o | i   n X| | i   S(   Ni(   Rt   RG   (   Ry   Rt   Rz   R{   t
   startswithR@   t   rstrip(	   RA   t   commentR   Rt   R   R8   R   R   R   (    (    s   c:\py\lib\flib.pyt
   readheader  s       	c         C   s  | d j o | d j o t  d   n t |   t |  j o t  d   n t |   d j  o |  | f Sn | d j  o$ t |  t |   t |   } nf | d j o
 | } nO | d j  o$ t |  t |  t |  } n | d j o
 | } n d } | d j o t i | |  g  } n t i |  | g  } | i   i   } | i	   | d d g } | d d g } d } | d j o d   }	 d   }
 n d   }	 d	   }
 x t
 d t |   D] } | | d } | | d } |	 | | | d
 | d
 |  o. |
 | | |  |
 | | |  | d 7} q| i |  | i |  d } qW| d j o | | f Sn | | f Sd S(   s  
    Reduce size of an array by combining similar points into their centre of mass.
    'x' and 'y' are expected to be 1D arrays of the same length, containing the data.

    If both 'xstep' and 'ystep' are 0, the function deletes multiple occurances of
    the same points.
    
    If 'xstep' is positive, points with similar 'y' and scatter in 'x' smaller than 'xstep'
    are combined into a single point with an average 'x'. For a negative 'xstep' the
    absolute value is treated as an xstep, relative to the total span of values in 'x'.

    If 'ystep' is non-zero, the behaviour is similar to the above, but with the roles
    of 'x' and 'y' swapped.
    
    Both 'xstep' and 'ystep' currently can not be non-zero.
    
    i    s+   Only one of xstep and ystep can be non-zeros   x and y must be the same lengthi   i   c         S   s   |  | j o
 | | j S(   N(    (   t   x1t   y1t   x2t   y2t   step(    (    s   c:\py\lib\flib.pyt   are_similar  s    c         S   s   d  S(   N(    (   R   t   newvalR-   (    (    s   c:\py\lib\flib.pyt   average_array  s    c         S   s!   |  | j o t  | |  | j S(   N(   t   abs(   R   R   R   R   R   (    (    s   c:\py\lib\flib.pyR     s    c         S   s"   | |  d | | d |  d <d  S(   Nii   (    (   R   R   R-   (    (    s   c:\py\lib\flib.pyR     s    iN(   R]   R0   R   R   t   minR   R   R   t   tolistt   sortRP   R}   (   Rv   t   yt   xstept   ystepR   t   xyt   xrt   yrR-   R   R   R   t   xit   yi(    (    s   c:\py\lib\flib.pyt   reduce_similar  sT    $
$

			 !
s   , s   ...c         C   s   | d j p t |   d | d j o
 |  } n' t |  |   | g t |  |  } | i g  } | D] } | t |  qi ~  S(   s  
    Return a comma-separated list of str() representations of the elements of
    the given array. Like str(array), but without surrounding square brackets.
    
    'array' - the array to represent
    'sep' - separator between elements
    'maxlen' - maximum number of elements to represent the tail and the head.
    If array length exceeds 2*maxlen+1, only first and last 'maxlen' 
    elements are represented and the gap is filled with 'gap'.
    i   i   N(   R   R0   t   listR   R   (   R   t   sept   maxlent   gapR   R   R   (    (    s   c:\py\lib\flib.pyt   a2str  s    (
&c          G   sV   t  |   d j o# t i |  d  o |  d }  n t i |   }  |  t i |   S(   s   
    Return a subset of an array containing only finite elements
    Either a single array argument is expected or an arbitrary length
    set of scalar arguments that are combined into an array
    i   i    (   R0   R   R   R=   Re   (   t   args(    (    s   c:\py\lib\flib.pyt   finites  s    (c         C   sh   t  i d  i   d j oF |  i d d  }  |  d  d j o t i i d |  d  Sqd |  Sn |  Sd S(	   sG   
    convert smb filename to what operating system will understand
    i   t   linuxs   \t   /i   s   //s   /mntN(   t   syst   platformR[   t   replaceR   R   R   (   R   (    (    s   c:\py\lib\flib.pyt   smbpath  s    c         C   sp   |  d |  d  } t  i d g | f  } t  i | d g f  } | o
 t  i n t  i t |  t |   S(   s  
    Return an array of absolute values of differences between nearest
    neighbours in the array. For every element the maximum (if max is True) or
    minumum (otherwise) of abs diffs between its value and its neighbors is
    stored in the returned array.
    i   ig        (   R   RJ   t   maximumt   minimumR   (   R   R   t   difft   diff12t   diff21(    (    s   c:\py\lib\flib.pyt   absdiff#  s    i
   c         C   s   t  |  d   | | |  S(   sf   
    Average 'Nwindow' long sections of 'array' along given 'axis'
    to reduce size and scatter
    c         S   s   |  i  d  d  S(   t   axisi   (   t   mean(   R   (    (    s   c:\py\lib\flib.pyt   <lambda>6  s    (   t   slice(   R   t   Nwindowt   truncatet   extend_last_bin(    (    s   c:\py\lib\flib.pyt   average1  s    c         C   s4   t  |  d   | | |  t  |  d   | | |  f S(   s   
    divide the original 'array' into bins 'Nwindow' elements wide
    and return arrays containing the means and standard deviations over the bins.
    
    'truncate' and 'extend_last_bin' govern the treatment of the very end of
    the supplied array if its size is not a multiple of 'Nwindow':
    
    If 'truncate' is True, the last up to 'Nwindow-1' elements are truncated.
    Otherwise they are treated as a separate bin if 'extend_last_bin' is False
    or added to the preceeding 'Nwindow' elements if 'extend_last_bin' is True.
    c         S   s   |  i  d  d  S(   R   i   (   R   (   R   (    (    s   c:\py\lib\flib.pyR   R  s    c         S   s   |  i  d  d  S(   R   i   (   t   std(   R   (    (    s   c:\py\lib\flib.pyR   S  s    (   R   (   R   R   R   R   (    (    s   c:\py\lib\flib.pyt   slice_mean_and_stdF  s    c         C   s2  t  i |   }  |  i   } t | i d |  } | i | | f | i d  | |  } | o |  i | | j o | o\ | i d j oL |  | | d } | i d t |  f | i d  } | |  | d )q.|  | | } | i d t |  f | i d  } t  i	 | | |  g  } n | S(   s  
    divide the original 'array' into bins 'Nwindow' elements wide along the first axis.
    and do similar processing of the bins.
    'function' is called on the reshaped array of MxNwindow and
    bins are along the axis=1 (second dimension).
    
    'truncate' and 'extend_last_bin' govern the treatment of the very end of
    the supplied array if its size is not a multiple of 'Nwindow':
    
    If 'truncate' is True, the last up to 'Nwindow-1' elements (rows) are truncated.
    Otherwise they are treated as a separate bin if 'extend_last_bin' is False
    or added to the preceeding 'Nwindow' elements if 'extend_last_bin' is True.
    i    i   i(
   R   R=   t   copyt   intR~   t   resizet   sizet   reshapeR0   RJ   (   R   t   functionR   R   R   t   dt   NR   (    (    s   c:\py\lib\flib.pyR     s    &&c         C   sK   t  i |   o t i |  i |  i  n" t  i t  i |   t  i |    S(   sa   
    Return Argument (phase in radians) of complex number or an array of complex
    numbers
    (   R   R   t   matht   atan2t   imagRp   t   arctan2(   R   (    (    s   c:\py\lib\flib.pyt   phase  s    t   Periodc           B   s#   e  Z d    Z d   Z d   Z RS(   c         K   sn   t  |  |  _ t  |  |  _ xI | i   D]; } | |  i j o t d |   n | | |  i | <q+ Wd S(   s   
        Construct a period.
        'start' and 'end' determine the moments in time that bound the Period
        then any number of keyword arguments can be specified. This will be
        transfered into properties of the period.
        sC   Can not add attribute '%s' to a Period, because it is already thereN(   R   R   R   R>   t   __dict__R]   (   t   thisR   R   t   vargsR+   (    (    s   c:\py\lib\flib.pyt   __init__  s     c         C   si   t  | t  o | |  }  } n y* t |  } |  i | j | |  i j @SWn t j
 o t Sn Xd S(   s]   
        Comparision. True if 'b' is a moment of time that belongs to the period 'a'
        N(   R   R   R   R   R   t	   Exceptiont   False(   R   R   R   (    (    s   c:\py\lib\flib.pyt   __eq__  s    c         C   s    d t  |  i  t  |  i  f S(   Ns   Period(%s-%s)(   R   R   R   (   R   (    (    s   c:\py\lib\flib.pyt   __repr__  s    (   t   __name__t
   __module__R   R   R   (    (    (    s   c:\py\lib\flib.pyR     s   		c         C   sq   t  i |  } x[ t d t |    D]D } | | d d | | | | d |  | |  | d | | <q% W| S(   s?   
    Return an array
        i[x0] = \int_{x[0]}^{x0} y dx
    i   g      ?(   R   t
   zeros_likeRP   R0   (   Rv   R   R   t   k(    (    s   c:\py\lib\flib.pyt	   integrate  s
     Bc         C   sJ   |  | j p= t  i |   o t  i |  p t  i |   o t  i |  S(   s   
    Return True if x and y are similar numbers in a sense, that 1 equals 1.0 and NaN equals NaN,
    infinities are unsigned
    False otherwise.
    (   R   t   isnanRf   (   Rv   R   (    (    s   c:\py\lib\flib.pyt   floatcmp  s    c         C   s@   d d k  } | i | i |  | i  } | i | | |  |  S(   sI   Behaves like string.replace(), but does so in a case-insensitive fashion.iN(   t   ret   compilet   escapet   It   sub(   t   selft   oldt   newR-   R   t   pattern(    (    s   c:\py\lib\flib.pyt   ireplace  s    c         C   sQ   | o0 t  |  d t } | d d | d d f Sn t |  d d	 d t Sd S(
   s   
    load data from a two channel scope trace, either in binary of ascii format.
    return a tuple of two arrays, x and y.
    RS   i   R9   t   channel1R   i   R   N(   i   i   (   RY   R   R   (   RA   t   binaryRB   (    (    s   c:\py\lib\flib.pyt   load_scope_xy  s    c   	      C   s  | d j o t i } n t |  } | d j o t i } n t |  } g  } zMt |  d  } x7| D]/} | i   } t |  d j  p | d d j o ql n | i d  } | i d  } | d j o | | j  o
 | } n | i d | d  } | i d | d  } | d j o | | j  o
 | } n yV t | |   } | | j o5 | | j o( | i t	 | | d | | d  n Wql ql Xql WWd | i
   X| S(	   s(  
    Parse a file in a format 'date time event name'
    with '# ...' as comments.
    Return an array of 'Period' objects with start and end equal to the given
    date and time and event name saved as 'name'.
    If 'start'/'end' are given, only events after start/before end are selected.
    R   i   i    Rs   s   	Ru   RC   N(   R   R   t   infR   R:   Rl   R0   t   findR}   R   R@   (	   RA   R   R   t   eventst   fR   t   i_tabt   i_spR   (    (    s   c:\py\lib\flib.pyt   read_timelog  s<     $

,(8   t   __doc__R   t   datetimeR   R   t   os.pathR   R   R   R   R   R   R   R   R   R   R    R!   R"   R$   R%   R;   RM   R^   R1   RN   R,   R/   R3   R5   RE   R   RY   RQ   Ri   Rr   R   R   R   R   R   R   R   R   R   R   R   R   R   t   objectR   R   R   R  R  R  (    (    (    s   c:\py\lib\flib.pys   <module>   sb   							
	
							#7z	lMP		A 	 		
