简体   繁体   English

`pint` 中派生维度中数量的标准格式

[英]Standard format for quantity in derived dimension in `pint`

TLDR: I'd like to have pint quantities, that are in a certain (derived) dimension, to be converted into a pre-set unit by default. TLDR:我希望在默认情况下将特定(派生)维度中的pint数量转换为预设单位。


Details:细节:

I deal with 5 dimensions, as specified below.我处理 5 个维度,如下所述。 Note that [power] (usually in MW) and [price] (usually in Eur/MWh) are the derived dimensions.请注意,[power](通常以 MW 为单位)和 [price](通常以 Eur/MWh 为单位)是派生的维度。

# units.txt

# Base dimensions and their units
hour = [time] = h = hr
megawatthour = [energy] = MWh
euro = [currency] = Eur = €

# Derived dimensions and their units
[power] = [energy] / [time]
megawatt = megawatthour / hour = MW
[price] = [currency] / [energy]
euro_per_MWh = euro / megawatthour  = Eur/MWh

My question: is it possible to specify that calculated quantities with dimension [power] are by default to be converted to MW?我的问题:是否可以指定尺寸为 [power] 的计算量默认转换为 MW?


Here's an example:下面是一个例子:

import pint

ureg = pint.UnitRegistry("units.txt",)
ureg.default_format = ".0f~P"

energy = 100 * ureg.MWh
time = 4 * ureg.h
revenue = 4000 * ureg.euro

price = revenue / energy
power = energy / time

print(energy, time, revenue, price, power) 
# 100 MWh 4 h 4000 Eur 40 Eur/MWh 25 MWh/h

Here, the power is expressed in MWh/h , because of the way it is calculated, and I can 'convert' it to MW by calling power.to('MW') .在这里,功率以MWh/h表示,因为它的计算方式,我可以通过调用power.to('MW')将其“转换”为MW Is it possible to automatically do this every time a quantity in this dimension is calculated?每次计算此维度中的数量时是否可以自动执行此操作?

Note that doing a blanket .to_base_units() on all quantities reverts it back to MWh/h .请注意,对所有数量执行一揽子.to_base_units()会将其恢复为MWh/h

Remarks:评论:

  • I don't want to use [power] as a base dimension.我不想使用 [power] 作为基本维度。 It moves the problem to the price anyway.无论如何,它会将问题转移到价格上。
  • I'm aware of prefixes;我知道前缀; I just left them out here to keep the example as short as possible.我只是把它们留在这里是为了使示例尽可能简短。

The issue is that MWh is defined as a base unit in the units.txt file instead of using MW as base unit and constructing MWh from MW and hour.问题是 MWh 在units.txt文件中被定义为基本单位,而不是使用 MW 作为基本单位并从 MW 和小时构建 MWh。 Once the unit construction is sorted, .to_base_units() will yield the desired result.一旦单元构造被排序, .to_base_units()将产生所需的结果。

# units-2.txt

# Base dimensions and their units
hour = [time] = h = hr
megawatt = [power] = MW 
euro = [currency] = Eur = €

# Derived dimensions and their units
[energy] = [power] * [time]
megawatthour = megawatt * hour = MWh
[price] = [currency] / [energy]
euro_per_MWh = euro / megawatthour  = Eur/MWh
>>> def test_defs(file):
...     ureg = pint.UnitRegistry(file,)
...     ureg.default_format = ".0f~P"
...     
...     energy = 100 * ureg.MWh
...     time = 4 * ureg.h
...     revenue = 4000 * ureg.euro
...     
...     price = revenue / energy
...     power = energy / time
...     
...     print(energy, time, revenue, price, power) 
...     print(power.to('MW'))
...     print(power.to_base_units())
... 
>>> test_defs("units.txt")
100 MWh 4 h 4000 Eur 40 Eur/MWh 25 MWh/h
25 MW
25 MWh/h
>>> test_defs("units-2.txt")
100 MWh 4 h 4000 Eur 40 Eur/MWh 25 MWh/h
25 MW
25 MW
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM