【问题标题】:Xarray Dask.delayed slow: how to be fast to select/interpolate between two datasetsXarray Dask.delayed 慢:如何在两个数据集之间快速选择/插值
【发布时间】:2018-09-24 11:28:19
【问题描述】:

我有两个数据集(称为 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, time: 100), and atmosdata (latitude: 71, level: 37, longitude: 1440, time: 72),计算需要很长时间。我想我的编码并不是使用 DASK 快速解决这个问题的最佳选择。

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

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

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

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

提前致谢!

最好的问候

小妮

【问题讨论】:

  • 嗨小妮,欢迎来到SO。你介意分享mcvemcve2吗?我想有比 for 循环更好的方法来过滤数据。
  • 嗨,我试着缩短帖子,希望它更好。
  • 如果您能提供数据集的样本会更好。即使是它的虚拟版本也很棒。
  • 是的,我创建了一个非常小的数据集来测试代码。提前致谢!
  • 如果您还将 atmosdata 定义为一个简单的虚拟数据集,那将很有帮助,然后人们可以测试您的功能。

标签: dask python-xarray dask-delayed


【解决方案1】:

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

https://github.com/pytroll/pyresample

Pyresample 有一个可以做你想做的事情的条状网格

我使用https://github.com/storpipfugl/pykdtree 找到最近的点

【讨论】:

    猜你喜欢
    • 2016-12-13
    • 2023-03-30
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2017-02-15
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多