Accessing ERDDAP data with R#

R Binder Badge

The following notebook is a quick demostration on how to use R to access the ERDDAP server data. This is designed for users that prefer the programing interface to be R. There are already couple of great resource related to preprocessing and visualizing data in R. Therefore, the notebook will not repeat that part of the code but focusing on how to accessing the data from a R interface. The resources is listed below

Launch R in Jupyterlab

In addition to the standard R code demonstrated below, our current interface leverages JupyterLab to execute R code using the R kernel. To enable this functionality, we utilize the environment.yml file to create a Conda/Mamba environment. The primary objective is to install the r-irkernel package within this environment.

This installation of r-irkernel through Conda/Mamba ensures that the R kernel becomes available as an option when launching JupyterLab. Selecting the R kernel empowers users to utilize the JupyterLab interface for running R code effortlessly.

Packages used#

To use R to access the ERDDAP server directly from your R script or R-notebook, you will need

  • rerddap

There are couple of ways to install the rerddap package, here we provide two popular ways below

  1. In the R environment, install.packages('rerddap')

  2. In the conda environment for different package version control, conda install r-rerddap (many r package can be install this way by adding “r-” in front of the package name)

Import R packages#

The way to import packages to the current R environment is to use the require or library functions Both functions will import the package but with subtle different of

The require() and library() functions can both be used to load packages in R, but they have one subtle difference:

  • require() will output a warning if a package is not installed and then continue to execute the code.

  • library() will output an error and stop the execution of the code.

Detail explanation cna be find here

library("rerddap")

Direct ERDDAP server based search of datasets#

The rerddap::ed_search helps user search for data directly on a ERDDAP server. It give user an output of all dataset full names and the associated dataset_id which is important for the downloading. Here we demostrate the ed_search function with query of sea surface temperature (sst) dataset.

whichSST <- rerddap::ed_search(query = "sst")
whichSST
# A tibble: 980 × 2
   title                                                              dataset_id
   <chr>                                                              <chr>     
 1 COBE SST Analysis (sst.mon.ltm.1981-2010), 1.0°, 0001              noaa_psl_…
 2 COBE SST Analysis (sst.mon.ltm.1991-2020), 1.0°, 0001              noaa_psl_…
 3 COBE SST Analysis (sst.mon.mean), 1.0°, 1891-present               noaa_psl_…
 4 HadISST Average Sea Surface Temperature, 1°, Global, Monthly, 187… erdHadISST
 5 HadISST Average Sea Surface Temperature, 1°, Global, Monthly, 187… erdHadISS…
 6 HadISST Sea Ice Component, 1°, Global, Monthly, 1870-present       erdHadISS…
 7 HadISST Sea Ice Component, 1°, Global, Monthly, 1870-present, Lon… erdHadISS…
 8 NOAA ERSSTv4 (in situ only), 2°, Global, Monthly, 1854-2020        nceiErsst…
 9 NOAA ERSSTv4 (in situ only), 2°, Global, Monthly, 1854-2020, Lon+… nceiErsst…
10 NOAA ERSSTv5 (in situ only) (sst.mnmean), 2.0°, 1854-present       noaa_psl_…
# ℹ 970 more rows

If the url argument is not specified, the search will be the default ERDDAP server which is located at NOAA NMFS SWFSC ERD. To switch from the default server to a specific server, one can set the server url manually in one of the argument as the following.

whichSST2 <- rerddap::ed_search(query = "sst", url = "https://erddap.ifremer.fr/erddap/")

Once the url is changed to other server, a different set of SST dataset can been seen below.

whichSST2
# A tibble: 1 × 2
  title                        dataset_id             
  <chr>                        <chr>                  
1 Daily MUR SST, Final product SST_Anomalies_Caledonie

Information about the dataset#

For a quick peek of the data meta data, one can also use the info function

info('SST_Anomalies_Caledonie',url = "https://erddap.ifremer.fr/erddap/")
<ERDDAP info> SST_Anomalies_Caledonie 
 Base URL: https://erddap.ifremer.fr/erddap 
 Dataset Type: griddap 
 Dimensions (range):  
     time: (2014-01-16T09:00:00Z, 2021-12-16T09:00:00Z) 
     latitude: (-27.0, -14.01) 
     longitude: (155.01, 175.01) 
 Variables:  
     analysed_sst: 
         Units: kelvin 

Subsetting and download to local memory#

Here we focus on downloading a gridded dataset in the default ERDDAP server and subsetting it in the notebook.

sstInfo <- info('nceiErsstv5_LonPM180')
erSST <- griddap(sstInfo, latitude = c(22., 51.), longitude = c(-140., -105), time = c("2017-01-01", "2017-01-02"), fields = 'ssta')
info() output passed to x; setting base url to: https://upwell.pfeg.noaa.gov/erddap

erSST

The data can be viewed or used in the form of a data frame in R as following

