Accessing CEFI data with R#

R Binder Badge

The following notebook is a quick demostration on how to use R to access the OPeNDAP 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 OPeNDAP server directly from your R script or R-notebook, you will need

  • ncdf4

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

  1. In the R environment, install.packages('<package name>')

  2. In the conda/mamba environment for package version control, conda install r-<package name> or mamba install r-<package name> (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 can be find here

library("ncdf4")

Direct OPeNDAP server data access#

With the help of the ncdf4 package we are able to load the data that is hosted on the OPeNDAP server and grab the coordinate and attribute without the need to downloading the entire dataset.

  • first we need to find the URL that can provide the OPeNDAP access for R (this can be find on the THREDDS server here)

  • click on OPeNDAP option when clicking on any of the netcdf files

  • on top of the OPeNDAP form there is a URL for accessing the data

Tip

Understanding of the OPeNDAP server and what it provides is highly recommended before reading the following intructions.

Load the data to local memory#

Warning

There is a 500MB limit per request so making sure the data request each time is not over the limit. For loop to seperate the request is one of the solution to avoid the single time request becoming to large.

Here we only request a single slice in the time domain but the whole northeast Atlantic domain in the regional mom6 model. Since this is the part that actually downloading the data. It would takes a bit longer.

# Read the data into memory
timeslice = 1
lon <- ncvar_get(ncopendap, "lon")
lat <- ncvar_get(ncopendap, "lat")
time <- ncvar_get(ncopendap, "time",start = c(timeslice), count = c(1))

# Read a slice of the data into memory
sos <- ncvar_get(ncopendap, "sos", start = c(1, 1, timeslice), count = c(-1, -1, 1))
# the matrix output of sea surface salinity
sos
A matrix: 774 × 844 of type dbl
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NA36.0668136.0697436.0729236.0763236.0799836.0829136.0852236.0872536.0893434.7369634.7353934.7364734.7413134.7466634.7533434.7571634.75456 NANA
NA36.0629536.0666136.0704536.0745236.0789736.0831036.0864836.0892336.0915534.7404534.7380834.7382334.7423134.7470734.7541334.7597634.75694 NANA
NA36.0584936.0631436.0678036.0725736.0775936.0823736.0867236.0904636.0934734.7474634.7420134.7401234.7422734.7457634.7523434.7590234.75777 NANA
NA36.0536936.0592536.0647636.0703136.0759236.0811636.0861836.0907636.0946434.7554834.7488534.7441634.7436634.7453534.7509734.7575534.75734 NANA
NA36.0490236.0552636.0616136.0679236.0740236.0797736.0853036.0904436.0950134.7616034.7560834.7501534.7478134.7479034.7518134.7576634.7581934.75734NA
NA36.0440836.0505336.0574536.0645436.0714136.0778136.0839236.0895836.0946334.7655034.7615234.7572134.7539034.7528334.7546834.7590134.7602134.75845NA
NA36.0419036.0476636.0541336.0611136.0682836.0752236.0819936.0882736.0938034.7687634.7655134.7620734.7581634.7570634.7579634.7608734.7623934.76061NA
NA36.0401836.0460336.0521436.0586436.0656136.0726936.0798036.0865936.0926634.7698034.7654134.7618934.7591334.7587734.7606434.7635334.7641634.76292NA
NA36.0371736.0439436.0504036.0567836.0634536.0704736.0776936.0847936.0913234.7678834.7626534.7586934.7576834.7593834.7627034.7663334.7666534.76496NA
NA36.0335036.0410636.0481036.0548136.0615236.0684936.0757036.0828636.0896034.7639634.7609934.7604234.7603434.7613334.7647934.7682934.7690234.76764NA
NA36.0294336.0379136.0457436.0530336.0600536.0670936.0742536.0812836.0879134.7638634.7681834.7716634.7710034.7677634.7680834.7698634.7710034.77069NA
NA36.0245836.0336336.0420836.0501036.0577736.0652736.0726636.0797336.0862634.7676434.7781334.7832534.7829534.7777534.7734534.7720634.7730934.77344NA
NA36.0196336.0295036.0385936.0471136.0553236.0632836.0709736.0782136.0847934.7658934.7785534.7893334.7922034.7865934.7796734.7759734.7758734.77601NA
NA36.0157236.0260836.0355136.0442436.0525936.0609136.0689936.0766136.0834934.7587434.7730134.7884334.7942334.7935734.7838534.7800934.7791534.77828NA
NA36.0126236.0233936.0332336.0422136.0505736.0588636.0670536.0749136.0821434.7556434.7701634.7868634.7930734.7939134.7873734.7819934.7809634.78004NA
NA36.0088936.0196236.0295636.0389036.0476336.0562736.0647936.0730436.0807234.7557834.7705134.7871034.7915134.7927934.7892234.7822234.7812734.78144NA
NA36.0035736.0131836.0223136.0315636.0405536.0498036.0591036.0681836.0766534.7575634.7733834.7881134.7888034.78954 NA NA NA NANA
NA35.9972736.0068636.0154836.0236536.0319936.0406536.0497436.0590836.06821 NA NA NA NA NA NA NA NA NANA
NA35.9937536.0030136.0115036.0194836.0275536.0358036.0443636.0530236.06137 NA NA NA NA NA NA NA NA NANA
NA35.9906735.9997636.0084336.0167736.0249236.0331336.0414836.0498836.05798 NA NA NA NA NA NA NA NA NANA
NA35.9892935.9973336.0049336.0124036.0203236.0289236.0378536.0467936.05525 NA NA NA NA NA NA NA NA NANA
NA35.9885335.9956636.0027236.0096736.0169536.0248036.0332636.0421036.05079 NA NA NA NA NA NA NA NA NANA
NA35.9898435.9956236.0015136.0076036.0140336.0210936.0287136.0367236.04471 NA NA NA NA NA NA NA NA NANA
NA35.9923635.9969436.0018036.0071836.0129136.0192736.0261536.0334236.04070 NA NA NA NA NA NA NA NA NANA
NA35.9957235.9989136.0023036.0063536.0114036.0172836.0236936.0304736.03728 NA NA NA NA NA NA NA NA NANA
NA35.9994336.0019336.0045336.0075236.0114336.0167936.0226736.0289436.03537 NA NA NA NA NA NA NA NA NANA
NA36.0033936.0050636.0066836.0086836.0117536.0167836.0224536.0286136.03501 NA NA NA NA NA NA NA NA NANA
NA36.0058736.0073036.0089036.0109336.0138736.0184436.0239736.0301736.03662 NA NA NA NA NA NA NA NA NANA
NA36.0093736.0105936.0120636.0141036.0170336.0214036.0266836.0326736.03910 NA NA NA NA NA NA NA NA NANA
36.0097236.0103636.0116736.0140236.0175736.0223036.0274036.0331336.0395036.04632 NA NA NA NA NA NA NA NA NANA

Converting time array to datetime#

The origial time would be only in the unit of time “days since 1993-01-01”. In order to understand what is the actual time. We need to grab the time unit and the time number and convert to a datetime object.

# Get the units
tunits <- ncatt_get(ncopendap, "time", "units")
datesince <- tunits$value
datesince <- substr(datesince, nchar(datesince)-9, nchar(datesince))
datesince
'1993-01-01'
# convert the number to datetime (input should be in second while the time is in unit of days)
datetime_var <- as.POSIXct(time*86400, origin=datesince, tz="UTC")
datetime_var
[1] "1993-01-16 12:00:00 UTC"

Quick view of the data#

filled.contour(lon, lat, sos, main = paste("Sea surface salinity at ", datetime_var), xlab = "Longitude", ylab = "Latitude", levels = pretty(c(20,40), 20))
../../../_images/bee65a53cdd075d14006f1f961645027dcf41f0f47c8c4911c6f922c3d24e5a8.png

Convert the matrix to dataframe#

df <- expand.grid(X = lon, Y = lat)
data <- as.vector(t(sos))
df$Data <- data
names(df) <- c("lon", "lat", "sos")
df
A data.frame: 653256 × 3
lonlatsos
<dbl[1d]><dbl[1d]><dbl>
261.55775.272542NA
261.63845.272542NA
261.71915.272542NA
261.79985.272542NA
261.88045.272542NA
261.96115.272542NA
262.04185.272542NA
262.12255.272542NA
262.20315.272542NA
262.28385.272542NA
262.36455.272542NA
262.44525.272542NA
262.52585.272542NA
262.60655.272542NA
262.68725.272542NA
262.76795.272542NA
262.84855.272542NA
262.92925.272542NA
263.00995.272542NA
263.09065.272542NA
263.17135.272542NA
263.25195.272542NA
263.33265.272542NA
263.41335.272542NA
263.49405.272542NA
263.57465.272542NA
263.65535.272542NA
263.73605.272542NA
263.81675.272542NA
263.89735.272542NA
321.580458.16076NA
321.661158.16076NA
321.741858.16076NA
321.822458.16076NA
321.903158.16076NA
321.983858.16076NA
322.064558.16076NA
322.145158.16076NA
322.225858.16076NA
322.306558.16076NA
322.387258.16076NA
322.467958.16076NA
322.548558.16076NA
322.629258.16076NA
322.709958.16076NA
322.790658.16076NA
322.871258.16076NA
322.951958.16076NA
323.032658.16076NA
323.113358.16076NA
323.193958.16076NA
323.274658.16076NA
323.355358.16076NA
323.436058.16076NA
323.516658.16076NA
323.597358.16076NA
323.678058.16076NA
323.758758.16076NA
323.839358.16076NA
323.920058.16076NA

Load the data to local memory#

# Read the data into memory
timeslice = 1
lon <- ncvar_get(ncstaticopendap, "geolon")
lat <- ncvar_get(ncstaticopendap, "geolat")
x <- ncvar_get(ncopendap, "xh")
y <- ncvar_get(ncopendap, "yh")
time <- ncvar_get(ncopendap, "time",start = c(timeslice), count = c(1))

# Read a slice of the data into memory
sos <- ncvar_get(ncopendap, "sos", start = c(1, 1, timeslice), count = c(-1, -1, 1))
# the matrix output of sea surface salinity in raw grid
sos
A matrix: 775 × 845 of type dbl
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
NANANANANANANANANANANANANANANANANANANANA
36.0631636.0666636.0706936.0751636.0801736.0842936.0870136.0895836.0926536.0960734.8065934.8109834.8102234.7994834.7910834.7864234.7824334.7777534.7765134.77795
36.0583336.0630336.0679836.0732036.0790536.0845936.0887536.0918036.0946836.0976034.8030334.8075634.8066634.7964834.7867434.7822034.7797134.7770334.7750934.77502
36.0523836.0588036.0649336.0711436.0776736.0838536.0894236.0936136.0966736.0994134.7946634.7958034.7977834.7918734.7811134.7741634.7754734.7757634.7744734.77347
36.0467036.0542336.0614536.0687036.0759936.0827136.0891536.0945136.0982936.1012834.7862534.7866534.7857434.7818334.7769334.7710634.7720334.7736934.7740934.77260
36.0420636.0496736.0579336.0662336.0740936.0815036.0885036.0946136.0993736.1025534.7737434.7722734.7710234.7694334.7694834.7699934.7706334.7715534.7722934.77136
36.0375136.0445736.0532036.0624636.0714036.0796436.0873836.0940136.0995636.1031634.7588634.7569234.7556734.7578834.7631534.7678434.7693634.7700834.7706534.77069
36.0369036.0428936.0504036.0590836.0682136.0771436.0857836.0930936.0989636.1026534.7489934.7471134.7457834.7497634.7584534.7645634.7672734.7691134.7694734.77027
36.0338936.0414736.0489336.0569136.0656736.0746936.0838236.0918836.0981036.1014434.7415234.7381134.7377834.7415534.7495734.7573734.7627234.7672134.7685034.77018
36.0290836.0386236.0471936.0552036.0636036.0725236.0818536.0904836.0971136.1001934.7287634.7290734.7315834.7365834.7442734.7508334.7587334.7629234.7666134.76955
36.0246036.0351136.0445736.0531836.0617236.0705836.0798836.0886436.0958636.0987634.7209734.7235234.7281334.7343334.7412034.7481034.7566034.7603534.7641534.76849
36.0194536.0312836.0419336.0514036.0603836.0693336.0784536.0869836.0943136.0968434.7203134.7231334.7276034.7332334.7387634.7447734.7504734.7562334.7608034.76653
36.0137936.0265436.0377036.0481436.0580136.0676036.0769436.0853536.0926336.0943834.7247134.7269234.7305534.7343634.7380734.7427934.7461834.7515434.7565934.76956
36.0079836.0218836.0341136.0451036.0556136.0657736.0754036.0839436.0910536.0917134.7306734.7327334.7361634.7389534.7417234.7453134.7472534.7504334.7550434.77031
36.0037236.0183136.0310436.0422536.0528736.0634836.0736736.0826736.0897936.0895034.7348834.7370834.7403634.7434734.7465934.7502234.7522934.7522634.7522334.76383
36.0005636.0153936.0287236.0403736.0509836.0614836.0718236.0813136.0885336.0877334.7372334.7391834.7429434.7461534.7498634.7545134.7574334.7572034.7540834.75927
35.9966436.0116336.0247236.0368136.0479536.0589636.0697636.0798636.0874136.0867634.7393034.7400334.7429234.7456434.7495934.7554034.7596434.7607234.7560234.75869
35.9916236.0060336.0171936.0289836.0404236.0522836.0642236.0753936.0839636.0845634.7434934.7422434.7429334.7445134.7482134.7534834.7585134.7607034.7564534.75818
35.9855535.9996136.0111236.0212736.0317636.0426936.0544836.0664936.0755636.0770634.7492934.7465434.7456134.7458934.7482734.7529734.7572334.7589634.7560334.75731
35.9834635.9961536.0075036.0175936.0278136.0382536.0492536.0599836.0685136.0698534.7563434.7534134.7516434.7505134.7514334.7546734.7581434.7593034.7567434.75726
35.9809235.9929736.0043036.0149836.0253436.0357736.0464336.0570036.0650136.0656534.7612334.7581534.7569534.7560234.7559534.7577634.7600734.7610034.7589034.75813
35.9805935.9915236.0013236.0105536.0205836.0316036.0431936.0542936.0625636.0638434.7611734.7593834.7588934.7583534.7593634.7606834.7620434.7628634.7618234.75990
35.9817035.9903135.9994236.0081236.0173036.0272436.0383336.0496836.0584436.0609234.7576334.7574834.7585334.7597634.7619134.7645534.7649134.7645034.7637934.76223
35.9842135.9914635.9987036.0062636.0143636.0233336.0332836.0436136.0517936.0546534.7608034.7598234.7596534.7612634.7642634.7671234.7679634.7672734.7656834.76424
35.9877235.9937835.9994036.0060736.0133136.0213936.0304036.0398336.0471936.0496034.7734134.7701534.7664434.7652134.7671034.7688334.7696534.7696834.7683834.76686
35.9923535.9968136.0006036.0053536.0117236.0192536.0276736.0365036.0435036.0458034.7851834.7825134.7771334.7721434.7710834.7706534.7713334.7718434.7713834.77016
35.9969336.0002636.0033436.0068236.0117336.0186736.0263736.0347036.0413736.0440334.7933734.7928734.7857134.7797134.7773634.7737634.7738134.7742034.7740534.77278
36.0013836.0040536.0059436.0081336.0119936.0185836.0260936.0343936.0411036.0440334.7937434.7957534.7930834.7840034.7814434.7786434.7773834.7772134.7766434.77501
36.0044836.0063036.0080936.0104136.0141336.0201036.0276836.0360036.0430636.0464034.7924734.7940934.7938334.7880134.7836234.7812534.7808834.7806534.7784934.77674
36.0080236.0097736.0112436.0135736.0172936.0229836.0302136.0383936.0464036.0514034.7910734.7921234.7931234.7896334.7832734.7813034.7808934.7820934.7796634.77679
36.0097236.0105436.0125136.0166636.0226736.0291836.0368636.0455636.0539436.0597934.7878834.7878234.7906834.7896734.7830834.7817534.7816134.7836434.7803534.77749

Converting time array to datetime#

The origial time would be only in the unit of time “days since 1993-01-01”. In order to understand what is the actual time. We need to grab the time unit and the time number and convert to a datetime object.

# Get the units
tunits <- ncatt_get(ncopendap, "time", "units")
datesince <- tunits$value
datesince <- substr(datesince, nchar(datesince)-18, nchar(datesince)-8)
datesince
'1993-01-01 '
# convert the number to datetime (input should be in second while the time is in unit of days)
datetime_var <- as.POSIXct(time*86400, origin=datesince, tz="UTC")
datetime_var
[1] "1993-01-16 12:00:00 UTC"

Quick view of the data#

filled.contour(x, y, sos, main = paste("Sea surface salinity on raw grid at ", datetime_var), xlab = "xh", ylab = "yh", levels = pretty(c(20,40), 20))
../../../_images/cd47122bcb5acfa4e9e436e4a5be12c2dbed89d591f020d6a55d00cd96338df8.png

Convert the matrix to dataframe#

Since we might want the dataframe to represent the lon lat data structure. We can convert the lon, lat,(not xh,yh which represents the grid number and not the actual lon lat) and data matrix to the dataframe.

X <- as.vector(t(lon))
Y <- as.vector(t(lat))
data <- as.vector(t(sos))

df <- data.frame(
  "lon" = X,
  "lat" = Y,
  "sos" = data
)
df
A data.frame: 654875 × 3
lonlatsos
<dbl><dbl><dbl>
-985.272542NA
-985.352199NA
-985.431845NA
-985.511480NA
-985.591105NA
-985.670719NA
-985.750322NA
-985.829914NA
-985.909494NA
-985.989064NA
-986.068621NA
-986.148167NA
-986.227701NA
-986.307223NA
-986.386733NA
-986.466230NA
-986.545714NA
-986.625186NA
-986.704646NA
-986.784092NA
-986.863526NA
-986.942946NA
-987.022352NA
-987.101746NA
-987.181125NA
-987.260490NA
-987.339842NA
-987.419179NA
-987.498502NA
-987.577811NA
-36.9319556.9466034.74023
-36.9401956.9889534.74160
-36.9484957.0312534.74315
-36.9568557.0735334.74638
-36.9652457.1157734.74915
-36.9736957.1579834.74821
-36.9821557.2001534.74909
-36.9906957.2422934.75011
-36.9992757.2843934.75198
-37.0078757.3264734.75169
-37.0165457.3685034.74945
-37.0252457.4105034.74542
-37.0339757.4524734.74216
-37.0427657.4944034.74070
-37.0516157.5363034.74549
-37.0604957.5781734.75808
-37.0694057.6200034.76130
-37.0783757.6618034.77727
-37.0874057.7035634.78886
-37.0964757.7452934.78878
-37.1055657.7869934.78788
-37.1147257.8286534.78782
-37.1239057.8702834.79068
-37.1331557.9118834.78967
-37.1424357.9534434.78308
-37.1517657.9949734.78175
-37.1611358.0364634.78161
-37.1705658.0779334.78364
-37.1800558.1193634.78035
-37.1895458.1607634.77749