Skip to contents

Snaps each lon/lat to the nearest cell of a fetched grid, so sites that fall in the same cell collapse onto one identifier. Useful for deduplicating downstream output: build one driver file per cell and point every site in that cell at it. At 45 km (d01) a few thousand California sites ride on at most a couple hundred cells.

Usage

cae_grid_cells(points, grid)

Arguments

points

data.frame with columns lon and lat in WGS84 decimal degrees. Any other columns (such as site_id) are carried through.

grid

a stars object from cae_fetch() (any single variable, any timestep). Only its grid geometry and CRS are used.

Value

points with added columns: cell_i, cell_j (1-based grid indices), cell_id (a label like "x50_y58"), and cell_lon, cell_lat (the cell center in WGS84).

Details

Designed for the WRF (WUS-D3) grid, which uses projected x/y coordinates. Snapping picks the same cell that cae_fetch_point() would extract.

Examples

if (FALSE) { # \dontrun{
grid <- cae_fetch("t2", model = "CESM2", scenario = "ssp370",
                  start_time = "2050-07-01T00:00:00",
                  end_time   = "2050-07-01T00:00:00")
sites <- data.frame(site_id = c("davis", "west_sac", "fresno"),
                    lon = c(-121.74, -121.53, -119.77),
                    lat = c(38.54, 38.58, 36.75))
cells <- cae_grid_cells(sites, grid)
length(unique(cells$cell_id))   # davis and west_sac share a 45 km cell
} # }