python - 从 netCDF 中提取位置和跨时间范围的值

标签 python pandas netcdf netcdf4 python-xarray

是否有 xarray 函数的 netCDF4 等价物,用于从 netCDF 文件中为经纬度和特定时间范围选择值:

hndl_nc.sel(time=slice(start_date, end_date)).sel(lon=lon, lat=lat, method='nearest')

我不想使用 cdo 或 nco

最佳答案

根据您的特定数据集会有差异,但是像这样的函数应该在特定坐标和时间片上提取值:

def variable_pull(site_lat, site_lon, lat, lon, var, time_slice):
    lat_idx = (np.abs(lat-site_lat)).argmin()
    lon_idx = (np.abs(lon-site_lon)).argmin()
    return var[time_slice, lat_idx, lon_idx]

对于使用 netCDF4 打开的数据集,与此类似:

f = ncdf.Dataset("filepath","r")
lat = f.variables['lat'][:]
lon = f.variables['lon'][:]
var = f.variables['var'][:]
f.close()
site_lat = your coordinates of interest
site_lon = your coordinates of interest
values = variable_pull(site_lat, site_lon, lat, lon, var, time_slice)

关于python - 从 netCDF 中提取位置和跨时间范围的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153531/

相关文章:

matlab - 如何更改matlab世界地图中的经度标签位置

Python线程: Are arguments passed to thread evaluated in parallel?

python - 在 Python 3 中操作嵌套字典/列表树

python-3.x - 与 Pandas 数据框中的分组平均值的区别?

python - 如何在不同大小的数据帧之间使用 np.where ? 'operands could not be broadcast together'

windows - NCO netcdf 命令 .exe Windows

python - 如何使用 xarray 查找网格数据的每日百分位数?

python - fork:关闭所有打开的套接字

python - urllib3.exceptions.NewConnectionError 错误在一台机器上处理但在另一台机器上不处理

python - Groupby 并为组成员分配唯一 ID