UKCP18 SPEED bias-corrected scenarios: reproject variables to UK grid


Projecting speed variables (gdd, max_tas, max_tasmax, maxmonth_tas, min_tas, min_tasmin, smd and sms) to UK grid at 1km resolution.

Load packages, read data and source custom scripts

rm(list = ls())
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_raw <- file.path(path_data, "raw")
path_cleaned <- file.path(path_data, "cleaned")
path_processed <- file.path(path_data, "processed")

source(file.path(path_proj, "src", "34-read-sce-var.R"))
source(file.path(path_proj, "src", "36-proj-uk-1km.R"))
source(file.path(path_proj, "src", "61-vis-stars.R"))

uk <- st_read(file.path(path_cleaned, "uk_simple.gpkg"), "union")
#> Reading layer `union' from data source `/home/rstudio/Documents/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
uk_bbox_1km <- read_stars(file.path(path_processed, "uk_bbox_1km.tif"))

varnames <- c("gdd", "max_tas", "max_tasmax", "maxmonth_tas", "min_tas", "min_tasmin",
              "smd", "sms")
speed_meta <- readr::read_csv(file.path(path_cleaned, "ukcp18-speed_bias_corrected_01_metadata.csv")) %>%
    subset(variable %in% varnames)
#> Parsed with column specification:
#> cols(
#>   file = col_character(),
#>   rcp = col_character(),
#>   variable = col_character(),
#>   summary = col_character(),
#>   temp_resolution = col_character(),
#>   from = col_double(),
#>   to = col_double(),
#>   from_year = col_double(),
#>   from_month = col_double(),
#>   to_year = col_double(),
#>   to_month = col_double(),
#>   description = col_character()
#> )

Obtain files to read and to write

sm_regex <- paste0("^ukcp18-speed_rcp\\d+_bias_corrected_01_sm")
path_read <- c(file.path(path_cleaned, speed_meta$file),
               list.files(path_processed, sm_regex, full = TRUE))
path_write <- gsub("^(ukcp18.+)_(uk_1km)_(.+)$", "\\2_\\1_\\3", basename(path_read)) %>%
    file.path(path_processed, .)

Reproject speed-derived variables into UK 1km grid

walk2(path_read, path_write, ~ proj_uk_1km(.x, .y, uk_bbox_1km))

Visualize variables

rcps <- unique(speed_meta$rcp)
vars <- c(unique(speed_meta$variable), "smd", "sms")

for (rcp in rcps) {
    for (var in vars) {
        cat("\n\n")
        cat(paste0("### ", toupper(rcp), "\n\n"))
        cat(paste0("#### ", toupper(var), "\n\n"))
        pattern <- paste("^uk_1km_ukcp18-speed", rcp, "bias_corrected_01", var, sep = "_")
        x <- read_sce_var(path_processed, pattern)
        if (!is.null(x)) {
            plot_stars(x, uk, paste0(var, ":"), pal_rev = TRUE)
        }
    }
}

RCP26

GDD

RCP26

MAX_TAS

RCP26

MAXMONTH_TAS

RCP26

MIN_TAS

RCP26

SMD

RCP26

SMS

RCP45

GDD

RCP45

MAX_TAS

RCP45

MAXMONTH_TAS

RCP45

MIN_TAS

RCP45

SMD

RCP45

SMS

RCP60

GDD

RCP60

MAX_TAS

RCP60

MAXMONTH_TAS

RCP60

MIN_TAS

RCP60

SMD

RCP60

SMS

RCP85

GDD

RCP85

MAX_TAS

RCP85

MAXMONTH_TAS

RCP85

MIN_TAS

RCP85

SMD

RCP85

SMS

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>      user    system   elapsed 
#>   553.368    19.499 35925.381