-
Notifications
You must be signed in to change notification settings - Fork 12
/
get_c14data.R
172 lines (155 loc) · 5.56 KB
/
get_c14data.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#### get_c14data ####
#' @title Download radiocarbon source databases and convert them to a \strong{c14_date_list}
#'
#' @description \code{get_c14data()} allows to download source databases and adjust their variables to conform to the
#' definition in the
#' \href{https://github.com/ropensci/c14bazAAR/blob/master/data-raw/variable_reference.csv}{variable_reference}
#' table. That includes renaming and arranging the variables (with \code{c14bazAAR::order_variables()})
#' as well as type conversion (with \code{c14bazAAR::enforce_types()}) -- so all the steps undertaken by
#' \code{as.c14_date_list()}. \cr
#' All databases require different downloading and data wrangling steps. Therefore
#' there's a custom getter function for each of them (see \code{?get_all_dates}). \cr
#'
#' \code{get_c14data()} is a wrapper to download all dates from multiple databases and
#' \code{c14bazAAR::fuse()} the results.
#'
#' @param databases Character vector. Names of databases to be downloaded. "all" causes the download of all databases. \code{get_c14data()} prints a list of the currently available databases
#'
#' @rdname db_getter
#'
#' @examples
#'
#' \dontrun{
##' get_c14data(databases = c("adrac", "palmisano"))
#' get_all_dates()}
#'
#' @export
get_c14data <- function(databases = c()) {
# transfrom all input to lower
databases <- tolower(databases)
# message if no database is selected
names_of_available_parsers <- names(get_all_parser_functions())
if (length(databases) == 0) {
message("The following databases are available: ", paste0(names_of_available_parsers, collapse = ", "))
message("Learn more here: https://github.com/ropensci/c14bazAAR")
return()
}
# start processing
message("Trying to download all dates from the requested databases...")
# check if all requested databases are available (e.g. spelling errors)
database_availability_check <- databases %in% c("all", names_of_available_parsers)
if (any(!database_availability_check)) {
stop(
"The following databases are not in the list of available databases (spelling?): ",
paste0(databases[!database_availability_check], collapse = ", ")
)
}
available_databases <- databases[database_availability_check]
# setup progress bar
pb <- utils::txtProgressBar(
max = 100,
style = 3,
width = 50,
char = "+"
)
# define list of parser functions
parser_functions <- get_parser_functions(available_databases)
# loop to call all parser functions
date_lists <- list()
for (i in 1:length(parser_functions)) {
# call parser function
date_lists[[i]] <- tryCatch(
parser_functions[[i]](),
error = function(e, name = names(parser_functions)[i]) { list(e = e, name = name) }
)
# increment progress bar
utils::setTxtProgressBar(pb, 99 * i/length(parser_functions))
}
error_ind <- sapply(date_lists, function(x) !('c14_date_list' %in% class(x)))
errors <- date_lists[error_ind]
date_lists <- date_lists[!error_ind]
if (any(error_ind)) {
warning(
paste(
"There were errors:\n\n",
paste(sapply(errors, function(x) { paste0(x$name, " --> ", x$e$message) }), collapse = "\n"),
"\n\nNot all data might have been downloaded accurately!",
"\nIn case of a timout try increasing the download time with options(timeout=300).",
sep = ""
)
)
}
# check if any database could be downloaded
if (all(error_ind)) {
stop("\n\nDownload failed for all databases.\n\n")
}
# fuse radiocarbon lists
all_dates <- do.call(c14bazAAR::fuse, date_lists)
# close progress bar
utils::setTxtProgressBar(pb, 100)
close(pb)
return(all_dates)
}
#' get_parser_functions
#'
#' @return vector with all parser functions in c14bazAAR
#'
#' @keywords internal
#' @noRd
get_parser_functions <- function(databases) {
pfs <- get_all_parser_functions()
if ("all" %in% databases) {
return(pfs)
} else {
return(pfs[names(pfs) %in% tolower(databases)])
}
}
#' get_all_parser_functions
#'
#' @return vector with all parser functions in c14bazAAR
#'
#' @keywords internal
#' @noRd
get_all_parser_functions <- function() {
return(c(
"14sea" = c14bazAAR::get_14sea,
"adrac" = c14bazAAR::get_adrac,
"agrichange" = c14bazAAR::get_agrichange,
"aida" = c14bazAAR::get_aida,
"austarch" = c14bazAAR::get_austarch,
"calpal" = c14bazAAR::get_calpal,
"caribbean" = c14bazAAR::get_caribbean,
"eubar" = c14bazAAR::get_eubar,
"euroevol" = c14bazAAR::get_euroevol,
"rado.nb" = c14bazAAR::get_rado.nb,
"kiteeastafrica" = c14bazAAR::get_kiteeastafrica,
"palmisano" = c14bazAAR::get_palmisano,
"irdd" = c14bazAAR::get_irdd,
"pacea" = c14bazAAR::get_pacea,
"14cpalaeolithic" = c14bazAAR::get_14cpalaeolithic,
"medafricarbon" = c14bazAAR::get_medafricarbon,
"jomon" = c14bazAAR::get_jomon,
"mesorad" = c14bazAAR::get_mesorad,
"katsianis" = c14bazAAR::get_katsianis,
"nerd" = c14bazAAR::get_nerd,
"bda" = c14bazAAR::get_bda,
"rxpand" = c14bazAAR::get_rxpand,
"sard" = c14bazAAR::get_sard,
"p3k14c" = c14bazAAR::get_p3k14c,
"neonet" = c14bazAAR::get_neonet,
"neonet" = c14bazAAR::get_neonetatl
))
}
#' @title Backend functions for data download
#'
#' @description Backend functions to download data. See \code{?\link{get_c14data}}
#' for a more simple interface and further information.
#'
#' @param db_url Character. URL that points to the c14 archive file. \code{c14bazAAR::get_db_url()}
#' fetches the URL from a reference list
#'
#' @rdname db_getter_backend
#' @export
get_all_dates <- function() {
get_c14data(names(get_all_parser_functions()))
}