forked from ropensci/tidync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
214 lines (137 loc) · 8.66 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# tidync
<!-- badges: start -->
[![](https://badges.ropensci.org/174_status.svg)](https://github.com/ropensci/software-review/issues/174)
[![CRAN status](https://www.r-pkg.org/badges/version/tidync)](https://CRAN.R-project.org/package=tidync)
[![R-CMD-check](https://github.com/ropensci/tidync/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ropensci/tidync/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
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:
* Connect and summarize `tidync()`.
* (optionally) Specify source variables `activate()`.
* (optionally) Specify array sub-setting (slicing) `hyper_filter()`.
* Read array data in native form `hyper_array()` or long-form `hyper_tibble()` or bespoke form `hyper_tbl_cube()`.
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](https://www.unidata.ucar.edu/software/netcdf/). R has (independent) support for NetCDF via the [ncdf4](https://CRAN.R-project.org/package=ncdf4), [rhdf5](https://bioconductor.org/packages/release/bioc/html/rhdf5.html), [RNetCDF](https://CRAN.R-project.org/package=RNetCDF), [rgdal](https://CRAN.R-project.org/package=rgdal), [sf](https://CRAN.R-project.org/package=sf) and [vapour](https://CRAN.R-project.org/package=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](https://CRAN.R-project.org/package=ncmeta) over RNetCDF is used to obtain information about data sources.
## Installation
Install tidync from CRAN.
```{r cran, eval = FALSE}
install.packages("tidync")
```
You can install the development version from github with:
```{r gh-installation, eval = FALSE}
# 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.
```{r,eval=FALSE}
install.packages("ncdf4")
install.packages("RNetCDF")
```
If you have problems, please see the [INSTALL instructions for RNetCDF](https://CRAN.R-project.org/package=RNetCDF/INSTALL), 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.
```bash
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
```R
install.packages("remotes")
remotes::install_github("ropensci/tidync")
```
More general information about system dependencies libnetcdf-dev and libudunits2-dev is available from [Unidata](https://www.unidata.ucar.edu).
## Usage
This is a basic example which shows how to connect to a file.
```{r start}
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).
```{r interactive,eval=TRUE}
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:
```{r pseudo1, eval=FALSE}
df %>% dplyr::filter(longitude > 100, longitude < 150)
```
in hyper_filter we must load them into one named expression.
```{r pseudo2, eval=FALSE}
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.
```{r extraction, eval=TRUE}
## 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:
```R
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!
* we can't do "grouped filters"" (i.e. polygon-overlay extraction), but it's in the works
* compound types are not supported, though see the "rhdf5" branch on Github
* NetCDF groups are not exposed (groups are like a "files within a file", analogous to a file system directory)
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
* **slab**, **hyperslab** - array variable that may be read from a NetCDF
* **shape**, **grid** - set of dimensions that define variables in NetCDF
* **activation** - choice of a given grid to apply subsetting and read operations to
---
## Code of conduct
Please note that this project is released with a [Contributor Code of Conduct](https://github.com/ropensci/tidync/blob/master/CONDUCT.md). By participating in this project you agree to abide by its terms.
[![ropensci_footer](https://ropensci.org/public_images/ropensci_footer.png)](https://ropensci.org)