diff --git a/NAMESPACE b/NAMESPACE index a995943..7d9c196 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,9 @@ S3method(hyper_tbl_cube,tidync) S3method(hyper_tibble,character) S3method(hyper_tibble,tidync) S3method(hyper_transforms,default) +S3method(nc_get,NetCDF) +S3method(nc_get,character) +S3method(nc_get,ncdf4) S3method(print,tidync) S3method(print,tidync_data) S3method(tidync,character) @@ -32,6 +35,7 @@ export(hyper_tbl_cube) export(hyper_tibble) export(hyper_transforms) export(hyper_vars) +export(nc_get) export(tidync) importFrom(RNetCDF,close.nc) importFrom(RNetCDF,open.nc) diff --git a/R/netcdf-utils.R b/R/netcdf-utils.R index d7c6655..156f25b 100644 --- a/R/netcdf-utils.R +++ b/R/netcdf-utils.R @@ -1,12 +1,24 @@ +#' Helper to get a variable from NetCDF. +#' +#' This exists so we can (internally) use a file path, uri, or open +#' NetCDF connection (ncdf4 or RNetCDF) in a simpler way. +#' +#' This function just reads the whole array. It is equivalent to the +#' angstroms package function 'rawdata(x, varname)'. +#' @param x file path, uri, or NetCDF connection +#' +#' @param v variable name +#' @param test if true we make sure the connection can be open, not applied for connections themselves +#' #' @importFrom ncdf4 nc_open nc_close ncvar_get #' @importFrom RNetCDF open.nc close.nc var.get.nc #' @importFrom purrr safely -#' @noRd -nc_get <- function(x, v, test = FALSE, ...) { +#' @export +nc_get <- function(x, v, test = FALSE) { UseMethod("nc_get") } -#' @noRd -nc_get.character <- function(x, v, test = FALSE, ...) { +#' @export +nc_get.character <- function(x, v, test = FALSE) { if (!test) { con <- RNetCDF::open.nc(x) on.exit(RNetCDF::close.nc(con), add = TRUE) @@ -33,12 +45,12 @@ nc_get.character <- function(x, v, test = FALSE, ...) { stop(sprintf("no variable found %s", v)) } -#' @noRd -nc_get.NetCDF <- function(x, v) { +#' @export +nc_get.NetCDF <- function(x, v, test = FALSE) { RNetCDF::var.get.nc(x, v) } -#' @noRd -nc_get.ncdf4 <- function(x, v) { +#' @export +nc_get.ncdf4 <- function(x, v, test = FALSE) { ncdf4::ncvar_get(x, v) } diff --git a/man/nc_get.Rd b/man/nc_get.Rd new file mode 100644 index 0000000..772dc2c --- /dev/null +++ b/man/nc_get.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/netcdf-utils.R +\name{nc_get} +\alias{nc_get} +\title{Helper to get a variable from NetCDF.} +\usage{ +nc_get(x, v, test = FALSE) +} +\arguments{ +\item{x}{file path, uri, or NetCDF connection} + +\item{v}{variable name} + +\item{test}{if true we make sure the connection can be open} +} +\description{ +This exists so we can (internally) use a file path, uri, or open +NetCDF connection (ncdf4 or RNetCDF) in a simpler way. +} +\details{ +This function just reads the whole array. It is equivalent to the +angstroms package function 'rawdata(x, varname)'. +}