From 43adf5b6000ec22494c0d6ca245d1da8a740b81e Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Tue, 9 Jan 2024 16:31:25 +0100 Subject: [PATCH 1/2] Relax condition for crop = T adding tolerance in the condition --- R/CDORemap.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/R/CDORemap.R b/R/CDORemap.R index 0b09505..a95f6ec 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -491,19 +491,20 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, # The signif is needed because cdo sellonlatbox crashes with too many digits lon_extremes[2] <- tmp_lon[1] - first_lon_cell_width / 2 } + tolerance <- 1e-10 lon_extremes <- round(lon_extremes, 10) # Adjust the crop window if possible in order to keep lons from 0 to 360 # or from -180 to 180 when the extremes of the cropped window are contiguous. if (right_is_max) { if (lon_extremes[1] < -180) { if (!((lon_extremes[2] < 180) && - !((180 - lon_extremes[2]) <= last_lon_cell_width / 2))) { + !(abs((180 - lon_extremes[2]) - last_lon_cell_width / 2) <= tolerance))) { lon_extremes[1] <- -180 lon_extremes[2] <- 180 } } else if (lon_extremes[1] < 0) { if (!((lon_extremes[2] < 360) && - !((360 - lon_extremes[2]) <= last_lon_cell_width / 2))) { + !(abs((360 - lon_extremes[2]) - last_lon_cell_width / 2) <= tolerance))) { lon_extremes[1] <- 0 lon_extremes[2] <- 360 } @@ -512,13 +513,13 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, if (left_is_min) { if (lon_extremes[2] > 360) { if (!((lon_extremes[1] > 0) && - !(lon_extremes[1] <= first_lon_cell_width / 2))) { + !(abs(lon_extremes[1] - first_lon_cell_width / 2) <= tolerance))) { lon_extremes[1] <- 0 lon_extremes[2] <- 360 } } else if (lon_extremes[2] > 180) { if (!((lon_extremes[1] > -180) && - !((180 + lon_extremes[1]) <= first_lon_cell_width / 2))) { + !(abs((180 + lon_extremes[1]) - first_lon_cell_width / 2) <= tolerance))) { lon_extremes[1] <- -180 lon_extremes[2] <- 180 } -- GitLab From 49785ea19afeb630c0aeeb8f8ffd4f357c7da145 Mon Sep 17 00:00:00 2001 From: EVA RIFA ROVIRA Date: Mon, 15 Jan 2024 11:46:35 +0100 Subject: [PATCH 2/2] Add unit test for crop = T, R 4.2.1 predict() different value --- tests/testthat/test-CDORemap.R | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-CDORemap.R b/tests/testthat/test-CDORemap.R index c8f74f2..81d4964 100644 --- a/tests/testthat/test-CDORemap.R +++ b/tests/testthat/test-CDORemap.R @@ -47,6 +47,12 @@ dim(data6) <- c(time = 3, latitude = 180, longitude = 360) lats6 <- seq(89.5, -89.5, -1) lons6 <- seq(359.5,0.5, -1) +# data7: regular grid, crop = T +data7 <- array(1:50, dim = c(25, 50)) +names(dim(data7)) <- c('lat', 'lon') +lons7 <- seq(0, 360 - 360/50, length.out = 50) +lats7 <- seq(-90, 90, length.out = 25) + ############################################## test_that("1. Input checks", { @@ -319,7 +325,7 @@ as.vector(res5_1$lats), ############################################################ -test_that("7. dat5: regular regrid, descent order", { +test_that("7. data6: regular regrid, descent order", { suppressWarnings( res6 <- CDORemap(data6, lats = lats6, lons = lons6, grid = 'r360x181', method = 'bil') ) @@ -348,3 +354,34 @@ test_that("7. dat5: regular regrid, descent order", { 90 ) }) + +############################################################ + +test_that("8. data7: regular grid, crop = T, global", { + suppressWarnings( + res7 <- CDORemap(data7, lons7, lats7, 't170grid', 'bil', TRUE) + ) + res_lon7 <- res7$lons + res_lat7 <- res7$lats + expect_equal( + dim(res7$data_array), + c(lat = 256, lon = 512) + ) + expect_equal( + as.vector(res7$lats)[1:10], + c(89.46282, 88.76695, 88.06697, 87.36606, 86.66480, 85.96337, 85.26185, + 84.56026, 83.85864, 83.15699), + tolerance = 0.000001 + ) + expect_equal( + as.vector(res7$lons)[1:10], + c(0.000000, 0.703125, 1.406250, 2.109375, 2.812500, 3.515625, 4.218750, + 4.921875, 5.625000, 6.328125), + tolerance = 0.000001 + ) + expect_equal( + c(min(res_lon7), max(res_lon7), min(res_lat7), max(res_lat7)), + c(0.00000, 359.29688, -89.46282, 89.46282), + tolerance = 0.000001 + ) +}) -- GitLab