From 21744b259b253d061edce2053ed406c44b563fbb Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 2 Dec 2019 16:24:00 +0100 Subject: [PATCH 01/17] New function to interprete lons and regions --- R/Lon2Index.R | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 R/Lon2Index.R diff --git a/R/Lon2Index.R b/R/Lon2Index.R new file mode 100644 index 0000000..422ffce --- /dev/null +++ b/R/Lon2Index.R @@ -0,0 +1,134 @@ +# Obtain the index of positions for a region in longitudes +# +#'@description This auxiliary function returns the index of position of a region of longitudes in a given vector of longitudes. +#' +#'@param lon vector of longitudes values. +#'@param lonmin a numeric value indicating the minimum longitude of the region (understand as the left marging of the region). +#'@param lonmax a numeric value indicating the maximum longitude of the region (understand as the right mariging of the region). +#' +#'@return the index of positions of all values inside the region in the vector lon. +#' +#'@examples +#' +#'lon <- 1 : 360 +#'pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) +#'lon[pos] # fails however it works +#'pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) +#'lon[pos] # Fails no, it works +#'pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) +#'lon[pos] # Fails +#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) +#'lon[pos] #Fails however it works for now. +#'lon <- -180 : 180 +#'pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) +#'lon[pos] # fails +#'pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) +#'lon[pos] # Fails +#'pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) +#'lon[pos] # Fails +#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) +#'lon[pos] #Fails however it works for now. +#'lon <- -360 : 360 +#'pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) +#'lon[pos] #Fails +#'pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) +#'lon[pos] +#'pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) +#'lon[pos] # Fails +#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) +#'lon[pos] #Fails again it works +#' +#'@export +Lon2Index <- function(lon, lonmin, lonmax) { + if (is.null(lon)) { + stop("Parameter 'lon' cannot be NULL.") + } + if (!is.numeric(lon)) { + stop("Parameter 'lon' must be numeric.") + } + if (!is.vector(lon)) { + stop("Parameter 'lon' must be a vector.") + } + if (!is.numeric(lonmin) | !is.numeric(lonmax)) { + stop("Parameter 'lonmin' and 'lonmax' must be numeric.") + } + if (!is.vector(lonmin) | !is.vector(lonmax)) { + stop("Parameter 'lonmin'and 'lonmax' must be a vector.") + } + + vlonmax <- max(lon) + vlonmin <- min(lon) + if (vlonmin < 0 & !(vlonmax > 180)) { # -180 to 180 + if (lonmin < -180) { + stop("Change parameter 'lonmin' to match longitudes ", + "in the range -180 to 180.") + } else if (lonmin > 180) { + lonmin <- lonmin - 360 + } + if (lonmax < -180) { + stop("Change parameter 'lonmax' to match longitudes ", + "in the range -180 to 180.") + } else if (lonmax > 180) { + lonmax <- lonmax - 360 + } + if (lonmin > lonmax) { + index <- which(lon >= lonmin | lon <= lonmax) + } else { + index <- which(lon >= lonmin & lon <= lonmax) + } + } else if (vlonmin < 0 & vlonmax > 180) { # -360 to 360 + if (lonmin > lonmax) { + index <- which(lon >= lonmin | lon <= lonmax) + } else { + index <- which(lon >= lonmin & lon <= lonmax) + } + } else { # 0 : 360 + if (lonmin < 0) { + lonmin <- lonmin + 360 + } else if (lonmin > 360) { + lonmin <- lonmin - 360 + } + if (lonmax < 0) { + lonmax <- lonmax + 360 + } else if (lonmax > 360) { + lonmax <- lonmax - 360 + } + if (lonmin > lonmax) { + index <- c(which(lon >= lonmin), which(lon <= lonmax)) + } else { + index <- which(lon >= lonmin & lon <= lonmax) + } + } + +return(index) +} -- GitLab From 0330e0cf42b3775e38733dae9c01d1f99b32ad62 Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 11 Dec 2019 19:02:58 +0100 Subject: [PATCH 02/17] Reordering output --- NAMESPACE | 1 + R/Lon2Index.R | 4 +-- man/Lon2Index.Rd | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 man/Lon2Index.Rd diff --git a/NAMESPACE b/NAMESPACE index 8c0c72b..8364b61 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(DTRIndicator) export(DTRRef) export(DailyAno) export(Extremes) +export(Lon2Index) export(SeasonSelect) export(SelBox) export(Subset) diff --git a/R/Lon2Index.R b/R/Lon2Index.R index 422ffce..5b9ff88 100644 --- a/R/Lon2Index.R +++ b/R/Lon2Index.R @@ -102,13 +102,13 @@ Lon2Index <- function(lon, lonmin, lonmax) { lonmax <- lonmax - 360 } if (lonmin > lonmax) { - index <- which(lon >= lonmin | lon <= lonmax) + index <- c(which(lon >= lonmin), which(lon <= lonmax)) } else { index <- which(lon >= lonmin & lon <= lonmax) } } else if (vlonmin < 0 & vlonmax > 180) { # -360 to 360 if (lonmin > lonmax) { - index <- which(lon >= lonmin | lon <= lonmax) + index <- c(which(lon >= lonmin), which(lon <= lonmax)) } else { index <- which(lon >= lonmin & lon <= lonmax) } diff --git a/man/Lon2Index.Rd b/man/Lon2Index.Rd new file mode 100644 index 0000000..4fecf2b --- /dev/null +++ b/man/Lon2Index.Rd @@ -0,0 +1,82 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Lon2Index.R +\name{Lon2Index} +\alias{Lon2Index} +\usage{ +Lon2Index(lon, lonmin, lonmax) +} +\arguments{ +\item{lon}{vector of longitudes values.} + +\item{lonmin}{a numeric value indicating the minimum longitude of the region (understand as the left marging of the region).} + +\item{lonmax}{a numeric value indicating the maximum longitude of the region (understand as the right mariging of the region).} +} +\value{ +the index of positions of all values inside the region in the vector lon. +} +\description{ +This auxiliary function returns the index of position of a region of longitudes in a given vector of longitudes. +} +\examples{ + +lon <- 1 : 360 +pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) +lon[pos] +pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) +lon[pos] +pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) +lon[pos] # fails however it works +pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) +lon[pos] # Fails no, it works +pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) +lon[pos] # Fails +pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) +lon[pos] #Fails however it works for now. +lon <- -180 : 180 +pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) +lon[pos] +pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) +lon[pos] +pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) +lon[pos] # fails +pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) +lon[pos] # Fails +pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) +lon[pos] # Fails +pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) +lon[pos] #Fails however it works for now. +lon <- -360 : 360 +pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) +lon[pos] #Fails +pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) +lon[pos] +pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) +lon[pos] +pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) +lon[pos] +pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) +lon[pos] +pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) +lon[pos] +pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) +lon[pos] # Fails +pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) +lon[pos] #Fails again it works + +} + -- GitLab From 5a5973f613e90a060950f27dad07223c7feafe9a Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:12:53 +0100 Subject: [PATCH 03/17] reorder and tests --- tests/testthat.R | 5 ++ tests/testthat/test-Lon2index.R | 98 +++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-Lon2index.R diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..6b1f95f --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,5 @@ +library(testthat) +library(ClimProjDiags) + +test_check("ClimProjDiags") + diff --git a/tests/testthat/test-Lon2index.R b/tests/testthat/test-Lon2index.R new file mode 100644 index 0000000..8f53079 --- /dev/null +++ b/tests/testthat/test-Lon2index.R @@ -0,0 +1,98 @@ +context("Generic tests") +test_that("Sanity checks", { + expect_error(Lon2Index(lon = NULL), "Parameter 'lon' cannot be NULL.") + expect_error(Lon2Index(lon = 'a'), "Parameter 'lon' must be numeric.") + expect_error(Lon2Index(lon = 1), + 'argument "lonmin" is missing, with no default') + expect_error(Lon2Index(lon = 1, lonmin = 'a'), + 'argument "lonmax" is missing, with no default') + expect_error(Lon2Index(lon = 1, lonmin = 'a', lonmax = 1), + "Parameter 'lonmin' and 'lonmax' must be numeric.") + expect_error(Lon2Index(lon = 1, lonmin = 1, lonmax = 'b'), + "Parameter 'lonmin' and 'lonmax' must be numeric.") + expect_equal(Lon2Index(lon = 1, lonmin = 1, lonmax = 1), 1) +}) + +test_that("Case 1 to 360", { +lon <- 1 : 360 + expect_equal(Lon2Index(lon, lonmin = -20, lonmax = 20), + c(340 : 360, 1 : 20)) + expect_equal(Lon2Index(lon, lonmin = 340, lonmax = 20), + c(340 : 360, 1 : 20)) + expect_equal(Lon2Index(lon, lonmin = 20, lonmax = 340), + 20 : 340) + expect_equal(Lon2Index(lon, lonmin = -40, lonmax = -20), + 320 : 340) + expect_equal(Lon2Index(lon, lonmin = 320, lonmax = 340), + 320 : 340) + expect_equal(Lon2Index(lon, lonmin = -220, lonmax = -170), + 140 : 190) + expect_equal(Lon2Index(lon, lonmin = -350, lonmax = -300), + 10 : 60) + expect_equal(Lon2Index(lon, lonmin = -400, lonmax = -370), integer(0)) + expect_equal(Lon2Index(lon, lonmin = 340, lonmax = 380), + c(340 : 360, 1 : 20)) +}) + +test_that("Case -180 to 180", { +lon <- -180 : 180 + expect_equal(Lon2Index(lon, lonmin = -20, lonmax = 20), + 161 : 201) + expect_equal(Lon2Index(lon, lonmin = 340, lonmax = 20), + 161 : 201) + expect_equal(Lon2Index(lon, lonmin = 20, lonmax = 340), + c(201 : 361, 1 : 161)) + expect_equal(Lon2Index(lon, lonmin = -40, lonmax = -20), + 141 : 161) + expect_equal(Lon2Index(lon, lonmin = 320, lonmax = 340), + 141 : 161) + expect_error(Lon2Index(lon, lonmin = -220, lonmax = -170), + "Change parameter 'lonmin' to match longitudes in the range -180 to 180.") + expect_error(Lon2Index(lon, lonmin = -350, lonmax = -300), + "Change parameter 'lonmin' to match longitudes in the range -180 to 180.") + expect_error(Lon2Index(lon, lonmin = -400, lonmax = -370), + "Change parameter 'lonmin' to match longitudes in the range -180 to 180.") + expect_equal(Lon2Index(lon, lonmin = 340, lonmax = 380), + 161 : 201) +}) + +test_that("Case -360 to 360", { +lon <- -360 : 360 + expect_equal(Lon2Index(lon, lonmin = -20, lonmax = 20), + 341 : 381) + expect_equal(Lon2Index(lon, lonmin = 340, lonmax = 20), + c(701 : 721, 1 : 381)) + expect_equal(Lon2Index(lon, lonmin = 20, lonmax = 340), + 381 : 701) + expect_equal(Lon2Index(lon, lonmin = -40, lonmax = -20), + 321 : 341) + expect_equal(Lon2Index(lon, lonmin = 320, lonmax = 340), + 681 : 701) + expect_equal(Lon2Index(lon, lonmin = -220, lonmax = -170), + 141 : 191) + expect_equal(Lon2Index(lon, lonmin = -350, lonmax = -300), + 11 : 61) + expect_equal(Lon2Index(lon, lonmin = -400, lonmax = -370), integer(0)) + expect_equal(Lon2Index(lon, lonmin = 340, lonmax = 380), + 701 : 721) +}) + + + + + + + + + + + + + + + + + + + + -- GitLab From 903f77d4f7f6e463117e20e9ba997969bb92fd98 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:13:41 +0100 Subject: [PATCH 04/17] testthat in DESCRIPTION --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index d7691f7..e03c07b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,5 +30,6 @@ LazyData: true RoxygenNote: 5.0.0 Suggests: knitr, + testthat, rmarkdown VignetteBuilder: knitr -- GitLab From 992ddfc04d5e931aba2c1dcbe99cfead318eeed6 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:14:52 +0100 Subject: [PATCH 05/17] Continuous integration yml --- .gitlab-ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..d7d8605 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,10 @@ +stages: + - build +build: + stage: build + script: + - module load R + - R CMD build --resave-data . + - R CMD check --as-cran --no-manual ClimProjDiags_*.tar.gz + - R -e 'covr::package_coverage()' + -- GitLab From 3604abf9587adc4035d00b4821631e6f3cbb67a4 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:26:31 +0100 Subject: [PATCH 06/17] Remove examples included in tests --- R/Lon2Index.R | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/R/Lon2Index.R b/R/Lon2Index.R index 5b9ff88..78e2982 100644 --- a/R/Lon2Index.R +++ b/R/Lon2Index.R @@ -15,58 +15,11 @@ #'lon[pos] #'pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) #'lon[pos] -#'pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) -#'lon[pos] # fails however it works -#'pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) -#'lon[pos] # Fails no, it works -#'pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) -#'lon[pos] # Fails -#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) -#'lon[pos] #Fails however it works for now. #'lon <- -180 : 180 #'pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) #'lon[pos] #'pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) #'lon[pos] -#'pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) -#'lon[pos] # fails -#'pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) -#'lon[pos] # Fails -#'pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) -#'lon[pos] # Fails -#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) -#'lon[pos] #Fails however it works for now. -#'lon <- -360 : 360 -#'pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) -#'lon[pos] #Fails -#'pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) -#'lon[pos] -#'pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) -#'lon[pos] # Fails -#'pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) -#'lon[pos] #Fails again it works #' #'@export Lon2Index <- function(lon, lonmin, lonmax) { -- GitLab From c410220fb42bae865c1b59c947e659db63b8158d Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:28:49 +0100 Subject: [PATCH 07/17] documentation updated --- man/Lon2Index.Rd | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/man/Lon2Index.Rd b/man/Lon2Index.Rd index 4fecf2b..605cba4 100644 --- a/man/Lon2Index.Rd +++ b/man/Lon2Index.Rd @@ -25,58 +25,11 @@ pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) lon[pos] pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) lon[pos] -pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) -lon[pos] -pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) -lon[pos] -pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) -lon[pos] -pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) -lon[pos] # fails however it works -pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) -lon[pos] # Fails no, it works -pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) -lon[pos] # Fails -pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) -lon[pos] #Fails however it works for now. lon <- -180 : 180 pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) lon[pos] pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) lon[pos] -pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) -lon[pos] -pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) -lon[pos] -pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) -lon[pos] -pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) -lon[pos] # fails -pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) -lon[pos] # Fails -pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) -lon[pos] # Fails -pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) -lon[pos] #Fails however it works for now. -lon <- -360 : 360 -pos <- Lon2Index(lon, lonmin = -20, lonmax = 20) -lon[pos] -pos <- Lon2Index(lon, lonmin = 340, lonmax = 20) -lon[pos] #Fails -pos <- Lon2Index(lon, lonmin = 20, lonmax = 340) -lon[pos] -pos <- Lon2Index(lon, lonmin = -40, lonmax = -20) -lon[pos] -pos <- Lon2Index(lon, lonmin = 320, lonmax = 340) -lon[pos] -pos <- Lon2Index(lon, lonmin = -220, lonmax = -170) -lon[pos] -pos <- Lon2Index(lon, lonmin = -350, lonmax = -300) -lon[pos] -pos <- Lon2Index(lon, lonmin = -400, lonmax = -370) -lon[pos] # Fails -pos <- Lon2Index(lon, lonmin = 340, lonmax = 380) -lon[pos] #Fails again it works } -- GitLab From 4cb5bb135cc3509f64bb4c9c7fafbff1d02ac921 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:36:01 +0100 Subject: [PATCH 08/17] Fix documentation --- R/Lon2Index.R | 4 ++-- man/Lon2Index.Rd | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/R/Lon2Index.R b/R/Lon2Index.R index 78e2982..1ab7197 100644 --- a/R/Lon2Index.R +++ b/R/Lon2Index.R @@ -1,5 +1,5 @@ -# Obtain the index of positions for a region in longitudes -# +#'Obtain the index of positions for a region in longitudes +#' #'@description This auxiliary function returns the index of position of a region of longitudes in a given vector of longitudes. #' #'@param lon vector of longitudes values. diff --git a/man/Lon2Index.Rd b/man/Lon2Index.Rd index 605cba4..b2012a6 100644 --- a/man/Lon2Index.Rd +++ b/man/Lon2Index.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/Lon2Index.R \name{Lon2Index} \alias{Lon2Index} +\title{Obtain the index of positions for a region in longitudes} \usage{ Lon2Index(lon, lonmin, lonmax) } -- GitLab From 585490b08876eae967e94a3a7dca7f858b7721f5 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:44:57 +0100 Subject: [PATCH 09/17] Add format to as.PCICt to solve check to CRAN error in Extreme() example --- R/Extremes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Extremes.R b/R/Extremes.R index 46ecbaf..2fcb7aa 100644 --- a/R/Extremes.R +++ b/R/Extremes.R @@ -114,7 +114,7 @@ Extremes <- function(data, threshold, op = ">", min.length = 6, spells.can.span. if (stop_error) { stop("Parameter 'dates' must be of the same length as the 'time' dimension of the parameter 'data'.") } - dates <- as.PCICt(dates, cal = calendar) + dates <- as.PCICt(dates, cal = calendar, format = "%Y%m%d") dates = as.character(dates) jdays <- as.numeric(strftime(dates, format = "%j")) if (calendar == "gregorian" | calendar == "standard" | calendar == "proleptic_gregorian") { -- GitLab From 4e43e5f23656c96e1c88514ef27a5366b03da1cf Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 11:51:27 +0100 Subject: [PATCH 10/17] add format in as.PCICt --- R/Extremes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Extremes.R b/R/Extremes.R index 2fcb7aa..870735c 100644 --- a/R/Extremes.R +++ b/R/Extremes.R @@ -94,7 +94,7 @@ Extremes <- function(data, threshold, op = ">", min.length = 6, spells.can.span. if (is.character(dates)) { as.POSIXct(dates, format = "%Y%m%d") } else { - as.POSIXct(dates) + as.POSIXct(dates, format = "%Y%m%d") } }) if ('try-error' %in% class(dates) | sum(is.na(dates)) == length(dates)) { -- GitLab From 99991e8e161b6694a8689da6ccae811126aaa38f Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 12:01:18 +0100 Subject: [PATCH 11/17] trying fixing the example --- R/Extremes.R | 3 ++- man/Extremes.Rd | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/R/Extremes.R b/R/Extremes.R index 870735c..ef81024 100644 --- a/R/Extremes.R +++ b/R/Extremes.R @@ -27,7 +27,8 @@ #'##Example synthetic data: #'data <- 1:(2 * 3 * 372 * 1) #'dim(data) <- c(time = 372, lon = 2, lat = 3, model = 1) -#'time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), tz = "CET") +#'time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), tz = "CET", +#' format = "%Y-%m-%d") #'metadata <- list(time = list(standard_name = 'time', long_name = 'time', calendar = 'noleap', #' units = 'days since 1970-01-01 00:00:00', prec = 'double', #' dim = list(list(name = 'time', unlim = FALSE)))) diff --git a/man/Extremes.Rd b/man/Extremes.Rd index 0939fa6..61b312a 100644 --- a/man/Extremes.Rd +++ b/man/Extremes.Rd @@ -46,7 +46,8 @@ This routine compares data to the thresholds using the given operator, generatin ##Example synthetic data: data <- 1:(2 * 3 * 372 * 1) dim(data) <- c(time = 372, lon = 2, lat = 3, model = 1) -time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), tz = "CET") +time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), tz = "CET", + format = "\%Y-\%m-\%d") metadata <- list(time = list(standard_name = 'time', long_name = 'time', calendar = 'noleap', units = 'days since 1970-01-01 00:00:00', prec = 'double', dim = list(list(name = 'time', unlim = FALSE)))) -- GitLab From cd1de1fcceea051b5ee47ec5fabc145dd240a649 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 12:15:34 +0100 Subject: [PATCH 12/17] trying tz parameter --- R/Extremes.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/Extremes.R b/R/Extremes.R index ef81024..d80a4f3 100644 --- a/R/Extremes.R +++ b/R/Extremes.R @@ -27,8 +27,7 @@ #'##Example synthetic data: #'data <- 1:(2 * 3 * 372 * 1) #'dim(data) <- c(time = 372, lon = 2, lat = 3, model = 1) -#'time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), tz = "CET", -#' format = "%Y-%m-%d") +#'time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), tz = "CET") #'metadata <- list(time = list(standard_name = 'time', long_name = 'time', calendar = 'noleap', #' units = 'days since 1970-01-01 00:00:00', prec = 'double', #' dim = list(list(name = 'time', unlim = FALSE)))) @@ -93,7 +92,7 @@ Extremes <- function(data, threshold, op = ">", min.length = 6, spells.can.span. if (!any(class(dates) %in% c('POSIXct'))) { dates <- try( { if (is.character(dates)) { - as.POSIXct(dates, format = "%Y%m%d") + as.POSIXct(dates, format = "%Y%m%d", tz = "CET") } else { as.POSIXct(dates, format = "%Y%m%d") } -- GitLab From 3ec076c08520cfbf2587b21956b0fbb5d8a33e3a Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 12:17:51 +0100 Subject: [PATCH 13/17] trying tz parameter second place --- R/Extremes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Extremes.R b/R/Extremes.R index d80a4f3..4b07c16 100644 --- a/R/Extremes.R +++ b/R/Extremes.R @@ -94,7 +94,7 @@ Extremes <- function(data, threshold, op = ">", min.length = 6, spells.can.span. if (is.character(dates)) { as.POSIXct(dates, format = "%Y%m%d", tz = "CET") } else { - as.POSIXct(dates, format = "%Y%m%d") + as.POSIXct(dates, format = "%Y%m%d", tz = "CET") } }) if ('try-error' %in% class(dates) | sum(is.na(dates)) == length(dates)) { -- GitLab From 2dae68d6b581e7056ee8dc61e833e674dece9b74 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 12:22:49 +0100 Subject: [PATCH 14/17] Try latest R version --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d7d8605..3d9d3b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ stages: build: stage: build script: - - module load R + - module load R/3.6.1-foss-2015a-bare - R CMD build --resave-data . - R CMD check --as-cran --no-manual ClimProjDiags_*.tar.gz - R -e 'covr::package_coverage()' -- GitLab From 6f8cae7162f0a6d101703d4a9ddffd2bd7a43cc1 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 12:33:58 +0100 Subject: [PATCH 15/17] remove as.character --- .gitlab-ci.yml | 2 +- R/Extremes.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3d9d3b6..d7d8605 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ stages: build: stage: build script: - - module load R/3.6.1-foss-2015a-bare + - module load R - R CMD build --resave-data . - R CMD check --as-cran --no-manual ClimProjDiags_*.tar.gz - R -e 'covr::package_coverage()' diff --git a/R/Extremes.R b/R/Extremes.R index 4b07c16..3b503a9 100644 --- a/R/Extremes.R +++ b/R/Extremes.R @@ -115,7 +115,7 @@ Extremes <- function(data, threshold, op = ">", min.length = 6, spells.can.span. stop("Parameter 'dates' must be of the same length as the 'time' dimension of the parameter 'data'.") } dates <- as.PCICt(dates, cal = calendar, format = "%Y%m%d") - dates = as.character(dates) +# dates = as.character(dates) jdays <- as.numeric(strftime(dates, format = "%j")) if (calendar == "gregorian" | calendar == "standard" | calendar == "proleptic_gregorian") { year <- as.numeric(strftime(dates, format = "%Y")) -- GitLab From 7315a243b5af979c6a321a7a2e7370590e83b5c8 Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 12 Dec 2019 18:28:52 +0100 Subject: [PATCH 16/17] automatic test fails in gitlab but not in win-builder --- .gitlab-ci.yml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index d7d8605..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,10 +0,0 @@ -stages: - - build -build: - stage: build - script: - - module load R - - R CMD build --resave-data . - - R CMD check --as-cran --no-manual ClimProjDiags_*.tar.gz - - R -e 'covr::package_coverage()' - -- GitLab From b5e2168479ec39e8dd286e31e632c3d4aa19440e Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 20 Jan 2020 16:55:02 +0100 Subject: [PATCH 17/17] Climdex fixed and Selbox using Lon2Index --- R/Climdex.R | 2 +- R/SelBox.R | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/R/Climdex.R b/R/Climdex.R index 261815c..20c3e43 100644 --- a/R/Climdex.R +++ b/R/Climdex.R @@ -373,7 +373,7 @@ Climdex <- function(data, metric, threshold = NULL, base.range = NULL, dates = N #' @noRd .nday.consec.prec.max <- function(daily.prec, date.factor, ndays, center.mean.on.last.day=FALSE) { if(ndays == 1) { - return(suppressWarnings(tapply.fast(daily.prec, date.factor, max, na.rm=TRUE))) + return(suppressWarnings(.tapply.fast(daily.prec, date.factor, max, na.rm=TRUE))) } ## Ends of the data will be de-emphasized (padded with zero precip data); NAs replaced with 0 daily.prec[is.na(daily.prec)] <- 0 diff --git a/R/SelBox.R b/R/SelBox.R index 608e19e..5b7cd59 100644 --- a/R/SelBox.R +++ b/R/SelBox.R @@ -83,11 +83,13 @@ SelBox <- function(data, lon, lat, region, londim = NULL, latdim = NULL, mask = } else { LatIdx <- which(lat <= region[3] | lat >= region[4]) } - if (region[1] <= region[2]) { - LonIdx <- which(lon >= region[1] & lon <= region[2]) - } else { - LonIdx <- which(lon >= region[1] | lon <= region[2]) - } + #if (region[1] <= region[2]) { + # LonIdx <- which(lon >= region[1] & lon <= region[2]) + #} else { + # LonIdx <- which(lon >= region[1] | lon <= region[2]) + #} + LonIdx <- Lon2Index(lon, lonmin = region[1], lonmax = region[2]) + data <- Subset(data, along = londim, indices = LonIdx, drop = "none") data <- Subset(data, along = latdim, indices = LatIdx, drop = "none") if (!is.null(mask)) { -- GitLab