< 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