-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetfrost_obs_region.R
48 lines (35 loc) · 1.78 KB
/
getfrost_obs_region.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
#
# Function: get regional observations from frost.met.no
#
# March 2021 / June 2021
#
# List of all regions: https://frost.met.no/sources/v0.jsonld?types=RegionDataset
# List of elements Norway : https://frost.met.no/observations/availableTimeSeries/v0.jsonld?sources=GR0&fields=sourceId,elementId,validFrom,validTo
# Examples:
# Norway: getfrost_obs_region("GR0","1900-01-01","2020-01-01","mean(air_temperature_anomaly P1Y 1961_1990)")
# Trøndelag: getfrost_obs_region("GR4","2000-01-01","2020-01-01","sum(precipitation_amount P1M)")
# -------------------------------------------------------------------------------------------------
library(jsonlite)
library(tidyr)
library(readr)
source("~/projects/frost/R_scripts/getfrost/err_messages.R")
# -------------------------------------------------------------------------------------------------
# get observations from Frost
getfrost_obs_region <- function(region, start, stop, element){
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/observations/v0.jsonld?",
"sources=", region,
"&referencetime=", start, "T00:00/", stop, "T23:59",
"&elements=", element)
cat("------------------------------------------------------------------\n")
output <- try(fromJSON(URLencode(url), flatten = TRUE))
if (class(output) != 'try-error') {
print(paste0("Observations retrieved from frost.met.no! (", object.size(output), " bytes)"))
} else {
err_messages()
}
obs <- unnest(output$data, cols = c(observations))
obs$referenceTime <- as.POSIXct(strptime(x = as.character(obs$referenceTime), format = "%Y-%m-%dT%H:%M:%S", tz = "UTC")) # transform date format
return(obs)
}