Skip to content

Commit

Permalink
adding functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed Nov 4, 2024
1 parent 3a26bab commit 9c9f72c
Show file tree
Hide file tree
Showing 44 changed files with 3,447 additions and 0 deletions.
48 changes: 48 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,region)
export("%>%")
export(WB_evaporation)
export(add_nc)
export(add_t_range)
export(add_xy_area_range)
export(add_xy_point_range)
export(climatic_details)
export(climatic_missing)
export(climdex)
export(climdex_single_station)
export(convert_SST)
export(convert_to_character_matrix)
export(convert_to_dec_deg)
export(convert_yy_to_yyyy)
export(cumulative_inventory)
export(dd_to_dms)
export(dekad)
export(fourier_series)
export(get_default_significant_figures)
export(get_lat_from_data)
export(get_lon_from_data)
export(get_quarter_label)
export(get_years_from_data)
export(ggwalter_lieth)
export(import_from_iri)
export(lat_lon_dataframe)
export(make_factor)
export(multiple_nc_as_data_frame)
export(nc_as_data_frame)
export(nc_get_dim_min_max)
export(other_rose_plots)
export(output_CPT)
export(pentad)
export(plot_declustered)
export(plot_mrl)
export(plot_multiple_threshold)
export(prepare_walter_lieth)
export(spei_input)
export(spei_output)
export(spells)
export(threshold_Plot)
export(wind_pollution_rose)
export(write_weather_data)
export(yday_366)
import(raster)
import(sp)
importFrom(magrittr,"%>%")
importFrom(rlang,":=")
importFrom(stats,na.exclude)
18 changes: 18 additions & 0 deletions R/add_nc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' Modify a path
#'
#' @description: This function takes a path as input and returns a modified path by appending "data.nc" to it.
#'
#' @param path A character string representing the file path.
#'
#' @return: A character string representing the modified file path with "data.nc" appended to it.
#'
#' @export
#'
#' @examples
#' add_nc("my_folder/") # Returns "my_folder/data.nc"
#' add_nc("/path/to/file.txt") # Returns "/path/to/file.txtdata.nc"
#' add_nc("") # Returns "data.nc"
#'
add_nc <- function(path) {
paste0(path, "data.nc")
}
31 changes: 31 additions & 0 deletions R/add_t_range.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' add time range
#'
#' @description This function generates a string representing a time range to be added to a given path.
#'
#' @param path The base path to which the time range will be added.
#' @param min_date The minimum date of the time range.
#' @param max_date The maximum date of the time range.
#' @param dim_t The dimension of time to be included in the range (default: "T").
#'
#' @return A string representing the path with the time range added.
#'
#' @export
#'
#' @examples
#' # Example 1: Generate a time range string with default dimension
#' #path <- "http://example.com/"
#' #min_date <- lubridate::ymd("2023-01-01")
#' #max_date <- lubridate::ymd("2023-12-31")
#' #t_range <- add_t_range(path, min_date, max_date)
#'
#'
add_t_range <- function(path, min_date, max_date, dim_t = "T") {
paste0(
path, dim_t, "/",
"(", lubridate::day(min_date), "%20", lubridate::month(min_date, label = TRUE),
"%20", lubridate::year(min_date), ")", "/",
"(", lubridate::day(max_date), "%20", lubridate::month(max_date, label = TRUE),
"%20", lubridate::year(max_date), ")", "/",
"RANGEEDGES", "/"
)
}
39 changes: 39 additions & 0 deletions R/add_xy_area_range.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' Add xy area range
#'
#' @description
#' A function that generates a string representing an XY area range to be added to a given path.
#'
#' @param path The base path to which the XY area range will be added.
#' @param min_lon The minimum longitude of the area range.
#' @param max_lon The maximum longitude of the area range.
#' @param min_lat The minimum latitude of the area range.
#' @param max_lat The maximum latitude of the area range.
#' @param dim_x The dimension for longitude (default: "X").
#' @param dim_y The dimension for latitude (default: "Y").
#'
#' @return A string representing the path with the XY area range added.
#'
#' @export
#'
#' @examples
#' #Example: Generate an XY area range string with custom dimensions
#' #path <- "http://example.com"
#' #min_lon <- -90
#' #max_lon <- -80
#' #min_lat <- 30
#' #max_lat <- 40
#' #xy_range <- add_xy_area_range(path, min_lon, max_lon, min_lat,
#' # max_lat,dim_x = "LON", dim_y = "LAT")
#'
add_xy_area_range <- function(path, min_lon, max_lon, min_lat, max_lat, dim_x = "X", dim_y = "Y") {
paste0(
path, "/", dim_x, "/",
"(", ifelse(min_lon < 0, paste0(abs(min_lon), "W"), paste0(min_lon, "E")), ")", "/",
"(", ifelse(max_lon < 0, paste0(abs(max_lon), "W"), paste0(max_lon, "E")), ")", "/",
"RANGEEDGES", "/",
dim_y, "/",
"(", ifelse(min_lat < 0, paste0(abs(min_lat), "S"), paste0(min_lat, "N")), ")", "/",
"(", ifelse(max_lat < 0, paste0(abs(max_lat), "S"), paste0(max_lat, "N")), ")", "/",
"RANGEEDGES", "/"
)
}
26 changes: 26 additions & 0 deletions R/add_xy_point_range.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
##' add xy area range
#'
#' This function generates a file path for XY point range data based on the provided parameters.
#'
#' @param path (character) The base path where the data file will be stored.
#' @param min_lon (numeric) The minimum longitude value for the range.
#' @param min_lat (numeric) The minimum latitude value for the range.
#' @param dim_x (character) The name of the X dimension. (Default: "X")
#' @param dim_y (character) The name of the Y dimension. (Default: "Y")
#'
#' @return The generated file path for the XY point range data as a character string.
#' @export
#'
#' @examples
#' add_xy_point_range("data", -90, 30, "X", "Y")
#'
add_xy_point_range <- function(path, min_lon, min_lat,
dim_x = "X", dim_y = "Y") {
paste0(
path, "/", dim_x, "/", "(",
ifelse(min_lon < 0, paste0(abs(min_lon), "W"), paste0(min_lon, "E")),
")", "/", "VALUES", "/", dim_y, "/", "(",
ifelse(min_lat < 0, paste0(abs(min_lat), "S"), paste0(min_lat, "N")),
")", "/", "VALUES", "/"
)
}
156 changes: 156 additions & 0 deletions R/climatic_details.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#' Climatic Details
#'
#' @description
#' This function extracts climatic details from a given dataset based on specified parameters such as date, elements, stations, and level. It provides information about the duration and frequency of missing values for each element at different levels (day, month, year).
#'
#' @param data A dataset containing climatic data.
#' @param date The date variable in the dataset.
#' @param elements A character vector specifying the climatic elements to analyze.
#' @param stations A character vector specifying the stations to analyze.
#' @param order A logical value indicating whether the resulting tables should be ordered.
#' @param day A logical value indicating whether to include information at the day level.
#' @param month A logical value indicating whether to include information at the month level.
#' @param year A logical value indicating whether to include information at the year level.
#' @param level A logical value indicating whether to include the level information in the output.
#'
#' @return A data frame containing climatic details such as start and end dates of missing values and the count of missing values for each element at the specified levels.
#'
#' @export
#'
#' @examples
#' #climatic_details(data = climatic_data,
#' # date = "Date",
#' # elements = c("Temperature", "Precipitation"),
#' # stations = c("Station1", "Station2"),
#' # order = TRUE,
#' # day = TRUE,
#' # month = FALSE,
#' # year = TRUE,
#' # level = TRUE)

