From e3759c94eea0b690b0842796d4558cf56c6c0993 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 25 Nov 2021 18:20:24 +0100 Subject: [PATCH 1/2] Add unit test for reordering when selector is 'all'; no transform --- tests/testthat/test-Start-reorder_all.R | 146 ++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 tests/testthat/test-Start-reorder_all.R diff --git a/tests/testthat/test-Start-reorder_all.R b/tests/testthat/test-Start-reorder_all.R new file mode 100644 index 0000000..b8279de --- /dev/null +++ b/tests/testthat/test-Start-reorder_all.R @@ -0,0 +1,146 @@ +# No transform, test reorder function Sort() and CircularSort() with selector 'all'. + + +context("No transform, reorder test: 'all'") + +#--------------------------------------------------------------- +# cdo is used to verify the data values +library(easyNCDF) +path <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20000101.nc" +file <- NcOpen(path) +arr <- NcToArray(file, + dim_indices = list(time = 1, ensemble = 1, + latitude = 1:640, longitude = 1:1296), + vars_to_read = 'tas') + +#lat is from 90 to -90. +#lats <- NcToArray(file, +# dim_indices = list(latitude = 1:640), vars_to_read = 'latitude') +#lon is from 0 to 360. +#lons <- NcToArray(file, +# dim_indices = list(longitude = 1:1296), vars_to_read = 'longitude') +NcClose(file) +#--------------------------------------------------------------- + +path <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc' + +test_that("1. lat", { +# lon_reorder = CircularSort(0, 360) + +# lat should be ascending +suppressWarnings( +res1 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = 'all', + latitude_reorder = Sort(), + longitude = 'all', + longitude_reorder = CircularSort(0, 360), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'sdate'), + retrieve = T) +) + +# Because Sort() is not specified, it follows the original order (descending). +suppressWarnings( +res2 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = 'all', +# latitude_reorder = Sort(), + longitude = 'all', + longitude_reorder = CircularSort(0, 360), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = NULL, #'dat', + longitude = NULL, #'dat', + time = 'sdate'), + retrieve = T) +) + +# lat should be descending +suppressWarnings( +res3 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = 'all', + latitude_reorder = Sort(decreasing = T), + longitude = 'all', + longitude_reorder = CircularSort(0, 360), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'sdate'), + retrieve = T) +) + +# longitude_reorder = CircularSort(-180, 180) +suppressWarnings( +res4 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = 'all', + latitude_reorder = Sort(decreasing = T), + longitude = 'all', + longitude_reorder = CircularSort(-180, 180), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'sdate'), + retrieve = T) +) + +expect_equal( +all(diff(attr(res1, 'Variables')$dat1$latitude) > 0), +TRUE +) +expect_equal( +as.vector(drop(res1)[640:1, ]), +as.vector(arr), +tolerance = 0.0001 +) +expect_equal( +all(diff(attr(res2, 'Variables')$common$latitude) < 0), +TRUE +) +expect_equal( +as.vector(res2), +as.vector(arr), +tolerance = 0.0001 +) +expect_equal( +all(diff(attr(res3, 'Variables')$dat1$latitude) < 0), +TRUE +) +expect_equal( +as.vector(res3), +as.vector(arr), +tolerance = 0.0001 +) + +expect_equal( +range(attr(res4, 'Variables')$dat1$longitude), +c(-180, 179.7222), +tolerance = 0.0001 +) +expect_equal( +as.vector(drop(res4)[, c(649:1296, 1:648)]), +as.vector(arr), +tolerance = 0.0001 +) + +}) + -- GitLab From ee049b262b8accacd587bfabdc50f658b63a6e29 Mon Sep 17 00:00:00 2001 From: aho Date: Tue, 30 Nov 2021 12:51:01 +0100 Subject: [PATCH 2/2] Add unit test for reordering indices(); no transform --- tests/testthat/test-Start-reorder_indices.R | 145 ++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 tests/testthat/test-Start-reorder_indices.R diff --git a/tests/testthat/test-Start-reorder_indices.R b/tests/testthat/test-Start-reorder_indices.R new file mode 100644 index 0000000..b2ca0ac --- /dev/null +++ b/tests/testthat/test-Start-reorder_indices.R @@ -0,0 +1,145 @@ +# No transform, test reorder function Sort() and CircularSort() with selector indices(). + +context("No transform, reorder test: indices()") + +#--------------------------------------------------------------- +# cdo is used to verify the data values +library(easyNCDF) +path <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20000101.nc" +file <- NcOpen(path) +arr <- NcToArray(file, + dim_indices = list(time = 1, ensemble = 1, + latitude = 1:640, longitude = 1:1296), + vars_to_read = 'tas') + +#lat is from 90 to -90. +#lats <- NcToArray(file, +# dim_indices = list(latitude = 1:640), vars_to_read = 'latitude') +#lon is from 0 to 360. +#lons <- NcToArray(file, +# dim_indices = list(longitude = 1:1296), vars_to_read = 'longitude') +NcClose(file) +#--------------------------------------------------------------- + +path <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc' + +test_that("1. lat", { +# lon_reorder = CircularSort(0, 360) + +# lat should be ascending +suppressWarnings( +res1 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = indices(1:640), + latitude_reorder = Sort(), + longitude = indices(1:1296), + longitude_reorder = CircularSort(0, 360), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'sdate'), + retrieve = T) +) + +# Because Sort() is not specified, it follows the original order (descending). +suppressWarnings( +res2 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = indices(1:640), +# latitude_reorder = Sort(), + longitude = indices(1:1296), + longitude_reorder = CircularSort(0, 360), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = NULL, #'dat', + longitude = NULL, #'dat', + time = 'sdate'), + retrieve = T) +) + +# lat should be descending +suppressWarnings( +res3 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = indices(1:640), + latitude_reorder = Sort(decreasing = T), + longitude = indices(1:1296), + longitude_reorder = CircularSort(0, 360), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'sdate'), + retrieve = T) +) + +# longitude_reorder = CircularSort(-180, 180) +suppressWarnings( +res4 <- Start(dat = path, + var = 'tas', + sdate = '20000101', + ensemble = indices(1), + time = indices(1), + latitude = indices(1:640), + latitude_reorder = Sort(decreasing = T), + longitude = indices(1:1296), + longitude_reorder = CircularSort(-180, 180), + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('longitude', 'lon')), + return_vars = list(latitude = 'dat', + longitude = 'dat', + time = 'sdate'), + retrieve = T) +) + +expect_equal( +all(diff(attr(res1, 'Variables')$dat1$latitude) > 0), +TRUE +) +expect_equal( +as.vector(drop(res1)[640:1, ]), +as.vector(arr), +tolerance = 0.0001 +) +expect_equal( +all(diff(attr(res2, 'Variables')$common$latitude) < 0), +TRUE +) +expect_equal( +as.vector(res2), +as.vector(arr), +tolerance = 0.0001 +) +expect_equal( +all(diff(attr(res3, 'Variables')$dat1$latitude) < 0), +TRUE +) +expect_equal( +as.vector(res3), +as.vector(arr), +tolerance = 0.0001 +) + +expect_equal( +range(attr(res4, 'Variables')$dat1$longitude), +c(-180, 179.7222), +tolerance = 0.0001 +) +expect_equal( +as.vector(drop(res4)[, c(649:1296, 1:648)]), +as.vector(arr), +tolerance = 0.0001 +) + +}) + -- GitLab