From f2248cfb62e7b7f2382cff99c9caa80e3836d175 Mon Sep 17 00:00:00 2001 From: vagudets Date: Mon, 7 Jul 2025 09:58:52 +0200 Subject: [PATCH 1/3] Save character arrays --- R/ArrayToNc.R | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/R/ArrayToNc.R b/R/ArrayToNc.R index 6952edc..55d7057 100644 --- a/R/ArrayToNc.R +++ b/R/ArrayToNc.R @@ -231,7 +231,8 @@ ArrayToNc <- function(arrays, file_path) { if (is.array(arrays)) { arrays <- list(arrays) } - if (any(!sapply(arrays, function(x) is.array(x) && (is.numeric(x) || is.logical(x))))) { + if (any(!sapply(arrays, function(x) is.array(x) && + (is.numeric(x) || is.logical(x) || is.character(x))))) { stop("The parameter 'arrays' must be one or a list of numeric or logical arrays.") } # Check parameter file_path. @@ -444,6 +445,7 @@ ArrayToNc <- function(arrays, file_path) { var_info[['prec']] <- 'short' } else if (typeof(arrays[[i]]) == 'character') { var_info[['prec']] <- 'char' + attr(arrays[[i]], "variables")[[var_info[['name']]]] <- list(prec = 'char') } else if (typeof(arrays[[i]]) == 'integer') { var_info[['prec']] <- 'integer' } else { @@ -455,6 +457,14 @@ ArrayToNc <- function(arrays, file_path) { } var_info[['prec']] <- var_info[['prec']][1] } + + if (var_info[['prec']] == 'char') { + dimnchar_name <- paste0(var_info[['name']], "_length") + dimnchar <- ncdim_def(dimnchar_name, "", 1:max(sapply(arrays[[i]], nchar)), + create_dimvar = FALSE) + defined_dims[[dimnchar_name]] <- dimnchar + var_built_dims <- c(dimnchar_name, var_built_dims) + } new_var <- list(ncvar_def(var_info[['name']], var_info[['units']], defined_dims[var_built_dims], var_info[['missval']], @@ -502,14 +512,17 @@ ArrayToNc <- function(arrays, file_path) { } add_offset <- var_info[['addOffset']][1] } - if (is.null(var_dim)) { + if (!is.null(var_info[['prec']]) && var_info[['prec']] == 'char') { + ncvar_put(ncdf_object, defined_vars[[var_counter]]$name, + arrays[[i]]) + } else if (is.null(var_dim)) { if (scale_factor != 1 || add_offset != 0) { ncvar_put(ncdf_object, defined_vars[[var_counter]]$name, (arrays[[i]] - add_offset) / scale_factor, - count = dim(arrays[[i]])) + count = dim(arrays[[i]])) } else { ncvar_put(ncdf_object, defined_vars[[var_counter]]$name, - arrays[[i]], + arrays[[i]], count = dim(arrays[[i]])) } } else { -- GitLab From dc65deb0e1239ae0ddff8ece8f73dd150a0b349f Mon Sep 17 00:00:00 2001 From: vagudets Date: Mon, 7 Jul 2025 10:19:43 +0200 Subject: [PATCH 2/3] unignore tests --- .Rbuildignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.Rbuildignore b/.Rbuildignore index 4f5040a..1dfae08 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,5 +7,5 @@ .*^(?!inst)\.nc$ .gitlab-ci.yml # unit tests should be ignored when building the package for CRAN -^tests$ +# ^tests$ ^CONTRIBUTING\.md$ -- GitLab From ec0ad7c417fb9743afe3f4eb9673a9e31639e0c9 Mon Sep 17 00:00:00 2001 From: vagudets Date: Mon, 7 Jul 2025 14:33:24 +0200 Subject: [PATCH 3/3] Include creation of out_dir in unit test --- tests/testthat/test-ArrayToNc.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-ArrayToNc.R b/tests/testthat/test-ArrayToNc.R index 4a660d0..c5fdd0c 100644 --- a/tests/testthat/test-ArrayToNc.R +++ b/tests/testthat/test-ArrayToNc.R @@ -1,5 +1,6 @@ -out_dir <- "./tests/" +out_dir <- "./tests-output/" +dir.create(out_dir, showWarnings = FALSE) ################################### test_that("1. dat1", { -- GitLab