"""
Manipulating the ULT fridge Kelvinox logs.
"""

import os
import os.path
import time
import datetime
import numpy
import matplotlib.dates
import flib
import timelog
import ascii2numpy

class OneKPotLog(timelog.TimeLog):
    """
    Represents a ULT 1K Pot log over a certain time.
    Two logs can be concatenated with a '&' sign

    log format:
    1  [:,0]  Time (sec, since 00:00 1 January 1970 GMT)
    2  [:,1]  T_p [K]
    3  [:,2]  P2 [mbar]
    4  [:,3]  G3 [mbar]
    """

    @staticmethod
    def fieldcount(): "return number of columns in the self.data"; return 4
    
    def T_p(self): "Pot temperature [K]"; return self.data[:,1]
    def P2(self): "P2 [mbar]"; return self.data[:,2]
    def G3(self): "G3 [mbar]"; return self.data[:,3]

    def __repr__(self): return "1KPotLog %s" % (self.datespan())

    @staticmethod
    def load(filename):
        """
        Load 1K Pot log from a given file
        The function determines file format from the date:
            1: modern format, since 21 December 2008
            2: old format, does not contain absolute time. winter/summer time changes are not handled correctly.
        """
        basename = os.path.basename(filename)
        
        if basename > '20081221' and basename[8:].lower() == '_potmonitor.dat' and basename.lower() != '20081221_PotMonitor-before23_00.dat':
            # Loads a new (Run 20+, since 21 December 2008) format log file
            #
            # 1  [:,0]  Time (sec, since midnight GMT)
            # 2  [:,1]  T_Pot [K]
            # 3  [:,2]  P2 [mbar]
            # 4  [:,3]  G3 [mbar]
            # 5  [:,4]  N/V [%]
            # 6  [:,5]  Time (sec, since 00:00 1 January 1904 GMT)
            
            data = ascii2numpy.loadascii(filename, cols=6)
            
            # timestamps in col 12 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[:,5])
            
            return OneKPotLog(data[:,0:4])
        elif basename > '20080206':
            # Loads the old format (Run 20, before 21 December 2008) format log file
            # some ambiguity on the days when the daylight saving is changed
            #
            # in year 2008 the time was shifted on Sundays of 30 March and 26 October
            # NOTE: earlier years are not supported (you can do it by hand if you like)
            #
            #
            # 1  [:,0]  Time (sec, since midnight GMT)
            # 2  [:,1]  T_Pot [K]
            # 3  [:,2]  P2 [mbar]
            # 4  [:,3]  G3 [mbar]
            # 5  [:,4]  N/V [%]

            data = ascii2numpy.loadascii(filename, cols=5)
            
            # date is encoded in the filename
            year = int(basename[0:4])
            month = int(basename[4:6])
            day = int(basename[6:8])
            
            if basename[:8] == '20080330':
                # Sunday 30 March 2008 01:00 summer time starts
                t = data[:,0]
                t[t > 60*60] -= 60*60
                data[:,0] = t
            elif (basename[:8] > '20080330') & (basename[:8] < '20081026'):
                # summer time 
                data[:,0] -= 60*60
            elif basename[:8] == '20081026':
                # Sunday 26 October 2008 01:00 summer time ends
                t = data[:,0]
                if len(t) > 0:
                    i = numpy.argwhere(numpy.diff(t) < 0)[0]
                    t[:i] -= 60*60
                    data[:,0] = t
                
            data[:,0] += flib.tm('%02d/%02d/%04d 00:00:00 UTC' % (day, month, year))
            
            return OneKPotLog(data[:,0:4])
        else:
            return None
        # below is the code that uses filename to decode the date.
        # however on the days when time changes between summer/winter, there is an hour error 
        # in all the entries after the change.
##            # Loads an old (before 21 December 2008) format log file
##            #
##            # 1  [:,0]  Time (sec, since midnight GMT)
##            # 2  [:,1]  G1 [mbar]
##            # 3  [:,2]  G2 [mbar]
##            # 4  [:,3]  T_MixCh [mK]
##            # 5  [:,4]  R_s [Ohm]
##            # 6  [:,5]  R_cp [Ohm]
##            # 7  [:,6]  R_mc [Ohm]
##            # 8  [:,7]  T_s [K]
##            # 9  [:,8]  T_cp [K]
##            # 10 [:,9]  T_mc [K]
##            # 11 [:,10] flow, always zero
##            # 12  [:,11]  Time (sec, since 00:00 1 January 1904 GMT)
##
##            data = ascii2numpy.loadascii(filename, cols=10)
##            
##            # date is encoded in the filename
##            year = int(basename[0:4])
##            month = int(basename[4:6])
##            day = int(basename[6:8])
##            
##            data[:,0] += time.mktime(datetime.date(year, month, day).timetuple())
##            
##            return OneKPotLog(data)
    
    @staticmethod
    def loadrun(run, start = None, end = None):
        """
        Return plm log for a given run, withing [start, end] period if boundaries are specified.
        """
        logdir = flib.smbpath('/data/archive/pc144/shared_data/PotMonitor/RUN%d' % run)
        return OneKPotLog.loaddir(logdir, start=start, end=end).period(start, end)
