【问题标题】:How to compare missing data in two netcdf files using python?如何使用python比较两个netcdf文件中的缺失数据?
【发布时间】:2021-06-15 10:46:00
【问题描述】:

我有两个 netcdf 文件:file1 (cru pre) 和 file2 (chirps precip)。这两个文件已重新映射到同一个网格,包含月度数据并涵盖相同的时间段(1981-2017)。

如何使用python循环遍历文件,实现如下逻辑: 对于 file1 中的每个数据点 如果该值不缺失且对应的 file2 值不缺失 然后保留 file1 值和 file2 值 否则视为缺失

我基本上想得到一个 output_file1,它只包含 file2 不丢失的点的 file1 数据和一个 output_file2,它只包含 file1 不丢失的点的 file2 数据。在这两个文件中,我都将 missing_values 设置为 999。

我是 python 新手并使用 NetCDF 文件,希望得到任何指导。

【问题讨论】:

  • 你可以试试xarrarygithub.com/pydata/xarray有一个好的开始:)
  • 谢谢,我确实让它们进入了 xarrays 但还是遇到了一些问题。

标签: python netcdf


【解决方案1】:

您可以使用我的 nctoolkit 包 (https://nctoolkit.readthedocs.io/en/latest/) 用几行代码解决这个问题。

要执行第一个文件,请尝试以下操作。

import nctoolkit as nc
# read in the data
ds1 = nc.open_data("file1.nc")
ds2 = nc.open_data("file2.nc")

# create a mask, where 1 is non-missing data, missing is missing
# change var to whatever the variable is named
ds2.assign(mask = lambda x: x.var == x.var, drop = True)
# multiply the first file by the masked file
ds1.multiply(ds2)
# save the output file
ds1.to_nc("file1_fixed.nc)

这将在每个时间步进行屏蔽。

【讨论】:

  • 非常感谢,我试试这个方法。
  • nctoolkit 非常有用 - 谢谢!我想问,在做了一系列选择(年份和面积)并计算了数据集的 tmean 之后,我如何使用 matplotlib 绘制它?这些是我执行的命令,我需要对不同的子区域重复这些命令并将图平铺在一起,并且使用 matplotlib 这样做会感觉更舒服: ds1.select(variables = "pre") ds1.select(years = range( 1981, 2018)) ds1.crop(lon = [20, 27], lat = [-35, -33.5]) ds1.spatial_mean() ds1.tmean("月")
  • 有一个自动绘图功能。只需执行ds.plot()。如果你想使用 matplotlib,你可以使用 ds_xr = ds1.to_xarray() 导出到 xarray
  • 谢谢!我确实使用了 ds.plot() 但它并不是我想要的。我会尝试导出到 xarray。
猜你喜欢
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多