使用 python 从 netCDF 中随时间读取特定坐标处的值

Sto*_*ace 2 python netcdf

我有一个带有网格的 netCDF 文件(每步 0.25°)。我想要的是变量的值,比如 tempMax,在某个网格点,在过去 50 年里。

我知道您像这样将数据读入 python

lon = numpy.array(file.variables['longitude'][:])
lat = numpy.array(file.variables['latitude'][:])
temp = numpy.array(file.variables['tempMax'][:])
time = numpy.array(file.variables['time'][:])
Run Code Online (Sandbox Code Playgroud)

这给我留下了一个数组,我不知道如何“解开”它。如何在整个时间(存储在时间中)获取某个坐标(存储在 temp 中)的值?S 显示是在某个坐标处随时间变化的值。

任何想法我怎么能做到这一点?

谢谢!

N1B*_*1B4 5

我猜那tempMax是 3D (time x lat x lon),然后应该读入

temp = ncfile.variables['tempMAx'][:,:,:]
Run Code Online (Sandbox Code Playgroud)

(注意两件事:(1)如果您使用的是 Python v2,最好避免使用该词file,而是使用ncfile如上所示的内容,(2)temp将自动存储为numpy.ndarray上面的调用,您不会需要numpy.array()在读入变量期间使用该命令。)

现在,您可以使用以下命令提取特定位置的所有时间的温度

temp_crd = temp[:,lat_idx,lon_idx] 
Run Code Online (Sandbox Code Playgroud)

其中lat_idxlon_idx是对应于纬度和经度坐标索引的整数。如果您事先知道这些索引,很好,只需将它们插入,例如temp_crd = temp[:,25,30]. (您可以使用该工具ncdump查看 netCDF 文件的内容,https ://www.unidata.ucar.edu/software/netcdf/docs/netcdf/ncdump.html )

更可能的情况是您事先知道坐标,但不知道它们的索引。假设您想要 50N 和 270E 的温度。您可以使用该numpy.where函数提取已读入的latlon数组的坐标索引。

lat_idx = numpy.where(lat==50)[0][0]
lon_idx = numpy.where(lon==270)[0][0]

tmp_crd = temp[:,lat_idx,lon_idx]
Run Code Online (Sandbox Code Playgroud)