grid_df = pd.read_csv(DATA + "jura_grid.csv", index_col=0)
g = grid_df[["Xloc", "Yloc"]].to_numpy(float)
i50 = np.rint((g[:, 0] - g[:, 0].min()) / 0.05).astype(int)
j50 = np.rint((g[:, 1] - g[:, 1].min()) / 0.05).astype(int)
grid = g[(i50 % 2 == 0) & (j50 % 2 == 0)]
gi = np.rint((grid[:, 0] - grid[:, 0].min()) / 0.1).astype(int)
gj = np.rint((grid[:, 1] - grid[:, 1].min()) / 0.1).astype(int)
EXT = [grid[:, 0].min() - 0.05, grid[:, 0].max() + 0.05, grid[:, 1].min() - 0.05, grid[:, 1].max() + 0.05]
t0 = time.perf_counter()
gmap = bayesian_kriging(xy, z, grid, model="spherical", range_grid=RANGE_GRID, nugget_grid=NUGGET_GRID)
print(f"{len(grid)} nodes in {time.perf_counter() - t0:.1f} s")
fig, axes = plt.subplots(1, 2, figsize=(15, 6.5))
for ax, (v, title, cm) in zip(axes, [(np.exp(gmap.q50), "Median Cd (mg/kg)", "YlOrRd"),
(np.exp(gmap.q95) - np.exp(gmap.q05), "Width of the 90% interval (mg/kg)", "magma")]):
img = np.full((gj.max() + 1, gi.max() + 1), np.nan)
img[gj, gi] = v
im = ax.imshow(img, origin="lower", extent=EXT, cmap=cm, interpolation="nearest")
ax.scatter(xy[:, 0], xy[:, 1], s=4, facecolors="none", edgecolors="k", linewidths=0.4)
ax.set_title(title)
ax.set_aspect("equal")
plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
plt.tight_layout()
plt.show()