"""
Manipulating the Paroscientific pressure logs.

Log format:
[:,0] (1) Time (sec, since 1 January 1970)
[:,1] (2) T_p (usec)
[:,3] (3) T_t (usec)
[:,4] (4) T_p zero (usec),
[:,5] (5) T_t zero (usec)
[:,6] (6) P (mbar)
[:,7] (7) T (degC)

27 May 2008, Lev
"""

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

class ParoLog(timelog.TimeLog):
    """
    Represents a ULT Paroscientific 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] (1) Time (sec, since 1 January 1970)
    [:,1] (2) T_p (usec)
    [:,2] (3) T_t (usec)
    [:,3] (4) T_p zero (usec)
    [:,4] (5) T_t zero (usec)
    [:,5] (6) P (mbar)
    [:,6] (7) T (degC)
    """

    @staticmethod
    def fieldcount(): "return number of columns in the self.data"; return 7
    
    def P(self): "pressure [mbar]"; return self.data[:,5]
    def T(self): "temperature [degC]"; return self.data[:,6]
    def T_p(self): "pressure period raw reading [usec]"; return self.data[:,1]
    def T_t(self): "temperature period raw reading [usec]"; return self.data[:,2]
    def T_p0(self): "zero-offset pressure period raw reading [usec]"; return self.data[:,3]
    def T_t0(self): "zero-offset temperature period raw reading [usec]"; return self.data[:,4]

    def __repr__(self): return "Paroscientific Log %s, gauge='%s'" % (self.datespan(), self.device)
    
    gaugeNames = {
        '_Paro_high_P_sample.txt': 'highP',
        '_Paro_MCT.txt': 'MCT',
        '_Paro_2D_sample.txt': 'lowP'}
    
    pressureFactors = {
        '_Paro_high_P_sample.txt': 1.0,
        '_Paro_MCT.txt': 1.0e3,
        '_Paro_2D_sample.txt': 1.0}

    @staticmethod
    def load(filename):
        """
        Load a Paroscientific log from a given file
        A gauge is infered from the filename suffix. The following gauges are known:

            filename suffix            gauge name           pressure units
            
        "_Paro_high_P_sample.txt"       "highP"             mbar   
        "_Paro_MCT.txt"                 "MCT"               bar
        "_Paro_2D_sample.txt"           "lowP"              mbar
        """

        basename = os.path.basename(filename)
        suffix = basename[8:]
        
        if not ParoLog.gaugeNames.has_key(suffix):
            return None

        data = flib.loadascii(filename, usecols=range(ParoLog.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])

        # MCT pressure is stored in bar, others in mbar
        # do a conversion into mbar
        data[:,5] *= ParoLog.pressureFactors[suffix]
        
        return ParoLog(data, ParoLog.gaugeNames[suffix])

    @staticmethod
    def loadHighP(run, start = None, end = None):
        """
        Return highP paroscientific log for a given run, withing [start, end] period if boundaries are specified.
        """
        return ParoLog.loaddir(r'\\phpc338\Run%d\Paroscientific\highP-cell-paro' % run, start=start, end=end).period(start, end)

#        return ParoLog.loaddir(r'C:\pub\highP-cell-paro\run%d' % run, start=start, end=end).period(start, end)
