beaumontmeteo/R/getAllFromCoord.R
2026-04-11 09:24:56 +01:00

57 lines
2.3 KiB
R

combine_station_payloads <- function(station_payloads, station_lookup) {
station_payloads <- Filter(function(x) !is.null(x) && nrow(x), station_payloads)
if (!length(station_payloads)) {
return(NULL)
}
combined <- do.call("rbind.data.frame", station_payloads)
station_column <- c("POSTE", "geo_id_insee")
station_column <- station_column[station_column %in% names(combined)][1]
if (is.na(station_column)) {
return(combined)
}
combined$Nom_usuel <- unname(station_lookup[as.character(combined[[station_column]])])
combined
}
# Define function
getAllFromCoord <- function(coord, start_date, end_date, allstations, N = 3, headers, base = "https://public-api.meteofrance.fr",btw_station_sleep = 1, within_statio_sleep=5,endpoint="public/DPClim/v1/") {
three_station = getIdFromCoords(coord, allstations, N = N)
alldata = lapply(three_station$Id_station, function(statid) {
print(paste("recuperer station", statid))
allstat = tryCatch(
getStationData(start_date = start_date, end_date = end_date, station_id = statid, headers = headers, base = base, timesleep=within_statio_sleep,dpclim=endpoint),
error = function(e) {print(e); NULL}
)
print(paste("done, sleep",btw_station_sleep,"sec"))
print(dim(allstat))
Sys.sleep(btw_station_sleep)
return(allstat)
})
ids = three_station$Nom_usuel
names(ids) = three_station$Id_station
combine_station_payloads(alldata, ids)
}
# Define function
getAllFromCoordDPObs <- function(coord, start_date, end_date, allstations, N = 3, headers, base = "https://public-api.meteofrance.fr",btw_station_sleep = 1, within_statio_sleep=5,endpoint="public/DPClim/v1/") {
three_station = getIdFromCoords(coord, allstations, N = N)
alldata = lapply(three_station$Id_station, function(statid) {
print(paste("recuperer station", statid))
allstat = tryCatch(
getStationDataDPObs(start_date = start_date, end_date = end_date, station_id = statid, headers = headers, base = base, timesleep=within_statio_sleep,dpclim=endpoint),
error = function(e) {print(e); NULL}
)
print(paste("done, sleep",btw_station_sleep,"sec"))
print(dim(allstat))
Sys.sleep(btw_station_sleep)
return(allstat)
})
ids = three_station$Nom_usuel
names(ids) = three_station$Id_station
combine_station_payloads(alldata, ids)
}