【问题标题】:Read value over time at certain coordinate from netCDF with python使用python从netCDF读取某个坐标随时间变化的值
【发布时间】:2015-03-04 02:35:20
【问题描述】:

我有一个带有网格的 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'][:])

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

有什么想法可以实现吗?

谢谢!

【问题讨论】:

    标签: python netcdf


    【解决方案1】:

    我猜tempMax 是 3D(时间 x 纬度 x 经度),然后应该读为

    temp = ncfile.variables['tempMAx'][:,:,:]
    

    (注意两点:(1)如果您使用 Python v2,最好避免使用 file 一词,而是使用类似 ncfile 的词,如上所示,(2)temp 将被自动存储作为numpy.ndarray,只需通过上面的调用,就不需要在读入变量的过程中使用numpy.array() 命令。)

    现在您可以使用

    提取特定位置的所有时间温度
    temp_crd = temp[:,lat_idx,lon_idx] 
    

    其中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]
    

    【讨论】:

    • 甜蜜。非常感谢!这有帮助
    猜你喜欢
    • 2017-03-08
    • 2014-11-18
    • 2013-07-05
    • 1970-01-01
    • 2019-06-23
    • 2018-06-06
    • 2013-07-10
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多