【问题标题】:Creating a toy 3D Array contains hourly frequency, temperature and lat,lon coordinates创建一个玩具 3D 数组包含每小时频率、温度和纬度、经度坐标
【发布时间】:2020-12-26 04:47:09
【问题描述】:

我想创建一个 3 维数据数组,该数组存储具有相应纬度和经度的温度数据以及每小时频率的时间。所以我做了如下编码;

np.random.seed(123)

temperature_3d = 15 + 10 * np.random.randn(24,4,4)    # 3-dimensional
lat = np.random.uniform(low=-90, high=90, size=(1,4))
lon = np.random.uniform(low=-180, high=180, size=(1,4))

# round to two digits after decimal point
temperature_3d = np.around(temperature_3d, decimals=2)
lat , lon = np.around([lat, lon], decimals=2)
date_rng = pd.date_range(start='1/1/2018T01:00', end='1/2/2018', freq='H')
da = xr.DataArray(data=temperature_3d,
                  coords={"lat": (["x","y"], lat),
                          "lon": (["x","y"], lon), 
                          "day": date_rng},
                  dims=["x","y","day"])
                  
da

我得到了这个错误,

ValueError: conflicting sizes for dimension 'x': length 24 on the data but length 1 on coordinate 'lat'

在维度上它是错误的,我可以在不包括时间的情况下修复它,我的目标是创建一个具有 720x40x40 维度(timexlat,lonxtime)的数据数组。

我也是这类图书馆的新手。因此,我将不胜感激。

【问题讨论】:

    标签: python pandas numpy multidimensional-array python-xarray


    【解决方案1】:

    看起来形状需要正确匹配。

    首先,因为您的温度数据的形状为 (24,4,4),

    temperature_3d = 15 + 10 * np.random.randn(24,4,4)    # 3-dimensional
    

    你需要把日期放在你的昏暗中:

    da = xr.DataArray(data=temperature_3d,
                  coords={"lat": (["x","y"], lat),
                          "lon": (["x","y"], lon), 
                          "day": date_rng},
                  dims=["day","x","y"])
    

    其次,您的 lat 和 lon 坐标具有根据 ["x","y"] 的形状,因此在示例中,您需要一个 4x4 网格来表示每个点的纬度,另一个 4x4 网格用于 lon在每个点。为此,我们可以使用 np.meshgrid:

    latgrid, longrid = np.meshgrid(lat,lon)
    

    所以,完整的:

    import numpy as np
    import xarray as xa
    
    np.random.seed(123)
    
    temperature_3d = 15 + 10 * np.random.randn(24,4,4)    # 3-dimensional
    lat = np.random.uniform(low=-90, high=90, size=(4))
    lon = np.random.uniform(low=-180, high=180, size=(4))
    
    # round to two digits after decimal point
    temperature_3d = np.around(temperature_3d, decimals=2)
    lat , lon = np.around([lat, lon], decimals=2)
    latgrid, longrid = np.meshgrid(lat,lon)
    date_rng = pd.date_range(start='1/1/2018T01:00', end='1/2/2018', freq='H')
    
    da = xa.DataArray(data=temperature_3d,
                  coords={"lat": (["x","y"], latgrid),
                          "lon": (["x","y"], longrid), 
                          "day": date_rng},
                  dims=["day","x","y"])
    da
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2014-12-16
      • 2018-02-27
      相关资源
      最近更新 更多