How do you use nearest neighbor interpolation?

Hello, I have a question regarding the nearest neighbor interpolation using pygmt.
I want to interpolate magnetic data using the nearest neighbor interpolation and furthermore I wanted to have the interpolated data within the ‘‘black area’’ that I made by uploading a shapefile.

I’ve been trying to to make it work and so far I’ve got the following code but I am constantly running into some new problems, and I am not sure how I can make it work. Does someone have some smart ideas on how I could solve this problem?:slight_smile:

fig = pygmt.Figure()
region=[-21.287473, -21.210487,63.941199,63.971433]
grid = pygmt.datasets.load_earth_relief() 


fig.basemap(region=region, projection="M15c", frame=True)
fig.coast(land="lightgray", water="skyblue",shorelines=True)
magnetic_data = np.loadtxt('Official_all_data_snyrt.txt',skiprows=1)
pygmt.makecpt(cmap="magma", series=[magnetic_data[:,3].min(), 
magnetic_data[:,3].max()],reverse=True)

fig.plot(
    x=magnetic_data[:,1],
    y=magnetic_data[:,0],
    style="c0.15c",
    fill=magnetic_data[:,3],
    cmap=True,
)
gdf = gpd.read_file(filename="borderline_1.shp")
gdf1=gdf.dropna()
print(gdf1)
linestrings = [geom for geom in gdf1.geometry]


output = pygmt.nearneighbor(
    x=magnetic_data[:,1],
    y=magnetic_data[:,0],
    z=magnetic_data[:,3],
    spacing="5m",
    region= linestrings#[-21.252270,-21.248104,63.955552,63.956860]
    #search_radius="20m",
)

#fig.colorbar(position="JMR+o1.5c/0c+w3c/0.3c", frame=True)
fig.colorbar(frame="af+lTotal Magnetic Field (nT)")
#fig.show()

# Plot in PyGMT
linestrings = [geom for geom in gdf1.geometry]
for line in linestrings:
    x, y =line.coords.xy
    fig.plot(x=x, y=y, pen="thin")
fig.show()

With the current code I get the following Errors:


AssertionError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
724 # If so, use np.datetime_as_string instead.
→ 725 assert " " not in str(item)
726 except AssertionError:

AssertionError:

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9980\2569536425.py in
22
23
—> 24 output = pygmt.nearneighbor(
25 x=magnetic_data[:,1],
26 y=magnetic_data[:,0],

~\anaconda3\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
592 warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
593
→ 594 return module_func(*args, **kwargs)
595
596 new_module.aliases = aliases

~\anaconda3\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
728 # string format like YYYY-MM-DDThh:mm:ss.ffffff
729 value[index] = np.datetime_as_string(
→ 730 np.asarray(item, dtype=np.datetime64)
731 )
732 kwargs[arg] = separators[fmt].join(f"{item}" for item in value)

ValueError: Could not convert object to NumPy datetime

Why are you using nearneighbor? That’s not a fantastic interpolater.
And maybe simpler if you try the classic command line approach.

1 Like

It was just the first thing that came to mind to be honest. Could you elaborate a bit more on what you mean by ‘‘the classic command line approach’’ ? :slight_smile:

I meant, not to use pygmt. Probably the error message will be easier to interpret.

I see, allright I’ll do that
Thank you