-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthetic_met_functions.R
325 lines (267 loc) · 13.2 KB
/
synthetic_met_functions.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
##### This script houses functions used to create mash-up timeseries data for synthetic meteorological datasets
## Last Updated 4/27/22
## HARP Group
# met timeseries data downloading function
# inputs a landsegment
# inputs start and end date
# inputs website and linux locations of data
# outputs a list of lseg_csv timesieries for entire downloaded time period
get_lseg_csv <- function(landseg, startdate, enddate, site, dir){
# creating timeframe variable for grabing data
timeframe <- paste0(substring(startdate, 1, 4), substring(startdate, 6, 7), substring(startdate, 9, 10), "00-",
substring(enddate, 1, 4), substring(enddate, 6, 7), substring(enddate, 9, 10), "23")
# downloading entire timeseries data
# using web directory
dfRAD <- fread(paste0(site,landseg, ".RAD"))
dfTMP <- fread(paste0(site,landseg, ".TMP"))
dfPET <- fread(paste0(site,landseg, ".PET"))
dfPRC <- fread(paste0(site,landseg, ".PRC"))
dfWND <- fread(paste0(site,landseg, ".WND"))
dfDPT <- fread(paste0(site,landseg, ".DPT"))
# using linux terminal directory
#dfRAD <- read.table(paste0(dir, "/", landseg,".RAD"), header = FALSE, sep = ",")
#dfTMP <- read.table(paste0(dir, "/", landseg,".TMP"), header = FALSE, sep = ",")
#dfPET <- read.table(paste0(dir, "/", landseg,".PET"), header = FALSE, sep = ",")
#dfPRC <- read.table(paste0(dir, "/", landseg,".PRC"), header = FALSE, sep = ",")
#dfWND <- read.table(paste0(dir, "/", landseg,".WND"), header = FALSE, sep = ",")
#dfDPT <- read.table(paste0(dir, "/", landseg,".DPT"), header = FALSE, sep = ",")
# adding date column for date manipulation
colnames(dfRAD) = c("year","month","day","hour","RAD")
dfRAD$date <- as.Date(paste(dfRAD$year,dfRAD$month,dfRAD$day,sep="-"))
colnames(dfTMP) = c("year","month","day","hour","TMP")
dfTMP$date <- as.Date(paste(dfTMP$year,dfTMP$month,dfTMP$day,sep="-"))
colnames(dfPET) = c("year","month","day","hour","PET")
dfPET$date <- as.Date(paste(dfPET$year,dfPET$month,dfPET$day,sep="-"))
colnames(dfPRC) = c("year","month","day","hour","PRC")
dfPRC$date <- as.Date(paste(dfPRC$year,dfPRC$month,dfPRC$day,sep="-"))
colnames(dfWND) = c("year","month","day","hour","WND")
dfWND$date <- as.Date(paste(dfWND$year,dfWND$month,dfWND$day,sep="-"))
colnames(dfDPT) = c("year","month","day","hour","DPT")
dfDPT$date <- as.Date(paste(dfDPT$year,dfDPT$month,dfDPT$day,sep="-"))
# filter by inputted date range
dfRAD <- sqldf(paste0("SELECT year, month, day, hour, RAD
FROM dfRAD
WHERE date between ",
as.numeric(as.Date(startdate)),
" AND ",
as.numeric(as.Date(enddate)),
""))
dfTMP <- sqldf(paste0("SELECT year, month, day, hour, TMP
FROM dfTMP
WHERE date between ",
as.numeric(as.Date(startdate)),
" AND ",
as.numeric(as.Date(enddate)),
""))
dfPET <- sqldf(paste0("SELECT year, month, day, hour, PET
FROM dfPET
WHERE date between ",
as.numeric(as.Date(startdate)),
" AND ",
as.numeric(as.Date(enddate)),
""))
dfPRC <- sqldf(paste0("SELECT year, month, day, hour, PRC
FROM dfPRC
WHERE date between ",
as.numeric(as.Date(startdate)),
" AND ",
as.numeric(as.Date(enddate)),
""))
dfWND <- sqldf(paste0("SELECT year, month, day, hour, WND
FROM dfWND
WHERE date between ",
as.numeric(as.Date(startdate)),
" AND ",
as.numeric(as.Date(enddate)),
""))
dfDPT <- sqldf(paste0("SELECT year, month, day, hour, DPT
FROM dfDPT
WHERE date between ",
as.numeric(as.Date(startdate)),
" AND ",
as.numeric(as.Date(enddate)),
""))
# return new time series as list
dfALL <- list(
"RAD" = dfRAD,
"TMP" = dfTMP,
"PET" = dfPET,
"PRC" = dfPRC,
"WND" = dfWND,
"DPT" = dfDPT)
return(dfALL)
}
# mash up time series function
# inputs lseg_csv data list for one land segment and entire timeperiod (output of get_lseg_csv function)
# inputs two start dates and end dates in "YYYY-MM-DD" format
# outputs a synthetic timeseries for modeling purposes
generate_synthetic_timeseries <- function(lseg_csv, startdate1, enddate1, startdate2, enddate2){
# seperate list into individual data frames
dfRAD <- lseg_csv$RAD
dfTMP <- lseg_csv$TMP
dfPET <- lseg_csv$PET
dfPRC <- lseg_csv$PRC
dfWND <- lseg_csv$WND
dfDPT <- lseg_csv$DPT
# adding date column for date manipulation
colnames(dfRAD) = c("year","month","day","hour","RAD")
dfRAD$date <- as.Date(paste(dfRAD$year,dfRAD$month,dfRAD$day,sep="-"))
colnames(dfTMP) = c("year","month","day","hour","TMP")
dfTMP$date <- as.Date(paste(dfTMP$year,dfTMP$month,dfTMP$day,sep="-"))
colnames(dfPET) = c("year","month","day","hour","PET")
dfPET$date <- as.Date(paste(dfPET$year,dfPET$month,dfPET$day,sep="-"))
colnames(dfPRC) = c("year","month","day","hour","PRC")
dfPRC$date <- as.Date(paste(dfPRC$year,dfPRC$month,dfPRC$day,sep="-"))
colnames(dfWND) = c("year","month","day","hour","WND")
dfWND$date <- as.Date(paste(dfWND$year,dfWND$month,dfWND$day,sep="-"))
colnames(dfDPT) = c("year","month","day","hour","DPT")
dfDPT$date <- as.Date(paste(dfDPT$year,dfDPT$month,dfDPT$day,sep="-"))
# declaring difference in years for naming purposes
year_diff = as.numeric(substring(enddate1, 1, 4)) - as.numeric(substring(startdate2, 1, 4))
# filter by inputted date ranges
dfRAD1 <- sqldf(paste0("SELECT year, month, day, hour, RAD
FROM dfRAD
WHERE date between ",
as.numeric(as.Date(startdate1)),
" AND ",
as.numeric(as.Date(enddate1)),
""))
dfRAD2 <- sqldf(paste0("SELECT year + ", year_diff, " , month, day, hour, RAD
FROM dfRAD
WHERE date between ",
as.numeric(as.Date(startdate2)),
" AND ",
as.numeric(as.Date(enddate2)),
""))
dfTMP1 <- sqldf(paste0("SELECT year, month, day, hour, TMP
FROM dfTMP
WHERE date between ",
as.numeric(as.Date(startdate1)),
" AND ",
as.numeric(as.Date(enddate1)),
""))
dfTMP2 <- sqldf(paste0("SELECT year + ", year_diff, " , month, day, hour, TMP
FROM dfTMP
WHERE date between ",
as.numeric(as.Date(startdate2)),
" AND ",
as.numeric(as.Date(enddate2)),
""))
dfPET1 <- sqldf(paste0("SELECT year, month, day, hour, PET
FROM dfPET
WHERE date between ",
as.numeric(as.Date(startdate1)),
" AND ",
as.numeric(as.Date(enddate1)),
""))
dfPET2 <- sqldf(paste0("SELECT year + ", year_diff, " , month, day, hour, PET
FROM dfPET
WHERE date between ",
as.numeric(as.Date(startdate2)),
" AND ",
as.numeric(as.Date(enddate2)),
""))
dfPRC1 <- sqldf(paste0("SELECT year, month, day, hour, PRC
FROM dfPRC
WHERE date between ",
as.numeric(as.Date(startdate1)),
" AND ",
as.numeric(as.Date(enddate1)),
""))
dfPRC2 <- sqldf(paste0("SELECT year + ", year_diff, " , month, day, hour, PRC
FROM dfPRC
WHERE date between ",
as.numeric(as.Date(startdate2)),
" AND ",
as.numeric(as.Date(enddate2)),
""))
dfWND1 <- sqldf(paste0("SELECT year, month, day, hour, WND
FROM dfWND
WHERE date between ",
as.numeric(as.Date(startdate1)),
" AND ",
as.numeric(as.Date(enddate1)),
""))
dfWND2 <- sqldf(paste0("SELECT year + ", year_diff, " , month, day, hour, WND
FROM dfWND
WHERE date between ",
as.numeric(as.Date(startdate2)),
" AND ",
as.numeric(as.Date(enddate2)),
""))
dfDPT1 <- sqldf(paste0("SELECT year, month, day, hour, DPT
FROM dfDPT
WHERE date between ",
as.numeric(as.Date(startdate1)),
" AND ",
as.numeric(as.Date(enddate1)),
""))
dfDPT2 <- sqldf(paste0("SELECT year + ", year_diff, " , month, day, hour, DPT
FROM dfDPT
WHERE date between ",
as.numeric(as.Date(startdate2)),
" AND ",
as.numeric(as.Date(enddate2)),
""))
# renaming columns to match before merging timeseries tables
colnames(dfRAD2) = c("year","month","day","hour","RAD")
colnames(dfTMP2) = c("year","month","day","hour","TMP")
colnames(dfPET2) = c("year","month","day","hour","PET")
colnames(dfPRC2) = c("year","month","day","hour","PRC")
colnames(dfWND2) = c("year","month","day","hour","WND")
colnames(dfDPT2) = c("year","month","day","hour","DPT")
# combining two timeseries
dfRAD_MASH <- rbind(dfRAD1, dfRAD2)
dfTMP_MASH <- rbind(dfTMP1, dfTMP2)
dfPET_MASH <- rbind(dfPET1, dfPET2)
dfPRC_MASH <- rbind(dfPRC1, dfPRC2)
dfWND_MASH <- rbind(dfWND1, dfWND2)
dfDPT_MASH <- rbind(dfDPT1, dfDPT2)
# return new time series as list
dfSYNTHETIC <- list(
"RAD" = dfRAD_MASH,
"TMP" = dfTMP_MASH,
"PET" = dfPET_MASH,
"PRC" = dfPRC_MASH,
"WND" = dfWND_MASH,
"DPT" = dfDPT_MASH)
return(dfSYNTHETIC)
}
# posting timeseries function
# inputs a land segment
# inputs two start dates and end dates
# inputs a lseg_csv synthetic timeseries for given dates (output of generate_synthetic_timesieries function)
# inputs saving directory
# posts new synthetic timeseries to terminal for wdm generation
post_synthetic_timeseries <- function(landseg, startdate1, enddate1, startdate2, enddate2, lseg_csv, dir){
# create mashup date format for saving
mashupdate <- paste0(substring(startdate1, 1, 4), substring(startdate1, 6, 7), substring(startdate1, 9, 10), "00-",
substring(enddate2, 1, 4), substring(enddate2, 6, 7), substring(enddate2, 9, 10), "23")
# saving and posting new timeseries
# first line is for local testing
# second line saves to ouput directory on linux
write.table(lseg_csv$RAD, paste0("C:/Users/kylew/Documents/HARP/NLDAS/mashups/", mashupdate, landseg, ".RAD"),
row.names = FALSE, col.names = FALSE, sep = ",")
#write.table(lseg_csv$RAD,paste0(dir, mashupdate, landseg,".RAD"),
# row.names = FALSE, col.names = FALSE, sep = ",")
write.table(lseg_csv$TMP, paste0("C:/Users/kylew/Documents/HARP/NLDAS/mashups/", mashupdate, landseg, ".TMP"),
row.names = FALSE, col.names = FALSE, sep = ",")
#write.table(lseg_csv$TMP,paste0(dir, mashupdate, landseg,".TMP"),
# row.names = FALSE, col.names = FALSE, sep = ",")
write.table(lseg_csv$PET, paste0("C:/Users/kylew/Documents/HARP/NLDAS/mashups/", mashupdate, landseg, ".PET"),
row.names = FALSE, col.names = FALSE, sep = ",")
#write.table(lseg_csv$PET,paste0(dir, mashupdate, landseg,".PET"),
# row.names = FALSE, col.names = FALSE, sep = ",")
write.table(lseg_csv$PRC, paste0("C:/Users/kylew/Documents/HARP/NLDAS/mashups/", mashupdate, landseg, ".PRC"),
row.names = FALSE, col.names = FALSE, sep = ",")
#write.table(lseg_csv$PRC,paste0(dir, mashupdate, landseg,".PRC"),
# row.names = FALSE, col.names = FALSE, sep = ",")
write.table(lseg_csv$WND, paste0("C:/Users/kylew/Documents/HARP/NLDAS/mashups/", mashupdate, landseg, ".WND"),
row.names = FALSE, col.names = FALSE, sep = ",")
#write.table(lseg_csv$WND,paste0(dir, mashupdate, landseg,".WND"),
# row.names = FALSE, col.names = FALSE, sep = ",")
write.table(lseg_csv$DPT, paste0("C:/Users/kylew/Documents/HARP/NLDAS/mashups/", mashupdate, landseg, ".DPT"),
row.names = FALSE, col.names = FALSE, sep = ",")
#write.table(lseg_csv$DPT,paste0(dir, mashupdate, landseg,".DPT"),
# row.names = FALSE, col.names = FALSE, sep = ",")
}