-
Notifications
You must be signed in to change notification settings - Fork 3
/
1_prep.R
193 lines (161 loc) · 6.89 KB
/
1_prep.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
source('1_prep/src/munge_meteo.R')
source('1_prep/src/build_model_config.R')
source('1_prep/src/munge_nmls.R')
source('1_prep/src/munge_obs.R')
source('1_prep/src/repair_obs_spatial.R')
p1 <- list(
# Prep model ------------------------------------
# Pull in GLM 3 template
tar_target(p1_glm_template_nml, '1_prep/in/glm3_template.nml', format = 'file'),
# Pull in files from lake-temperature-model-prep
## list of lake-specific attributes for nml modification
## file copied from lake-temperature-model-prep repo '7_config_merge/out/nml_list.rds'
tar_target(p1_nml_list_rds,
'1_prep/in/nml_list.rds',
format = 'file'),
tar_target(p1_nml_site_ids,
names(readr::read_rds(p1_nml_list_rds))),
# Temperature observations `7b_temp_merge/out/merged_temp_data_daily.feather`
tar_target(p1_obs_feather,
'1_prep/in/merged_temp_data_daily.feather',
format = 'file'),
# filter observed data to mo lakes
tar_target(p1_obs_mo_lakes,
read_feather(p1_obs_feather) %>%
filter(site_id %in% p1_nldas_site_ids)
),
# University of MO xwalk used to define site_ids for NLDAS runs
# file copied from lake-temperature-model-prep repo '2_crosswalk_munge/out/univ_mo_nhdhr_xwalk.rds'
tar_target(p1_univ_mo_xwalk_rds,
'1_prep/in/univ_mo_nhdhr_xwalk.rds',
format='file'),
tar_target(p1_lake_to_univ_mo_xwalk_df,
readr::read_rds(p1_univ_mo_xwalk_rds) %>%
filter(site_id %in% p1_nml_site_ids) %>%
arrange(site_id)),
# Pull vector of site ids
tar_target(p1_nldas_site_ids,
p1_lake_to_univ_mo_xwalk_df %>%
pull(site_id)),
# Subset nml list
tar_target(p1_nldas_nml_list_subset,
readr::read_rds(p1_nml_list_rds)[p1_nldas_site_ids]),
# Define NLDAS time period
tar_target(p1_nldas_time_period, c('1980_2021')),
# track unique NLDAS meteo files
tar_target(p1_nldas_csvs,
list.files('1_prep/in/NLDAS_GLM_csvs', full.names = T) %>%
unlist,
format = 'file'
),
# Prep observed data for calibration ------------------
# create an RDS file for each MO model for calibration
tar_target(p1_obs_rds,
subset_model_obs_data(data = p1_obs_mo_lakes,
site_id = p1_nldas_site_ids,
path_out = '1_prep/out/field_data_all'),
pattern = map(p1_nldas_site_ids),
format = 'file'
),
# Prep observed data subsets for for calibration ----------------------
#' These targets subdivide obs data into groups based on distances from each
#' dam. The pair process is imperfect because `p1_merged_temp_data_daily_feather`
#' does not contain spatial information.
# list manual cooperator crosswalk files
tar_target(p1_obs_manual_xwalks_csv,
list.files('1_prep/in/obs_review/spatial/csvs',
full.names = TRUE),
format = 'file'
),
# create missing cooperator crosswalks and dam crosswalk
tar_target(p1_obs_coop_missing_xwalks_rds,
create_missing_xwalk(file_in = p1_obs_manual_xwalks_csv,
path_out = '1_prep/in/obs_review/spatial'),
format = 'file'),
# list all cooperator data crosswalks
tar_target(p1_obs_coop_xwalks_rds,
c(p1_obs_coop_missing_xwalks_rds,
list.files('1_prep/in/obs_review/spatial',
full.names = TRUE)) %>%
.[str_detect(., 'rds')] %>%
.[!str_detect(., 'wqp')] %>%
.[!str_detect(., 'dam')] %>%
unique(),
format = 'file'
),
# list all cooperator data files from `7a_temp_coop_munge/tmp` in `lake-temperature-model-prep`
tar_target(p1_obs_coop_tmp_rds,
c(
'1_prep/in/obs_review/temp/Bull_Shoals_and_LOZ_profile_data_LMVP.rds',
'1_prep/in/obs_review/temp/Bull_Shoals_Lake_DO_and_Temp.rds',
'1_prep/in/obs_review/temp/Temp_DO_BSL_MM_DD_YYYY.rds',
'1_prep/in/obs_review/temp/Missouri_USACE_2009_2021.rds',
'1_prep/in/obs_review/temp/Waterbody_Temperatures_by_State.rds',
'1_prep/in/obs_review/temp/UniversityofMissouri_LimnoProfiles_2017_2020.rds'
),
format = 'file'
),
tar_target(p1_obs_coop_files,
tibble(
temp_files = p1_obs_coop_tmp_rds,
xwalk_files = p1_obs_coop_xwalks_rds
) %>%
mutate(
temp_file_hash = tools::md5sum(temp_files),
xwalk_file_hash = tools::md5sum(xwalk_files)
)
),
# add spatial information back into the observed data for WQP sites
tar_target(p1_obs_wqp_repaired,
repair_wqp_data(data = p1_obs_mo_lakes,
xwalk = '1_prep/in/obs_review/spatial/wqp_lake_temperature_sites_sf.rds')
),
tar_target(p1_obs_coop_repaired,
repair_coop_data(tbls = p1_obs_coop_files,
data = p1_obs_mo_lakes)),
# combine both data sets
tar_target(
p1_obs_data_w_spatial,
bind_rows(p1_obs_wqp_repaired, p1_obs_coop_repaired)
),
# set dam buffer distances - units = meters
tar_target(p1_dam_buffer,
c(5000, 10000, 15000)),
# subset data based on distance from the dam
tar_target(
p1_obs_buff_sf,
readRDS(p1_obs_coop_missing_xwalks_rds[agrep('dam_sf',
p1_obs_coop_missing_xwalks_rds)]) %>%
st_buffer(., dist = p1_dam_buffer) %>%
st_intersection(p1_obs_data_w_spatial, .) %>%
mutate(buffer_dist = p1_dam_buffer),
pattern = p1_dam_buffer,
iteration = 'list'
),
# subdivide data based on `site_id`
tar_target(
p1_obs_buffer_from_dam_rds,
subset_model_obs_data(data = p1_obs_buff_sf,
site_id = p1_nldas_site_ids,
remove_dups = TRUE,
path_out = '1_prep/out'),
format = 'file',
pattern = cross(p1_obs_buff_sf, p1_nldas_site_ids)
),
# model config and set up------------------------------
# Set up NLDAS model config
tar_target(p1_nldas_model_config,
build_nldas_model_config(nml_list = p1_nldas_nml_list_subset,
nldas_csvs = p1_nldas_csvs,
nldas_time_period = p1_nldas_time_period,
obs_rds = c(p1_obs_rds, p1_obs_buffer_from_dam_rds)
)
),
# Set up nmls for NLDAS model runs
tar_target(p1_nldas_nml_objects,
munge_model_nmls(nml_list = p1_nldas_nml_list_subset,
base_nml = p1_glm_template_nml,
driver_type = 'nldas'),
packages = c('glmtools'),
iteration = 'list')
)