UKCP18 SPEED bias-corrected land suitability scenarios: maskout interact models


Load packages, read data and source custom scripts

rm(list = ls())
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(purrr)

path_proj <- day2day::git_path()
path_data <- file.path(path_proj, "data")
path_cleaned <- file.path(path_data, "cleaned")
path_processed <- file.path(path_data, "processed")
path_modelled <- file.path(path_data, "modelled")
path_uk_1km_suitability <- file.path(path_modelled, "uk-1km-suitability")

source(file.path(path_proj, "src", "54-vars-to-raster.R"))
source(file.path(path_proj, "src", "61-vis-stars.R"))
source(file.path(path_proj, "src", "63-tidy-names.R"))
source(file.path(path_proj, "src", "64-vis-suitability.R"))

uk_bbox_1km <- read_stars(file.path(path_processed, "uk_bbox_1km.tif"))
uk <- st_read(file.path(path_cleaned, "uk_simple.gpkg"), "union")
#> Reading layer `union' from data source `/home/rstudio/Cloud/lancs/projects/land-suitability/data/cleaned/uk_simple.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -116.1923 ymin: 7054.099 xmax: 655644.8 ymax: 1218625
#> projected CRS:  OSGB 1936 / British National Grid
land_cover <- fst::read_fst(file.path(path_processed, "uk_1km_dataframe_train_full.fst"))

prefix <- "binom_maskout_interact"
speed_tag <- "bias_corrected_01"
regex <- paste("^uk_1km_suitability", prefix, "ukcp18-speed_(rcp\\d+)", speed_tag,
               "20yr-mean-annual_.+$", sep = "_")
suitability_files <- list.files(path_uk_1km_suitability, regex, full.names = TRUE)

Custom function

# Custom function
custom_plot <- function (data_plot, rcp, land_name, nbreaks = 11, ...) {
    cat("\n\n")
    cat(paste0("### ", toupper(rcp), "\n\n"))
    suitability_check(data_plot, uk_bbox_1km, land_name, nbreaks, uk, shortname = TRUE, ...)
}

Visualize UKCP18 SPEED scenarios

rcps <- unique(sub(regex, "\\1", basename(suitability_files)))
land_names <- get_land_classes(names(fst::read_fst(suitability_files[1])), prefix)

for (land_name in land_names) {
    cat("\n\n")
    cat(paste0("## ", tidy_make_classes(land_name, 2), "\n\n"))
    for (rcp in rcps) {
        files <- suitability_files[grep(rcp, basename(suitability_files))]
        data_plot <- left_join(land_cover, read_sce(files, prefix), by = "id")
        data_plot <- suitability_prepare(data_plot, paste0("^", prefix,"_(.+)$"))
        custom_plot(data_plot, rcp, land_name)
    }
}

(1) Arable

RCP26

RCP45

RCP60

RCP85

(2) Wetland

RCP26

RCP45

RCP60

RCP85

(3) Improved grassland

RCP26

RCP45

RCP60

RCP85

(4) Forest

RCP26

RCP45

RCP60

RCP85

(5) Semi natural grassland

RCP26

RCP45

RCP60

RCP85

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#> 360.578  18.907 379.561