Skip to content

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

R
# 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)
bash
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_plotcode_variablefirst_observationlast_observationvalues_count
1101AT1991-01-012025-10-0121120
1103AT2004-01-012006-12-311096
1201AT1991-01-012025-10-0113057
1202AT1991-01-012025-10-0118806
1203AT1991-01-012025-09-3018879
1204AT1991-01-012025-09-3018886
1205AT1991-01-012025-10-0118516
1206AT1991-01-012025-10-0113057
1207AT2017-01-012023-12-312556
1208AT1991-01-012025-10-0116341
1209AT2018-01-012023-12-312190
1101TF2005-01-012023-12-316937
1102TF2005-01-012006-12-31730
1103TF2005-01-012006-12-31730
1201TF2003-01-012007-12-311826
1202TF2003-01-012023-11-077600
1203TF2003-01-012023-12-317655
1204TF2003-01-012023-12-317666
1205TF2003-01-012023-12-317663
1206TF2003-01-012007-12-311826
1208TF2014-01-012023-12-313648
1209TF2018-01-012023-12-312190