execute_intersection: Execute Intersection

View source: R/execute_intersection.R

execute_intersectionR Documentation

Execute Intersection

Description

Traverses many time steps of data applying intersection weights for each time step.

Usage

execute_intersection(
  nc_file,
  variable_name,
  intersection_weights,
  cell_geometry,
  x_var,
  y_var,
  t_var,
  start_datetime = NULL,
  end_datetime = NULL,
  status = FALSE,
  writer_fun = NULL,
  out_file = NULL,
  out_var_meta = NULL
)

Arguments

nc_file

character path or OPeNDAP URL to NetCDF source

variable_name

character name of variable in NetCDF source to access

intersection_weights

data.frame as output by calculate_area_intersection_weights

cell_geometry

sf data.frame with X_ind and Y_ind columns as output by create_cell_geometry

x_var

character variable with X axis data

y_var

character variable with Y axis data

t_var

character variable with T axis data

start_datetime

character or POSIX character format is strptime default e.g. "2010-10-10 10:10:10"

end_datetime

character or POSIX

status

boolean print status if TRUE

writer_fun

a function that implements the writer API described in details

out_file

character path for output file to be used with writer_fun

out_var_meta

list passed to writer_fun with ids

Details

The writer_fun input can be a function passed in that supports a basic API. The function should the following inputs:

  1. file_handle: character or otherwise see description

  2. step: the step to write. Special values are 0 and -1 for initialization and closing respectively

  3. size: a length 2 integer vector giving rows and cols of the output e.g. c(100, 10) for 100 time steps and 10 columns

  4. var_meta: a named list containing variable name, long_name, units, and ids. e.g. list(name = "prcp", long_name = "precpiptation in mm", units = "mm", ids = c("1", "2", "3")) the first three values of this list are taken from the out_var_meta input.

  5. timestep_data: a one row data.frame with time in the first column and output data in 2:ncol(timestep_data)

The function will be called once with step = 0. This should be used to initialize the output.

Initialization should return a file path or open file handle, such as an RNetCDF object.

The function will be called once for each timestep with step from 1:timesteps Each row should be written as it is provided. This should return the file path or open file handle.

The function will be called once with step = -1 to indicate the file can be closed. The final return value is discarded and the out_file input is returned.

Examples

library(ncdfgeom)

nc_file <- system.file("extdata/metdata.nc", package = "intersectr")
nc <- rnz::open_nz(nc_file)

variable_name <- "precipitation_amount"
(cv <- ncmeta::nc_coord_var(nc, variable_name))

geom <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))[20, ]
geom <- sf::st_transform(geom, "+init=epsg:5070")

cell_geometry <-
  create_cell_geometry(X_coords = rnz::get_var(nc, cv$X, unpack = TRUE),
                       Y_coords = rnz::get_var(nc, cv$Y, unpack = TRUE),
                       prj = ncmeta::nc_gm_to_prj(ncmeta::nc_grid_mapping_atts(nc)),
                       geom = geom,
                       buffer_dist = 2000)

plot(sf::st_transform(sf::st_geometry(cell_geometry), "+init=epsg:5070"))
plot(sf::st_geometry(geom), add = TRUE)

area_weights <- calculate_area_intersection_weights(
  sf::st_sf(dplyr::select(cell_geometry, grid_ids), agr = "constant"),
  sf::st_sf(dplyr::select(geom, NAME), agr = "constant"))

execute_intersection(nc_file, variable_name, area_weights,
                     cell_geometry, cv$X, cv$Y, cv$T)



USGS-R/intersectr documentation built on June 9, 2025, 4:06 p.m.