1 2 3 4 5 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
.. _nonmult:
Temperature conversion
======================
Unlike meters and seconds, fahrenheits, celsius and kelvin are not
multiplicative units. Temperature is expressed in a system with a
reference point, and relations between temperature units include
not only an scaling factor but also an offset. Pint supports these
type of units and conversions between them. The default definition
file includes fahrenheits, celsius, kelvin and rankine abbreviated
as degF, degC, degK, and degR.
For example, to convert from celsius to fahrenheit:
.. testsetup:: *
from pint import UnitRegistry
ureg = UnitRegistry()
Q_ = ureg.Quantity
.. doctest::
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> home = 25.4 * ureg.degC
>>> print(home.to('degF'))
77.72000039999993 degF
or to other kelvin or rankine:
.. doctest::
>>> print(home.to('degK'))
298.54999999999995 degK
>>> print(home.to('degR'))
537.39 degR
Additionally, for every temperature unit in the registry,
there is also a *delta* counterpart to specify differences.
For example, the change in celsius is equal to the change
in kelvin, but not in fahrenheit (as the scaling factor
is different).
.. doctest::
>>> increase = 12.3 * ureg.delta_degC
>>> print(increase.to(ureg.delta_degK))
12.3 delta_degK
>>> print(increase.to(ureg.delta_degF))
6.833333333333334 delta_degF
..
Subtraction of two temperatures also yields a *delta* unit.
.. doctest::
>>> 25.4 * ureg.degC - 10. * ureg.degC
15.4 delta_degC
Differences in temperature are multiplicative:
.. doctest::
>>> speed = 60. * ureg.delta_degC / ureg.min
>>> print(speed.to('delta_degC/second'))
1.0 delta_degC / second
The parser knows about *delta* units and use them when a temperature unit
is found in a multiplicative context. For example, here:
.. doctest::
>>> print(ureg.parse_units('degC/meter'))
delta_degC / meter
but not here:
.. doctest::
>>> print(ureg.parse_units('degC'))
degC
You can override this behaviour:
.. doctest::
>>> print(ureg.parse_units('degC/meter', to_delta=False))
degC / meter
To define a new temperature, you need to specify the offset. For example,
this is the definition of the celsius and fahrenheit::
degC = degK; offset: 273.15 = celsius
degF = 5 / 9 * degK; offset: 255.372222 = fahrenheit
You do not need to define *delta* units, as they are defined automatically.
|