簡體   English   中英

如何使用 python 比較兩個 netcdf 文件中缺失的數據?

[英]How to compare missing data in two netcdf files using python?

我有兩個 netcdf 文件:file1(cru pre)和 file2(chirps precip)。 這兩個文件已重新映射到同一個網格,包含月度數據並涵蓋相同的時間段(1981-2017)。

如何使用 python 循環遍歷文件並實現以下邏輯:對於 file1 中的每個數據點,如果值不丟失且對應的 file2 值不丟失,則保留 file1 值,否則將 file2 值視為丟失

我基本上想得到一個 output_file1 ,它只包含 file1 沒有丟失的點的 file1 數據和一個 output_file2 ,它只包含 file2 沒有丟失的點的 file2 數據。 在這兩個文件中,我都將 missing_values 設置為 999。

我是 python 和使用 NetCDF 文件的新手,希望得到任何指導。

您可以使用我的 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)

這將在每個時間步進行掩蔽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM