
# (C) 2019 Simon Funk simon@sifter.org

known_conversions = {
        'Pa'  : {  'mBar': lambda x: x/100.     },
        'mBar': {    'Pa': lambda x: x*100.     },
        'C'   : {     'F': lambda x: x*9/5 + 32 },
        'F'   : {     'C': lambda x: (x-32)*5/9 },
       'Bq/m3': { 'pCi/L': lambda x: x/37       },
       'pCi/L': { 'Bq/m3': lambda x: x*37       },
    }

def convert_units(value, from_units, to_units):
    """This convenience function expects string-values units names,
    and converts the given value in from_units to to_units.
    Throws an exception of the conversion is unknown.
    """
    try:
        convert = known_conversions[from_units][to_units]
    except KeyError:
        raise Exception("Don't know how to convert from %r to %r"%(from_units, to_units))
    return convert(value)

