# Functions for editing metadata attributes
#' Get a data.frame of attributes from a NetCDF object
#'
#' Get a data.frame of attributes from a NetCDF object.
#'
#' @param nc (ncdf4/character) Either a ncdf4 object or a file path.
#'
#' @return (data.frame) A data.frame of the attributes.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' get_ncdf4_attributes("./path/to/my.nc")
#' }
get_ncdf4_attributes <- function(nc) {
stopifnot(is(nc, "ncdf4") || file.exists(nc))
if (!requireNamespace("ncdf4")) {
stop(call. = FALSE,
"The package 'ncdf4' must be installed to run this function. ",
"Please install it and try again.")
}
# Read the file in if `nc` is a character vector
if (is.character(nc)) {
nc <- ncdf4::nc_open(nc)
}
dims <- nc$dim
unitlist <- c()
for (i in 1:length(dims)) {
unitlist[i] <- dims[[i]]$units
}
inds <- which(unitlist != '')
dims <- dims[inds]
attributes <- c(names(nc$var), attributes(dims)$names)
result <- data.frame(attributeName = NA)
for (i in seq_along(attributes)) {
result[i,"attributeName"] <- attributes[i]
attribute <- ncdf4::ncatt_get(nc, attributes[i])
att_names <- names(attribute)
for (name in att_names) {
if (length(attribute[[name]]) > 1){
result[i, name] <- paste(attribute[[name]], collapse = ";")
}
else if (length(attribute[[name]]) == 1)
result[i, name] <- attribute[[name]]
}
}
result
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.