Chapter 4 Accessing weather data in R
4.1 Objective: To be able to demonstrate how to get TERRA REF meteorological data
This vignette shows how to read weather data for the month of January 2017 from the weather station at the University of Arizona’s Maricopa Agricultural Center into R. These data are stored online on the data management system Clowder, which is accessed using an API. More detailed information about the structure of the database and how API URLs are created is available in the weather tutorial. Data across time for one weather variable, temperature, is plotted in R. Then all eight of the weather variables have their times series plotted.
4.2 Read in data using R
A set of weather data can be accessed with a URL using the R package jsonlite
.
We are calling that library along with several others that will be used to clean and plot the data.
The data is read in by the fromJSON
function as a dataframe that also has two nested dataframes, called properties
and geometries
.
library(dplyr)
library(ggplot2)
library(jsonlite)
library(lubridate)
library(tidyr)
weather_all <- fromJSON('https://terraref.org/clowder/api/geostreams/datapoints?stream_id=46431&since=2017-01-02&until=2017-01-31', flatten = FALSE)
The geometries
dataframe is then pulled out from these data, which contains the datapoints from this stream.
This is combined with a transformed version of the end of the time period in the correct time zone from the stream.
weather_data <- weather_all$properties %>%
mutate(time = with_tz(ymd_hms(weather_all$end_time), "America/Phoenix"))
The temperature data, which is five minute averages for the entire month of January 2017, is used to calculate the growing degree days for each day. Growing degree days is a measurement that is used to predict when certain plant developmental phases happen. This new dataframe will be used in the last vignette to synthesize the trait, weather, and image data.
4.3 Plot data using R
The five minute summary weather variables in the weather_data
dataframe can be plotted across time, as shown below for temperature.
ggplot(data = weather_data) +
geom_point(aes(x = time, y = air_temperature), size = 0.1) +
labs(x = "Date", y = "Temperature (K)")
We can also plot the time series for all eight of the weather variables in a single figure.
We first have to rearrange the data to making plotting possible using R package ggplot
.
weather_data_long <- weather_data %>%
select(-source, -source_file) %>%
gather(weather_variable, observation, -time)
ggplot(data = weather_data_long) +
geom_point(aes(x = time, y = observation), size = 0.1) +
facet_wrap(~weather_variable, scales = "free_y") +
labs(x = "Date", y = "Weather variable")
You should now be able to find, get, and use weather data from the TERRA REF project via Clowder.