-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActualizar_info_BelesarV2.R
381 lines (281 loc) · 12.3 KB
/
Actualizar_info_BelesarV2.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
library(here)
source(here::here('libraries.R'))
# Función de descarga -----------------------------------------------------
Down_E001_Belesar<- function(){
###LLUVIA
url<- "http://saih.chminosil.es/index.php?url=/datos/graficas_numeros/tag:E001_ACPA01H&historia="
Lluvia_list<- list()
i<- 0
while(TRUE) {
url1<- paste0(url,i)
Lluvia<- httr::GET(url1)
html_encoded<-content(Lluvia, "text", encoding = "ISO-8859-1")
Belesar_real<-readHTMLTable(htmlParse(html_encoded), rm_nodata_cols = F)
if(length(Belesar_real)==1){break}else{
Belesar_precipitacion<- Belesar_real[[2]]
colnames(Belesar_precipitacion)<- c("Date", "mm")
Belesar_precipitacion<- Belesar_precipitacion [2:length(Belesar_precipitacion$Date),]
Belesar_precipitacion$Date<- dmy_hm(Belesar_precipitacion$Date,tz=Sys.timezone())
Belesar_precipitacion$Date<- with_tz(Belesar_precipitacion$Date, tzone="UTC")
j<- i+1
Lluvia_list[[j]]<- Belesar_precipitacion
i<- i+1
}
}
###NIVEL
url<- "http://saih.chminosil.es/index.php?url=/datos/graficas_numeros/tag:E001_AINEMBA&historia="
Nivel_list<- list()
i<- 0
while(TRUE) {
url1<- paste0(url,i)
Nivel<- httr::GET(url1)
html_encoded<-content(Nivel, "text", encoding = "ISO-8859-1")
Belesar_real<-readHTMLTable(htmlParse(html_encoded), rm_nodata_cols = F)
if(length(Belesar_real)==1){break}else{
Belesar_nivel<- Belesar_real[[2]]
colnames(Belesar_nivel)<- c("Date", "msnm")
Belesar_nivel<- Belesar_nivel [2:length(Belesar_nivel$Date),]
Belesar_nivel$Date<- dmy_hm(Belesar_nivel$Date,tz=Sys.timezone())
Belesar_nivel$Date<- with_tz(Belesar_nivel$Date, tzone="UTC")
j<- i+1
Nivel_list[[j]]<- Belesar_nivel
i<- i+1
}
}
###TEMP
url<- "http://saih.chminosil.es/index.php?url=/datos/graficas_numeros/tag:E001_AITEMEX&historia="
Temp_list<- list()
i<- 0
while(TRUE) {
url1<- paste0(url,i)
Temp<- httr::GET(url1)
html_encoded<-content(Temp, "text", encoding = "ISO-8859-1")
Belesar_real<-readHTMLTable(htmlParse(html_encoded), rm_nodata_cols = F)
if(length(Belesar_real)==1){break}else{
Belesar_Temp<- Belesar_real[[2]]
colnames(Belesar_Temp)<- c("Date", "Temp[ºC]")
Belesar_Temp<- Belesar_Temp [2:length(Belesar_Temp$Date),]
Belesar_Temp$Date<- dmy_hm(Belesar_Temp$Date, tz=Sys.timezone())
Belesar_Temp$Date<- with_tz(Belesar_Temp$Date, tzone="UTC")
j<- i+1
Temp_list[[j]]<- Belesar_Temp
i<- i+1
}
}
###Vol
url<- "http://saih.chminosil.es/index.php?url=/datos/graficas_numeros/tag:E001_ACVEMBA&historia="
Vol_list<- list()
i<- 0
while(TRUE) {
url1<- paste0(url,i)
Vol<- httr::GET(url1)
html_encoded<-content(Vol, "text", encoding = "ISO-8859-1")
Belesar_real<-readHTMLTable(htmlParse(html_encoded), rm_nodata_cols = F)
if(length(Belesar_real)==1){break}else{
Belesar_Vol<- Belesar_real[[2]]
colnames(Belesar_Vol)<- c("Date", "Vol[Hm³]")
Belesar_Vol<- Belesar_Vol [2:length(Belesar_Vol$Date),]
Belesar_Vol$Date<- dmy_hm(Belesar_Vol$Date, tz=Sys.timezone())
Belesar_Vol$Date<- with_tz(Belesar_Vol$Date, tzone="UTC")
j<- i+1
Vol_list[[j]]<- Belesar_Vol
i<- i+1
}
}
###PEr
url<- "http://saih.chminosil.es/index.php?url=/datos/graficas_numeros/tag:E001_ACPORCE&historia="
Per_list<- list()
i<- 0
while(TRUE) {
url1<- paste0(url,i)
Porcentaje<- httr::GET(url1)
html_encoded<-content(Porcentaje, "text", encoding = "ISO-8859-1")
Belesar_real<-readHTMLTable(htmlParse(html_encoded), rm_nodata_cols = F)
if(length(Belesar_real)==1){break}else{
Belesar_Porcentaje<- Belesar_real[[2]]
colnames(Belesar_Porcentaje)<- c("Date", "Porcentaje[%]")
Belesar_Porcentaje<- Belesar_Porcentaje [2:length(Belesar_Porcentaje$Date),]
Belesar_Porcentaje$Date<- dmy_hm(Belesar_Porcentaje$Date, tz=Sys.timezone())
Belesar_Porcentaje$Date<- with_tz(Belesar_Porcentaje$Date, tzone="UTC")
j<- i+1
Per_list[[j]]<- Belesar_Porcentaje
i<- i+1
}
}
df_lluvia <- bind_rows(Lluvia_list)
df_per <- bind_rows(Per_list)
df_vol <- bind_rows(Vol_list)
df_nivel <- bind_rows(Nivel_list)
df_temp <- bind_rows(Temp_list)
Belesar_total<- as.data.frame(cbind(df_lluvia,
df_nivel$msnm,
df_temp$`Temp[ºC]`,
df_vol$`Vol[Hm³]`,
df_per$`Porcentaje[%]`))
colnames(Belesar_total)<- c("Date", "Lluvia[mm]", "Nivel[msnm]", "Temp[ºC]","Vol[Hm³]","Vol[%]")
nombre<- str_replace(as.character(ymd_hms(now())), " ", "_")
path<- here::here('Data/Parques/Belesar/Historico/WEB/')
path1<- paste0(path, "BelesarE001_", nombre,".RDS")
path_base<- paste0(path, "BelesarE001_", nombre)
n=2
while(TRUE){
if(file.exists(path1)){
path1<- paste0(path_base,"_V",n,".RDS")}else{break}
if(file.exists(path1)){n<- n+1}}
saveRDS(Belesar_total, file = path1)
}
Down_E001_Belesar()
# Juntar y guardar histórico ----------------------------------------------
#Accedemos al último hiscorico guardado
Hist_path<- list.files(here::here('Data/Parques/Belesar/Historico/WEB/'),
full.names = T) %>% .[str_detect(., "HIST_WEB_")] %>%
.[which.max(
str_remove(str_remove(., "HIST_WEB_"), ".RDS") %>%
str_replace(.,"_"," ") %>% ymd_hms())]
Hist_web<- readRDS(Hist_path)
##Accedemos al ultimo archivo descargado de la web
Last_Web<- list.files(here::here('Data/Parques/Belesar/Historico/WEB'),
full.names = T) %>%
.[str_detect(.,"E001")] %>% .[str_detect(., ":")] %>%
.[which.max(
str_remove(str_remove(., "BelesarE001_"), ".RDS") %>%
str_replace(.,"_"," ") %>% ymd_hms())]
WEB<- readRDS(Last_Web) %>% cbind(ymd_hms(.[,1]), apply(.[,2:length(.)],2,
function(x)
as.numeric(as.character(str_replace_all( x,",", "."))) )) %>%
as.data.frame() %>% .[,7:12]
colnames(WEB)<- c("Date", "lluvia","nivel",
"Temp", "Vol", "porcentaje")
completando<- Hist_web[!Hist_web$Date%in%WEB$Date,]
completado<- bind_rows(WEB, completando)
completado<- completado[order(completado$Date),]
path_hist<- here::here('Data/Parques/Belesar/Historico/WEB/')
name<- paste0("HIST_WEB_",str_replace(as.character(ymd_hms(now())), " ", "_"), ".RDS")
path_total<- paste0(path_hist, name)
saveRDS(completado, path_total)
# Actualizar histórico WRF ------------------------------------------------
#Listamos archivos dentro de la carpeta de Belesar
All_files_Belesar<- list.files(here::here('Data/Parques/Belesar/'),
recursive = F, full.names = T) %>%
.[str_detect(., ".RDS")] %>% .[!str_detect(., "E001")] %>% .[(length(.)-1):length(.)]
#Construimos Lista para cada instante de tiempo
Lista_localizacion<- list()
for (i in 1:length(All_files_Belesar)) {
Belesar_data<- readRDS(All_files_Belesar[i])
Belesar_lolat<- lon_lat_df_ls(Belesar_data)
Belesar_lolat1<- lapply(Belesar_lolat, uv_transformation)
Belesar_rain<- lapply(Belesar_lolat1, extract_rain_data2)
Lista_localizacion[[i]]<- Belesar_rain
}
#Nombramos la lista
names_fechas<- sapply(All_files_Belesar, function(x){
r<- str_split(x, "/")
return(str_remove(str_remove(r[[1]][length(r[[1]])], ".RDS"), "Belesar_"))
})
names(Lista_localizacion)<- names_fechas
#Creamos una lista por localización
Lista_localizacion2<- list()
for (i in 1:length(Lista_localizacion[[1]])) {
Lista_localizacion2[[i]]<- lapply(Lista_localizacion,
function(x) return(x[[i]]))
}
#Nombramos la lista
names(Lista_localizacion2)<- names(Lista_localizacion[[1]])
#Juntamos todos los Dataframes
vector_localizacion<- str_split(names(Lista_localizacion2), "__") %>%
sapply(., function(x){x[1]}) %>% as.numeric()
Lista_total_MF<- lapply(Lista_localizacion2, function(x) bind_rows(x))
for (i in 1:length(Lista_total_MF)){
x<- Lista_total_MF[[i]]
Lista_total_MF[[i]]<- x[which(!(x$lon > (vector_localizacion[i]+0.05))),]
}
#creamos dos data.frames... uno para D1 y otro para D2
Lista_d1_d2_loc<- list()
for (i in 1:length(Lista_total_MF)) {
p<- Lista_total_MF[[i]]
d1<- p[duplicated(p$fechas),]
d1<-d1[!duplicated(d1$fechas),]
d2<- p[!duplicated(p$fechas),]
d2_qneed1<-d2[!(d2$fechas%in%d1$fechas),]
d1_2<-bind_rows(d1,d2_qneed1)
d1_2<-d1_2[order(d1_2$fechas),]
d2<-d2[order(d2$fechas),]
d1_2$pre_acum<- NULL
d2$pre_acum<- NULL
colnames(d1_2)<- c("Date", "LON", "LAT", "RAINC", "RAINNC","RAINSH", "T02_MEAN","PSFC","WS_MAX", "prep_hourly")
colnames(d2)<- c("Date", "LON", "LAT", "RAINC", "RAINNC","RAINSH", "T02_MEAN","PSFC","WS_MAX", "prep_hourly")
lista_loc_d12<- list(d1_2,d2)
names(lista_loc_d12)<- c("D1", "D2")
Lista_d1_d2_loc[[i]]<- lista_loc_d12
}
#nombramos la lsta
names(Lista_d1_d2_loc)<- names(Lista_total_MF)
Tabla_periodo1<- Return_periodo_Belesar()
colnames(Tabla_periodo1)<- c("Date", "LON", "LAT", "RAINC", "RAINNC","RAINSH", "T02_MEAN","PSFC","WS_MAX", "prep_hourly")
Tabla_periodo1<- Tabla_periodo1 %>% as.data.frame()
Lista_d1_d2_loc2<- list()
for (j in 1:length(Lista_d1_d2_loc)) {
prueba_list<- Lista_d1_d2_loc[[j]]
lista_retorno<- list()
for(i in 1:2){
prueba<- prueba_list[[i]]
lon_guay<- prueba$LON %>% table() %>% which.max() %>% names() %>% as.numeric()
prueba<- prueba[round(lon_guay,1)==round(prueba$LON, 1), ]
Tabla_periodo<- Tabla_periodo1[match(prueba$Date,Tabla_periodo1$Date),]
Tabla_periodo<- Tabla_periodo[complete.cases(Tabla_periodo$Date), ]
Tabla_periodo$LON[match(prueba$Date,Tabla_periodo$Date)]<- prueba$LON
Tabla_periodo$LAT[match(prueba$Date,Tabla_periodo$Date)] <- prueba$LAT
Tabla_periodo$RAINC[match(prueba$Date,Tabla_periodo$Date)] <- prueba$RAINC
Tabla_periodo$RAINNC[match(prueba$Date,Tabla_periodo$Date)] <- prueba$RAINNC
Tabla_periodo$RAINSH[match(prueba$Date,Tabla_periodo$Date)] <- prueba$RAINSH
Tabla_periodo$T02_MEAN[match(prueba$Date,Tabla_periodo$Date)] <- prueba$T02_MEAN
Tabla_periodo$PSFC[match(prueba$Date,Tabla_periodo$Date)] <- prueba$PSFC
Tabla_periodo$WS_MAX[match(prueba$Date,Tabla_periodo$Date)] <- prueba$WS_MAX
Tabla_periodo$prep_hourly[match(prueba$Date,Tabla_periodo$Date)] <- prueba$prep_hourly
lista_retorno[[i]]<- Tabla_periodo
}
names(lista_retorno)<- c("D1", "D2")
Lista_d1_d2_loc2[[j]]<- lista_retorno
}
names(Lista_d1_d2_loc2)<- names(Lista_d1_d2_loc)
#Creamos lista con las variables afinadas
Lista_d1_d2_loc3<- lapply(Lista_d1_d2_loc2, function(x){
x[[1]]$RAINC<- NULL
x[[1]]$RAINNC<- NULL
x[[1]]$RAINSH<- NULL
x[[2]]$RAINC<- NULL
x[[2]]$RAINNC<- NULL
x[[2]]$RAINSH<- NULL
return(x)})
names(Lista_d1_d2_loc3)<- names(Lista_d1_d2_loc2)
NEW_WRF<- Lista_d1_d2_loc3
LAST_WRF<- list.files(here::here('Data/Parques/Belesar/Historico/WEB/PM'), full.names = T) %>%
.[str_detect(., "WRF")] %>%
.[length(.)] %>% readRDS()
MERGE_WRF<- list()
for (i in 1:length(NEW_WRF)) {
D1D2<- list()
for (j in 1:2) {
D1D2[[j]]<-bind_rows(LAST_WRF[[i]][[j]][which(!LAST_WRF[[i]][[j]]$Date%in%NEW_WRF[[i]][[j]]$Date), ],
NEW_WRF[[i]][[j]])
}
names(D1D2)<- names(LAST_WRF[[1]])
MERGE_WRF[[i]]<- D1D2
}
names(MERGE_WRF)<- names(LAST_WRF)
# FINALMENTE GUARDAMOS LOS NUEVOS HISTORICOS DE LA WEB Y WRF ------------------------------------------------------
Hist_path<- list.files(here::here('Data/Parques/Belesar/Historico/WEB/'),
full.names = T) %>% .[str_detect(., "HIST_WEB_")] %>%
.[which.max(
str_remove(str_remove(., "HIST_WEB_"), ".RDS") %>%
str_replace(.,"_"," ") %>% ymd_hms())]
Belesar_hist<- readRDS(Hist_path)
Belesar_WRF<- MERGE_WRF
path_PM<- here::here('Data/Parques/Belesar/Historico/WEB/PM/')
if(!dir.exists(path_PM)){dir.create(path_PM)}
nombre_hist<- as.character(now()) %>% str_replace(.," ","--") %>%
paste0("Obs_",., ".RDS")
nombre_WRF<- as.character(now()) %>% str_replace(.," ","--") %>%
paste0("WRF_",., ".RDS")
saveRDS(Belesar_hist, paste0(path_PM, nombre_hist))
saveRDS(Belesar_WRF, paste0(path_PM, nombre_WRF))