Chess baseline: reproject variables to UK grid


Projecting chess variables (gdd, max_tmax, min_tmin, 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(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(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", "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")
year_from <- 1991; year_to <- 2011;
chess_meta <- readr::read_csv(file.path(path_cleaned, "chess-metadata.csv")) %>%
    subset(from_year == year_from & to_year == year_to & variable %in% varnames)
#> Parsed with column specification:
#> cols(
#>   file = col_character(),
#>   rcp = col_logical(),
#>   variable = col_character(),
#>   summary = col_character(),
#>   temp_resolution = col_character(),
#>   from = col_double(),
#>   to = col_double(),
#>   from_year = col_double(),
#>   from_month = col_character(),
#>   to_year = col_double(),
#>   to_month = col_double(),
#>   description = col_character()
#> )

Obtain files to read and to write

sm_regex <- paste0("^chess-met_sm.+", year_from, ".+", year_to, ".+")
path_read <- c(file.path(path_cleaned, chess_meta$file),
                list.files(path_processed, sm_regex, full = TRUE))
path_write <- gsub("^.+met_(\\w+)_gb_1km", "uk_1km_chess_\\1", 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

varnames <- gsub("^uk_1km_chess_(\\w+)_20yr.+$", "\\1", basename(path_write))
chess_1km <- lapply(path_write, function(x) read_stars(x, proxy = TRUE))

for (i in seq_along(varnames)) {
    plot_stars(chess_1km[[i]], uk, varnames[i], pal_rev = TRUE)
}

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#> 100.087   2.790 103.229