climatic_details <- function(data, date, elements = ..., stations,
order = FALSE,
day = TRUE,
month = FALSE,
year = FALSE, level = FALSE){


if (missing(date)){
stop('argument "date" is missing, with no default')
}

if (missing(elements)){
stop('argument "elements" is missing, with no default')
}

i <- 0
list_tables <- NULL

# stack data
data.stack <- data %>%
tidyr::pivot_longer(cols = c({{ elements }}),
names_to = "Element",
values_to = "Value") %>%
dplyr::mutate(Element = make_factor(Element))

# sort start/end times

if (!any(day, month, year)){
warning('At least one of day, month, year need to be selected')
}

if (day){
i = i + 1
detail.table.day = data.stack %>%
dplyr::group_by({{ stations }}, Element) %>%
dplyr::mutate(element.na = data.table::rleid(Value)) %>%
dplyr::filter(is.na(Value)) %>%
dplyr::group_by(element.na, {{ stations }}, Element) %>%
dplyr::summarise(From = dplyr::first({{ date }}),
To = dplyr::last({{ date }}),
Count = dplyr::n()) %>%
dplyr::mutate(Level = "Day")

if (order){
detail.table.day <- detail.table.day %>% dplyr::arrange(From)
} else {
detail.table.day <- detail.table.day %>% dplyr::arrange(Element)
}

detail.table.day <- detail.table.day %>% dplyr::ungroup() %>% dplyr::select(-c("element.na"))
list_tables[[i]] <- detail.table.day

}

if (month){
i = i + 1
detail.table.month <- data.stack %>%
dplyr::mutate(Date.ym = zoo::as.yearmon({{ date }})) %>%
dplyr::group_by(Date.ym, {{ stations }}, Element)

detail.table.month <- detail.table.month %>%
dplyr::summarise(no = dplyr::n(),
na = sum(is.na(Value)),
From = dplyr::first({{ date }}),
To = dplyr::last({{ date }})) %>%
dplyr::mutate(is.complete = ifelse(no == na, 1, 0)) # 0 if all are missing

detail.table.month <- detail.table.month %>%
dplyr::group_by({{ stations }}, Element) %>%
dplyr::mutate(element.na = data.table::rleid(is.complete)) %>%
dplyr::filter(is.complete == 1) %>%
dplyr::group_by(element.na, {{ stations }}, Element) %>%
dplyr::summarise(From = dplyr::first(From),
To = dplyr::last(To),
Count = dplyr::n()) %>%
dplyr::mutate(Level = "Month")

if (order){
detail.table.month <- detail.table.month %>% dplyr::arrange(From)
} else {
detail.table.month <- detail.table.month %>% dplyr::arrange(Element)
}

detail.table.month <- detail.table.month %>% dplyr::ungroup() %>% dplyr::select(-c("element.na"))
list_tables[[i]] <- detail.table.month
}

if (year) {
i = i + 1
detail.table.year <- data.stack %>%
dplyr::mutate(Date.y = lubridate::year({{ date }})) %>%
dplyr::group_by(Date.y, {{ stations }}, Element)

detail.table.year <- detail.table.year %>%
dplyr::summarise(no = dplyr::n(),
na = sum(is.na(Value)),
From = dplyr::first({{ date }}),
To = dplyr::last({{ date }})) %>%
dplyr::mutate(is.complete = ifelse(no == na, 1, 0)) # 0 if all are missing

detail.table.year <- detail.table.year %>%
dplyr::group_by({{ stations }}, Element) %>%
dplyr::mutate(element.na = data.table::rleid(is.complete)) %>%
dplyr::filter(is.complete == 1) %>%
dplyr::group_by(element.na, {{ stations }}, Element) %>%
dplyr::summarise(From = dplyr::first(From),
To = dplyr::last(To),
Count = dplyr::n()) %>%
dplyr::mutate(Level = "Year")

if (order){
detail.table.year <- detail.table.year %>% dplyr::arrange(From)
} else {
detail.table.year <- detail.table.year %>% dplyr::arrange(Element)
}

detail.table.year <- detail.table.year %>% dplyr::ungroup() %>% dplyr::select(-c("element.na"))
list_tables[[i]] <- detail.table.year
}

detail.table.all <- plyr::ldply(list_tables, data.frame) %>%
dplyr::mutate(Level = make_factor(Level))

return(detail.table.all)

}
Loading

0 comments on commit 9c9f72c

Please sign in to comment.