library(rerddap)
library(dplyr)
library(stringr)
library(ggplot2)
library(maps)Downloading Environmental Data using rerddap
This vignette will detail how to download environmental/oceanographic satellite data from ERDDAP servers using the rerddap package.
Load Packages
Install and load the necessary packages; rerddap to access ERDDAP servers with R, dplyr to manipulate the incoming data and ggplot2 plus maps to to create visuals. The stringr package is used to clean up received data later.
Select Data Product
Choose a data product from an available ERDDAP server. Ensure that the URL connects, by using the info() function from the rerddap package and viewing the information from the selected data product.
For this example, we’ll use the NOAA Coastwatch ERDDAP server - found at https://coastwatch.noaa.gov/erddap/ - and use the dataset titled: “Sea-Surface Temperature, NOAA ACSPO Daily Global 0.02° Gridded Super-collated SST # and Thermal Fronts Reanalysis, 2012-present, Daily (L3S-LEO Kelvin)”. The info() function will use the dataset’s ID from the linked server to pull an information summary:
info("noaacwLEOACSPOSSTL3SCDaily", url = "https://coastwatch.noaa.gov/erddap/")We see that this data product has three dimensions, which form the limits of the spatio-temporal range of the data: time, latitude and longitude; along with a number of variables that each contain environmental data for each of the dimensions. We will extract sea surface temperature (SST) from the variable named sea_surface_temperature. There are two methods to do this: downloading a NetCDF file and saving it to your PC’s storage, or loading the data straight into your R environment as a data frame.
Set Up Variables
Let’s explicitly set up all the variables we’ll need, including the dataset, URL, time and area bounds.
# Set data set ID and URL of chosen data product
dataset_id <- "noaacwLEOACSPOSSTL3SCDaily"
dataset_url <- "https://coastwatch.noaa.gov/erddap/"
# Set spatial and temporal bounds
lats = c(-38, -24)
longs = c(14, 35)
dates = c('2020-01-01', '2020-01-01') # This bound CAN be just one day if needed (but two values are still required)This sets up our functions to download just one day of data within the general bounds of southern Afrcia.
Download Data
1. Save .nc File
The first method includes saving a NetCDF file someone onto your storage using the griddap() function. The parameters of this include include the dataset ID, URL, and temporal and spatial bounds set earlier. Pay careful attention to the store parameter, and ensure that this is set to the exact spot on your local storage where you would like this .nc file to be saved. Additionally, ensure that the read parameter in this function is set to FALSE, so that the data is not read into your memory (RAM).
# This code chunk is commented out so that it is not run.
# griddap(
# datasetx = dataset_id,
# time = dates,
# latitude = lats,
# longitude = longs,
# url = dataset_url,
# store = disk(path = "~/ocean_stats/data"), # Edit pathway here
# read = FALSE
# )2. Load Data into Environment
sst_data <- griddap(
datasetx = dataset_id,
time = dates,
latitude = lats,
longitude = longs,
fields = "sea_surface_temperature", # Specifiacallty only pulls the SST variable
fmt = "csv", # To be read as a dataframe in the R environment
url = dataset_url,
store = memory()
) %>%
mutate(time = as.Date(stringr::str_remove(time, "T00:00:00Z"))) %>%
dplyr::rename(
t = time,
lat = latitude,
lon = longitude,
sst = sea_surface_temperature
) %>%
na.omit()
# The second half of the function (after the pipe symbol) uses dplyr functions to edit the resulting data
# frame into something a bit more useable than before.Plots
# Save plot as an object
sst_map <- ggplot(sst_data, aes(x = lon, y = lat)) +
geom_raster(aes(fill = sst)) +
annotation_borders(col = "black", fill = "cornsilk", linewidth = 0.6) +
coord_equal(xlim = longs, ylim = lats, expand = 0) +
# geom_point(
# data = coastal_towns,
# aes(x = lon, y = lat),
# shape = 21,
# size = 3,
# fill = "#db0e0eff"
# ) +
# geom_label(
# data = coastal_towns,
# aes(x = lon, y = lat, label = names),
# nudge_y = 0.5
# ) +
# scale_x_continuous(
# breaks = seq(15, 35, 5),
# labels = c("15°E", "20°E", "25°E", "30°E", "35°E"),
# position = "bottom"
# ) +
# scale_y_continuous(
# breaks = seq(-36, -24, 4),
# labels = c("36.0°S", "32.0°S", "28.0°S", "24.0°S"),
# position = "right"
# ) +
scale_fill_gradient(low = "#03064eff", high = "#35beebff") +
labs(
title = "SST around southern Africa",
#subtitle = sst_data$t,
x = "",
y = "",
fill = "SST (°C)",
#caption = "Sea Surface Temperature, NOAA Coral Reef Watch Daily Global 5km Satellite SST (CoralTemp), 1985-present, Daily"
) +
theme(
legend.position = c(0.5, 0.9),
legend.direction = "horizontal",
plot.title = element_text(face = "bold")
)
# FOR TESTING: show plot in viewer pane
sst_map