From 4a83aeaa359f788cc6c4031134ebb32d167d19d7 Mon Sep 17 00:00:00 2001 From: aho Date: Tue, 9 Mar 2021 13:16:19 +0100 Subject: [PATCH 1/2] Fix the time unit attribute retrieval if the format is YYYY-M-D. Also, split_multiselected_dims is changed to FALSE if it is useless. --- R/NcDataReader.R | 7 +++++++ R/Start.R | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/R/NcDataReader.R b/R/NcDataReader.R index 741ffbb..ebc58fc 100644 --- a/R/NcDataReader.R +++ b/R/NcDataReader.R @@ -221,6 +221,13 @@ NcDataReader <- function(file_path = NULL, file_object = NULL, # Origin year and month ori_year <- as.numeric(substr(parts[2], 1, 4)) ori_month <- as.numeric(substr(parts[2], 6, 7)) + if (is.na(ori_month)) { + ori_month <- as.numeric(substr(parts[2], 6, 6)) + } + if (!is.numeric(ori_year) | !is.numeric(ori_month)) { + stop(paste0("The time unit attribute format is not 'YYYY-MM-DD' or 'YYYY-M-D'. ", + "Check the file or contact the maintainer.")) + } if (calendar == 'gregorian') { # Find how many years + months diff --git a/R/Start.R b/R/Start.R index 80e1c83..d15fdb7 100644 --- a/R/Start.R +++ b/R/Start.R @@ -3647,6 +3647,9 @@ Start <- function(..., # dim = indices/selectors, } } } + #========================================================================= + # Find the dimension to split if split_multiselected_dims = TRUE. + # If there is no dimension able to be split, change split_multiselected_dims to FALSE. all_split_dims <- NULL if (split_multiselected_dims) { for (dim_param in 1:length(dim_params)) { @@ -3678,6 +3681,13 @@ Start <- function(..., # dim = indices/selectors, } } } + if (is.null(all_split_dims)) { + split_multiselected_dims <- FALSE + .warning(paste0("Not found any dimensions able to be split. The parameter ", + "'split_multiselected_dims' is changed to FALSE.")) + } + #====================================================================== + if (merge_across_dims_narm) { # only merge_across_dims -> the 'time' dim length needs to be adjusted across_inner_dim <- inner_dims_across_files[[1]] #TODO: more than one? @@ -3730,6 +3740,7 @@ Start <- function(..., # dim = indices/selectors, if (split_multiselected_dims & merge_across_dims) { # TODO: More than one split? inner_dim_pos_in_split_dims <- which(names(all_split_dims[[1]]) == inner_dims_across_files) + # if inner_dim is not the first, change! if (inner_dim_pos_in_split_dims != 1) { split_dims <- c(split_dims[inner_dim_pos_in_split_dims], -- GitLab From c18c265d7f091c5f7d206c17bc188d0e5766c0d7 Mon Sep 17 00:00:00 2001 From: aho Date: Tue, 9 Mar 2021 13:31:34 +0100 Subject: [PATCH 2/2] Change the warning place. --- R/Start.R | 10 +++++----- tests/testthat/test-Start-split-merge.R | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R/Start.R b/R/Start.R index d15fdb7..22eb323 100644 --- a/R/Start.R +++ b/R/Start.R @@ -3680,11 +3680,11 @@ Start <- function(..., # dim = indices/selectors, } } } - } - if (is.null(all_split_dims)) { - split_multiselected_dims <- FALSE - .warning(paste0("Not found any dimensions able to be split. The parameter ", - "'split_multiselected_dims' is changed to FALSE.")) + if (is.null(all_split_dims)) { + split_multiselected_dims <- FALSE + .warning(paste0("Not found any dimensions able to be split. The parameter ", + "'split_multiselected_dims' is changed to FALSE.")) + } } #====================================================================== diff --git a/tests/testthat/test-Start-split-merge.R b/tests/testthat/test-Start-split-merge.R index e998844..d73cf1e 100644 --- a/tests/testthat/test-Start-split-merge.R +++ b/tests/testthat/test-Start-split-merge.R @@ -5,6 +5,7 @@ context("Start() split + merge dim and value check") var_name <- 'sfcWind' path.exp <- '/esarchive/exp/ecmwf/s2s-monthly_ensforhc/daily/$var$_f24h/$sdate$/$var$_$syear$.nc' +suppressWarnings( hcst <- Start(dat = path.exp, var = var_name, sdate = c('20160704', '20161222'), @@ -19,6 +20,7 @@ hcst <- Start(dat = path.exp, time = c('sdate', 'syear')), retrieve = F, silent = TRUE) +) dates <- attr(hcst, 'Variables')$common$time file_date <- sort(unique(gsub('-', '', sapply(as.character(dates), substr, 1, 7)))) -- GitLab