Skip to content

Commit

Permalink
export nc_get
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Aug 6, 2024
1 parent 4259085 commit 74afe39
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
28 changes: 20 additions & 8 deletions R/netcdf-utils.R
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
23 changes: 23 additions & 0 deletions man/nc_get.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 74afe39

Please sign in to comment.