具有填充值/缺失值的 NetCDF 变量

NetCDF variables that have the fill value/ missing value

我在 NetCDF 文件中有一个变量,如果该变量为空,则该变量具有默认值。当变量缺少值时,如何删除此值或将其更改为 0?

听起来问题在于,当变量被填充到 NetCDF 文件中时,它被设置为为缺失的值插入一些默认值。现在,我假设您需要删除这些默认值 文件已写入并且您正在处理数据之后。

因此(取决于您访问变量的方式)我会将变量从 NetCDF 文件中拉出并将其分配给 python 变量。这是第一个想到的方法。

使用 for 循环遍历并将默认值替换为 0

variable=NetCDF_variable  #Assume default value is 1e10
cleaned_list=[]
for i in variable:
    if i == 1e10:
        cleaned_list.append(0)  #0 or whatever you want to fill here
    else: 
        cleaned_list.append(i)

如果默认值为浮点数,您可能需要查看 numpy.isclose if the above code isn't working. You might also be interested in masking 您的数据,以防插入 0 导致您所做的任何计算失败。

编辑:用户 N1B4 提供了一种更简洁有效的方法来完成与上述完全相同的事情。

variable[variable == 1e10] = 0