【问题标题】:How to re-gridding xarray from higher to lower resolution using IDW如何使用 IDW 将 xarray 从更高的分辨率重新网格化到更低的分辨率
【发布时间】:2018-10-03 00:35:58
【问题描述】:

我使用的是 winpython 3.6。我有一个给定区域的 xarray 数据,如下所示:

sea_clt=clt.sel(lat=slice(-13, 31), lon=slice(89,152))
clt_sea_array=sea_clt[:,:,:]

Out[20]: 
<xarray.DataArray 'clt' (time: 20075, lat: 23, lon: 25)>
[11543125 values with dtype=float32]
Coordinates:
  * lat      (lat) float64 -13.0 -11.0 -9.0 -7.0 -5.0 -3.0 -1.0 1.0 3.0 5.0 ...
  * lon      (lon) float64 91.25 93.75 96.25 98.75 101.2 103.8 106.2 108.8 ...
  * time     (time) datetime64[ns] 1950-01-01T12:00:00 1950-01-02T12:00:00 ...

网格间距为 200km*200km(2.0°*2.0°尺度),每天时间序列变量。现在我想为每个时间步以(50km*50km 或 0.5°*0.5° 网格比例)重新网格化这些数据。我尝试使用重塑选项但没有成功。我无法得到任何解决方案。如何使用最近邻或 IDW 等任何标准方法来做到这一点?任何帮助,将不胜感激。

【问题讨论】:

    标签: python python-3.x python-2.7 python-requests python-xarray


    【解决方案1】:

    可以使用reindex 对保留在网格上的数据进行最近邻查找,

    sea_clt.reindex(lat=lat_new, lon=lot_new, method='nearest')
    

    其他插值,例如线性插值,尚未在 xarray 中实现。

    对于线性插值,我们现在能做的最好的可能是

    from scipy.interpolation import interp1d
    
    def interp(y_src, x_src, x_dest, **kwargs):
        return interp1d(x_src, y_src, **kwargs)(x_dest)
    
    new_da = sea_clt
    new_da = xr.apply_ufunc(interp, new_da, input_core_dims=[['lat']], 
                            output_core_dims=[['lat_new']], 
                            kwargs={'x_src': new_da['lat'], 'x_dest': lat_new})
    new_da.coords['lat_new'] = lat_new
    
    new_da = xr.apply_ufunc(interp, new_da, input_core_dims=[['lon']], 
                            output_core_dims=[['lon_new']], 
                            kwargs={'x_src': new_da['lon'], 'x_dest': lon_new})
    new_da.coords['lon_new'] = lon_new
    

    【讨论】:

      【解决方案2】:

      您也可以尝试xEMSF package,它似乎提供了强大的方法来重新网格 xarray 数据。

      【讨论】:

      • 但它没有使用 spyder 在 Windows 上工作的选项
      • Spyder 是一个 IDE(交互式开发环境),xEMSF 是一个 python 包,所以是两个不同的东西!
      【解决方案3】:

      在应用您的代码时,出现此错误:

      from scipy.interpolate import interp1d
      lat_new = np.linspace(0.5, -13, 31)
      lon_new = np.linspace(0.5, 89,152)
      
      
      def interp(y_src, x_src, x_dest, **kwargs):
          return interp1d(x_src, y_src, **kwargs)(x_dest)
      
      
      new_da = sea_clt
      new_da = xarray.apply_ufunc(interp, new_da, input_core_dims=[['lat']], 
                              output_core_dims=[['lat_new']], 
                              kwargs={'x_src': new_da['lat'], 'x_dest': lat_new})
      new_da.coords['lat_new'] = lat_new
      
      new_da = xarray.apply_ufunc(interp, new_da, input_core_dims=[['lon']], 
                              output_core_dims=[['lon_new']], 
                              kwargs={'x_src': new_da['lon'], 'x_dest': lon_new})
      new_da.coords['lon_new'] = lon_new
      

      没有找到我做错的地方。错误在这里:

        File "C:\python3\WinPython\python-3.6.5.amd64\lib\site-packages\scipy\interpolate\interpolate.py", line 663, in _check_bounds
          raise ValueError("A value in x_new is below the interpolation "
      
      ValueError: A value in x_new is below the interpolation range.
      

      【讨论】:

      • 默认情况下,如果您尝试推断,scipy.interpolate.interp1d 会引发错误。为避免这种情况,请添加一个额外的 kwargs,fill_value : “extrapolate”。详情请见docs.scipy.org/doc/scipy/reference/generated/…
      • 我解决了,但问题是我的区域太大了,所以当我从 200 公里的规模以 50 公里的规模重新网格化时,python 花费了太多时间,而我将这些数据保存到 csv 所以文件以 GB 为单位的大小太大。如何快速处理以及如何在保存数据的同时进行压缩
      【解决方案4】:

      我认为您想在空间尺度上从较低分辨率重新网格化到较高分辨率。一种方法是使用 xarray 的 interp_like 方法。以下是示例代码。

      import xarray as xr
      import matplotlib.pyplot as plt
      import numpy as np
      ds1=xr.open_dataset("etopo40.cdf")
      ds2=xr.open_dataset("etopo20.cdf")
      ds1=ds1.rename({'ETOPO40Y': 'lat', 'ETOPO40X':'lon'})
      ds2=ds2.rename({'ETOPO20Y': 'lat', 'ETOPO20X1_1081':'lon'})
      # print(ds1)
      # print(ds2)
      rose40=ds1.ROSE
      rose20=ds2.ROSE
      rose_interp=rose40.interp_like(rose20)
      

      【讨论】:

        猜你喜欢
        • 2016-09-10
        • 2012-09-26
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 2021-01-20
        相关资源
        最近更新 更多