-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path99-distribute-data.R
227 lines (196 loc) · 5.79 KB
/
99-distribute-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
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
219
220
221
222
223
224
225
226
227
# read function
source("data-raw/_helper_function.R")
# GitHub --------------------------------------
save(tbi_rmPII, file = "data/tbi.rda")
# Geospatial commons --------------------------------------
# Contact GIS
# tbi_desktop_path <- file.path("Desktop", "TBI_data_out")
dir.create("TBI_data_out")
tbi_geospatialCommons_path <- file.path("TBI_data_out", "geospatial_commons")
dir.create(tbi_geospatialCommons_path)
# remove emojis and special charactors from the data.
# lapply(tbi_rmPII, \(tab){
# cols <- tab %>%
# sapply(typeof) %>%
# str_detect("character")
# col_names <- names(tab)[cols]
# tab[, c(col_names) :=
# lapply(.SD, str_replace_all, pattern = "[^\x01-\xFF]", replacement = ""),
# .SDcols = cols
# ]
# })
# output tables --------
tables <- names(tbi_rmPII)
years <- c("2019", "2021", "2023")
lapply(tables, \(tab){
lapply(years, \(yr){
tbi_yr_path <- file.path(tbi_geospatialCommons_path, paste0("tbi", yr))
if (!dir.exists(tbi_yr_path)) dir.create(tbi_yr_path)
fwrite(tbi_rmPII[[tab]][survey_year == yr],
file = file.path(
tbi_yr_path,
sprintf("TravelBehaviorInventory%s_%s.csv", yr, tab %>% str_to_upper())
)
)
})
})
# MTS_Planning DB --------------------------------------
# remove emojis and special characters from the data.
# lapply(tbi, \(tab){
# cols <- tab %>%
# sapply(typeof) %>%
# str_detect("character")
# col_names <- names(tab)[cols]
# tab[, c(col_names) :=
# lapply(.SD, str_replace_all, pattern = "[^\x01-\xFF]", replacement = ""),
# .SDcols = cols
# ]
# })
# save to database. The process is faster if we do it in smaller chuncks
tables <- names(tbi_rmPII)
years <- c(2019, 2021, 2023)
db_con <- dbConnect(odbc::odbc(),
dsn = "MTS_Planning_Data",
uid = keyring::key_get("councilR.uid"),
pwd = keyring::key_get("councilR.pwd")
)
# tab <- "trip"
# yr <- 2023
lapply(tables, \(tab){
lapply(years, \(yr){
table_name <- sprintf("TBI%s_%s", yr, tab %>% str_to_upper()) %>% print()
if (!dbExistsTable(db_con, table_name)) {
message(tab, yr)
n <- 5000
nr <- nrow(tbi[[tab]][survey_year == yr])
table_subsets <- split(
tbi[[tab]][survey_year == yr],
rep(1:ceiling(nr / n),
each = n,
length.out = nr
)
)
# create an empty table using the function created in script 15
create_table(db_con, tab, table_name)
i <- 1
N <- length(table_subsets)
lapply(table_subsets, \(table_ss_){
message(tab, " ", i, " of ", N)
dbWriteTable(db_con, name = table_name, value = table_ss_, append = T)
i <<- i + 1
})
}
})
})
dbDisconnect(db_con)
# N Drive -
# put data in the N drive
# fwrite(table_subsets[['2915']], '~/Desktop/test.csv')
# for(i in 1:nrow(table_subsets[['583']])){
# message(i)
# DBI::dbWriteTable(db_con, name = table_name, value = table_subsets[['583']][i], append = T)
# }
# DBI::dbWriteTable(db_con, name = table_name, value = table_subsets[['583']], append = T)
# # MTS_Planning DB -
# # location file is not modified in this process and in the
# # data base as TBI19_RAW_LOCATION
# db_con <- db_connect()
# tbi19_PII %>%
# names() %>%
# setdiff(c("var_list", 'value_list')) %>%
# lapply(\(table_){
# table_name <- paste0("TBI19_FINAL_", str_to_upper(table_))
# if (!dbExistsTable(db_con, table_name)) {
# message(table_)
# n <- 5000
# nr <- nrow(tbi19_PII[[table_]])
# table_subsets <- split(tbi19_PII[[table_]], rep(1:ceiling(nr/n), each=n, length.out=nr))
#
# i <- 1
# N <- length(table_subsets)
# lapply(table_subsets, \(table_ss_){
# message(table_, ' ', i, " of ", N)
# dbWriteTable(db_con, name = table_name, value = table_ss_, append = T)
# i <<- i + 1
# })
# }
# })
#
#
# # 2021
# # location file is not modified in this process and in the
# # data base as TBI21_RAW_LOCATION
# tbi21_PII %>%
# names() %>%
# setdiff(c("var_list", 'value_list')) %>%
# lapply(\(table_){
# table_name <- paste0("TBI21_FINAL_", str_to_upper(table_))
# if (!dbExistsTable(db_con, table_name)) {
# message(table_)
# n <- 5000
# nr <- nrow(tbi21_PII[[table_]])
# table_subsets <- split(tbi21_PII[[table_]], rep(1:ceiling(nr/n), each=n, length.out=nr))
#
# i <- 1
# N <- length(table_subsets)
# lapply(table_subsets, \(table_ss_){
# message(table_, ' ', i, " of ", N)
# dbWriteTable(db_con, name = table_name, value = table_ss_, append = T)
# i <<- i + 1
# })
# }
# })
# National Renewable Energy Laboratory -----
# Work with [email protected] and [email protected]
tbi_database_NREL <- file.path(tbi_desktop_path, "database_NREL")
dir.create(tbi_database_NREL)
# 2019
tbi19_path <- file.path(tbi_database_NREL, "tbi19_PII")
dir.create(tbi19_path)
tbi19_PII %>%
names() %>%
lapply(\(table_){
fwrite(tbi19_PII[[table_]],
file = file.path(
tbi19_path,
paste0(
"TravelBehaviorInventory2019",
str_to_title(table_),
".csv"
)
)
)
})
# 2021
tbi21_path <- file.path(tbi_database_NREL, "tbi21_PII")
dir.create(tbi21_path)
tbi21_PII %>%
names() %>%
lapply(\(table_){
fwrite(tbi21_PII[[table_]],
file = file.path(
tbi21_path,
paste0(
"TravelBehaviorInventory2019",
str_to_title(table_),
".csv"
)
)
)
})
dbListTables(db_con) %>%
str_subset("TBI") %>%
str_subset("OBS", T) %>%
str_subset("v_", T) %>%
str_subset("TOUR", T) %>%
paste0(collapse = "\n") %>%
cat()
lapply(names(tbi), \(dt_name){
dt <- copy(tbi[[dt_name]])
lapply(c(2019, 2021), \(yr){
message(
"table: ", dt_name, " yr: ", yr, " nrow: ",
dt[survey_year == yr, .N %>% prettyNum(",")]
)
})
})