"""
Routines to reformat data
"""

def fromHardness (hardness):
# convert the general hardness number to Shore A or D

    a = [30.0 , 31.0 , 32.0 , 33.0 , 34.0 ,  \
        35.0 , 36.0 , 37.0 , 38.0 , 39.0 ,  \
        40.0 , 41.0 , 42.0 , 43.0 , 44.0 ,  \
        45.0 , 46.0 , 47.0 , 48.0 , 49.0 ,  \
        50.0 , 51.0 , 52.0 , 53.0 , 54.0 ,  \
        55.0 , 56.0 , 57.0 , 58.0 , 59.0 ,  \
        60.0 , 61.0 , 62.0 , 63.0 , 64.0 ,  \
        65.0 , 66.0 , 67.0 , 68.0 , 69.0 ,  \
        70.0 , 71.0 , 72.0 , 73.0 , 74.0 ,  \
        75.0 , 76.0 , 77.0 , 78.0 , 79.0 ,  \
        80.0 , 81.0 , 82.0 , 83.0 , 84.0 ,  \
        85.0 , 86.0 , 87.0 , 88.0 , 89.0 ,  \
        90.0 , 91.0 , 92.0 , 93.0 , 94.0 ,  \
        95.0 , 96.0 , 97.0 , 98.0 , 99.0 ,  \
        100.0]

    
    d = [6.0 , 6.2 , 6.4 , 6.6 , 6.8 ,  \
        7.0 , 7.2 , 7.4 , 7.6 , 7.8 ,  \
        8.0 , 8.4 , 8.8 , 9.2 , 9.6 ,  \
        10.0 , 10.4 , 10.8 , 11.2 , 11.6 ,  \
        12.0 , 12.4 , 12.8 , 13.2 , 13.6 ,  \
        14.0 , 14.4 , 14.8 , 15.2 , 15.6 ,  \
        16.0 , 16.6 , 17.2 , 17.8 , 18.4 ,  \
        19.0 , 19.6 , 20.2 , 20.8 , 21.4 ,  \
        22.0 , 22.6 , 23.2 , 23.8 , 24.4 ,  \
        25.0 , 25.8 , 26.6 , 27.4 , 28.2 ,  \
        29.0 , 29.8 , 30.6 , 31.4 , 32.2 ,  \
        33.0 , 34.2 , 35.4 , 36.6 , 37.8 ,  \
        39.0 , 40.4 , 41.8 , 43.2 , 44.6 ,  \
        46.0 , 48.4 , 50.8 , 53.2 , 55.6 ,  \
        58.0]

    shoreA = ''
    shoreD = ''

    if hardness < 30:
        # has to be Shore A
        shoreA = int (hardness)
        return str (shoreA)+' A', str (shoreD)+' D'

    if hardness > 100:
        # has to be Shore D
        shoreD = hardness - 42
        return str (shoreA)+' A', str (shoreD)+' D'

    # could be either shore A or D
    shoreA = int (hardness)
    if shoreA in a:
        loc = a.index (shoreA)
        shoreD = int (d [loc])
    return str (shoreA)+' A', str (shoreD)+' D'



def fromPercent (percent):
    # change internal float percentage to string + %

    return str (percent) + '%'
                


def fromPli (var):
    # change internal tear strength to string + pli

    return str (var) + ' pli'



def fromPsi (var):
    # change internal psi to string + psi

    return str (var) + ' psi'


    
def fromRatio (ratio):
    # changes internal mixRatio to normal

    from fractions import Fraction

    temp = Fraction.from_float (ratio).limit_denominator ()

    a = temp._numerator
    b = temp._denominator
    b = b - a

    mixRatio = str (a)+'A:'+str (b)+'B'

    return (mixRatio)



def fromSeconds (seconds):
    """
    convert seconds into days, hours, minutes, seconds
    """

    days = seconds / (24.*60.*60.)
    if days > 1.:
        return str (days)+' days'
    hours = seconds /(60.*60.)
    if hours > 1.:
        return str (hours)+' hrs'
    minutes = seconds / 60.
    if minutes > 1.:
        return str (minutes)+' min'
    return str (seconds)+' sec'



def fromViscosity (var):
    # convert internal representation of viscosity to
    # a string for output

    # check for the special cases
    if var == 91000:
        return 'brushable'
    if var == 92000:
        return 'paste'
    if var == 93000:
        return 'dough'
    if var == 94000:
        return 'putty'

    # some smaller number
    string = str (var)+ ' cps'

    return string



def toFloat (string):
    """
    Check var (string) for float conversion
    return 0 if it does not pass.
    The value as float if it does.
    """

    # remove < if present
    string = string.replace ('<','')
    
    try:
        return float (string)
    except:
        return 0.



