diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000000000000000000000000000000000..3faa54d96dda3af8d4e29ab8b4c8101470f6ca41 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,66 @@ +### Questions and bug reports + +If you have questions about the usage of a function or would like to report a bug, please follow these steps: + +1. Use the search function in GitLab to see if a similar problem has already been reported and/or solved. + +2. Rather than including a long script, try to run your code in small blocks to narrow down where the problem is coming from as much as possible. + +3. Open an issue tagging the maintainers (@tkariyat), and include a descriptive title and a piece of code to reproduce your problem (if applicable). + +### How to contribute + +If you would like to add a bugfix, an enhancement or a new functions, follow these steps: + +1. Open an issue to ask for help or describe a feature to be integrated + +2. Agree with the maintainers (@tkariyat) on the requirements + +3. Create a new branch from master with a meaningful name + +4. Once the development is finished, open a merge request to merge the branch on master + +*Note: Remember to work with multidimensional arrays with named dimensions when possible and use multiApply (https://earth.bsc.es/gitlab/ces/multiApply)* + +### Adding a function + +To add a new function in this R package, follow these considerations: + +* Each function exposed to the users should be in its own separate file in the R folder +* The name of the function should match the name of the file (e.g.: `Function()` included in file **Function.R** +* The documentation should be in roxygen2 format as a header of the function +* Once the function and the documentation are finished, run the command `devtools::document()` in your R terminal to automatically generate the **Function.Rd** file +* When doing the development, please use an R version between R/4.1.2 and R/4.3.x + +### Style guide + +* Use `<-` for variable assignment +* Include spaces between operators (e.g. `+`, `-`, `&`), before `{`, and after `for`, `if`, `while`, `,` and `)`. +* When possible, maximum line length should be 100 characters (soft limit of 80 characters). +* Number of indentation spaces is 2, using tabs for indentation is forbidden. +* Double quotes are recommended for strings. When writing quotes within quoted text, use double quotes outside and single quotes inside. E.g.: `“Parameter 'na.rm' is missing.”` +* Self-explanatory names are preferred for variables. Try to be consistent with the variable naming style. Generally: avoid special characters (except underscores) and reserved words (ex: if, for, else, …) +* Remember to include short comments to make the code easier to understand. Comments should be in their own line and they should start with `#` followed by a space. Comments that start with `##` and a space are for details not needed to understand the general procedure but useful to make note of more technical aspects of the code. + +#### Examples: + +```r +# Proper spacing, indentation spaces and text quotes: +NewFunction <- function(text = "default", uppercase = TRUE) { + # Check uppercase parameter + if (!is.logical(uppercase)) { + stop("Parameter 'uppercase' should be TRUE or FALSE.") + } + # Only transform text if needed + if (uppercase && is.character(text)) { + text <- toupper(text) + } + return(text) +} + +# How to format line breaks to avoid long lines: +my_strings <- list(one = "one", + two = "two", + three = "three", + four = "four") +``` diff --git a/R/PeriodStandardization.R b/R/PeriodStandardization.R index b71270f14086d2db804f4a046b0766b6f1952edc..00ebc71e9d89a57abad615e4d512c286582da3b1 100644 --- a/R/PeriodStandardization.R +++ b/R/PeriodStandardization.R @@ -125,7 +125,10 @@ CST_PeriodStandardization <- function(data, data_cor = NULL, time_dim = 'syear', if (is.null(data_cor)) { data$data <- std - data$attrs$Variable$varName <- paste0(data$attrs$Variable$varName, ' standardized') + data_longname <- data$attrs$Variable$metadata[[data$attrs$Variable$varName]]$longname + if (!is.null(data_longname)) { + data$attrs$Variable$metadata[[data$attrs$Variable$varName]]$longname <- paste(data_longname, 'standardized') + } if (return_params) { return(list(spei = data, params = params)) } else { @@ -133,7 +136,10 @@ CST_PeriodStandardization <- function(data, data_cor = NULL, time_dim = 'syear', } } else { data_cor$data <- std - data_cor$attrs$Variable$varName <- paste0(data_cor$attrs$Variable$varName, ' standardized') + data_cor_longname <- data_cor$attrs$Variable$metadata[[data_cor$attrs$Variable$varName]]$longname + if (!is.null(data_cor_longname)) { + data_cor$attrs$Variable$metadata[[data_cor$attrs$Variable$varName]]$longname <- paste(data_cor_longname, 'standardized') + } data_cor$attrs$Datasets <- c(data_cor$attrs$Datasets, data$attrs$Datasets) data_cor$attrs$source_files <- c(data_cor$attrs$source_files, data$attrs$source_files) return(data_cor) @@ -644,4 +650,4 @@ PeriodStandardization <- function(data, data_cor = NULL, dates = NULL, } else { return(NA) } -} \ No newline at end of file +} diff --git a/tests/testthat/test-PeriodStandardization.R b/tests/testthat/test-PeriodStandardization.R index 1e816c005efe765d119cfc0645b787d5780b8390..4def61da1641df7e5fe61f82c4384f647e8e7252 100644 --- a/tests/testthat/test-PeriodStandardization.R +++ b/tests/testthat/test-PeriodStandardization.R @@ -38,7 +38,15 @@ dims3 <- c(syear = 6, time = 2, lat = 2, ensemble = 25) set.seed(1) dat3 <- array(abs(rnorm(600, 21.19, 25.64)), dim = dims) - +# dat4 +test <- NULL +test$data <- array(rnorm(600, -204.1, 78.1), dim = dims) +test$dims <- dims +test$coords <- setNames(as.list(as.numeric(test$dims)), names(test$dims)) +test$attrs <- list(Variable = list(varName = 'prlr', + metadata = list(units = "m s-1", + longname = "Total precipitation"))) +class(test) <- 's2dv_cube' ############################################## test_that("1. Initial checks CST_PeriodStandardization", { @@ -152,11 +160,18 @@ test_that("1. Initial checks PeriodStandardization", { test_that("2. Output checks", { # CST_PeriodStandardization + prlr <- CST_PeriodStandardization(data = test) + expect_equal( + names(prlr), + c("data", "dims", "coords", "attrs") + ) + SPEI_s2dv_cube <- CST_PeriodStandardization(data = cube1) expect_equal( names(SPEI_s2dv_cube), - c('data', 'attrs') + c('data') ) + # PeriodStandardization SPEI <- PeriodStandardization(data = dat1) expect_equal(