Xarray 最有效的方法 select 变量并计算其平均值

Xarray most efficient way to select variable and calculate its mean

我有一个用 xarray 打开的 3Gb 数据立方体,它有 3 个我感兴趣的变量(v、vx、vy)。下面是代码说明。

我只对 2009 年到 2013 年之间的一个特定时间 window 感兴趣,而整个数据集从 1984 年到 2018 年。

我想做的是:

问题是太费时间了,1小时后我写的那几行代码还是运行。我不明白的是,如果我将我的“v”值保存为一个数组,这样加载它们并计算它们的平均值,它比我在下面写的(见代码)花费的时间少得多。 我不知道是否存在内存泄漏,或者这是否只是一种糟糕的方式。我的电脑有 16Gb 内存,其中 60% 在加载数据立方体之前可用。所以理论上它应该有足够的 RAM 来计算所有内容。

什么是将我的数据立方体截断到所需时间的有效方法-window,然后计算 3 个变量“v”、“vx”、“vy”的时间平均值(在轴 0 上) " ?

我试过这样做:

datacube = xr.open_dataset('datacube.nc')  # Load the datacube
datacube = datacube.reindex(mid_date = sorted(datacube.mid_date.values))  # Sort the datacube by ascending time, where "mid_date" is the time dimension
    
sdate = '2009-01'   # Start date
edate = '2013-12'   # End date
    
ds = datacube.sel(mid_date = slice(sdate, edate))   # Create a new datacube gathering only the values between the start and end dates
    
vvtot = np.nanmean(ds.v.values, axis=0)   # Calculate the mean of the values of the "v" variable of the new datacube
vxtot = np.nanmean(ds.vx.values, axis=0)
vytot = np.nanmean(ds.vy.values, axis=0)






Dimensions:                    (mid_date: 18206, y: 334, x: 333)
Coordinates:
  * mid_date                   (mid_date) datetime64[ns] 1984-06-10T00:00:00....
  * x                          (x) float64 4.868e+05 4.871e+05 ... 5.665e+05
  * y                          (y) float64 6.696e+06 6.696e+06 ... 6.616e+06
Data variables: (12/43)
    UTM_Projection             object ...
    acquisition_img1           (mid_date) datetime64[ns] ...
    acquisition_img2           (mid_date) datetime64[ns] ...
    autoRIFT_software_version  (mid_date) float64 ...
    chip_size_height           (mid_date, y, x) float32 ...
    chip_size_width            (mid_date, y, x) float32 ...
                        ...
    vy                         (mid_date, y, x) float32 ...
    vy_error                   (mid_date) float32 ...
    vy_stable_shift            (mid_date) float64 ...
    vyp                        (mid_date, y, x) float64 ...
    vyp_error                  (mid_date) float64 ...
    vyp_stable_shift           (mid_date) float64 ...
Attributes:
    GDAL_AREA_OR_POINT:         Area
    datacube_software_version:  1.0
    date_created:               30-01-2021 20:49:16
    date_updated:               30-01-2021 20:49:16
    projection:                 32607

尽量避免在两者之间调用“.values”,因为当您这样做时,您将切换到 np.array 而不是 xr.DataArray!

import xarray as xr
from dask.diagnostics import ProgressBar

# Open the dataset using chunks.
ds = xr.open_dataset(r"/path/to/you/data/test.nc", chunks = "auto")

# Select the period you want to have the mean for. 
ds = ds.sel(time = slice(sdate, edate))

# Calculate the mean for all the variables in your ds.
ds = ds.mean(dim = "time")

# The above code takes less than a second, because no actual
# calculations have been done yet (and no data has been loaded into your RAM).
# Once you use ".values", ".compute()", or
# ".to_netcdf()" they will be done. We can see progress like this:
with ProgressBar():
    ds = ds.compute()