Python 从不匹配的单位计算Iris中的指数(热指数)?

Python 从不匹配的单位计算Iris中的指数(热指数)?,python,python-2.7,python-iris,Python,Python 2.7,Python Iris,我是Iris库的新手,需要计算a,它是温度和相对湿度的多元非线性函数,类似于$HI=temp+rh+temp*rh+temp^2*rh+rh^2*temp$。这里,温度的单位是华氏度,相对湿度的单位是1 但是,Iris多维数据集不会添加不同的单位: In [147]: HI = temp + rh + temp*rh + temp**2*rh + rh**2*temp ----------------------------------------------------------------

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

但是,Iris多维数据集不会添加不同的单位:

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
[147]中的
HI=temp+rh+temp*rh+temp**2*rh+rh**2*temp
---------------------------------------------------------------------------
NotYetImplementedError回溯(最近一次呼叫最后一次)
在()
---->1高=温度+相对湿度+温度*相对湿度+温度**2*相对湿度+相对湿度**2*温度
/Users/me/anaconda/lib/python2.7/site-packages/iris/cube.pyc in_u___添加__(self,other)
2596
2597定义添加(自身、其他):
->2598返回iris.analysis.math.add(self、other、ignore=True)
2599 uuu radd uuuuuu=uuuuu add__
2600
/添加中的Users/me/anaconda/lib/python2.7/site-packages/iris/analysis/math.pyc(多维数据集、其他、dim、忽略、就地)
166 op=operator.iadd,如果在其他位置,则为operator.add
167返回加减法公共值(op,'加法','加法',立方体,其他,dim=dim,
-->168忽略=忽略,就地=就地)
169
170
/Users/me/anaconda/lib/python2.7/site-packages/iris/analysis/math.pyc in_add_subtract_common(操作函数、操作名词、操作过去时、立方体、其他、昏暗、忽略、原地)
216     """
217_断言_是_立方体(立方体)
-->218断言匹配单元(立方体、其他、操作名词)
219
220如果存在(其他,iris.cube.cube):
/Users/me/anaconda/lib/python2.7/site-packages/iris/analysis/math.pyc in\u assert\u matching\u units(立方体、其他、操作\u名词)
132升起iris.exceptions.NotYetImplementedError(
133“未实现不同的单位(%s&%s)%s”
-->134(立方单位,其他单位,操作名词))
135
136
NotYetImplementedError:未实施不同单位(华氏度和1度)的添加
如果我将数据称为numpy数组,则这是一种解决方法,例如: 热指数=-42.379+温度数据+相对湿度数据+温度数据2+相对湿度数据2 但这似乎首先违背了使用Iris的目的,需要重新编写元数据

这可能与iris多维数据集有关吗?是否有一个我缺少的单位更少的udunit允许这种情况发生

注意:如果不清楚错误,我正在运行Python2.7(和Iris1.7)

如果我将数据称为numpy数组……这似乎首先就违背了使用Iris的目的,需要重新写入元数据

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

在您的情况下,您可能希望自己更新一些元数据(例如,Iris不知道此数学转换生成表示“热指数”的数据)

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

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'

我认为当你开始使用数据阵列时,你的思路是正确的,但是它可以变得更干净一些。关键是从复制温度立方体开始,然后从那里开始工作

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

这让我走上了正确的道路,尽管它并没有真正解决添加temp+temp**2(热指数单位为华氏度+华氏度^2)的问题。我通过因子分解和乘以:temp*(a+b*temp)+c。计算要花很长的时间,所以希望这在将来是可行的。编辑:这个解决方案会导致python内存不足。我只是坚持使用Numpy数组来解决这个问题。