erSST$data
A data.frame: 304 × 5
longitudelatitudedepthtimessta
<dbl[1d]><dbl[1d]><dbl[1d]><chr><dbl>
-1402202017-01-15T00:00:00Z0.5141125
-1382202017-01-15T00:00:00Z0.5668831
-1362202017-01-15T00:00:00Z0.6465950
-1342202017-01-15T00:00:00Z0.7129841
-1322202017-01-15T00:00:00Z0.7436638
-1302202017-01-15T00:00:00Z0.7662201
-1282202017-01-15T00:00:00Z0.8154736
-1262202017-01-15T00:00:00Z0.8591919
-1242202017-01-15T00:00:00Z0.8391647
-1222202017-01-15T00:00:00Z0.7444363
-1202202017-01-15T00:00:00Z0.6146202
-1182202017-01-15T00:00:00Z0.4916267
-1162202017-01-15T00:00:00Z0.3766174
-1142202017-01-15T00:00:00Z0.3021393
-1122202017-01-15T00:00:00Z0.2422123
-1102202017-01-15T00:00:00Z0.2063828
-1082202017-01-15T00:00:00Z0.2279358
-1062202017-01-15T00:00:00Z0.2696514
-1042202017-01-15T00:00:00Z NaN
-1402402017-01-15T00:00:00Z0.5489960
-1382402017-01-15T00:00:00Z0.5842934
-1362402017-01-15T00:00:00Z0.6270676
-1342402017-01-15T00:00:00Z0.6492863
-1322402017-01-15T00:00:00Z0.6305580
-1302402017-01-15T00:00:00Z0.6110039
-1282402017-01-15T00:00:00Z0.6338577
-1262402017-01-15T00:00:00Z0.6734467
-1242402017-01-15T00:00:00Z0.6693363
-1222402017-01-15T00:00:00Z0.6090870
-1202402017-01-15T00:00:00Z0.5179253
-1245002017-01-15T00:00:00Z0.3403406
-1225002017-01-15T00:00:00Z NaN
-1205002017-01-15T00:00:00Z NaN
-1185002017-01-15T00:00:00Z NaN
-1165002017-01-15T00:00:00Z NaN
-1145002017-01-15T00:00:00Z NaN
-1125002017-01-15T00:00:00Z NaN
-1105002017-01-15T00:00:00Z NaN
-1085002017-01-15T00:00:00Z NaN
-1065002017-01-15T00:00:00Z NaN
-1045002017-01-15T00:00:00Z NaN
-1405202017-01-15T00:00:00Z0.1521754
-1385202017-01-15T00:00:00Z0.1631517
-1365202017-01-15T00:00:00Z0.1961660
-1345202017-01-15T00:00:00Z0.2636414
-1325202017-01-15T00:00:00Z0.3504291
-1305202017-01-15T00:00:00Z0.4085178
-1285202017-01-15T00:00:00Z0.4275451
-1265202017-01-15T00:00:00Z NaN
-1245202017-01-15T00:00:00Z NaN
-1225202017-01-15T00:00:00Z NaN
-1205202017-01-15T00:00:00Z NaN
-1185202017-01-15T00:00:00Z NaN
-1165202017-01-15T00:00:00Z NaN
-1145202017-01-15T00:00:00Z NaN
-1125202017-01-15T00:00:00Z NaN
-1105202017-01-15T00:00:00Z NaN
-1085202017-01-15T00:00:00Z NaN
-1065202017-01-15T00:00:00Z NaN
-1045202017-01-15T00:00:00Z NaN

Getting all dataset information in a ERDDAP server#

The following steps show the way of listing out all dataset that is available in a ERDDAP using the tabledap function.

urlBase <- "https://gliders.ioos.us/erddap/"
dataInfo <- rerddap::info("allDatasets", url = urlBase)
allGlider <- tabledap(dataInfo)
info() output passed to x; setting base url to: https://gliders.ioos.us/erddap
Warning message in set_units(temp_table, dds):
“NAs introduced by coercion”
Warning message in set_units(temp_table, dds):
“NAs introduced by coercion”
allGlider[0:10,0:3]
A tabledap: 10 × 3
datasetIDaccessibleinstitution
<chr><chr><chr>
2allDatasets publicAxiom Docker Install
3amelia-20180501T0000 publicVirginia Institute of Marine Science - William & Mary
4amelia-20200825T1929 publicVirginia Institute of Marine Science - William & Mary
5amelia-20201015T1436 publicVirginia Institute of Marine Science - William & Mary
6amlr01-20181216T0641-delayedpublicNOAA SWFSC Antarctic Ecosystem Research Division
7amlr01-20191206T0452-delayedpublicNOAA SWFSC Antarctic Ecosystem Research Division
8amlr02-20181211T1233-delayedpublicNOAA SWFSC Antarctic Ecosystem Research Division
9amlr02-20191206T1236-delayedpublicNOAA SWFSC Antarctic Ecosystem Research Division
10amlr03-20191206T0529-delayedpublicNOAA SWFSC Antarctic Ecosystem Research Division
11angus-20190522T0000 publicSkidaway Institute of Oceanography