Plots by Sensors
This example demonstrates how to retrieve and summarize data from multiple sensors (plots) using R. It fetches data for a specified variable and date range, processes it, and generates a summary table of the first and last observation dates for each plot.
ICP Documentation
In this example, we request all plots (code_plot) by the types of sensor types (code_variable) definied.
After fetching the data from the mm_mem table in the icp_download schema, we create a summary table showing the sensor types, value counts available, first and last observation dates for each code of plots and sensor types combination.
Script
Environment Variable
Do not forget to create a .env file in the same directory as the R script including your API key.
If you do not have an API key yet, please see Getting Started
# Load necessary libraries
library(dotenv) # Load dotenv package
library(httr)
library(ggplot2)
# Get the directory of the current R script
script_dir <- dirname(sys.frames()[[1]]$ofile)
# Load environment variables from .env file
dotenv::load_dot_env(file = file.path(script_dir, ".env"))
# Configuration
data_schema <- 'icp_download'
dictionaries_schema <- 'icp_dictionaries'
table <- 'mm_mem'
host <- 'https://db.forstliche-umweltkontrolle.de/' # Supabase host
apikey <- Sys.getenv("API_KEY") # Get apikey from .env
# Check if apikey is available
if (apikey == "") {
stop("API_KEY is not set in the .env file")
}
# https://icp-forests.org/documentation/Dictionaries/d_variable.html
code_variables <- c('AT', 'SF', 'TF') # Air Temperature, Stemflow, Throughfall
# Function to fetch data by plot, variable, and date range
fetch_data <- function(schema, table, host, apikey, code_variables) {
url <- paste0(host, "rest/v1/", table,
"?code_variable=in.(", paste(code_variables, collapse = ","), ")",
"&select=code_plot,code_variable,date_observation"
)
# Accept-Profile header to specify schema
response <- httr::GET(url, httr::add_headers(
apikey = apikey,
Authorization = paste("Bearer", apikey),
`Accept-Profile` = schema,
`Accept` = 'text/csv'
))
if (response$status_code != 200) {
stop("Failed to fetch data: ", response$status_code, " - ", httr::content(response, as = "text"))
}
data <- httr::content(response, as = "parsed", type = "text/csv", encoding = "UTF-8")
return(data)
}
# Fetch data
data <- fetch_data(data_schema, table, host, apikey, code_variables)
# Create a frequency table of code_plots, code_variables, first and last date_observation
data_summary <- aggregate(date_observation ~ code_plot + code_variable, data = data,
FUN = function(x) c(first = as.Date(min(x)), last = as.Date(max(x)), count = length(x)))
data_summary <- do.call(data.frame, data_summary)
# Ensure the date columns are properly named and retain their Date class
colnames(data_summary) <- c("code_plot", "code_variable", "first_observation", "last_observation", "values_count")
data_summary$first_observation <- as.Date(data_summary$first_observation, origin = "1970-01-01")
data_summary$last_observation <- as.Date(data_summary$last_observation, origin = "1970-01-01")
# Print the summary table
print("Data summary:")
print(data_summary)API_KEY=[your_api_key]Output
The output is a summary table showing the first and last observation dates along with the count of values for each combination of plot and sensor type.
| code_plot | code_variable | first_observation | last_observation | values_count |
|---|---|---|---|---|
| 1101 | AT | 1991-01-01 | 2025-10-01 | 21120 |
| 1103 | AT | 2004-01-01 | 2006-12-31 | 1096 |
| 1201 | AT | 1991-01-01 | 2025-10-01 | 13057 |
| 1202 | AT | 1991-01-01 | 2025-10-01 | 18806 |
| 1203 | AT | 1991-01-01 | 2025-09-30 | 18879 |
| 1204 | AT | 1991-01-01 | 2025-09-30 | 18886 |
| 1205 | AT | 1991-01-01 | 2025-10-01 | 18516 |
| 1206 | AT | 1991-01-01 | 2025-10-01 | 13057 |
| 1207 | AT | 2017-01-01 | 2023-12-31 | 2556 |
| 1208 | AT | 1991-01-01 | 2025-10-01 | 16341 |
| 1209 | AT | 2018-01-01 | 2023-12-31 | 2190 |
| 1101 | TF | 2005-01-01 | 2023-12-31 | 6937 |
| 1102 | TF | 2005-01-01 | 2006-12-31 | 730 |
| 1103 | TF | 2005-01-01 | 2006-12-31 | 730 |
| 1201 | TF | 2003-01-01 | 2007-12-31 | 1826 |
| 1202 | TF | 2003-01-01 | 2023-11-07 | 7600 |
| 1203 | TF | 2003-01-01 | 2023-12-31 | 7655 |
| 1204 | TF | 2003-01-01 | 2023-12-31 | 7666 |
| 1205 | TF | 2003-01-01 | 2023-12-31 | 7663 |
| 1206 | TF | 2003-01-01 | 2007-12-31 | 1826 |
| 1208 | TF | 2014-01-01 | 2023-12-31 | 3648 |
| 1209 | TF | 2018-01-01 | 2023-12-31 | 2190 |