def toHardness (shoreNo):
    # convert shore A or D to a standard hardness number

    import string

    a = [30.0, 35.0, 40.0, 42.5, 45.0, \
         47.5, 50.0, 52.5, 55.0, 57.5, \
         60.0, 61.666, 63.333, 65.0, 66.666, \
         68.333, 70.0, 71.666, 73.333, 75.0, \
         76.25, 77.5, 78.75, 80.0, 81.25, \
         82.5, 83.75, 85.0, 85.833, 86.666, \
         87.5, 88.333, 89.167, 90.0, 90.714, \
         91.428, 92.142, 92.857, 93.571, 94.285, \
         95.0, 95.417, 95.833, 96.25, 96.667, \
         97.083, 97.5, 97.917, 98.333, 98.75, \
         99.167, 99.583, 100.0]
    
    d = [6, 7, 8, 9, 10, \
         11, 12, 13, 14, 15, \
         16, 17, 18, 19, 20, \
         21, 22, 23, 24, 25, \
         26, 27, 28, 29, 30, \
         31, 32, 33, 34, 35, \
         36, 37, 38, 39, 40, \
         41, 42, 43, 44, 45, \
         46, 47, 48, 49, 50, \
         51, 52, 53, 54, 55, \
         56, 57, 58]
    
    # set to lower case
    shoreNo = shoreNo.lower ()
    
    if 'a' in shoreNo:
        loc = shoreNo.index ('a')
        try:
            hardness = int (shoreNo [:loc])
        except:
            hardness = 0
        return hardness
                        
        
    if 'd' in shoreNo:
        loc = shoreNo.index ('d')
        try:
            hardness = int (shoreNo [:loc])
        except:
            hardness = 0
        if hardness > 58:
            hardness += 42
        else:
            loc = hardness - 6
            if loc < 6:
                loc = 6
            hardness = a [loc]
        return hardness

    # don't know if it's A or D - assume A
    try:
        hardness = int (shoreNo)
    except:
        hardness = 0
    return hardness




def toInt (string):
    """
    Check string for int conversion
    return 0 if it does not pass.
    The value as int if it does.
    """
        
    try:
        return int (string)
    except:
        return 0



def toPercent (string):
    # convert percentage to internal
    # just remove the % sign

    string = string.replace ('%','')

    try:
        percent = float (string)
    except:
        percent = 0.

    return percent

                


def toPli (string):
    # convert tear strength to intermal

    # remove 'pli'
    string = string.replace ('pli','')

    try:
        var = int (string)
    except:
        var = 0

    return var



def toPsi (string):
    # remove 'psi' and convert to int

    string = string.replace ('psi','')

    try:
        var = int (string)
    except:
        var = 0

    return var



def toRatio (mixRatio):
    # convert the mix ratio to a number for storage & sorting

    # *** don't know how to handle weight vs. volume problem


    # lower case
    mixRatio = mixRatio.lower ()
    
    # mixRatio must have a :
    try:
        a,b = mixRatio.split (':')
    except:
        return 0.

    # remove trailer characters
    a = a.rstrip ('a')
    try:
        aFloat = float (a)
    except:
        return 0.
    b = b.rstrip ('pbv')
    b = b.rstrip ('pbw')
    b = b.rstrip ()
    b = b.rstrip ('b')
    try:
        bFloat = float (b)
    except:
        return 0.

    ratio = aFloat/(aFloat+bFloat)
    return ratio
    

    
def toSeconds (timeString):
    
    # convert days, hours, minutes into seconds
    

    import string

    # check if there is a dash -
    if '-' in timeString:
        # this should be something like "1-2 days"
        # drop the low end
        loc = timeString.index ('-') + 1
        timeString = timeString [loc:]
    # check for day, hour, minute, second
    if 'day' in timeString:
        loc = timeString.index ('day')
        multiplier = 24.*60.*60.
    elif 'hr' in timeString:
        loc = timeString.index ('hr')
        multiplier = 60.*60.
    elif 'hour' in timeString:
        loc = timeString.index ('hour')
        multiplier = 60.*60.
    elif 'min' in timeString:
        loc = timeString.index ('min')
        multiplier = 60.
    elif 'sec' in timeString:
        loc = timeString.index ('sec')
        multiplier = 1.
    else:
        # if there is no indication assume seconds
        loc = len (timeString)
        multiplier = 1.

    # pull numeric value 
    temp = timeString [:loc]
    # try to convert
    try:
        time = float (temp)
    except:
        time = 0.

    return time*multiplier
        
        

def toViscosity (string):
    # convert viscosity field (cps) to an integer

    # check for non numeric designation
    # 90000 series numbers are just arbitrary large nos.
    # not actual cps

    string = string.lower ()
    
    if 'brushable' in string:
        return 91000
    if 'paste' in string:
        return 92000
    if 'dough' in string:
        return 93000
    if 'putty' in string:
        return 94000

    # remove 'cps'
    string = string.replace ('cps','')

    try:
        var = int (string)
    except:
        var = 0

    return var