knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

tidync

CRAN_Status_Badge CRAN_Download_Badge R-CMD-check

The goal of tidync is to ease exploring the contents of a NetCDF source and to simplify the process of data extraction.

When extracting, data can be accessed as array/s, or in long-form as a data frame. In contrast to other packages tidync helps reduce the volume of code required to discover and read the contents of NetCDF, with simple steps:

NetCDF is Network Common Data Form a very common, and very general way to store and work with scientific array-based data. NetCDF is defined and provided by Unidata. R has (independent) support for NetCDF via the ncdf4, rhdf5, RNetCDF, rgdal, sf and vapour packages.

This project uses RNetCDF for the primary access to the NetCDF library, as well as the ncdf4 package in some cases. The wrapper provided by ncmeta over RNetCDF is used to obtain information about data sources.

Installation

Install tidync from CRAN.

install.packages("tidync")

You can install the development version from github with:

# install.packages("remotes")
remotes::install_github("ropensci/tidync", dependencies = TRUE)

The package packages ncdf4 and RNetCDF are required, so first make sure you can install and use these if it doesn't work the first time.

install.packages("ncdf4")
install.packages("RNetCDF")

If you have problems, please see the INSTALL instructions for RNetCDF, these should work as well for ncdf4. Below I note specifics for different operating systems, notably Ubuntu/Debian where I work the most - these aren't comprehensive details but might be helpful.

Windows

On Windows, everything should be easy as ncdf4 and RNetCDF are supported by CRAN. The RNetCDF package now includes OpenDAP/Thredds for 64-bit Windows (not 32-bit), and so tidync will work for those sources too.

MacOS

On MacOS, it should also be easy as there are binaries for ncdf4 and RNetCDF available on CRAN. As far as I know, only RNetCDF will support Thredds.

Ubuntu/Debian

On Linux you will need at least the following installed by an administrator, here tested on Ubuntu Xenial 16.04.

apt update 
apt upgrade --assume-yes

## Install 3rd parties for NetCDF
apt install libnetcdf-dev libudunits2-dev

## install 3rd parties needed for devtools + openssl git2r httr
apt install libssl-dev

Then in R

install.packages("remotes")
remotes::install_github("ropensci/tidync")

More general information about system dependencies libnetcdf-dev and libudunits2-dev is available from Unidata.

Usage

This is a basic example which shows how to connect to a file.

file <- system.file("extdata", "oceandata", "S20080012008031.L3m_MO_CHL_chlor_a_9km.nc", package = "tidync")
library(tidync)
tidync(file) 

There are two main ways of using tidync, interactively to explore what is there, and for extraction. The functions tidync and activate and hyper_filter allow us to hone in on the part/s of the data we want, and functions hyper_array, hyper_tibble and hyper_tbl_cube give raw-array or data frames.

Interactive

Use tidync() and hyper_filter() to discern what variables and dimensions are available, and to craft axis-filtering expressions by value or by index. (Use the name of the variable on the LHS to target it, use its name to filter by value and the special name index to filter it by its index).

filename <- system.file("extdata/argo/MD5903593_001.nc", package = "tidync")
## discover the available entities, and the active grid's dimensions and variables
tidync(filename)

## activate a different grid
grid_identifier <- "D7,D9,D11,D8"
tidync(filename) %>% activate(grid_identifier)

## pass named expressions to subset dimension by value or index (step)
(subs <- tidync(filename) %>% hyper_filter(N_PROF = N_PROF > 1, STRING256 = index > 10))

## with the saved filtering from above, choose data frame or tbl_cube output
## optionally with only selected variables
subs %>% hyper_tibble()
subs %>% hyper_tbl_cube(select_var = c("PRES", "PRES_QC", "PSAL_ADJUSTED"))

A grid is a "virtual table" in the sense of a database source. It's possible to activate a grid via a variable within it, so all variables are available by default. Grids have identifiers based on which dimensions they are defined with, so use i.e. "D1,D0" and can otherwise be activated by their count identifier (starting at 1). The "D0" is an identifier, it matches the internal 0-based indexing and identity used by NetCDF itself.

Please note that hyper_filter() expressions must be unique, unlike with dplyr::filter() we cannot load multiple comparisons into one.

While dplyr filter can load up multiple comparisons:

df %>% dplyr::filter(longitude > 100, longitude < 150)

in hyper_filter we must load them into one named expression.

tidync(filename) %>% hyper_filter(longitude = longitude > 100 & longitude < 150)

Extractive

Use what we learned interactively to extract the data, either in data frame or raw-array (hyper slice) form.

## we'll see a column for the variable activated, and whatever other 
## variables the grid has
tidync(filename) %>% activate("JULD") %>% 
  hyper_filter(N_PROF = N_PROF == 1) %>% 
  hyper_tibble()


## native array form, we'll see a (list of) R arrays with a dimension for 
## each seen by tidync(filename) %>% activate("JULD")
tidync(filename) %>% activate("JULD") %>% 
  hyper_filter(N_PROF = N_PROF == 1) %>% 
  hyper_array()

It's important to not actual request the data extraction until the expressions above would result in an efficient size (don't try a data frame version of a 20Gb ROMs variable ...). Use the interactive modes to determine the likely size of the output you will receive.

Functions seamlessly build the actual index values required by the NetCDF library. This can be used to debug the process or to define your own tools for the extraction. Currently each hyper_* function can take the filtering expressions, but it's not obvious if this is a good idea or not.

See the vignettes for more:

browseVignettes(package = "tidync")

Limitations

Please get in touch if you have specific workflows that tidync is not providing. There's a lot of room for improvement!

I'm interested in lighter and rawer access to the NetCDF library, I've explored that here and it may or may not be a good idea:

https://github.com/hypertidy/ncapi

Terminology


Code of conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

ropensci_footer



hypertidy/tidync documentation built on Oct. 27, 2023, 7:07 p.m.