0

我正在使用 python/iris 从日常数据中获取年度极值。我aggregated_by('season_year', iris.analysis.MIN)用来获取极值,但我还需要知道它们每年何时出现。我已经编写了下面的代码,但这真的很慢,所以我想知道是否有人知道可能有一种iris内置方式来做到这一点,或者可以想到另一种更有效的方式?

谢谢!

#--- get daily data
cma = iris.load_cube('daily_data.nc')

#--- get annual extremes
c_metric = cma.aggregated_by('season_year', iris.analysis.MIN)

#--- add date of when the extremes are occurring
extrdateli=[]

#loop over all years
for mij in range(c_metric.data.shape[0]):
#
    # get extreme value
    m = c_metric.data[mij]
    #
    #get values for this year
    cma_thisseasyr = cma.extract(iris.Constraint(season_year=lambda season_year:season_year==c_metric.coord('season_year').points[mij]))
    #
    #get date in data cube for when this extreme occurs and print add as string to a list
    extradateli += [ str(c_metric.coord('season_year').points[mij])+':'+','.join([''.join(_) for _ in zip([str(_) for _ in cma_thisseasyr.coord('day').points[np.where(cma_thisseasyr.data==m)]], [str(_) for _ in cma_thisseasyr.coord('month').points[np.where(cma_thisseasyr.data==m)]], [str(_) for _ in cma_thisseasyr.coord('year').points[np.where(cma_thisseasyr.data==m)]])])]

#add this list to the metric cube as attribute
c_metric.attributes['date_of_extreme_value'] = ' '.join(extrdateli)

#--- save to file
iris.save('annual_min.nc')
4

1 回答 1

0

我认为缓慢的部分是您提取每个季节年度的值。您可以通过省略 来加快速度lambda,即:

iris.Constraint(season_year=c_metric.coord('season_year').points[mij])

如果这仍然太慢,您可以直接处理numpy多维数据集中的数组。切片 numpy 数组比从多维数据集中提取要快得多。为简单起见,下面的示例假设您有一个时间坐标。

import iris
import numpy as np
import iris.coord_categorisation as cat

#--- create a dummy data cube
ndays = 12 * 365 + 3  # 12 years of data
tcoord = iris.coords.DimCoord(range(ndays), units='days since 2001-02-01',
                              standard_name='time')

cma = iris.cube.Cube(np.random.normal(0, 1, ndays), long_name='blah')
cma.add_dim_coord(tcoord, 0)
cat.add_season_year(cma, 'time')

#--- get annual extremes
c_metric = cma.aggregated_by('season_year', iris.analysis.MIN)

#--- add date of when the extremes are occurring
extrdateli=[]

#loop over all years
for mij in range(c_metric.data.shape[0]):
    #
    #get extreme value
    m = c_metric.data[mij]
    #
    #get values for this year
    year_index = cma.coord('season_year').points == c_metric.coord('season_year').points[mij]
    temperatures_this_syear = cma.data[year_index]
    dates_this_syear = tcoord.units.num2date(tcoord.points[year_index])
    #
    #get date in data cube for when this extreme occurs and print add as string to a list
    extreme_dates = dates_this_syear[temperatures_this_syear==m]
    extrdateli += [ str(c_metric.coord('season_year').points[mij])+':'+','.join(str(date) for date in extreme_dates)]


#add this list to the metric cube as attribute
c_metric.attributes['date_of_extreme_value'] = ' '.join(extrdateli)
于 2018-12-14T16:10:26.313 回答