The dmir package provides a few helper functions to make
interaction with the DMI API easier. To use the functions in
dmir that calls the API you’ll need to have at least one
API key. To setup an API key follow the instruction here.
You’ll be needing a key for each service (one for climate data, one for
meteorological observation data etc.)
Once you have your API key store it in a safe location where you can
also find it again. To use it in scripts it is recommended that you
store it in your .Renviron file like so:
You can use usethis::edit_r_environ() to open the
.Renviron file if you use RStudio.
Once you’ve defined your API key in the environment file you can
access it in scripts with
Sys.getenv("NAME-OF-ENV-VAR").
There is a limit to how much data the API will allow you to get in one go and the resolution on particularly the meteorological observation data is high. So I recommend that you only get one parameter at a time and possibly also limit the time period you request. Use multiple calls to the API if you need more than that will cover.
The DMI API is documented here. Find the ID for the station you are interested and find the parameter ID for your parameter of interest. Information about stations and parameters are nested under the service (Climate data, Meteorological observation etc.).
To show how it works we will try to get the daily mean temperature for Daneborg which we can see here has station ID 04330.
daneborg <-
dmi_get_climate(
parameter = "mean_temp",
time_resolution = "month",
api_key = Sys.getenv("DMI_CLIMATE_API_KEY"),
station_id = "04330",
date_start = "2020-01-01",
date_end = "2020-12-31"
)This downloads a geoJSON file with the data. The file is stored in a temporary location. It can be read using the sf package.
daneborg |>
ggplot(aes(x = as.Date(from),
y = value)) +
geom_line() +
geom_point() +
labs(y = "Daily mean temperature (\u00B0C)",
x = "Date",
title = "Temperature during 2020 at Daneborg, Greenland",
caption = "Data downloaded from DMI via {dmir}") +
theme_minimal()And as you figured, it is pretty cold at Daneborg!