"""
mctscale.py
3He Melting Curve Calibrations for Low Temperature Thermometry

14 Jan 2009 Lev - Greywall calibration only
"""
import numpy

def greywall_PA(): "3He melting pressure at the superfluid A-transition"; return 34.338

def greywall_T2P(T):
    """
    Convert temperature to 3He melting pressure according to Greywall Scale
    Ref: D. S. Greywall, Phys. Rev. B, vol. 33 (11), p. 7520 (1986).
    
    Temperature range is 0.931 to 250 mK, precision is 0.2%.
    Return values are offset at superfluid A-transition pressure
    34.338 bar. This value can be accessed as 'mctscale.greywall_PA()'
    """
    
    # Greywall scale ranges from 1 to 250 mK.
    # Pressure is given in bar.
    # Ref: D. S. Greywall, Phys. Rev. B, vol. 33 (11), p. 7520 (1986),
    # Coefficients taken from Michael's (presumably) labview code
    # and checked agains the original paper
    #
    # The pressure delta_P is measured relative to
    # the A-phase transition pressure p_A. 
    # It returns zero at the value T_A = 2.491433 mK
    
    Tmin = 0.931
    Tmax = 250

    P = (-0.019652970/T**3
        +0.061880268/T**2
        -0.078803055/T
        +0.13050600
        -0.043519381*T
        +0.13752791e-3*T**2
        -0.17180436e-6*T**3
        -0.22093906e-9*T**4
        +0.85450245e-12*T**5)

    if numpy.isscalar(P):
        if T < Tmin or T > Tmax:
            P = numpy.NaN
    else:
        P[(T < Tmin) | (T > Tmax)] = numpy.NaN
    return P

__greywall_TT = numpy.concatenate([[250.0], 0.931 * numpy.power(1.0005, range(11188, -1, -1))])
__greywall_PP = greywall_T2P(__greywall_TT)

def greywall_P2T(deltaP):
    """
    Convert temperature to 3He melting pressure according to Greywall Scale
    Ref: D. S. Greywall, Phys. Rev. B, vol. 33 (11), p. 7520 (1986).
    
    Temperature range is 0.931 to 250 mK, precision is 0.2%.
    Return values are offset at superfluid A-transition pressure
    34.338 bar. This value can be accessed as 'mctscale.greywall_PA()'
    """
    return numpy.interp(deltaP, __greywall_PP, __greywall_TT, left=numpy.NaN, right=numpy.NaN)

g_TT = __greywall_TT
g_PP = __greywall_PP
