Skip to content

Commit 61637ec

Browse files
Merge pull request #17 from NewGraphEnvironment/1-metadata-utilities
Add metadata and utility functions (#1, #2, #3)
2 parents 3ca80a4 + a320b59 commit 61637ec

14 files changed

Lines changed: 401 additions & 0 deletions

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(cd_cache_clear)
4+
export(cd_cache_info)
5+
export(cd_cache_path)
6+
export(cd_periods)
7+
export(cd_variables)

R/cd_cache.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#' Get cd cache directory path
2+
#'
3+
#' Returns the path to the cd cache directory. Creates it if
4+
#' it doesn't exist.
5+
#'
6+
#' @param cache_dir Character. Override the default cache location.
7+
#' If NULL, uses `rappdirs::user_cache_dir("cd")`.
8+
#'
9+
#' @return Character path to the cache directory.
10+
#'
11+
#' @examples
12+
#' cd_cache_path()
13+
#'
14+
#' @export
15+
cd_cache_path <- function(cache_dir = NULL) {
16+
path <- cache_dir %||% rappdirs::user_cache_dir("cd")
17+
if (!dir.exists(path)) {
18+
dir.create(path, recursive = TRUE, showWarnings = FALSE)
19+
}
20+
path
21+
}
22+
23+
#' Clear the cd cache
24+
#'
25+
#' Removes all cached files from the cd cache directory.
26+
#'
27+
#' @param cache_dir Character. Override the default cache location.
28+
#'
29+
#' @return Invisibly returns the number of files removed.
30+
#'
31+
#' @examples
32+
#' \dontrun{
33+
#' cd_cache_clear()
34+
#' }
35+
#'
36+
#' @export
37+
cd_cache_clear <- function(cache_dir = NULL) {
38+
path <- cd_cache_path(cache_dir)
39+
if (!dir.exists(path)) return(invisible(0L))
40+
files <- list.files(path, recursive = TRUE, full.names = TRUE)
41+
n <- length(files)
42+
if (n > 0) unlink(path, recursive = TRUE)
43+
invisible(n)
44+
}
45+
46+
#' Show cd cache info
47+
#'
48+
#' Reports the cache location and size.
49+
#'
50+
#' @param cache_dir Character. Override the default cache location.
51+
#'
52+
#' @return A list with `path`, `n_files`, and `size_mb`.
53+
#'
54+
#' @examples
55+
#' cd_cache_info()
56+
#'
57+
#' @export
58+
cd_cache_info <- function(cache_dir = NULL) {
59+
path <- cd_cache_path(cache_dir)
60+
files <- list.files(path, recursive = TRUE, full.names = TRUE)
61+
size <- if (length(files) > 0) sum(file.size(files), na.rm = TRUE) else 0
62+
list(
63+
path = path,
64+
n_files = length(files),
65+
size_mb = round(size / 1024^2, 2)
66+
)
67+
}

R/cd_periods.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#' Valid temporal aggregation periods
2+
#'
3+
#' Returns the set of temporal periods supported by the cd package.
4+
#' Used for input validation and iteration in extraction and analysis
5+
#' functions.
6+
#'
7+
#' @param include_monthly Logical. If `TRUE`, appends the 12 calendar
8+
#' months (Jan, Feb, ..., Dec) to the seasonal periods. Default `FALSE`.
9+
#'
10+
#' @return Character vector of period names.
11+
#'
12+
#' @examples
13+
#' cd_periods()
14+
#' cd_periods(include_monthly = TRUE)
15+
#'
16+
#' @export
17+
cd_periods <- function(include_monthly = FALSE) {
18+
periods <- c("annual", "winter", "spring", "summer", "fall")
19+
if (include_monthly) {
20+
periods <- c(periods, month.abb)
21+
}
22+
periods
23+
}

R/cd_variables.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#' Climate variable metadata
2+
#'
3+
#' Returns a tibble of metadata for the seven climate variables supported
4+
#' by the cd package. Used internally for unit labels, anomaly type routing,
5+
#' and ERA5-Land API variable names.
6+
#'
7+
#' @return A tibble with columns:
8+
#' \describe{
9+
#' \item{variable}{Short name used throughout the package.}
10+
#' \item{long_name}{Human-readable label for plots and tables.}
11+
#' \item{unit}{Measurement unit (degree C, percent, Pa).}
12+
#' \item{anomaly_type}{"absolute" for direct departures, "pct_normal"
13+
#' for percent-of-normal anomalies.}
14+
#' \item{era5_name}{ERA5-Land variable name for CDS API requests,
15+
#' or NA for derived variables.}
16+
#' }
17+
#'
18+
#' @examples
19+
#' cd_variables()
20+
#' cd_variables()$variable
21+
#'
22+
#' @export
23+
cd_variables <- function() {
24+
tibble::tibble(
25+
variable = c("tmean", "tmax", "tmin", "prcp", "vpd", "rh", "soil_moisture"),
26+
long_name = c(
27+
"Mean temperature", "Maximum temperature", "Minimum temperature",
28+
"Precipitation", "Vapour pressure deficit", "Relative humidity",
29+
"Soil moisture"
30+
),
31+
unit = c("\u00b0C", "\u00b0C", "\u00b0C", "%", "Pa", "%", "%"),
32+
anomaly_type = c(
33+
"absolute", "absolute", "absolute",
34+
"pct_normal",
35+
"absolute", "absolute",
36+
"pct_normal"
37+
),
38+
era5_name = c(
39+
"2m_temperature", NA_character_, NA_character_,
40+
"total_precipitation",
41+
NA_character_, NA_character_,
42+
NA_character_
43+
)
44+
)
45+
}

man/cd_cache_clear.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cd_cache_info.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cd_cache_path.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cd_periods.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cd_variables.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

planning/active/progress.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Progress
2+
3+
## Status: Complete
4+
5+
### cd_variables() — Issue #1
6+
- [x] Implementation
7+
- [x] Tests
8+
- [x] Docs
9+
- [x] Committed
10+
11+
### cd_periods() — Issue #2
12+
- [x] Implementation
13+
- [x] Tests
14+
- [x] Docs
15+
- [x] Committed
16+
17+
### cd_cache_*() — Issue #3
18+
- [x] Implementation
19+
- [x] Tests
20+
- [x] Docs
21+
- [x] Committed
22+
23+
### Final
24+
- [x] `devtools::test()` all pass (25/25)
25+
- [x] `lintr::lint_package()` clean
26+
- [ ] PR created

0 commit comments

Comments
 (0)