forked from rossyndicate/Light_Partitioning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_harmonize.R
More file actions
240 lines (185 loc) · 9.82 KB
/
3_harmonize.R
File metadata and controls
240 lines (185 loc) · 9.82 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Source the functions that will be used to build the targets in p3_targets_list
source("3_harmonize/src/clean_wqp_data.R")
source("3_harmonize/src/clean_wqp_data_strict.R")
source("3_harmonize/src/create_match_table.R")
source("3_harmonize/src/format_columns.R")
source("3_harmonize/src/get_p_codes.R")
source("3_harmonize/src/harmonization_report_helper_functions.R")
source("3_harmonize/src/harmonize_sdd.R")
source("3_harmonize/src/harmonize_tss.R")
source("3_harmonize/src/harmonize_chla.R")
source("3_harmonize/src/harmonize_doc.R")
source("3_harmonize/src/harmonize_sdd_strict.R")
source("3_harmonize/src/harmonize_tss_strict.R")
source("3_harmonize/src/harmonize_chla_strict.R")
source("3_harmonize/src/harmonize_doc_strict.R")
source("3_harmonize/src/find_simultaneous.R")
source("3_harmonize/src/change_ext.R")
p3_targets_list <- list(
# Pre-harmonization data prep ---------------------------------------------
# All columns in p2_wqp_data_aoi are of class character. Coerce select
# columns back to numeric, but first retain original entries in new columns
# ending in "_original". The default option is to format "ResultMeasureValue"
# and "DetectionQuantitationLimitMeasure.MeasureValue" to numeric, but
# additional variables can be added using the `vars_to_numeric` argument in
# format_columns(). By default, format_columns() will retain all columns, but
# undesired variables can also be dropped from the WQP dataset using the
# optional `drop_vars` argument.
tar_target(
p3_wqp_data_aoi_formatted,
format_columns(p2_wqp_data_aoi),
format = "feather"
),
# Creates a match table with column names from WQP and shorter names to use
# in renaming them
tar_target(wqp_col_match,
create_match_table()),
# Cleaning steps before breaking out by parameter:
# Remove duplicates, ensure meaningful results present, check data status,
# check media, remove white spaces
tar_target(wqp_data_aoi_ready,
clean_wqp_data(wqp_data = p3_wqp_data_aoi_formatted,
char_names_crosswalk = p1_char_names_crosswalk,
site_data = p2_site_counts,
match_table = wqp_col_match,
wqp_metadata = p1_wqp_inventory_aoi),
packages = c("tidyverse", "lubridate"),
format = "feather"),
tar_target(wqp_data_aoi_ready_strict,
clean_wqp_data_strict(wqp_data = p3_wqp_data_aoi_formatted,
char_names_crosswalk = p1_char_names_crosswalk,
site_data = p2_site_counts,
match_table = wqp_col_match,
wqp_metadata = p1_wqp_inventory_aoi),
packages = c("tidyverse", "lubridate", "feather")),
# Connect cleaned data output to the pipeline
tar_target(cleaned_wqp_data_strict,
read_feather(wqp_data_aoi_ready_strict$wqp_data_clean_path),
packages = "feather",
format = "feather"),
# Get parameter codes for use in cleaning processes
tar_target(
name = p_codes,
command = get_p_codes(),
packages = c("tidyverse", "rvest", "janitor")
),
# A quick separate step to export the dataset to a file for easier review
# Not integrating it deeper into existing targets for now
# tar_file(wqp_data_aoi_ready_out,
# {
# out_path <- "data/out/wqp_data_aoi_ready.feather"
#
# write_feather(x = wqp_data_aoi_ready,
# path = out_path)
#
# out_path
# },
# packages = c("feather")),
#
# Matchup tables ----------------------------------------------------------
# Secchi depth method matchup table
tar_file_read(name = sdd_analytical_method_matchup,
command = "data/in/sdd_analytical_method_matchup.csv",
read = read_csv(file = !!.x),
cue = tar_cue("always")),
# Secchi sample method matchup table
tar_file_read(name = sdd_sample_method_matchup,
command = "data/in/sdd_sample_method_matchup.csv",
read = read_csv(file = !!.x),
cue = tar_cue("always")),
# Secchi equipment matchup table
tar_file_read(name = sdd_equipment_matchup,
command = "data/in/sdd_collection_equipment_matchup.csv",
read = read_csv(file = !!.x),
cue = tar_cue("always")),
# Chla depth method matchup table
tar_file_read(name = chla_analytical_method_matchup,
command = "data/in/chla_analytical_method_matchup.csv",
read = read_csv(file = !!.x),
cue = tar_cue("always")),
# Harmonization process ---------------------------------------------------
tar_target(harmonized_tss,
harmonize_tss(raw_tss = cleaned_wqp_data_strict %>%
filter(parameter == "tss"),
p_codes = p_codes),
packages = c("tidyverse", "lubridate", "pander", "feather")),
tar_target(harmonized_tss_strict,
harmonize_tss_strict(raw_tss = cleaned_wqp_data_strict %>%
filter(parameter == "tss"),
p_codes = p_codes),
packages = c("tidyverse", "lubridate", "pander", "feather")),
tar_target(harmonized_chla,
harmonize_chla(raw_chla = cleaned_wqp_data_strict %>%
filter(parameter == "chlorophyll"),
p_codes = p_codes,
chla_analytical_method_matchup = chla_analytical_method_matchup),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(harmonized_chla_strict,
harmonize_chla_strict(raw_chla = cleaned_wqp_data_strict %>%
filter(parameter == "chlorophyll"),
p_codes = p_codes,
chla_analytical_method_matchup = chla_analytical_method_matchup),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(harmonized_sdd,
harmonize_sdd(raw_sdd = cleaned_wqp_data_strict %>%
filter(parameter == "secchi"),
p_codes = p_codes,
sdd_analytical_method_matchup = sdd_analytical_method_matchup,
sdd_sample_method_matchup = sdd_sample_method_matchup,
sdd_equipment_matchup = sdd_equipment_matchup),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(harmonized_sdd_strict,
harmonize_sdd_strict(raw_sdd = cleaned_wqp_data_strict %>%
filter(parameter == "secchi"),
p_codes = p_codes,
sdd_analytical_method_matchup = sdd_analytical_method_matchup,
sdd_sample_method_matchup = sdd_sample_method_matchup,
sdd_equipment_matchup = sdd_equipment_matchup),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(harmonized_doc,
harmonize_doc(raw_doc = cleaned_wqp_data_strict %>%
filter(parameter == "doc"),
p_codes = p_codes),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(harmonized_doc_strict,
harmonize_doc_strict(raw_doc = cleaned_wqp_data_strict %>%
filter(parameter == "doc"),
p_codes = p_codes),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(documented_drops,
map_df(.x = c(wqp_data_aoi_ready_strict$compiled_drops_path,
harmonized_chla_strict$compiled_drops_path,
harmonized_sdd_strict$compiled_drops_path,
harmonized_doc_strict$compiled_drops_path,
harmonized_tss_strict$compiled_drops_path),
.f = read_csv),
cue = tar_cue("always")),
# Find simultaneous records -----------------------------------------------
tar_target(simultaneous_data,
find_simultaneous(chla_path = harmonized_chla,
doc_path = harmonized_doc,
sdd_path = harmonized_sdd,
tss_path = harmonized_tss,
wqp_metadata = p1_wqp_inventory_aoi),
packages = c("tidyverse", "lubridate", "feather")),
tar_target(simultaneous_data_strict,
find_simultaneous(chla_path = harmonized_chla_strict$harmonized_chla_path,
doc_path = harmonized_doc_strict$harmonized_doc_path,
sdd_path = harmonized_sdd_strict$harmonized_sdd_path,
tss_path = harmonized_tss_strict$harmonized_tss_path,
wqp_metadata = p1_wqp_inventory_aoi),
packages = c("tidyverse", "lubridate", "feather")),
# A target using the harmonized outputs to prepare a dataset for the later
# analysis steps
tar_target(harmonized_wqp_w_methods,
{
# Read in the exported harmonized datasets
map_df(.x = c(harmonized_chla, harmonized_doc, harmonized_tss,
harmonized_sdd),
.f = ~ read_feather(.x) %>%
select(SiteID, date, lat, lon,
harmonized_parameter = parameter, orig_parameter,
analytical_method))
},
packages = c("tidyverse", "feather"))
)