add a script to get all data from the 3 closest station

This commit is contained in:
Simon 2024-08-16 15:10:46 +02:00
parent abb61cea8c
commit 6e5a918e22

48
getCloseStat.R Normal file
View file

@ -0,0 +1,48 @@
library(jsonlite)
library(httr)
devtools::load_all()
source("secrets")
# Create a Bearer token header
headers.default <- add_headers(
accept = "*/*",
apikey = token4
)
getAllFromCoord <- function(coord,start_date,end_date,allstations,N=3,headers,base="https://public-api.meteofrance.fr"){
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),
error=function(e){print(e);NULL})
print(paste("done, sleep 5 sec"))
print(dim(allstat))
Sys.sleep(5)
return(allstat)
})
alldata= do.call("rbind.data.frame",alldata)
ids=three_station$Nom_usuel
names(ids)=three_station$Id_station
cbind.data.frame(alldata,Nom_usuel=ids[as.character(alldata[,1])])
}
allstations=read.csv("allstations.csv") #get all station
lacouch.coor <- c(45.3722971,5.6387118)
lamure.coor <- c(44.9167, 5.8000)
vignass.coor=c(44.8550665,5.8441789)
foreve=list()
for(y in 0:3){
enddate=format(Sys.Date()-y*365, "%Y-%m-%d")
startdate=format(Sys.Date()-(y*365+364), "%Y-%m-%d")
foreve=tryCatch(getAllFromCoord(vignass.coor,startdate,enddate,allstations,headers=headers.default,base="https://public-api.meteofrance.fr"),error=function(e)e)
}
test1=getAllFromCoord(vignass.coor,start_date=startdate,end_date=enddate,allstations,headers=headers.default)
cols=palette.colors()[1:length(unique(test1$Nom_usuel))]
names(cols)=unique(test1$Nom_usuel)
plot(getDate(test1[,2]),test1[,3],pch=20,col=cols[test1$Nom_usuel],cex=2)
legend("topleft",col=cols,legend=names(cols),pch=20,cex=2)
abline(v=as.numeric(as.POSIXlt("2024-08-07",format="%Y-%m-%d")),lwd=3,col="red")