"""
Manipulating the processed Triton Dry Cryostat logs.

27 Jan 2012, Lev
"""

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

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

    'self.device' is the gauge name, two logs must have the same gauge names in order to concatenate them.

    log format:

    # [0] Time(secs)
    # [1] P2 Condense (Bar)
    # [2] P1 Tank (Bar)
    # [3] P5 ForepumpBack (Bar)
    # [4] P3 Still (mbar)
    # [5] P4 TurboBack (mbar)
    # [6] Dewar (mbar)
    # [7] Input Water Temp
    # [8] Output Water Temp
    # [9] Helium Temp
    # [10] Oil Temp
    # [11] PT2 Head t(s)
    # [12] PT2 Head T(K)
    # [13] PT2 Head R(Ohm)
    # [14] PT2 Plate t(s)
    # [15] PT2 Plate T(K)
    # [16] PT2 Plate R(Ohm)
    # [17] Still t(s)
    # [18] Still T(K)
    # [19] Still R(Ohm)
    # [20] 100mK Plate t(s)
    # [21] 100mK Plate T(K)
    # [22] 100mK Plate R(Ohm)
    # [23] MC cernox t(s)
    # [24] MC cernox T(K)
    # [25] MC cernox R(Ohm)
    # [26] MC RuO2 t(s)
    # [27] MC RuO2 T(K)
    # [28] MC RuO2 R(Ohm)
    # [29] Test cal t(s)
    # [30] Test cal T(K)
    # [31] Test cal R(Ohm)
    # [32] chan[7] t(s)
    # [33] chan[7] T(K)
    # [34] chan[7] R(Ohm)
    # [35] PT1 Head t(s)
    # [36] PT1 Head T(K)
    # [37] PT1 Head R(Ohm)
    # [38] PT1 Plate t(s)
    # [39] PT1 Plate T(K)
    # [40] PT1 Plate R(Ohm)
    # [41] chan[10] t(s)
    # [42] chan[10] T(K)
    # [43] chan[10] R(Ohm)
    # [44] chan[11] t(s)
    # [45] chan[11] T(K)
    # [46] chan[11] R(Ohm)
    # [47] chan[12] t(s)
    # [48] chan[12] T(K)
    # [49] chan[12] R(Ohm)
    # [50] chan[13] t(s)
    # [51] chan[13] T(K)
    # [52] chan[13] R(Ohm)
    # [53] chan[14] t(s)
    # [54] chan[14] T(K)
    # [55] chan[14] R(Ohm)
    # [56] chan[15] t(s)
    # [57] chan[15] T(K)
    # [58] chan[15] R(Ohm)
    # [59] Still heater (W)
    # [60] chamber heater (W)
    # [61] IVC sorb heater (W)
    """

    @staticmethod
    def fieldcount(): "return number of columns in the self.data"; return 62

    def P2(self): "P2 Condense (Bar)"; return self.data[:,1]
    def P1(self): "P1 Tank (Bar)"; return self.data[:,2]
    def P5(self): "Forepump Back (Bar)"; return self.data[:,3]
    def P3(self): "P3 Still (mbar)"; return self.data[:,4]
    def P4(self): "P4 Turbo Back (mbar)"; return self.data[:,5]
    def P_VC(self): "Dewar Pressure (mbar)"; return self.data[:,6]
    # [7] Input Water Temp
    # [8] Output Water Temp
    # [9] Helium Temp
    # [10] Oil Temp
    # [11] PT2 Head t(s)
    def T_PT2head(self): "PT2 Head T(K)"; return self.data[:,12]
    # [13] PT2 Head R(Ohm)
    # [14] PT2 Plate t(s)
    def T_PT2plate(self): "PT2 Plate T(K)"; return self.data[:,15]
    # [16] PT2 Plate R(Ohm)
    # [17] Still t(s)
    def Ts(self): "Still T(K)"; return self.data[:,18]
    # [19] Still R(Ohm)
    # [20] 100mK Plate t(s)
    def Tcp(self): "100mK Plate T(K)"; return self.data[:,21]
    # [22] 100mK Plate R(Ohm)
    # [23] MC cernox t(s)
    def Tmc_cernox(self): "MC cernox T(K)"; return self.data[:,24]
    # [25] MC cernox R(Ohm)
    # [26] MC RuO2 t(s)
    def Tmc(self): "MC RuO2 T(K)"; return self.data[:,27]
    # [28] MC RuO2 R(Ohm)
    # [29] Test cal t(s)
    # [30] Test cal T(K)
    # [31] Test cal R(Ohm)
    # [32] chan[7] t(s)
    # [33] chan[7] T(K)
    # [34] chan[7] R(Ohm)
    # [35] PT1 Head t(s)
    def T_PT1head(self): "PT1 Head T(K)"; return self.data[:,36]
    # [37] PT1 Head R(Ohm)
    # [38] PT1 Plate t(s)
    def T_PT1plate(self): "PT1 Plate T(K)"; return self.data[:,39]
    # [40] PT1 Plate R(Ohm)
    # [41] chan[10] t(s)
    # [42] chan[10] T(K)
    # [43] chan[10] R(Ohm)
    # [44] chan[11] t(s)
    # [45] chan[11] T(K)
    # [46] chan[11] R(Ohm)
    # [47] chan[12] t(s)
    # [48] chan[12] T(K)
    # [49] chan[12] R(Ohm)
    # [50] chan[13] t(s)
    # [51] chan[13] T(K)
    # [52] chan[13] R(Ohm)
    # [53] chan[14] t(s)
    # [54] chan[14] T(K)
    # [55] chan[14] R(Ohm)
    # [56] chan[15] t(s)
    # [57] chan[15] T(K)
    # [58] chan[15] R(Ohm)
    def still_power(self): "Still heater (W)"; return self.data[:,59]
    def mc_power(self): "chamber heater (W)"; return self.data[:,60]
    def sorb_power(self): "IVC sorb heater (W)"; return self.data[:,61]

    def __repr__(self): return "Triton Log %s" % (self.datespan())

    @staticmethod
    def load(filename):
        """
        Load a Triton log from a given file
        """
        # on 18/07/2011 the log had 63 rather than 64 columns - ignore
        if filename.endswith('110718_191657.dat'):
            return None
        
        data = ascii2numpy.loadascii(filename, cols=TritonLog.fieldcount())
        
        # time shifted on 11/01/2011
##        if filename.endswith('20120112_triton_120111_111333.dat'):
##            data[:926,0] -= 3060
##        elif data[0,0] < 1326326440.0:
##            data[:,0] -= 3060
        
        return TritonLog(data)

    @staticmethod
    def loadlog(start=None, end=None):
        """
        Return triton cryostat log for a given run, withing [start, end] period if boundaries are specified.
        """
        return TritonLog.loaddir('/data/exp/triton/log', start=start, end=end).period(start, end)
