Skip to content

Commit

Permalink
Merge branch 'devel' of github.com:SantanderMetGroup/transformeR into…
Browse files Browse the repository at this point in the history
… devel
  • Loading branch information
jbedia committed Jun 8, 2018
2 parents 31a1420 + 321caf1 commit 6a6a978
Show file tree
Hide file tree
Showing 26 changed files with 486 additions and 230 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(array3Dto2Dmat)
export(binaryGrid)
export(bindGrid)
export(bindGrid.member)
export(bindGrid.spatial)
export(bindGrid.time)
export(checkDim)
export(checkSeason)
Expand Down Expand Up @@ -41,8 +42,11 @@ export(gridArithmetics)
export(gridDepth)
export(gridFromPCA)
export(interpGrid)
export(intersectGrid)
export(intersectGrid.spatial)
export(intersectGrid.time)
export(isGrid)
export(isMultigrid)
export(isRegular)
export(localScaling)
export(makeMultiGrid)
Expand Down
13 changes: 7 additions & 6 deletions R/binaryGrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@
#' ybin2 <- binaryGrid(VALUE_Iberia_pr,threshold = 1, partial = TRUE)
#' head(ybin2$Data)

binaryGrid <- function(x, condition = "GE", threshold = 1, partial = FALSE, ref.obs = NULL, ref.pred = NULL) {
binaryGrid <- function(x, condition = "GE", threshold = NULL, partial = FALSE, ref.obs = NULL, ref.pred = NULL) {
condition <- match.arg(condition, choices = c("GT", "GE", "LT", "LE"))
dimNames <- getDim(x)
loc <- FALSE
if (!isRegular(x)) {loc <- TRUE}
x <- redim(x, loc = loc)
for (j in 1:dim(x$Data)[which(getDim(x) == "member")]) {
if (is.null(threshold)) {
ref.obs <- redim(ref.obs, loc = loc)
if (isRegular(x)) {
xx <- suppressWarnings(array3Dto2Dmat(subsetGrid(x,members = 1)$Data))
xx <- suppressWarnings(array3Dto2Dmat(subsetGrid(x,members = j)$Data))
xx.obs <- suppressWarnings(array3Dto2Dmat(subsetGrid(ref.obs,members = 1)$Data))
if (is.null(ref.pred)) {xx.pred <- xx} else {xx.pred <- suppressWarnings(array3Dto2Dmat(subsetGrid(ref.pred,members = 1)$Data))}
} else {
xx <- x$Data[1,,]
xx <- x$Data[j,,]
xx.obs <- ref.obs$Data[1,,]
if (is.null(ref.pred)) {xx.pred <- xx} else {xx.pred <- ref.pred$Data[1,,]}
if (is.null(ref.pred)) {xx.pred <- xx} else {xx.pred <- redim(ref.pred, loc = loc)$Data[1,,]}
}

frec <- apply(X = xx.obs, MARGIN = 2, function(X) {
Expand All @@ -71,9 +72,9 @@ binaryGrid <- function(x, condition = "GE", threshold = 1, partial = FALSE, ref.

} else {
if (isRegular(x)) {
xx <- suppressWarnings(array3Dto2Dmat(subsetGrid(x,members = 1)$Data))
xx <- suppressWarnings(array3Dto2Dmat(subsetGrid(x,members = j)$Data))
} else {
xx <- subsetGrid(x,members = 1)$Data
xx <- subsetGrid(x,members = j)$Data
}
xbin <- binaryGrid.(xx, condition = condition, threshold = threshold, partial = partial)
}
Expand Down
7 changes: 4 additions & 3 deletions R/bindGrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ bindGrid <- function(..., dimension = c("member", "time", "lat", "lon"),
bindGrid.member(..., spatial.tolerance = spatial.tolerance)
} else if (dimension == "time") {
bindGrid.time(..., spatial.tolerance = spatial.tolerance)
} else if (dimension == "lat") {
bindGrid.spatial(..., dimesnion = dimension, spatial.tolerance = spatial.tolerance)
} else if (dimension == "lat" | dimension == "lon") {
bindGrid.spatial(..., dimension = dimension, spatial.tolerance = spatial.tolerance)
}
}
#end
Expand Down Expand Up @@ -232,12 +232,13 @@ bindGrid.member <- function(..., spatial.tolerance = 1e-3) {
#' @importFrom abind abind
#' @family internal.helpers
#' @author M Iturbide
#' @export


bindGrid.spatial <- function(..., dimension = c("lat", "lon"), spatial.tolerance = 1e-3) {
dimension <- match.arg(dimension, choices = c("lat", "lon"))
dimsort <- "y"
if(dimension == "lon") dimsort <- "x"
if (dimension == "lon") dimsort <- "x"
grid.list <- list(...)
if (length(grid.list) == 1) {
grid.list <- unlist(grid.list, recursive = FALSE)
Expand Down
57 changes: 43 additions & 14 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,22 +565,30 @@ isRegular <- function(grid) {
gr <- tryCatch({getGrid(grid)}, error = function(err) {grid})
x <- sort(gr$x)
y <- sort(gr$y)
if (length(x) == 1 && length(y) == 1) {
FALSE
if (!is.null(attr(gr, "resX")) && !is.null(attr(gr, "resY"))) {
if (attr(gr, "resX") == 0 && attr(gr, "resY") == 0) {
FALSE
} else {
TRUE
}
} else {
xdists <- lapply(1:(length(x) - 1), function(l) {
x[l + 1] - x[l]
})
ydists <- lapply(1:(length(y) - 1), function(l) {
y[l + 1] - y[l]
})
xa <- sum(unlist(xdists) - unlist(xdists)[1])
ya <- sum(unlist(ydists) - unlist(ydists)[1])
if (any(abs(c(xa, ya)) > 1e-05)) {
FALSE
if (length(x) == 1 && length(y) == 1) {
FALSE
} else {
TRUE
}
xdists <- lapply(1:(length(x) - 1), function(l) {
x[l + 1] - x[l]
})
ydists <- lapply(1:(length(y) - 1), function(l) {
y[l + 1] - y[l]
})
xa <- sum(unlist(xdists) - unlist(xdists)[1])
ya <- sum(unlist(ydists) - unlist(ydists)[1])
if (any(abs(c(xa, ya)) > 1e-05)) {
FALSE
} else {
TRUE
}
}
}
}

Expand Down Expand Up @@ -891,3 +899,24 @@ isGrid <- function(grid) {
}
}


#' Check if object is a multigrid
#'
#'
#' @param grid Input object.
#' @return Logical.
#' @keywords internal
#' @export
#' @author M. Iturbide

isMultigrid <- function(grid) {
if (is.list(grid)) {
if (all(c("Variable", "Data", "xyCoords", "Dates") %in% names(grid))) {
gridDepth(grid$Dates) > 1
} else {
FALSE
}
} else {
FALSE
}
}
117 changes: 117 additions & 0 deletions R/intersectGrid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#' @title Intersection of multiple grids
#' @description Takes multiple input grids and crops the overlapping part
#' @param ... Input grids
#' @param type Character. Options are "temporal" (default) or "spatial".
#' @param which.return Integer of the index to specify which grids in "..." are to be returned.
#' @return The grids indicated in \code{which.return}, encompassing the overlapping time period.
#' @author M Iturbide
#' @family subsetting
#' @seealso \code{\link{subsetGrid}}
#' @export
#' @example {
# data("EOBS_Iberia_tas")
# a <- subsetGrid(EOBS_Iberia_tas, lonLim = c(-8,-1), latLim = c(37, 40))
# b <- subsetGrid(EOBS_Iberia_tas, lonLim = c(-4,3), latLim = c(39, 43))
# z <- intersectGrid(a, b, type = "spatial", which.return = 1)
# }

intersectGrid <- function(..., type = c("temporal", "spatial"), which.return = 1) {
type <- match.arg(type, choices = c("temporal", "spatial"))
if (type == "temporal") {
outgrid <- intersectGrid.time(..., which.return = which.return)
} else if (type == "spatial") {
outgrid <- intersectGrid.spatial(..., which.return = which.return)
} else {
stop("Invalid option for argument 'type'.")
}
return(outgrid)
}

#' @title Temporal intersection of multiple grids
#' @description Takes multiple input grids and crops the overlapping part along time dimension
#' @param ... Input grids
#' @param which.return Integer of the index to specify which grids in "..." are to be returned.
#' @return The grids indicated in \code{which.return}, encompassing the overlapping time period.
#' @importFrom magrittr %<>% %>%
#' @author M Iturbide
#' @family subsetting
#' @seealso \code{\link{checkDim}}, \code{\link{checkSeason}}, \code{\link{getYearsAsINDEX}}, \code{\link{getSeason}}, for other time dimension helpers
#' @export

intersectGrid.time <- function(..., which.return = 1) {
grid.list <- list(...)
if (length(grid.list) < length(which.return)) stop("Wrong value for argument which.return")
ref.dates <- lapply(1:length(grid.list), function(x){
getRefDates(grid.list[[x]]) %>% as.Date(tz = "GMT", format = "%Y-%m-%d")
})
auxDates <- ref.dates[[1]]
for (i in 2:length(grid.list)) {
auxDates <- intersect(auxDates, ref.dates[[i]]) %>% as.Date(origin = "1970-01-01", tz = "GMT", format = "%Y-%m-%d")
}
ind <- lapply(ref.dates, function(x) which(is.element(x, auxDates)))
out <- lapply(1:length(grid.list), function(x) {
out.l <- subsetDimension(grid.list[[x]], dimension = "time", indices = ind[[x]])
seas <- getSeason(out.l)
attr(out.l$Variable, "time_subset") <- "intersectGrid.time"
attr(out.l$Dates, "season") <- seas
out.l
})
out <- out[which.return]
if (length(out) == 1) out <- out[[1]]
return(out)
}



#' @title Temporal intersection of multiple grids
#' @description Takes multiple input grids and crops the overlapping part along time longitude and latitude dimensions
#' @param ... Input grids
#' @param which.return Integer of the index to specify which grids in "..." are to be returned.
#' @return The grids indicated in \code{which.return}, encompassing the overlapping time period.
#' @importFrom magrittr %<>% %>%
#' @author M Iturbide
#' @family subsetting
#' @seealso \code{\link{checkDim}}, \code{\link{checkSeason}}, \code{\link{getYearsAsINDEX}}, \code{\link{getSeason}}, for other time dimension helpers
#' @export

intersectGrid.spatial <- function(..., which.return = 1) {
grid.list <- list(...)
if (length(grid.list) < length(which.return)) stop("Wrong value for argument which.return")
# longitudes
ref.lons <- lapply(1:length(grid.list), function(x){
getCoordinates(grid.list[[x]])$x
})
auxLons <- ref.lons[[1]]
for (i in 2:length(grid.list)) {
auxLons <- intersect(auxLons, ref.lons[[i]])
}
if (length(auxLons) == 0) {
message("Longitude intersection skipped. There are not intersecting longitudes")
} else {
ind <- lapply(ref.lons, function(x) which(is.element(x, auxLons)))
out <- lapply(1:length(grid.list), function(x) {
subsetDimension(grid.list[[x]], dimension = "lon", indices = ind[[x]])
})
grid.list <- out
}
#latitudes
ref.lats <- lapply(1:length(grid.list), function(x){
getCoordinates(grid.list[[x]])$y
})
auxLats <- ref.lats[[1]]
for (i in 2:length(grid.list)) {
auxLats <- intersect(auxLats, ref.lats[[i]])
}
if (length(auxLats) == 0) {
message("Latitude intersection skipped. There are not intersecting latitudes")
out <- grid.list
} else {
ind <- lapply(ref.lats, function(x) which(is.element(x, auxLats)))
out <- lapply(1:length(grid.list), function(x) {
subsetDimension(grid.list[[x]], dimension = "lat", indices = ind[[x]])
})
}
out <- out[which.return]
if (length(out) == 1) out <- out[[1]]
return(out)
}
34 changes: 0 additions & 34 deletions R/intersectGrid.time.R

This file was deleted.

Loading

0 comments on commit 6a6a978

Please sign in to comment.