-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetfrost_stations.R
37 lines (23 loc) · 951 Bytes
/
getfrost_stations.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
# Function: get dataframe with all stations in either municipality, county or country
#
# 25 Aug 2020
#
# Examples:
# df <- getfrost_stations("county", "TROMS OG FINNMARK")
# df <- getfrost_stations("municipality", "TROMSØ")
# df <- getfrost_stations("country", "NORGE")
#
# NB: add your client_id to a file named client_id.txt
# -----------------------------------------------------------------------------------
library(jsonlite)
library(tidyr)
library(readr)
# -----------------------------------------------------------------------------------
getfrost_stations <- function(area_type, area){
client_id <- as.character(read_tsv("client_id.txt", col_names = TRUE, cols(client_id = col_character())))
url <- paste0("https://", client_id, "@frost.met.no/sources/v0.jsonld?", area_type, "=", area)
output <- try(fromJSON(URLencode(url), flatten = TRUE))
df <- unnest(output$data, cols = c())
return(df)
}