BreakingBytes / simkit

Model Simulation Framework
http://breakingbytes.github.io/simkit/
BSD 3-Clause "New" or "Revised" License
27 stars 16 forks source link

Error in XLRD reader when reading temperature #101

Open anomam opened 7 years ago

anomam commented 7 years ago

https://github.com/SunPower/Carousel/blob/master/carousel/core/data_readers.py#L306

It looks like 1 * UREG['degC'] leads to an error:

OffsetUnitCalculusError: Ambiguous operation with offset unit (degC).

But 1 * UREG.degC works... So strange

mikofski commented 7 years ago

@anomam thanks for this. Pint handles temperature slightly differently than other units because it can also have an offset in addition to a conversion factor. Please read their documentation. I've been burned by it before too, but basically you can't just multiply by a unit, you have to use the Q_(blah, blah

However, multiplication, division and exponentiation of quantities with offset units is problematic just like addition. Pint (since version 0.6) will by default raise an error when a quantity with offset unit is used in these operations. Due to this quantities with offset units cannot be created like other quantities by multiplication of magnitude and unit but have to be explicitly created:

>> ureg = UnitRegistry()
>> home = 25.4 * ureg.degC
Traceback (most recent call last):
...
pint.errors.OffsetUnitCalculusError: Ambiguous operation with offset unit (degC).
>> Q_(25.4, ureg.degC)
<Quantity(25.4, 'degC')>

It should be stated more clearly in the documentation, and any data readers or other classes that use units or convert them should be changed to use Q_(value, units) instead of value * units. Sorry!

Note that this issue was addressed in data_readers.py in ccb634991ec3257c97c0c7bcb7c04caec075eb8. Originally PVSimLife used Quantities instead of Pint, and Quantities does not allow unit offsets, so it doesn't do Celsius or Fahrenheit, so it didn't have non-multiplicative units and this issue.