首页 > 解决方案 > Xarray Dask.delayed 慢:如何在两个数据集之间快速选择/插值

问题描述

我有两个数据集(称为 satdata 和 atmosdata)。Atmosdata 在纬度和经度上均匀网格化。Atmosdata的维度为(纬度:713,级别:37,经度:1440,时间:72),总大小为12GB。Atmosdata 有几个变量,例如温度、湿度等。湿度具有(时间、水平、纬度、经度)的形状。

Satdata包含卫星观测数据,维度为(across_track: 90, channel: 3, time: 32195),数据点数为90*3*32195=8692650。Across_track 表示卫星 FOV 越过轨道位置。卫星数据在纬度/经度上不是均匀网格化的。例如,satdata.latitude 的维度为(时间、通道、cross_track),与 satdata.longitude、satdata.sft 相同。

Atmosdata 和 satdata 中的“时间”变量包含同一天的时间,但在这两个数据集中具有不同的值。我需要找到与 satdata 具有相同纬度、经度和时间的 atmosdata(例如湿度和温度)。

为了实现这一点,我遍历 satdata 以找到每次观察的位置和时间;然后我找到相应的 atmosdata(首先是离卫星数据位置最近的网格,然后插值到卫星时间)。最后,我将所有迭代生成的 atmosdata 连接到一个数据集中。

通过使用一小段数据,我的代码的一部分如下。

import xarray as xr
import numpy as np
import dask
import pandas as pd

# define a simple satdata dataset
lon = np.array([[18.717, 18.195, 17.68  ], [18.396, 17.87 , 17.351, ]])
lat = np.array([[-71.472, -71.635, -71.802],
   [-71.52 , -71.682, -71.847]])
sft = np.array([[1, 1, 1],
   [1, 1, 1]])
time = np.array(['2010-09-07T00:00:01.000000000', '2010-09-07T00:00:03.000000000'],dtype='datetime64[ns]')
satdata = xr.Dataset({'sft': (['time','across_track'], sft)}, coords = {'longitude': (['time','across_track'], lon), 'latitude': (['time','across_track'], lat), 'time':time })

# atmosdata
atmoslat = np.array([-71.75, -71.5 , -71.25, -71.  , -70.75, -70.5 , -70.25, -70.  , -69.75 ])
atmoslon = np.array([17.25, 17.5 , 17.75, 18.  , 18.25, 18.5 , 18.75, 19.  , 19.25])
atmostime = np.array(['2010-09-07T00:00:00.000000000', '2010-09-07T01:00:00.000000000'],dtype='datetime64[ns]')

atmosq = np.random.rand(2,9,9)
atmosdata = xr.Dataset({'q': (['time', 'latitude', 'longitude'], atmosq)}, coords={'longitude':(['longitude'], atmoslon), 'latitude': (['latitude'], atmoslat), 'time':(['time'], atmostime)})

# do the matching:
matched = dask.compute(match(atmosdata, satdata),scheduler='processes',  num_workers=20)[0]

匹配函数如下:

@dask.delayed
def match(atmosdata, satdata):
    newatmos = []
    newind = 0
    # iterate over satdata
    for i in np.ndenumerate(satdata.sft):
        if i[1] != np.nan:
           # find one latitude and longitude of satellite data
           temp_lat = satdata.latitude.isel(time=[i[0][0]], across_track=[i[0][1]])
           temp_lon = satdata.longitude.isel(time=[i[0][0]],  across_track=[i[0][1]])
           # find the atmosdata in the grid nearest to this location
           temp_loc  =  atmosdata.sel(latitude =temp_lat.values.ravel()[0], longitude = temp_lon.values.ravel()[0], method='nearest')
           if temp_loc.q.all() > 0:
               # find atmosdata at the satellite time by interpolation
               temp_time = satdata.time.isel(time=[i[0][0]])
               newatmos.append(temp_loc.interp( time = temp_time.data.ravel() ))
               newind += 1

    return xr.concat(newatmos,dim=pd.Index(range(newind), name='NewInd'))

1)当我启动代码时,它可以工作。但是,如果我不在代码中使用小数据大小,而是使用我的原始数据(具有上述尺寸),然后我启动计算并出现错误。

---> 52 matched = dask.compute(match(ecmwfdata, ssmis_land), scheduler='processes', num_workers=20 )
error: 'i' format requires -2147483648 <= number <= 2147483647

2)如果我使用其他维度的数据集,satdata(across_track:90,channel:3,时间:100)和atmosdata(纬度:71,水平:37,经度:1440,时间:72),计算时间很长时间。我想我的编码并不是使用 DASK 快速解决这个问题的最佳选择。

2)有没有比使用 for 循环更好的方法?为了快速计算,for 循环可能不会利用 DASK 吗?

3)将satdata分块,然后在satdata块中找到纬度和经度的限制,然后根据此限制对atmosdata进行分块,最后将匹配函数应用于每个satdata和atmosdata块?如果这是个好主意,我还不知道如何手动迭代每个 satdata 块......

4) 该函数使用两个参数,satdata 和 atmosdata。由于这两个数据集可能相当大(atmosdata 为 12G),那么计算会更慢吗?

5)在我必须在选择中使用 .value 的函数中,当使用大输入数据时,这是否会使计算变慢?

提前致谢 !

此致

小妮

标签: daskpython-xarraydask-delayed

解决方案


除了说我认为您需要使用使用距离树的重采样包之外,我不确定我是否可以提供帮助

https://github.com/pytroll/pyresample

Pyresample 有一个 swath 网格,可以做你想做的事

我已经使用https://github.com/storpipfugl/pykdtree来查找最近的点


推荐阅读