Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dankelley committed Jan 26, 2024
2 parents baa1876 + 5a056fe commit c130238
Show file tree
Hide file tree
Showing 77 changed files with 2,770 additions and 859 deletions.
5 changes: 4 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
^docs$
^README\.md$
^_pkgdown.yml$

^.lintr$
^check_package.R$
^codemeta\.json$
^revdep$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ rsconnect/
*.txt
*~
*.cdl
^revdep$
9 changes: 9 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters:linters_with_defaults(
indentation_linter=NULL,
line_length_linter(150),
object_name_linter=NULL,
infix_spaces_linter=NULL,
brace_linter=NULL,
commented_code_linter=NULL,
object_length_linter(length=40L),
cyclocomp_linter(complexity_limit=400L))
13 changes: 8 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
Package: ocencdf
Title: Provide Netcdf Interface for Oce
Version: 0.0.3
Title: NetCDF Interface for Oce Objects
Version: 0.0.4
Authors@R: c(
person(given="Dan", family="Kelley", email="[email protected]", role=c("aut", "cre"), comment=c(ORCID="https://orcid.org/0000-0001-7808-5911")))
Description: This provides a way to export `oce` objects to `netcdf` format, perhaps for data archiving, or perhaps for secondary analysis outside R.
person(given="Dan", family="Kelley", email="[email protected]", role=c("aut", "cre"), comment=c(ORCID="https://orcid.org/0000-0001-7808-5911")),
person(given="Clark", family="Richards", email="[email protected]", role=c("ctb"), comment=c(ORCID="https://orcid.org/0000-0002-7833-206X")))
Description: This provides a way to export `oce` objects to `NetCDF` format, perhaps for data archiving, or perhaps for secondary analysis outside R.
Depends: R (>= 4.00), oce (>= 1.8.0)
Imports: jsonlite, ncdf4, methods, yaml
License: GPL (>= 2)
Encoding: UTF-8
URL: https://dankelley.github.io/ocencdf/
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
BugReports: https://github.com/dankelley/ocencdf/issues
Config/testthat/edition: 3
BuildVignettes: true
VignetteBuilder: knitr
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# ocencdf 0.0.4

* Specify `force_v4` in all conversions to NetCDF, which permits the handling
of large files.

# ocencdf 0.0.3

