diff --git a/R/CDORemap.R b/R/CDORemap.R index ecee32d9b89a7762d81b47dee736c1dbf95190bc..0b0950520f6f0a24a1eecc398ee66cc81da08a10 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -476,7 +476,9 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, # The signif is needed because cdo sellonlatbox crashes with too many digits lon_extremes[1] <- tmp_lon[1] - first_lon_cell_width / 2 } else { - lon_extremes[1] <- min(tmp_lon) + next_lon <- predict(lon_model, data.frame(i = length(tmp_lon) + 1)) + last_lon_cell_width <- (next_lon - tmp_lon[length(tmp_lon)]) + lon_extremes[1] <- tmp_lon[length(tmp_lon)] + last_lon_cell_width / 2 } if (which.max(tmp_lon) == length(tmp_lon)) { right_is_max <- TRUE @@ -484,8 +486,12 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, last_lon_cell_width <- (next_lon - tmp_lon[length(tmp_lon)]) lon_extremes[2] <- tmp_lon[length(tmp_lon)] + last_lon_cell_width / 2 } else { - lon_extremes[2] <- max(tmp_lon) + prev_lon <- predict(lon_model, data.frame(i = 0)) + first_lon_cell_width <- (tmp_lon[1] - prev_lon) + # The signif is needed because cdo sellonlatbox crashes with too many digits + lon_extremes[2] <- tmp_lon[1] - first_lon_cell_width / 2 } + 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) { @@ -536,14 +542,17 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, prev_lat <- predict(lat_model, data.frame(i = 0)) lat_extremes[1] <- tmp_lat[1] - (tmp_lat[1] - prev_lat) / 2 } else { - lat_extremes[1] <- min(tmp_lat) + next_lat <- predict(lat_model, data.frame(i = length(tmp_lat) + 1)) + lat_extremes[1] <- tmp_lat[length(tmp_lat)] + (next_lat - tmp_lat[length(tmp_lat)]) / 2 } if (which.max(tmp_lat) == length(tmp_lat)) { next_lat <- predict(lat_model, data.frame(i = length(tmp_lat) + 1)) lat_extremes[2] <- tmp_lat[length(tmp_lat)] + (next_lat - tmp_lat[length(tmp_lat)]) / 2 } else { - lat_extremes[2] <- max(tmp_lat) + prev_lat <- predict(lat_model, data.frame(i = 0)) + lat_extremes[2] <- tmp_lat[1] - (tmp_lat[1] - prev_lat) / 2 } + lat_extremes <- round(lat_extremes, 10) ## lat_extremes <- signif(lat_extremes, 5) # Adjust crop window if (lat_extremes[1] < -90) { diff --git a/tests/testthat/test-CDORemap.R b/tests/testthat/test-CDORemap.R index 5492d51f6b3611e04b293b2922750636aa23a502..c8f74f2cab703df8baaefc3aeaa789f8bbd3457e 100644 --- a/tests/testthat/test-CDORemap.R +++ b/tests/testthat/test-CDORemap.R @@ -37,9 +37,15 @@ data4_1 <- drop(data4) data4_2 <- ClimProjDiags::Subset(data4, c(1,2,3,4,8), list(1,1,1,1,1), drop = 'selected') data4_3 <- .aperm2(data4_2, c(1, 3, 2)) - # data5: regular grid, more dimensions - data5 <- array(1:(4*10*8*3*2), dim = c(dat = 1, var = 1, memb = 4, lon = 10, lat = 8, sdate = 3, sweek = 2)) - data5_1 <- aperm(data5, c(1,2,3,6,4,5,7)) +# data5: regular grid, more dimensions +data5 <- array(1:(4*10*8*3*2), dim = c(dat = 1, var = 1, memb = 4, lon = 10, lat = 8, sdate = 3, sweek = 2)) +data5_1 <- aperm(data5, c(1,2,3,6,4,5,7)) + +# data6: regular grid, latitudes and longitudes in descent order +data6 <- rnorm(3*180*360) +dim(data6) <- c(time = 3, latitude = 180, longitude = 360) +lats6 <- seq(89.5, -89.5, -1) +lons6 <- seq(359.5,0.5, -1) ############################################## @@ -311,3 +317,34 @@ as.vector(res5_1$lats), ) }) +############################################################ + +test_that("7. dat5: regular regrid, descent order", { + suppressWarnings( + res6 <- CDORemap(data6, lats = lats6, lons = lons6, grid = 'r360x181', method = 'bil') + ) + expect_equal( + length(res6$lons), + 360 + ) + expect_equal( + length(res6$lats), + 181 + ) + expect_equal( + min(res6$lons), + 0 + ) + expect_equal( + max(res6$lons), + 359 + ) + expect_equal( + min(res6$lats), + -90 + ) + expect_equal( + max(res6$lats), + 90 + ) +})