import timelog, os, ascii2numpy, flib

class ClockLog(timelog.TimeLog):
    """
    Represents a Clock Comparison log over a certain time.
    Two logs can be concatenated with a '&' sign

    log format:

    [:,0] (1) Time (sec, since 1 January 1970)
    [:,1] (2) T_A (usec)
    [:,2] (3) T_B (usec)
    """

    @staticmethod
    def fieldcount(): "return number of columns in the self.data"; return 3
    
    def TA(self): "period A [us]"; return self.data[:,1]
    def TB(self): "period B [us]"; return self.data[:,2]
    def __repr__(self): return "Clock Comparison Log %s" % (self.datespan())
    
    def fA(self): "frequency A [Hz]"; return 1/(self.TA() * 1e-6)
    def fB(self): "frequency B [Hz]"; return 1/(self.TB() * 1e-6)
    
    @staticmethod
    def load(filename):
        """
        Load a Clock comparison log from a given file
        """
        
        basename = os.path.basename(filename)
        suffix = basename[8:]
                
        data = ascii2numpy.loadascii(filename, cols=ClockLog.fieldcount())
        
        # timestamps are stored as seconds since 00:00 1 Jan 1904.
        # Convert to seconds since 00:00 1 Jan 1970, C and UNIX time format.
        data[:,0] = flib.labview2tm(data[:,0])
                
        return ClockLog(data)
