如何在与一个变量中的最大值相对应的所有变量中找到最大值? [英] How can I find the maximum across all variables corrresponding to the max in one variable?
问题描述
我有一个包含大量变量的每日数据xarray.我想每年提取最大值q_routed
,并在最大值q_routed
发生的那一天提取其他变量的相应值.
I have an xarray of daily data with a number of variables. I want to extract the maximum q_routed
every year and the corresponding values of other variables on the day that the maximum q_routed
happens.
<xarray.Dataset>
Dimensions: (latitude: 1, longitude: 1, param_set: 1, time: 17167)
Coordinates:
* time (time) datetime64[ns] 1970-01-01 ...
* latitude (latitude) float32 44.5118
* longitude (longitude) float32 -111.435
* param_set (param_set) |S1 b''
Data variables:
ppt (time, param_set, latitude, longitude) float64 ...
pet (time, param_set, latitude, longitude) float64 ...
obsq (time, param_set, latitude, longitude) float64 ...
q_routed (time, param_set, latitude, longitude) float64 ...
下面的命令为我提供了一年中每个变量的最大值,但这不是我想要的.
The command below gives me the maximum of every variable in a year, but that's not what I want.
ncdat['q_routed'].groupby('time.year').max( )
试用版
我尝试过
Trial
I tried this
ncdat.groupby('time.year').argmax('time')
这将导致此错误:
ValueError: All-NaN slice encountered
我该怎么做?
推荐答案
对于这种操作,您可能想使用自定义的reduce函数:
For this sort of operation, you probably want to use a custom reduce function:
def my_func(ds, dim=None):
return ds.isel(**{dim: ds['q_routed'].argmax(dim)})
new = ncdat.groupby('time.year').apply(my_func, dim='time')
现在,当您使用完整的nan数组时,argmax
不能很好地发挥作用,因此您可能只想将此功能应用于包含数据的位置,或者预填充现有的nan.这样的事情可能会起作用:
Now, argmax
doesn't play nice when you have a full array of nans, so you may want to either only apply this function to locations with data or pre-fill the existing nans. Something like this could work:
mask = ncdat['q_routed'].isel(time=0).notnull() # determine where you have valid data
ncdat2 = ncdat.fillna(-9999) # fill nans with a missing flag of some kind
new = ncdat2.groupby('time.year').apply(my_func, dim='time').where(mask) # do the groupby operation/reduction and reapply the mask
这篇关于如何在与一个变量中的最大值相对应的所有变量中找到最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!