/
report_no_data.R
99 lines (82 loc) · 3.25 KB
/
report_no_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#' @title Report NoData value
#'
#' @description This function reports the defined NoData value of a raster layer. The NoData
#' value of a raster layer represents the absence of data. In computations the
#' NoData value can be treated in different ways. Either the NoData value will
#' be reported or the Nodata value will be ignored and a value is computed from
#' the available values of a specified location.
#'
#' @param data_dir character. Path to the directory containing all input data.
#' @param var_layer character vector of variable raster layers on disk,
#' e.g. "slope_grad_dw_cel_h00v00.tif".
#' @param n_cores numeric. Number of cores used for parallelization, in case
#' multiple .tif files are provided to var_layer.
#' @importFrom processx run
#' @importFrom tidyr separate
#' @importFrom parallel detectCores
#' @export
#'
#' @author Afroditi Grigoropoulou, Marlene Schürz
#'
#' @references
#' \url{https://gdal.org/programs/gdalinfo.html}
#'
#' @examples
#' # Download test data into the temporary R folder
#' # or define a different directory
#' my_directory <- tempdir()
#' download_test_data(my_directory)
#'
#' # Report the NoData value
#' report_no_data(data_dir = paste0(my_directory, "/hydrography90m_test_data"),
#' var_layer = c("subcatchment_1264942.tif", "flow_1264942.tif",
#' "spi_1264942.tif"),
#' n_core = 2)
#'
report_no_data <- function(data_dir, var_layer, n_cores = NULL) {
# Check if path exists
if (!dir.exists(data_dir))
stop(paste0(data_dir, " does not exist."))
for(name in var_layer) {
file <- paste(data_dir, name, sep = "/")
if (!file.exists(file))
stop(paste0("File: ", var_layer, " does not exist."))
}
# Setting up parallelization if n_cores is not provided
if (is.null(n_cores)) {
# Detect number of available cores
n_cores <- detectCores(logical = FALSE) - 1
}
# Format var_layer vector so that it can be read
# as an array in the bash script
var_layer_array <- paste(unique(var_layer), collapse = "/")
# Check operating system
sys_os <- get_os()
# Make bash scripts executable
make_sh_exec()
if (sys_os == "linux" || sys_os == "osx") {
# Call the external .sh script report_no_data()
reports <- processx::run(system.file("sh", "report_no_data.sh",
package = "hydrographr"),
args = c(data_dir, var_layer_array, n_cores),
echo = FALSE)$stdout
} else {
# Check if WSL and Ubuntu are installed
check_wsl()
# Change paths for WSL
wsl_data_dir <- fix_path(data_dir)
wsl_sh_file <- fix_path(system.file("sh", "report_no_data.sh",
package = "hydrographr"))
# Open GRASS GIS session on WSL
# Call external GRASS GIS command r.reclass
reports <- processx::run(system.file("bat", "report_no_data.bat",
package = "hydrographr"),
args = c(wsl_data_dir, var_layer_array, n_cores,
wsl_sh_file),
echo = FALSE)$stdout
}
# Format output message
reports <- as.data.table(strsplit(reports, "\n")) %>%
separate(col = V1, c("Raster", "NoData"), sep = "=")
return(reports)
}