Extracts climate and/or elevation data for generated over an area or at fixed point/s.

ce_extract(
  path = NULL,
  location = NULL,
  location_g = NULL,
  c_source = "WorldClim",
  var = "all"
)

Arguments

path

Character. Path to folder containing the climate and elevation raster data. The folders should be named prec, tmax, tavg and tmin and elev. Within each climate folder there should be 12 raster files (e.g. "prec01.tif") corresponding to the monthly climate data. There should be only one raster file within the elev subfolder named "srtm.tif".

location

A "sp", "sf" polygon or point object. See sf::st_polygon to make polygons and sf::st_as_sf to make point objects.

location_g

Character. Informs how the zonal statistics are exported. Must correspond to a column of the "location" argument. If NULL, the zonal statistics are calculated for all features of "location" and a warning issued.

c_source

Character (e.g., "CHELSA or WorldClim"). Indicating the climate data source.

var

Character. If supplied will download a subset of the climate data. Must be one of "all" (default), "prec", "tmax", "tmin" or "tmean" to download the corresponding climate data.

Value

Returns a list storing matrices containing the mean and standard deviation of the climate and/or elevation data. Each column represents a month, each row represents a feature of the location

sp, sf polygon or point object. Values returned are either degrees Celsius for (tmax, tavg, tmin) or mm (prec).

See also

The downloading (ce_download()), and the plotting (plot_h() & plot_wl()) functions.

Author

James L. Tsakalos and Martin R. Smith

Examples

# Create some random data

# Create temporary file
temp_path <- tempfile()
on.exit(unlink(file.path(temp_path)), add = TRUE)

# Create the required subdirectories
dir.create(file.path(temp_path, "/elev"), recursive = TRUE)
dir.create(file.path(temp_path, "/prec"), recursive = TRUE)
dir.create(file.path(temp_path, "/tmax"), recursive = TRUE)
dir.create(file.path(temp_path, "/tavg"), recursive = TRUE)
dir.create(file.path(temp_path, "/tmin"), recursive = TRUE)

# Create an empty raster
r <- terra::rast(ncol = 10, nrow = 10)

# Modify the base Raster
#* Elevation 100m ####
terra::values(r) <- 1:100
terra::writeRaster(r, paste0(temp_path, "/elev/srtm.tif"))

# create and save precipitation and temperature rasters ####
x <- c(5, 10, 15, 20, 25, 34, 25, 20, 15, 10, 5, 0) * 8
for (i in sprintf("%02d", 1:12)) {
terra::writeRaster(r, paste0(temp_path, paste0("/prec/prec_", i, ".tif")))
terra::writeRaster(r, paste0(temp_path, paste0("/tmax/tmax_", i, ".tif")))
terra::writeRaster(r, paste0(temp_path, paste0("/tmin/tmin_", i, ".tif")))
terra::writeRaster(r, paste0(temp_path, paste0("/tavg/tavg_", i, ".tif")))
}

# Create a polygon file from the raster
terra::values(r) <- 1:100
pol_py <- sf::st_as_sf(terra::as.polygons(r))
pol_py$grp <- c(rep("low", 25), rep("high", 75))

# Run the download function
ce_extract(
  path = temp_path,
  location = pol_py,
  location_g = "grp"
)
#> $tavg_m
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> high  63  63  63  63  63  63  63  63  63  63  63  63
#> low   13  13  13  13  13  13  13  13  13  13  13  13
#> 
#> $tavg_sd
#>            Jan       Feb       Mar       Apr       May       Jun       Jul
#> high 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102  7.211102  7.211102
#>            Aug       Sep       Oct       Nov       Dec
#> high 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102
#> 
#> $tmin_m
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> high  63  63  63  63  63  63  63  63  63  63  63  63
#> low   13  13  13  13  13  13  13  13  13  13  13  13
#> 
#> $abmt
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> high  26  26  26  26  26  26  26  26  26  26  26  26
#> low    1   1   1   1   1   1   1   1   1   1   1   1
#> 
#> $tmin_sd
#>            Jan       Feb       Mar       Apr       May       Jun       Jul
#> high 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102  7.211102  7.211102
#>            Aug       Sep       Oct       Nov       Dec
#> high 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102
#> 
#> $tmax_m
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> high  63  63  63  63  63  63  63  63  63  63  63  63
#> low   13  13  13  13  13  13  13  13  13  13  13  13
#> 
#> $tmax_sd
#>            Jan       Feb       Mar       Apr       May       Jun       Jul
#> high 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102  7.211102  7.211102
#>            Aug       Sep       Oct       Nov       Dec
#> high 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102
#> 
#> $prec_m
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> high  63  63  63  63  63  63  63  63  63  63  63  63
#> low   13  13  13  13  13  13  13  13  13  13  13  13
#> 
#> $prec_sd
#>            Jan       Feb       Mar       Apr       May       Jun       Jul
#> high 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102  7.211102  7.211102
#>            Aug       Sep       Oct       Nov       Dec
#> high 21.648710 21.648710 21.648710 21.648710 21.648710
#> low   7.211102  7.211102  7.211102  7.211102  7.211102
#> 
#> $elev
#>      mean     stdev
#> high   63 21.648710
#> low    13  7.211102
#> 
#> $lat
#>        lat
#> high -22.2
#> low   66.6
#> 
#> $Readme
#> [1] "Contains data sourced from WorldClim"
#>