"""
Manipulating the Melting Curve Thermometry logs.

Log format:
[:,0] (1) Time (sec, since 1 January 1970 UTC)
[:,1] (2) Ratio transformer bridge ratio, set point
[:,2] (3) Off-balance voltage (V)
[:,3] (4) Bridge ratio (corrected for off-balance voltage)
[:,4] (5) Absolute pressure (bar, without A-transition correction)
[:,5] (6) Temperature (mK, Greywall scale)
[:,6] (7) Temperature (mK, PLTS2000 high temperature scale)
[:,7] (8) Temperature (mK, PLTS2000 low temperature scale)

07 June 2008, Lev
"""

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

class MCTLog(timelog.TimeLog):
    """
    Represents a Melting Curve Thermometry log over a certain time.
    Two logs can be concatenated with a '&' sign

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

    log format:

    [:,0] (1) Time (sec, since 1 January 1970 UTC)
    [:,1] (2) Ratio transformer bridge ratio, set point
    [:,2] (3) Off-balance voltage (V)
    [:,3] (4) Bridge ratio (corrected for off-balance voltage)
    [:,4] (5) Absolute pressure (bar, without A-transition correction)
    [:,5] (6) Temperature (mK, Greywall scale)
    [:,6] (7) Temperature (mK, PLTS2000 high temperature scale)
    [:,7] (8) Temperature (mK, PLTS2000 low temperature scale)
    """

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

    def r_set(self): "Ratio Transformer Set Point"; return self.data[:,1] 
    def V_off(self): "Off-balance voltage (V)"; return self.data[:,2]
    def r(self): "Bridge ratio"; return self.data[:,3]
    def P(self): "Pressure[bar], no A-transition correction "; return self.data[:,4]
    def T_grwl(self): "Greywall scale temperature [mK]"; return self.data[:,5]
    def T_pltsH(self): "PLTS2000 high scale temperature [mK]"; return self.data[:,6]
    def T_pltsL(self): "PLTS2000 low scale temperature [mK]"; return self.data[:,7]
    
    def __repr__(self): return "%s Log %s" % (self.device, self.datespan())
    
    devNames = {'_MCT_NuclearStage.dat': 'MCTNS', '_MCT_MixingChamber.dat': 'MCTMC'}
    

    @staticmethod
    def load(filename):
        """
        Load an MCT log from a given file
        The thermometer is infered from the filename suffix.
        """
        basename = os.path.basename(filename)
        suffix = basename[8:]
        
        if not MCTLog.devNames.has_key(suffix):
            return None
        
        data = flib.loadascii(filename, usecols=range(MCTLog.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])
        
        # out of range temperatures are encoded as -1. Change this to NaN
        data[data[:,5] == -1, 5] = numpy.NaN
        data[data[:,6] == -1, 6] = numpy.NaN
        data[data[:,7] == -1, 7] = numpy.NaN
        
        return MCTLog(data, MCTLog.devNames[suffix])

    @staticmethod
    def loadMCTNS(run, start = None, end = None):
        """
        Return highP paroscientific log for a given run, withing [start, end]
        period if boundaries are specified.
        """
        return MCTLog.loaddir(r'\\phpc338\Run%d\MCT_NuclearStage' % run, start=start, end=end).period(start, end)
#        return MCTLog.loaddir(r'C:\pub\MCTNS\run%d' % run, start=start, end=end).period(start, end)
