Skip to content

Commit a781e45

Browse files
committed
Add a function to read in NetCDF attributes
1 parent 70406fe commit a781e45

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export(get_doc_id)
4040
export(get_identifier)
4141
export(get_latest_release)
4242
export(get_mn_base_url)
43+
export(get_ncdf4_attributes)
4344
export(get_netcdf_format_id)
4445
export(get_or_create_pid)
4546
export(get_package)

R/attributes.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#' Get a data.frame of attributes from a NetCDF object
2+
#'
3+
#' @param nc (ncdf4 or character) Either a ncdf4 object or a file path
4+
#'
5+
#' @return (data.frame) A data.frame of the attributes
6+
#' @export
7+
#'
8+
#' @examples
9+
#' \dontrun{
10+
#' get_ncdf4_attributes("./path/to/my.nc")
11+
#' }
12+
get_ncdf4_attributes <- function(nc) {
13+
stopifnot(is(nc, "ncdf4") || file.exists(nc))
14+
15+
# Read the file in if `nc` is a character vector
16+
if (is.character(nc)) {
17+
nc <- ncdf4::nc_open(nc)
18+
}
19+
20+
attributes <- names(nc$var)
21+
result <- data.frame()
22+
23+
for (i in seq_along(attributes)) {
24+
attribute <- ncdf4::ncatt_get(nc, attributes[i])
25+
names <- names(attribute)
26+
27+
for (name in names) {
28+
result[i, name] <- attribute[[name]]
29+
}
30+
}
31+
32+
result
33+
}
34+

man/get_ncdf4_attributes.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)