Skip to content

Commit 99a0ec9

Browse files
authored
Merge pull request #3338 from Sweetdevil144/gsoc/convert-input
Refactor `convert_input` to Perform tasks via helper function
2 parents e043879 + 3d7ec43 commit 99a0ec9

12 files changed

Lines changed: 556 additions & 259 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ section for the next release.
4242
- `extract_soil_gssurgo` now supports spatial sampling using a grid of user-defined size and spacing. And supports ensemble simulation of soil organic carbon (SOC) stocks, using area-weighted aggregation
4343
- The ERA5 NC extraction function can now handle multi-site instead of one
4444
- All of the `met2model.*` functions no longer write a list of variables (`*.nc.var`) file alongside each output netcdf. If you need var files, use `PEcAn.utils::nc_write_vars()` after the run completes (#3611, #3616).
45+
- Refactor `convert_input` to Perform tasks via helper function. Subtask of [#3307](https://github.com/PecanProject/pecan/issues/3307)
4546
- Stopped testing on R 4.1, started testing on R 4.5, and updated prebuilt Docker images to match -- they are now available for R releases 4.2 through 4.5 as well as for R under development.
4647
- `write.config.STICS()` now modifies parameters with vectors rather than individually.
4748

base/db/R/check_missing_files.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#' Check for Missing or Empty Files in Conversion Results
2+
#'
3+
#' This function inspects the file paths in a list of data frames (typically produced by a download or conversion routine) to ensure that each file is present and non-empty. Specifically, it checks whether any file path is missing or has a file size of zero, and logs an error if such files are detected. It also normalizes `existing.input` and `existing.dbfile` so that each is returned as a list of data frames.
4+
#'
5+
#' @param result A list of data frames containing file information. Each data frame is expected to have a column named `file` with absolute file paths created by a data-conversion or download function. For example, this might be the structure returned by a "download_X" or "met2model_X" function when invoked via [convert_input()].
6+
#' @param existing.input A data frame or list of data frames (possibly zero rows) representing input records in the BETY `inputs` table that match (or partially match) the data being added. This is converted to a list of data frames if it is not already.
7+
#' @param existing.dbfile A data frame or list of data frames (possibly zero rows) representing dbfile records in the BETY `dbfiles` table that match (or partially match) the data being added. This is also converted to a list of data frames if it is not already.
8+
#'
9+
#' @return A list containing:
10+
#' \itemize{
11+
#' \item A list of data frames for `existing.input`
12+
#' \item A list of data frames for `existing.dbfile`
13+
#' }
14+
#'
15+
#' @details
16+
#' The function calculates the file size for each file specified in the `result` data frames. If any file path is missing (`NA`) or any file size is zero, the function raises a fatal error (via [PEcAn.logger::logger.severe]) indicating that an expected file is either nonexistent or empty. If no such issues are found, it merely ensures that `existing.input` and `existing.dbfile` are each wrapped in a list for consistent downstream usage.
17+
#'
18+
#' @author Betsy Cowdery, Michael Dietze, Ankur Desai, Tony Gardella, Luke Dramko
19+
20+
check_missing_files <- function(result, existing.input = NULL, existing.dbfile = NULL) {
21+
result_sizes <- purrr::map_dfr(
22+
result,
23+
~ dplyr::mutate(
24+
.,
25+
file_size = purrr::map_dbl(file, file.size),
26+
missing = is.na(file_size),
27+
empty = file_size == 0
28+
)
29+
)
30+
31+
if (any(result_sizes$missing) || any(result_sizes$empty)) {
32+
PEcAn.logger::logger.severe(
33+
"Requested Processing produced empty files or Nonexistent files:\n",
34+
log_format_df(result_sizes[, c(1, 8, 9, 10)]),
35+
"\n Table of results printed above.",
36+
wrap = FALSE
37+
)
38+
}
39+
40+
41+
# Wrap in a list for consistent processing later
42+
if (is.data.frame(existing.input)) {
43+
existing.input <- list(existing.input)
44+
}
45+
46+
if (is.data.frame(existing.dbfile)) {
47+
existing.dbfile <- list(existing.dbfile)
48+
}
49+
return(list(existing.input, existing.dbfile))
50+
}
51+
52+
log_format_df <- function(df) {
53+
formatted_df <- rbind(colnames(df), format(df))
54+
formatted_text <- purrr::reduce(formatted_df, paste, sep = " ")
55+
paste(formatted_text, collapse = "\n")
56+
}

0 commit comments

Comments
 (0)