Aggregation Function

aggregate(downscale_output, polygon_data, func = "mean")

Arguments

downscale_output

Raster file output from downscale_function.R. Read file in this way if stored locally: downscale_output <- readRDS("xxx.rds")

polygon_data

A spatial polygon object (e.g., an `sf` object) that defines the spatial units for aggregation. This data should be in a coordinate reference system compatible with the raster data (e.g., "EPSG:4326").

func

A character string specifying the aggregation function to use (e.g., 'mean', 'sum').

Value

It returns the `polygon_data` with added columns for mean and sum values of the aggregated raster data for each ensemble member.

Details

This function will aggregate previously downscaled carbon flux amount to a spatial unit of choice

Author

Harunobu Ishii

Examples

       if (FALSE) { # \dontrun{
       # Download a shapefile of U.S. (polygon data)
       url <- "https://www2.census.gov/geo/tiger/GENZ2020/shp/cb_2020_us_state_20m.zip"
       download.file(url, destfile = "polygon/us_states.zip")
       
       # Unzip the downloaded file and save locally
       unzip("polygon/us_states.zip", exdir = "polygon/us_states")
       us_states <- st_read("polygon/us_states/cb_2020_us_state_20m.shp")
       saveRDS(us_states, "polygon/us_states.rds")
       
       # Load the saved polygon data with Massachusetts as an example
       us_states <- readRDS("polygon/us_states.rds")
       state <- "MA"
       polygon_data <- st_transform(us_states[us_states$STUSPS == state, ], crs = "EPSG:4326")
     
       # Load the downscaled raster output
       downscale_output <- readRDS("path/to/downscale_output.rds")
       
       # Slot in as argument to the aggregate function
       result <- aggregate(downscale_output, polygon_data)
       print(result)
       } # }