<p>If I were you, I would exclude the NaN values and then perform gridding on the resulting irregularly spaced data.</p>

<p>There are already the tools to perform this in Python and using the library SciPy</p>

<p><a href="https://scipy-cookbook.readthedocs.io/items/Matplotlib_Gridding_irregularly_spaced_data.html" rel="nofollow noreferrer">https://scipy-cookbook.readthedocs.io/items/Matplotlib_Gridding_irregularly_spaced_data.html</a></p>

<p>But please document exactly what step you are doing and why. Excluding data is always ... interpreting the data.</p>

CC BY-SA 4.0
2020-04-02T09:51:41.897Z
2020-04-02T09:51:41.897Z
EarlGrey
<p>I was able to do this by the following functions</p>

<pre><code>def fill_mismatch(ds_grid_spec, ds_data):
    ds_data_o = ds_data.copy()
    count = 0.0
    for i in range(ds_grid_spec.shape[0]):
        for j in range(ds_grid_spec.shape[1]):
            if ds_grid_spec[i,j].values &gt; 0.0 and (not ds_data[i,j] &gt; 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 &lt; 0:
            bl_i = 0
        # Bottom left j
        if bl_j &lt; 0:
            bl_j = 0
        # Top right i
        if tr_i &gt; ds.lat.shape[0]-1:
            tr_i = ds.lat.shape[0]-1
        # Top right j
        if tr_j &gt; 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]&gt;0.0:
                    ds[lat_i, lon_j] = ds[lats_i_idx, lons_j_idx]
                    return
        cycle = cycle + 1
</code></pre>

<p>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</p>

<p>The complete notebook can be found at <a href="https://github.com/manmeet3591/python_class/blob/master/xarray_tutorial/grid_soiltype.ipynb" rel="nofollow noreferrer">https://github.com/manmeet3591/python_class/blob/master/xarray_tutorial/grid_soiltype.ipynb</a></p>

CC BY-SA 4.0
2020-04-05T06:51:03.663Z
2020-04-05T06:51:03.663Z
Manmeet Singh
<p>I have a 2d xarray dataset and I am having several nan values in the dataset. These nan values need to be filled with the nearest non-nan values in the 2d array. How do I do this? </p>

<p>I am trying to make my data consistent with the grid_spec file that the earth system model reads. And there are inconsistencies on what the land points are in the forcing data and grid_spec file leading to the blowing up of model.</p>

CC BY-SA 4.0
2020-04-02T00:31:26.647Z
Can you post the code you have so far?
Craeft
CC BY-SA 4.0
2020-04-03T13:19:46.100Z
How do you define distance?
Spencer
CC BY-SA 4.0
2020-04-06T17:16:27.967Z
@Spencer I have solved this, and the distance is defined as going around the missing value point round and round, For eg if the missing grid point is the middle 1, then we need to search 6,7,8,2,6,5,4,0 in cycle 1, still if we are not able to find the non-nan we will move to the second cycle

12345
56789
90123
34567
89012
Manmeet Singh
<meteorology><atmosphere-modelling><atmospheric-circulation><python>
How to search for the nearest non nan value in 2d xarray dataset
Manmeet Singh
# How to search for the nearest non nan value in 2d xarray dataset

(Asked by: username_0 on 2020-04-01)

I have a 2d xarray dataset and I am having several nan values in the dataset. These nan values need to be filled with the nearest non-nan values in the 2d array. How do I do this?

I am trying to make my data consistent with the grid\_spec file that the earth system model reads. And there are inconsistencies on what the land points are in the forcing data and grid\_spec file leading to the blowing up of model.

**Tags:** meteorology, atmosphere-modelling, atmospheric-circulation, python

> **Comments:**
> **username_1**: Can you post the code you have so far?  
> **username_2**: How do you define distance?  
> **username_0**: @username_2 I have solved this, and the distance is defined as going around the missing value point round and round, For eg if the missing grid point is the middle 1, then we need to search 6,7,8,2,6,5,4,0 in cycle 1, still if we are not able to find the non-nan we will move to the second cycle  12345 56789 90123 34567 89012  

---

## 2 answers

### Answer by username_3 on 2020-04-02. Score: 2

If I were you, I would exclude the NaN values and then perform gridding on the resulting irregularly spaced data.

There are already the tools to perform this in Python and using the library SciPy

<https://scipy-cookbook.readthedocs.io/items/Matplotlib_Gridding_irregularly_spaced_data.html>

But please document exactly what step you are doing and why. Excluding data is always ... interpreting the data.


---

### Answer by username_0 on 2020-04-05. Score: -1

I was able to do this by the following functions

```
def fill_mismatch(ds_grid_spec, ds_data):
    ds_data_o = ds_data.copy()
    count = 0.0
    for i in range(ds_grid_spec.shape[0]):
        for j in range(ds_grid_spec.shape[1]):
            if 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>


---

