微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用python比较两个netcdf文件中的缺失数据?

如何解决如何使用python比较两个netcdf文件中的缺失数据?

我有两个 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 文件,希望得到任何指导。

解决方法

您可以使用我的 nctoolkit 包 (async_write) 用几行代码解决这个问题。

要创建第一个文件,请尝试以下操作。

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)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。