Unverified Commit 6b8b7cde authored by Dan Baston's avatar Dan Baston
Browse files

Handle another case of flow direction grid adjustment

parent 88080697
Pipeline #375448711 failed with stages
in 89 minutes and 35 seconds
......@@ -33,14 +33,25 @@ adjust_flow_dirs <- function(fd, fd_extent, final_extent, final_dims) {
if (all(fd_extent[1:2] == final_extent[1:2])) {
# do nothing
} else if ((flow_dim[2] == 2*final_dims[2]) &&
all(fd_extent[1:2] == (final_extent[1:2] - flow_dlon))) {
# Flow direction raster is double the resolution of other variables and
# is offset by one half the pixel width of other variables. Rotate a single
# column from the left of the flow grid to the right.
fd <- cbind(fd[, -1, drop = FALSE], fd[, 1, drop = FALSE])
} else if (flow_dim[2] == 2*final_dims[2]) {
if (all(fd_extent[1:2] == (final_extent[1:2] - flow_dlon))) {
# Flow direction raster is double the resolution of other variables and
# is offset by one half the pixel width of other variables. Rotate a single
# column from the left of the flow grid to the right.
fd <- cbind(fd[, -1, drop = FALSE], fd[, 1, drop = FALSE])
fd_extent[1:2] <- final_extent[1:2]
fd_extent[1:2] <- final_extent[1:2]
} else if (all(fd_extent[1:2] == (final_extent[1:2] + flow_dlon))) {
# Flow direction raster is double the resolution of other variables and
# is offset by one half the pixel width of other variables. Rotate a single
# column from the right of the flow grid to the left.
fd <- cbind(fd[, ncol(fd), drop = FALSE],
fd[, -ncol(fd), drop = FALSE])
fd_extent[1:2] <- final_extent[1:2]
} else {
stop("Unable to adjust flow direction matrix to desired extent")
}
} else {
stop("Unable to adjust flow direction matrix to desired extent")
}
......
......@@ -233,7 +233,7 @@ test_that('Direction matrix can be integer multiple of flow matrix', {
)
})
test_that('Global 0.125-degree flow direction grid can be shifted and extended to match 0.25-degree ERA5 grid', {
test_that('Global 0.125-degree flow direction grid can be shifted and extended to match +/- 180 0.25-degree ERA5 grid', {
flowdir <- matrix(1L:(2880L*1120L), nrow=1120)
flowdir_extent <- c(-180, 180, -56, 84)
......@@ -260,6 +260,24 @@ test_that('Global 0.125-degree flow direction grid can be shifted and extended t
expect_equal(na.omit(flowdir_adj[, 2880]), 1:1120, check.attributes = FALSE)
})
test_that('Global 0.125-degree flow direction grid can be shifted and extended to match 0-360 0.25-degree ERA5 grid', {
flowdir <- matrix(1L:(2880L*1120L), nrow=1120)
flowdir_extent <- c(0, 360, -56, 84)
era5_extent <- c(-0.125, 359.875, -90.125, 90.125)
era5_dims <- c(721, 1440)
flowdir_adj <- adjust_flow_dirs(flowdir, flowdir_extent, era5_extent, era5_dims)
expect_equal(dim(flowdir_adj), 2 * era5_dims)
# right column of flowdir grid moved to left
# (western extent moved from 0 to -0.125)
expect_equal(na.omit(flowdir_adj[, 1]), flowdir[, ncol(flowdir)], check.attributes = FALSE)
expect_equal(na.omit(flowdir_adj[, 2]), flowdir[, 1], check.attributes = FALSE)
expect_equal(na.omit(flowdir_adj[, ncol(flowdir_adj)]), flowdir[, ncol(flowdir) - 1], check.attributes = FALSE)
})
test_that('adjust_flow_dirs is a no-op when grids are the same', {
flowdir <- matrix(1L:(360L*720L), nrow=360)
flowdir_extent <- c(-180, 180, -90, 90)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment