Skip to content

Commit fc3de2b

Browse files
committed
add environment api to enable cdse tests
1 parent dddb70b commit fc3de2b

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Collate:
131131
'api_data.R'
132132
'api_debug.R'
133133
'api_download.R'
134+
'api_environment.R'
134135
'api_factory.R'
135136
'api_file_info.R'
136137
'api_file.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ S3method(.source_collection_access_test,mpc_cube)
131131
S3method(.source_collection_access_test,stac_cube)
132132
S3method(.source_collection_access_test,usgs_cube)
133133
S3method(.source_cube,stac_cube)
134+
S3method(.source_filter_tiles,"cdse_cube_sentinel-1-rtc")
134135
S3method(.source_filter_tiles,"mpc_cube_sentinel-1-grd")
135136
S3method(.source_filter_tiles,stac_cube)
136137
S3method(.source_item_get_bands,stac_cube)

R/api_environment.R

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
# ---- Environment operations ----
3+
#' @title Function to patch environment variables (Developer only).
4+
#' @keywords internal
5+
#' @noRd
6+
#'
7+
#' @description
8+
#' This function patches environment variables, swapping them from a `source`
9+
#' to a `target` variable. You can rollback the swap operation using the
10+
#' `.environment_rollback.`
11+
#'
12+
#' @note
13+
#' This function is suitable only for internal `sits` tests and should not be
14+
#' used for any other purpose.
15+
#' @param env_config List with the configuration of the environment.
16+
#' @return Called for side effects.
17+
.environment_patch <- function(env_config) {
18+
env_prefix <- env_config[["name"]]
19+
env_variables <- env_config[["variables"]]
20+
21+
purrr::map(1:length(env_variables), function(var_idx) {
22+
var_source <- names(env_variables)[[var_idx]]
23+
var_target <- unname(env_variables)[[var_idx]]
24+
# Get current value of the target variable
25+
var_target_value <- Sys.getenv(var_target)
26+
# Save current value of the target variable in the "swap area"
27+
var_target_swap <- paste(env_prefix, var_target, sep = "_SWAP_")
28+
var_target_swap_value <- list(tmp = var_target_value)
29+
var_target_swap_value <- stats::setNames(
30+
var_target_swap_value,
31+
var_target_swap
32+
)
33+
# Save variable
34+
do.call(Sys.setenv, var_target_swap_value)
35+
# Get the new value of the target variable
36+
var_source_value <- Sys.getenv(var_source)
37+
# Save new value in the target variable
38+
var_target_new_value <- list(tmp = var_source_value)
39+
var_target_new_value <- stats::setNames(
40+
var_target_new_value,
41+
var_target
42+
)
43+
# Save variable
44+
do.call(Sys.setenv, var_target_new_value)
45+
})
46+
return(invisible(NULL))
47+
}
48+
49+
#' @title Function to rollback patch in environment variables (Developer only).
50+
#' @keywords internal
51+
#' @noRd
52+
#'
53+
#' @description
54+
#' This function rollback patches in environment variables created with the
55+
#' function `.environment_patch`.
56+
#' @note
57+
#' This function is suitable only for internal `sits` tests and should not be
58+
#' used for any other purpose.
59+
#' @param env_config List with the configuration of the environment.
60+
#' @return Called for side effects.
61+
.environment_rollback <- function(env_config) {
62+
env_prefix <- env_config[["name"]]
63+
env_variables <- env_config[["variables"]]
64+
65+
purrr::map(1:length(env_variables), function(var_idx) {
66+
var_source <- names(env_variables)[[var_idx]]
67+
var_target <- unname(env_variables)[[var_idx]]
68+
# Get current value of the target variable
69+
var_target_value <- Sys.getenv(var_target)
70+
# Save current value in the source variable
71+
var_source_new_value <- list(tmp = var_target_value)
72+
var_source_new_value <- stats::setNames(
73+
var_source_new_value,
74+
var_source
75+
)
76+
do.call(Sys.setenv, var_source_new_value)
77+
# Get current value of the target variable in the "swap area"
78+
var_target_swap <- paste(env_prefix, var_target, sep = "_SWAP_")
79+
var_target_swap_value <- Sys.getenv(var_target_swap)
80+
# Prepare current target
81+
var_target_swap_value <- list(tmp = var_target_swap_value)
82+
var_target_swap_value <- stats::setNames(
83+
var_target_swap_value,
84+
var_target
85+
)
86+
do.call(Sys.setenv, var_target_swap_value)
87+
})
88+
return(invisible(NULL))
89+
}
90+
91+
# ---- Environment configurations ----
92+
#' @title Function to create patch configuration for the CDSE source.
93+
#' @keywords internal
94+
#' @noRd
95+
#'
96+
#' @description
97+
#' This function creates a configuration for the `patch` and `rollback`
98+
#' specialized for the CDSE requirements.
99+
#' @note
100+
#' This function is suitable only for internal `sits` tests and should not be
101+
#' used for any other purpose.
102+
#' @return List with the configuration of the CDSE environment.
103+
.environment_cdse <- function() {
104+
# Define environment name. This name is used as a prefix for the
105+
# "swap area" which is used to keep values saved
106+
env_name <- "CDSE"
107+
# Create the environment variables
108+
env_variables <- list(
109+
"CDSE_ACCESS_KEY_ID" = "AWS_ACCESS_KEY_ID",
110+
"CDSE_SECRET_ACCESS_KEY" = "AWS_SECRET_ACCESS_KEY",
111+
"CDSE_S3_ENDPOINT" = "AWS_S3_ENDPOINT",
112+
"CDSE_VIRTUAL_HOSTING" = "AWS_VIRTUAL_HOSTING"
113+
)
114+
# Create a base environment definition
115+
env_definition <-
116+
list(name = env_name, variables = env_variables)
117+
# Define a class, based on the name
118+
class(env_definition) <- c(env_name, "list")
119+
# Done!
120+
env_definition
121+
}

tests/testthat/test-cube-cdse.R

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
test_that("Creating S2 cubes from CDSE with ROI", {
2+
# Configure environment
3+
cdse_env_config <- .environment_cdse()
4+
# Patch environment variables
5+
.environment_patch(cdse_env_config)
6+
# Test
7+
roi <- c(
8+
lon_min = -48.28579, lat_min = -16.05026,
9+
lon_max = -47.30839, lat_max = -15.50026
10+
)
11+
s2_cube_cdse <- .try(
12+
{
13+
sits_cube(
14+
source = "CDSE",
15+
collection = "SENTINEL-2-L2A",
16+
roi = roi,
17+
bands = c("B05", "CLOUD"),
18+
start_date = as.Date("2018-07-18"),
19+
end_date = as.Date("2018-08-23"),
20+
progress = FALSE,
21+
multicores = 1L
22+
)
23+
},
24+
.default = NULL
25+
)
26+
testthat::skip_if(purrr::is_null(s2_cube_cdse), "CDSE is not accessible")
27+
expect_true(all(sits_bands(s2_cube_cdse) %in% c("B05", "CLOUD")))
28+
expect_equal(nrow(s2_cube_cdse), 3)
29+
bbox_cube <- sits_bbox(s2_cube_cdse, as_crs = "EPSG:4326")
30+
bbox_cube_1 <- sits_bbox(.tile(s2_cube_cdse), as_crs = "EPSG:4326")
31+
expect_true(bbox_cube["xmax"] >= bbox_cube_1["xmax"])
32+
expect_true(bbox_cube["ymax"] >= bbox_cube_1["ymax"])
33+
r_obj <- .raster_open_rast(s2_cube_cdse$file_info[[1]]$path[1])
34+
cube_nrows <- .tile_nrows(s2_cube_cdse)
35+
expect_true(.raster_nrows(r_obj) == cube_nrows)
36+
# Rollback environment changes
37+
.environment_rollback(cdse_env_config)
38+
})
39+
test_that("Creating S2 cubes from CDSE with tiles", {
40+
# Configure environment
41+
cdse_env_config <- .environment_cdse()
42+
# Patch environment variables
43+
.environment_patch(cdse_env_config)
44+
# Test
45+
s2_cube <- .try(
46+
{
47+
sits_cube(
48+
source = "CDSE",
49+
collection = "SENTINEL-2-L2A",
50+
tiles = "20LKP",
51+
bands = c("B05", "CLOUD"),
52+
start_date = as.Date("2018-07-18"),
53+
end_date = as.Date("2018-08-23"),
54+
progress = FALSE,
55+
multicores = 1L
56+
)
57+
},
58+
.default = NULL
59+
)
60+
expect_true(all(sits_bands(s2_cube) %in% c("B05", "CLOUD")))
61+
r <- .raster_open_rast(.tile_path(s2_cube))
62+
expect_equal(s2_cube$xmax[[1]], .raster_xmax(r), tolerance = 1)
63+
expect_equal(s2_cube$xmin[[1]], .raster_xmin(r), tolerance = 1)
64+
r_obj <- .raster_open_rast(s2_cube$file_info[[1]]$path[1])
65+
cube_nrows <- .tile_nrows(s2_cube)
66+
expect_true(.raster_nrows(r_obj) == cube_nrows)
67+
# Rollback environment changes
68+
.environment_rollback(cdse_env_config)
69+
})
70+
71+
test_that("Creating Sentinel-1 RTC cubes from CDSE", {
72+
# Configure environment
73+
cdse_env_config <- .environment_cdse()
74+
# Patch environment variables
75+
.environment_patch(cdse_env_config)
76+
# Test
77+
cube_s1_rtc <- sits_cube(
78+
source = "CDSE",
79+
collection = "SENTINEL-1-RTC",
80+
bands = c("VV"),
81+
orbit = "descending",
82+
tiles = c("36NWH"),
83+
start_date = "2021-07-01",
84+
end_date = "2021-09-30",
85+
multicores = 1L
86+
)
87+
bbox <- sits_bbox(cube_s1_rtc[1,])
88+
expect_true(grepl("4326", bbox[["crs"]]))
89+
expect_equal(32, bbox[["xmin"]])
90+
expect_equal(34, bbox[["xmax"]])
91+
expect_equal(nrow(cube_s1_rtc$file_info[[1]]), 68)
92+
93+
output_dir <- paste0(tempdir(), "/s1rtcreg")
94+
if (!dir.exists(output_dir)) {
95+
dir.create(output_dir)
96+
}
97+
98+
cube_s1_rtc_reg <- sits_regularize(
99+
cube = cube_s1_rtc,
100+
period = "P1M",
101+
res = 240,
102+
tiles = c("36NXG", "36NVG"),
103+
multicores = 1,
104+
output_dir = output_dir,
105+
progress = TRUE
106+
)
107+
expect_equal(length(sits_timeline(cube_s1_rtc_reg)), 3)
108+
expect_true(all(c("36NXG", "36NVG") %in%
109+
cube_s1_rtc_reg$tile))
110+
expect_true("EPSG:32636" %in% cube_s1_rtc_reg$crs)
111+
112+
bbox <- sits_bbox(cube_s1_rtc_reg, as_crs = "EPSG:4326")
113+
roi_cube_s1 <- sits_mgrs_to_roi(c("36NXG", "36NVG"))
114+
115+
expect_equal(bbox[["xmin"]], roi_cube_s1[["lon_min"]], tolerance = 0.01)
116+
expect_equal(bbox[["xmax"]], roi_cube_s1[["lon_max"]], tolerance = 0.03)
117+
expect_equal(bbox[["ymin"]], roi_cube_s1[["lat_min"]], tolerance = 0.25)
118+
expect_equal(bbox[["ymax"]], roi_cube_s1[["lat_max"]], tolerance = 0.01)
119+
expect_true(all(c("VV") %in% sits_bands(cube_s1_rtc_reg)))
120+
121+
# Rollback environment changes
122+
.environment_rollback(cdse_env_config)
123+
})

tests/testthat/test-environment.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test_that("Test environment variables swap", {
2+
# Setup - Environment configuration
3+
env_config <- list(
4+
name = "SITS_TEST",
5+
variables = list(
6+
"A" = "B",
7+
"C" = "D"
8+
)
9+
)
10+
# Setup - Env variables
11+
Sys.setenv("A" = 123)
12+
Sys.setenv("B" = 321)
13+
Sys.setenv("C" = "ABC")
14+
Sys.setenv("D" = "CBA")
15+
# Test - Swapped variables
16+
.environment_patch(env_config)
17+
18+
expect_equal(Sys.getenv("B"), "123")
19+
expect_equal(Sys.getenv("D"), "ABC")
20+
expect_equal(Sys.getenv("SITS_TEST_SWAP_B"), "321")
21+
expect_equal(Sys.getenv("SITS_TEST_SWAP_D"), "CBA")
22+
# Test - Rollback variables
23+
.environment_rollback(env_config)
24+
25+
expect_equal(Sys.getenv("B"), "321")
26+
expect_equal(Sys.getenv("D"), "CBA")
27+
})

0 commit comments

Comments
 (0)