From 5da783d0a28f46973fdbdcc1964dc7d9d9ee7943 Mon Sep 17 00:00:00 2001 From: biopsichas <svajunas.plunge@gmail.com> Date: Tue, 29 Oct 2024 14:37:08 +0200 Subject: [PATCH] Output adjustment for the new executable (Katrin) --- R/run_soft_cal.R | 18 +++++++++++------- R/run_swat_verify.R | 11 +++++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/R/run_soft_cal.R b/R/run_soft_cal.R index fc32375..65f5433 100644 --- a/R/run_soft_cal.R +++ b/R/run_soft_cal.R @@ -317,8 +317,10 @@ read_wb_aa <- function(path) { # .. and add placeholder names for the last n columns which did not get a name # if we don't do this, dplyr gets angry in the next line - colnames(basin_wb_aa)[no_name_columns] <- - letters[1:length(no_name_columns)] + if(length(no_name_columns) > 0) { + colnames(basin_wb_aa)[no_name_columns] <- + letters[1:length(no_name_columns)] + } # remove rows 1 to 3, as they don't contain any real data basin_wb_aa <- basin_wb_aa %>% filter(!row_number() %in% c(1:3)) @@ -327,15 +329,17 @@ read_wb_aa <- function(path) { # the new column will be named description and describes what the softcal # algorithm did in that step. (So its probably important). # the united columns are separated by a space (trailing space exists now..) - basin_wb_aa = tidyr::unite(data = basin_wb_aa, - col = description, - no_name_columns, - sep = " ") + if(length(no_name_columns) > 0) { + basin_wb_aa = tidyr::unite(data = basin_wb_aa, + col = description, + no_name_columns, + sep = " ") + } # figure out which columns should be a double and not a string # (everything except for name and description columns) dbl_cols = basin_wb_aa %>% colnames() - dbl_cols = dbl_cols[!dbl_cols %in% c("name", "description")] + dbl_cols = dbl_cols[!dbl_cols %in% unique(c(c("name", "description"), dbl_cols[grepl("[A-Za-z]", basin_wb_aa)]))] # and convert them to numeric basin_wb_aa = basin_wb_aa %>% mutate_at(dbl_cols, as.numeric) diff --git a/R/run_swat_verify.R b/R/run_swat_verify.R index 2133948..5bde86a 100644 --- a/R/run_swat_verify.R +++ b/R/run_swat_verify.R @@ -143,8 +143,15 @@ read_tbl <- function(file, run_path, n_skip) { } } - fread(file_path, skip = n_skip, header = FALSE) %>% - set_names(., col_names) %>% + df <- fread(file_path, skip = n_skip, header = FALSE) + + if(ncol(df) != length(col_names)) { + warning(paste0('Number of columns in the ',"'", file, "'", ' does not match the number of column names. + Columns without values are dropped out. If you need these columns, please check the output file.')) + } + + df %>% + set_names(., col_names[c(1:ncol(df))]) %>% tibble(.) } -- GitLab