【问题标题】:Computing an index (heat index) in Iris from mismatched units?从不匹配的单位计算 Iris 中的指数(热指数)?
【发布时间】:2015-02-03 00:23:22
【问题描述】:

我是 Iris 库的新手,需要计算 heat index,它是温度和相对湿度的多元非线性函数,类似于 $HI = temp +rh + temp*rh + temp^ 2 *rh + rh^2*temp $ 。这里,temp 的单位是华氏度,rh 的单位是 1。

但是,虹膜立方体不会添加不同的单位:

In [147]: HI = temp + rh + temp*rh + temp**2*rh + rh**2*temp
---------------------------------------------------------------------------
NotYetImplementedError                    Traceback (most recent call last)
<ipython-input-147-675ea72a5d06> in <module>()
----> 1 HI = temp + rh + temp*rh + temp**2*rh + rh**2*temp

/Users/me/anaconda/lib/python2.7/site-packages/iris/cube.pyc in __add__(self, other)
  2596 
   2597     def __add__(self, other):
-> 2598         return iris.analysis.maths.add(self, other, ignore=True)
   2599     __radd__ = __add__
   2600 

/Users/me/anaconda/lib/python2.7/site-packages/iris/analysis/maths.pyc in add(cube, other, dim, ignore, in_place)
    166     op = operator.iadd if in_place else operator.add
167     return _add_subtract_common(op, 'addition', 'added', cube, other, dim=dim,
--> 168                                 ignore=ignore, in_place=in_place)
169 
170 

/Users/me/anaconda/lib/python2.7/site-packages/iris/analysis/maths.pyc     in _add_subtract_common(operation_function, operation_noun,     operation_past_tense, cube, other, dim, ignore, in_place)
    216     """
    217     _assert_is_cube(cube)
--> 218     _assert_matching_units(cube, other, operation_noun)
219 
220     if isinstance(other, iris.cube.Cube):

/Users/me/anaconda/lib/python2.7/site-packages/iris/analysis/maths.pyc in _assert_matching_units(cube, other, operation_noun)
132         raise iris.exceptions.NotYetImplementedError(
133             'Differing units (%s & %s) %s not implemented' %
--> 134             (cube.units, other.units, operation_noun))
135 
136 

NotYetImplementedError: Differing units (Fahrenheit & 1) addition not implemented

如果我将数据调用为 numpy 数组,那么这是一种解决方法,例如: heatIndex = -42.379 + temp.data + rh.data + temp.data2 + rh.data2 但这似乎违背了最初使用 Iris 的目的,并且需要重新编写元数据。

这可能与虹膜立方体有关吗?是否有我缺少的无单位 udunit 允许这种情况发生?

注意:如果从错误中不清楚,我正在运行 Python 2.7(和 Iris 1.7)。

【问题讨论】:

    标签: python python-2.7 python-iris


    【解决方案1】:

    如果我将数据称为 numpy 数组...这似乎违背了首先使用 Iris 的目的,并且需要重新编写元数据。

    首先,利用 numpy 和所有令人难以置信的 scipy 生态系统是 Iris 的强大功能。在 Iris 中存在功能的情况下,最好使用它而不是自己管理元数据,但如果某些东西不存在,那么希望它不会太难实现。

    在您的情况下,您可能希望自己更新一些元数据(即 Iris 不会知道这种数学转换会产生代表“热指数”的数据)。

    看来热量指数应该以华氏度为单位,而将华氏度立方体添加到标量立方体会导致此错误。最简单的解决方案可能是将相对湿度标量 data(即不是 cube)添加到温度:

    rh = rh.data
    heat_index = temp + rh + temp*rh + temp**2*rh + rh**2*temp
    heat_index.rename('heat_index')
    assert heat_index.units == 'Fahrenheit'
    

    【讨论】:

    • 这让我走上了正轨,虽然它并没有真正解决添加 temp + temp**2 的问题(热指数单位是华氏度 + 华氏度 ^2 度)。我通过因式分解和相乘来解决这个问题:temp*(a+b*temp) + c。计算需要非常长的时间,所以希望这在未来是可行的。
    • 编辑:此解决方案导致 python 内存不足。我只是坚持使用 Numpy 数组来解决这个问题。
    【解决方案2】:

    我认为当您开始使用数据数组时,您的思路是正确的,但它可以做得更简洁一些。关键是首先复制温度立方体并从那里开始工作

    C1 = -42.379
    C2 = 2.04901523
    # etc
    
    def get_heat_index(temp, rh):
        '''Calculate the heat index as defined by George Winterling'''
    
        # make sure we are using the right temperature units
        # other checks could be implemented as assertions
        temp.convert_units('Fahrenheit')
    
        # start by copying the temperature cube
        heat_index = temp
    
        # fix the name of the cube to be returned
        heat_index.rename('heat_index')
    
        # C2 - do this first as already have temperature data
        heat_index.data = (C1 + C2*temp.data +
                          C3*rh.data +
        # etc
                          C9*temp.data**2*rh.data**2)
    
        return heat_index
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多