* Handle ADV data with `adv2ncdf()` and `ncdf2adv()`.
Expand Down
129 changes: 70 additions & 59 deletions R/adp.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Save an adp object to a netcdf file
#' Save an ADP object to a NetCDF file
#'
#' Given an `adp` object created by the `oce` package, this function
#' creates a netcdf file that can later by read by [ncdf2adp()] to approximately
#' creates a NetCDF file that can later by read by [ncdf2adp()] to approximately
#' reproduce the original contents.
#'
#' Note that [adp2ncdf()] defaults `varTable` to `"adp"`.
Expand All @@ -21,97 +21,103 @@
#' and
#' `"oceCoordinate"`.
#'
#' @param x an oce object of class `adp`, as created by e.g. [oce::read.adp()].
#' @param x an `oce` object of class `adp`, as created by e.g. [oce::read.adp()].
#'
#' @inheritParams oce2ncdf
#'
#' @examples
#' library(ocencdf)
#'
#' # Example with an adp file from oce package
#' data(adp, package="oce")
#' # Example with an ADP file from `oce` package
#' data(adp, package = "oce")
#' summary(adp)
#' plot(adp)
#' # Transfer to netcdf and back to see if results make sense
#' oce2ncdf(adp, ncfile="adp.nc")
#' ADP <- ncdf2adp("adp.nc")
#' # Transfer to NetCDF and back to see if results make sense.
#' # Use a temporary nc file to let package pass CRAN checks.
#' ncfile <- tempfile(pattern = "adp", fileext = ".nc")
#' oce2ncdf(adp, ncfile = ncfile)
#' ADP <- ncdf2adp(ncfile)
#' summary(ADP)
#' plot(ADP)
#'
#' # Remove temporary file
#' file.remove("adp.nc")
#' file.remove(ncfile)
#'
#' @family things related to adp data
#'
#' @author Dan Kelley and Clark Richards
#'
#' @export
adp2ncdf <- function(x, varTable=NULL, ncfile=NULL, force_v4=TRUE, debug=0)
{
adp2ncdf <- function(x, varTable = NULL, ncfile = NULL, force_v4 = TRUE, debug = 0) {
dmsg(debug, "adp2ncdf(..., ncfile=\"", ncfile, "\") {\n")
if (!inherits(x, "adp"))
if (!inherits(x, "adp")) {
stop("'x' must be a adp object")
}
if (is.null(varTable)) {
varTable <- "adp"
#message("Defaulting varTable to \"", varTable, "\".")
# message("Defaulting varTable to \"", varTable, "\".")
}
if (is.null(ncfile)) {
ncfile <- "adp.nc"
#message("Will save adp object to \"", ncfile, "\".")
# message("Will save adp object to \"", ncfile, "\".")
}
varTableOrig <- varTable
varTable <- read.varTable(varTable)
vdim <- dim(x@data$v)
extant <- list()
for (item in c("v", "a", "g", "q"))
for (item in c("v", "a", "g", "q")) {
extant[[item]] <- item %in% names(x@data)
if (!extant$v)
}
if (!extant$v) {
stop("there is no data item named 'v', which is mandatory for an oce adp object")
#time <- ncdim_def(name="TIME", units="s", vals=as.numeric(x@data$time))
time <- ncdim_def(name="TIME", units="", vals=seq_len(vdim[1]), create_dimvar=FALSE,
longname="seconds since 1970-01-01 UTC")
#cell <- ncdim_def(name="DISTANCE", units="m", vals=x@data$distance, longname="Distance to cell")
distance <- ncdim_def(name="DISTANCE", units="", vals=seq_len(vdim[2]), create_dimvar=FALSE,
longname="Distance to cell")
beam <- ncdim_def(name="BEAM", units="", vals=seq_len(vdim[3]))
}
# time <- ncdim_def(name="TIME", units="s", vals=as.numeric(x@data$time))
time <- ncdim_def(
name = "TIME", units = "", vals = seq_len(vdim[1]), create_dimvar = FALSE,
longname = "seconds since 1970-01-01 UTC"
)
# cell <- ncdim_def(name="DISTANCE", units="m", vals=x@data$distance, longname="Distance to cell")
distance <- ncdim_def(
name = "DISTANCE", units = "", vals = seq_len(vdim[2]), create_dimvar = FALSE,
longname = "Distance to cell"
)
beam <- ncdim_def(name = "BEAM", units = "", vals = seq_len(vdim[3]))
vars <- list()
FillValue <- getVarInfo("-", varTable=varTable)$FillValue
FillValue <- getVarInfo("-", varTable = varTable)$FillValue
# time and distance (do they show up as n$var now?)
dmsg(debug, " time (length ", vdim[1], ")\n")
vars[["time"]] <- ncvar_def(name="time", units="seconds since 1970-01-01 UTC", dim=time, prec="double")
vars[["time"]] <- ncvar_def(name = "time", units = "seconds since 1970-01-01 UTC", dim = time, prec = "double")
dmsg(debug, " distance (length ", vdim[2], ")\n")
vars[["distance"]] <- ncvar_def(name="distance", units="m", dim=distance)
dmsg(debug, " Setting up variable dimensions for ", paste(vdim, collapse="x"), " arrays:\n")
vars[["distance"]] <- ncvar_def(name = "distance", units = "m", dim = distance)
dmsg(debug, " Setting up variable dimensions for ", paste(vdim, collapse = "x"), " arrays:\n")
# array data
dmsg(debug, " v\n")
vars[["v"]] <- ncvar_def(name="v", units="m/s", dim=list(time, distance, beam))
vars[["v"]] <- ncvar_def(name = "v", units = "m/s", dim = list(time, distance, beam))
if (extant$a) {
dmsg(debug, " a\n")
vars[["a"]] <- ncvar_def("a", units="", dim=list(time, distance, beam))
vars[["a"]] <- ncvar_def("a", units = "", dim = list(time, distance, beam))
}
if (extant$g) {
dmsg(debug, " g\n")
vars[["g"]] <- ncvar_def("g", units="", dim=list(time, distance, beam))
vars[["g"]] <- ncvar_def("g", units = "", dim = list(time, distance, beam))
}
if (extant$q) {
dmsg(debug, " q\n")
vars[["q"]] <- ncvar_def("q", units="", dim=list(time, distance, beam))
vars[["q"]] <- ncvar_def("q", units = "", dim = list(time, distance, beam))
}
# time-series data
dmsg(debug, " Setting up dimensions for time-series vectors of length ", vdim[1], ":\n")
#cat("NEXT: names in @data:\n");print(sort(names(x@data)))
# cat("NEXT: names in @data:\n");print(sort(names(x@data)))
for (item in names(x@data)) {
if (item != "time" && item != "distance" && is.vector(x@data[[item]])) {
dmsg(debug, " ", item, "\n")
vars[[item]] <- ncvar_def(item, units="", dim=time)
vars[[item]] <- ncvar_def(item, units = "", dim = time)
}
}
nc <- nc_create(ncfile, vars, force_v4=force_v4)
nc <- nc_create(ncfile, vars, force_v4 = force_v4)
dmsg(debug, " Storing time and distance\n")
dmsg(debug, " time\n")
ncvar_put(nc, "time", as.numeric(x@data$time))
#message("first 3 times: ", paste(x@data$time[1:3], collapse=" "))
#message("first 3 times: ", paste(as.numeric(x@data$time)[1:3], collapse=" "))
# message("first 3 times: ", paste(x@data$time[1:3], collapse=" "))
# message("first 3 times: ", paste(as.numeric(x@data$time)[1:3], collapse=" "))
dmsg(debug, " distance\n")
ncvar_put(nc, "distance", as.numeric(x@data[["distance"]]))
dmsg(debug, " Storing arrays:\n")
Expand All @@ -132,33 +138,37 @@ adp2ncdf <- function(x, varTable=NULL, ncfile=NULL, force_v4=TRUE, debug=0)
dmsg(debug, " metadata_explanation\n")
explanation <- paste("This file was created with adp2ncdf from the ocencdf R package,\n",
"available at www.github.com/dankelley/ocencdf.\n",
readLines(system.file("extdata", "ncdf_explanation.md", package="ocencdf")), collapse="\n")
readLines(system.file("extdata", "ncdf_explanation.md", package = "ocencdf")),
collapse = "\n"
)
ncatt_put(nc, 0, "metadata_explanation", explanation)
dmsg(debug, " metadata\n")
ncatt_put(nc, 0, "metadata", metadata2json(x@metadata))
# Store some individual metadata items, for simple access
for (item in c("beamAngle", "frequency",
"instrumentType", "instrumentSubtype",
"numberOfBeams", "numberOfBeams",
"oceCoordinate")) {
for (item in c(
"beamAngle", "frequency",
"instrumentType", "instrumentSubtype",
"numberOfBeams", "numberOfBeams",
"oceCoordinate"
)) {
dmsg(debug, " ", item, "\n")
storeNetcdfAttribute(x, item, nc, item)
storeNetCDFAttribute(x, item, nc, item)
}
dmsg(debug, " varTable\n")
ncatt_put(nc=nc, varid=0, attname="varTable", attval=varTableOrig)
ncatt_put(nc = nc, varid = 0, attname = "varTable", attval = varTableOrig)
dmsg(debug, " class\n")
ncatt_put(nc=nc, varid=0, attname="class", attval=as.character(class(x)))
ncatt_put(nc = nc, varid = 0, attname = "class", attval = as.character(class(x)))
dmsg(debug, " creator\n")
ncatt_put(nc=nc, varid=0, attname="creator", attval=paste0("ocencdf version ", packageVersion("ocencdf")))
ncatt_put(nc = nc, varid = 0, attname = "creator", attval = paste0("ocencdf version ", packageVersion("ocencdf")))
nc_close(nc)
dmsg(debug, paste0("} # adp2ncdf created file \"", ncfile, "\"\n"))
}

#' Read a netcdf file and create an adp object
#' Read a NetCDF file and create an ADP object
#'
#' This works by calling [ncdf2oce()] and then using [class()] on
#' the result to make it be of subclass `"adp"`. This is intended
#' to work with Netcdf files created with [adp2ncdf()], which embeds
#' to work with NetCDF files created with [adp2ncdf()], which embeds
#' sufficient information in the file to permit [ncdf2adp()] to
#' reconstruct the original adp object. See the documentation
#' for [adp2ncdf()] to learn more about what it stores, and therefore
Expand All @@ -173,25 +183,26 @@ adp2ncdf <- function(x, varTable=NULL, ncfile=NULL, force_v4=TRUE, debug=0)
#' @examples
#' library(ocencdf)
#'
#' # Example with an adp file from oce package
#' data(adp, package="oce")
#' # Example with an ADP file from `oce` package
#' data(adp, package = "oce")
#' summary(adp)
#' plot(adp)
#' # Transfer to netcdf and back to see if results make sense
#' oce2ncdf(adp, ncfile="adp.nc")
#' ADP <- ncdf2adp("adp.nc")
#' # Transfer to NetCDF and back to see if results make sense
#' # Use a temporary nc file to let package pass CRAN checks.
#' ncfile <- tempfile(pattern = "adp", fileext = ".nc")
#' oce2ncdf(adp, ncfile = ncfile)
#' ADP <- ncdf2adp(ncfile)
#' summary(ADP)
#' plot(ADP)
#' file.remove(ncfile)
#'
#' # Remove temporary file
#' file.remove("adp.nc")
#' @family things related to adp data
#'
#' @author Dan Kelley
#'
#' @export
ncdf2adp <- function(ncfile=NULL, varTable=NULL, debug=0)
{
adp <- ncdf2oce(ncfile=ncfile, varTable=varTable, debug=debug)
ncdf2adp <- function(ncfile = NULL, varTable = NULL, debug = 0) {
adp <- ncdf2oce(ncfile = ncfile, varTable = varTable, debug = debug)
class(adp) <- "adp"
adp
}
Loading

0 comments on commit c130238

Please sign in to comment.