从 NetCDF 文件读取和插值数据会抛出形状不匹配错误

问题描述 投票:0回答:0

我有这个 netcdf 文件,其中包含纬度、经度和温度的数据。温度在纬度和经度方面的维度为 103x61。我想通过应用最近插值将这个 103x61 网格的大小调整为 70x70。这是我的代码:

import numpy as np
from netCDF4 import Dataset
from scipy.interpolate import griddata

# load data from netCDF file
nc_file = Dataset('data.nc', 'r')
lat = nc_file.variables['lat'][:]
lon = nc_file.variables['lon'][:]
temp = nc_file.variables['temp'][:]

# create new grid
new_lat = np.linspace(lat.min(), lat.max(), 80)
new_lon = np.linspace(lon.min(), lon.max(), 80)
new_lat, new_lon = np.meshgrid(new_lat, new_lon)

# flatten lat, lon, and temp arrays
lat_flat = lat.flatten()
lon_flat = lon.flatten()
temp_flat = temp.flatten()

# interpolate to new grid
new_temp = griddata((lat_flat, lon_flat), temp_flat, (new_lat, new_lon), method='linear')

# save new_temp to npy file
np.save('new_temp.npy', new_temp)

但这会产生以下错误:

ValueError: shape mismatch: objects cannot be broadcast to a single shape.不匹配是在 arg 0 与形状 (103,) 和 arg 1 与形状 (61,) 之间。

我知道高度和宽度之间存在不匹配,但我无法从任何一侧裁剪数据以适应方形网格,然后对其进行插值。有没有办法解决这个错误?

python numpy scipy netcdf netcdf4
© www.soinside.com 2019 - 2024. All rights reserved.