Commit 975ae900 authored by Paulo Medeiros's avatar Paulo Medeiros
Browse files

Add thin_obs method to DomainGrid obj

parent 5380ab46
......@@ -7,6 +7,7 @@ import attr
import numpy as np
import pyproj
from .metrics import haversine_distance
from .plots import DEF_FIGSHOW_CONFIG, get_domain_fig
logger = logging.getLogger(__name__)
......@@ -295,6 +296,47 @@ class DomainGrid(Grid2D):
"""Return tuple of (lon, lat) coords of the extension zone corners."""
return tuple(self.proj.xy2lonlat(*xy) for xy in self.ezone_corners)
def thin_obs(self, df, method="nearest"):
"""Return df with only one entry per grid point."""
if method not in ["nearest", "first"]:
raise NotImplementedError(
"'method' must be one of: 'nearest', 'first'"
if len(df.index) == 0:
return df
# Add grid (i, j) info
icol, jcol = self.lonlat2grid(
df["lon"].to_numpy(), df["lat"].to_numpy()
df["i"] = icol
df["j"] = jcol
if method == "nearest":
# Get (lon, lat) for grid points
g2lon, g2lat = self.ij2lonlat_map()
# Sort data by distance to nearest grid point
def _dist(lon, lat, i, j):
p0 = np.array([lon, lat])
p1 = np.array([g2lon[i, j], g2lat[i, j]])
return haversine_distance(p0, p1)
df = df.loc[
df.apply(lambda x: _dist(x.lon,, x.i, x.j), axis=1)
# Remove all but one (the first) entry at each grid (i, j)
df = df.groupby(["i", "j"], as_index=False, sort=False).first()
# Remove no longer needed (i, j) info
df = df.drop(["i", "j"], axis=1)
return df
class Domain:
"""Model domain geometry and grid.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment