Available water storage (AWS): convert to tif


Converting available water storage to tif with associated metadata.

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
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")

source(file.path(path_proj, "src", "22-nc-to-tif.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
file_aws <- "ukcp18_uk_1km_soilparams_aws_hwsd_bc.nc"

Create metadata and save as tif

nc_to_tif(file_aws, "aws", file.path(path_raw, "water-storage"), path_cleaned)
#> Error in UseMethod("GPFN") : 
#>   no applicable method for 'GPFN' applied to an object of class "list"

Visualize available water storage (AWS) by depth

Units of AWS is kilopascal (KPa).

aws <- read_stars(file.path(path_cleaned, sub("\\.nc", "\\.tif", file_aws)))
aws_depth <- readRDS(file.path(path_cleaned, sub("\\.nc", "\\.RDS", file_aws)))
aws <- st_set_dimensions(aws, "band", value = aws_depth$labels)
plot_awc(aws, uk, main = "Depth:")

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#>  18.355   0.862  19.175