From e13e4a575d4eed205a319f0cad180d570e22c1e0 Mon Sep 17 00:00:00 2001 From: Andrea Manica Date: Thu, 21 Dec 2023 15:28:38 +0000 Subject: [PATCH] Get resolution of a dataset --- NAMESPACE | 1 + R/get_res.R | 30 ++++++++++++++++++++++++++++++ man/get_resolution.Rd | 23 +++++++++++++++++++++++ vignettes/a0_pastclim_overview.Rmd | 7 +++++++ 4 files changed, 61 insertions(+) create mode 100644 R/get_res.R create mode 100644 man/get_resolution.Rd diff --git a/NAMESPACE b/NAMESPACE index d3d29b08..adfbd473 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,7 @@ export(get_downloaded_datasets) export(get_ice_mask) export(get_land_mask) export(get_mis_time_steps) +export(get_resolution) export(get_time_bp_steps) export(get_time_ce_steps) export(get_time_steps) diff --git a/R/get_res.R b/R/get_res.R new file mode 100644 index 00000000..6b05ea49 --- /dev/null +++ b/R/get_res.R @@ -0,0 +1,30 @@ +#' Get resolution of a given dataset +#' +#' Get the resolution of a given dataset. +#' +#' @param dataset string defining dataset to be downloaded (a list of possible +#' values can be obtained with [list_available_datasets()]). If set to +#' "custom", then a single nc file is used from "path_to_nc" +#' @param path_to_nc the path to the custom nc file containing the palaeoclimate +#' reconstructions. All the variables of interest need to be included in +#' this file. +#' @returns a vector of resolution in the x and y axes +#' +#' @export + +get_resolution <- function(dataset, path_to_nc = NULL) { + check_dataset_path(dataset = dataset, path_to_nc = path_to_nc) + + if (is.null(path_to_nc)) { + possible_files <- dataset_list_included$file_name[dataset_list_included$dataset == dataset] + possible_files <- possible_files[possible_files %in% list.files(get_data_path())] + # check that at least one file is available + if (length(possible_files) == 0) { + stop("no variable has been downloaded for this dataset yet") + } + path_to_nc <- file.path(get_data_path(), possible_files[1]) + } + + climate_nc <- terra::rast(path_to_nc, subds = 1) + return(terra::res(climate_nc)) +} diff --git a/man/get_resolution.Rd b/man/get_resolution.Rd new file mode 100644 index 00000000..00026703 --- /dev/null +++ b/man/get_resolution.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_res.R +\name{get_resolution} +\alias{get_resolution} +\title{Get resolution of a given dataset} +\usage{ +get_resolution(dataset, path_to_nc = NULL) +} +\arguments{ +\item{dataset}{string defining dataset to be downloaded (a list of possible +values can be obtained with \code{\link[=list_available_datasets]{list_available_datasets()}}). If set to +"custom", then a single nc file is used from "path_to_nc"} + +\item{path_to_nc}{the path to the custom nc file containing the palaeoclimate +reconstructions. All the variables of interest need to be included in +this file.} +} +\value{ +a vector of resolution in the x and y axes +} +\description{ +Get the resolution of a given dataset. +} diff --git a/vignettes/a0_pastclim_overview.Rmd b/vignettes/a0_pastclim_overview.Rmd index 1e7453f9..b8cb8247 100644 --- a/vignettes/a0_pastclim_overview.Rmd +++ b/vignettes/a0_pastclim_overview.Rmd @@ -161,6 +161,13 @@ and the available time steps can be obtained with: get_time_bp_steps(dataset = "Example") ``` +We can also query the resolution of this dataset: +```{r} +get_resolution(dataset = "Example") +``` + +so, the *Example" dataset only has a resolution of 1x1 degree. + For *Beyer2020* and *Krapp2021*, you can get a list of available variables for each dataset with: