如何寻找最近的非nan值在2 d xarray数据集-地球科学栈交流吗江南电子竞技平台江南体育网页版 最近30从www.hoelymoley.com 2023 - 07 - 09 - t20:13:57z //www.hoelymoley.com/feeds/question/19542 https://creativecommons.org/licenses/by-sa/4.0/rdf //www.hoelymoley.com/q/19542 1 如何寻找最近的非nan值在2 d xarray数据集 Manmeet辛格 //www.hoelymoley.com/users/18076 2020 - 04 - 01 - t19:41:33z 2020 - 04 - 05 - t06:51:03z < p >我有一个2 d xarray数据集,我有几个nan值的数据集。这些nan值需要充满最近的non-nan值的二维数组。我怎么做呢?< / p > < p >我想让我的数据符合grid_spec文件读取的地球系统模型。的土地上有矛盾点是在迫使数据和grid_spec文件导致的爆炸模型。< / p > //www.hoelymoley.com/questions/19542/how-to-search-for-the-nearest-non-nan-value-in-2d-xarray-dataset/19544 # 19544 2 由EarlGrey回答如何寻找最近的非nan值2 d xarray数据集 EarlGrey //www.hoelymoley.com/users/5887 2020 - 04 - 02 - t09:51:41z 2020 - 04 - 02 - t09:51:41z < p >如果我是你,我就会排除NaN值,然后对结果执行网格间隔不规则数据。< / p > < p >在Python中已经有这个工具来执行和使用图书馆SciPy < / p > < p > < a href = " https://scipy-cookbook.readthedocs.io/items/Matplotlib_Gridding_irregularly_spaced_data。html nofollow noreferrer“rel = > https://scipy-cookbook.readthedocs.io/items/Matplotlib_Gridding_irregularly_spaced_data。html < / > < / p > < p >但请文档一步你做什么和为什么。不包括数据总是……解释数据。< / p > //www.hoelymoley.com/questions/19542/how-to-search-for-the-nearest-non-nan-value-in-2d-xarray-dataset/19559 # 19559 1 辛格Manmeet回答的如何寻找最近的非nan值2 d xarray数据集 Manmeet辛格 //www.hoelymoley.com/users/18076 2020 - 04 - 05 - t06:51:03z 2020 - 04 - 05 - t06:51:03z < p >我能够通过以下功能< / p > < pre > <代码> def fill_mismatch (ds_grid_spec ds_data): ds_data_o = ds_data.copy()数= 0.0我的范围(ds_grid_spec.shape [0]): j的范围(ds_grid_spec.shape[1]):如果ds_grid_spec (I, j)。values > 0.0 and (not ds_data[i,j] > 0.0): count = count + 1.0 fill_miss2d(ds_data_o, i, j) if ds_grid_spec[i,j].values == 0.0 and (not ds_data[i,j].values == 0.0): count = count + 1.0 ds_data_o[i,j] = 0.0 return ds_data_o def fill_miss2d(ds, lat_i, lon_j): # fist cycle cycle = 1 while True: bl_i, bl_j = lat_i - cycle, lon_j - cycle tr_i, tr_j = lat_i + cycle, lon_j + cycle # Bottom left i if bl_i < 0: bl_i = 0 # Bottom left j if bl_j < 0: bl_j = 0 # Top right i if tr_i > ds.lat.shape[0]-1: tr_i = ds.lat.shape[0]-1 # Top right j if tr_j > ds.lon.shape[0]-1: tr_j = ds.lon.shape[0]-1 lats_i = np.arange(bl_i,tr_i) lons_j = np.arange(bl_j,tr_j) for lats_i_idx in lats_i: for lons_j_idx in lons_j: if ds[lats_i_idx, lons_j_idx]>0.0: ds[lat_i, lon_j] = ds[lats_i_idx, lons_j_idx] return cycle = cycle + 1

The grid_spec (which is the most commonly used grid exchange file for Modular Ocean Model MOM) had a variable AREA_LND and the file soiltype.nc had a variable soiltype. Both these variables had 0.0 at the grid points classified as ocean. Hence we dont need to use fill_miss2d where a mismatch occurs and ocean is encountered in grid_spec, but land is there in soiltype. Rather we directly fill the soiltype grid point with 0.0

The complete notebook can be found at https://github.com/manmeet3591/python_class/blob/master/xarray_tutorial/grid_soiltype.ipynb

Baidu
map