diff --git a/DESCRIPTION b/DESCRIPTION index 4fc1887b6c8aa9b4cc2d2a584f4acbde2df3a65f..b2c44a58fe45ec86b744be37c4e094b712017203 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: SWATdoctR Type: Package Title: Finding the right diagnoses and treatments for SWAT+ models -Version: 0.1.24 +Version: 0.1.25 Author: c(person("Christoph", "Schürz", email = "christoph.schuerz@ufz.de", role = c("aut", "cre")), @@ -34,4 +34,4 @@ Suggests: License: GPL-3 Encoding: UTF-8 LazyData: true -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/NAMESPACE b/NAMESPACE index a91bc53b9c829e765a24ecb5a0bccfc264913658..24f8692faf313040c56b0eb73cb44625f2b0ec3e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(add_kill_op) export(check_hru_waterbalance) export(filter_hru_at_harvkill) +export(get_file_version) export(get_hru_id_by_attribute) export(plot_basin_var) export(plot_climate_annual) diff --git a/R/helper.R b/R/helper.R index bdfb9f137fd3b658d2007a2b6c20d38513f6e388..b33f0fffda953cbad749223289e6bf98e69b687b 100644 --- a/R/helper.R +++ b/R/helper.R @@ -114,6 +114,33 @@ remove_tail <- function(f, pattern){ return(f) } +#' Get the Highest Version Number of a File +#' +#' This function searches for files in a specified directory that match a given +#' pattern and extracts the highest version number from those files. +#' +#' @param parent_dir A string representing the path to the directory where the +#' files are located. +#' @param file_name A string representing the setup name. +#' @param add A string to be added to the file name pattern to match. +#' @return The highest version number found among the matching files as a +#' numeric value. Returns 0 if no matching files are found. +#' @export +#' @examples +#' \dontrun{ +#' file_version <- get_file_version(parent_dir, save_name, '_nostr_') + 1 +#' } + +get_file_version <- function(parent_dir, file_name, add) { + files <- list.files(path = parent_dir, + pattern = paste0(file_name, add, '\\d{1,2}', '\\.rds')) + vers_val <- as.numeric(gsub(paste(file_name, add, '\\.rds', sep = '|'), + '', files)) + return(max(vers_val, 0)) +} + +#' @keywords internal + lwr <- function(df){ names(df) <- tolower(names(df)) return(df) diff --git a/man/get_file_version.Rd b/man/get_file_version.Rd new file mode 100644 index 0000000000000000000000000000000000000000..85e693b698bebf3e90c63dd9fc0b5ba062fcdbf3 --- /dev/null +++ b/man/get_file_version.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/helper.R +\name{get_file_version} +\alias{get_file_version} +\title{Get the Highest Version Number of a File} +\usage{ +get_file_version(parent_dir, file_name, add) +} +\arguments{ +\item{parent_dir}{A string representing the path to the directory where the +files are located.} + +\item{file_name}{A string representing the setup name.} + +\item{add}{A string to be added to the file name pattern to match.} +} +\value{ +The highest version number found among the matching files as a +numeric value. Returns 0 if no matching files are found. +} +\description{ +This function searches for files in a specified directory that match a given +pattern and extracts the highest version number from those files. +} +\examples{ +\dontrun{ +file_version <- get_file_version(parent_dir, save_name, '_nostr_') + 1 +} +}