R/readCoordinatesNcdf.R

Defines functions readCoordinatesNcdf

Documented in readCoordinatesNcdf

readCoordinatesNcdf = function(
  ### read coordinate or dimension values from ncdf file
  fileCon ##<< ncdf file connection or character string: Connection to the
  ##  ncdf file or its file name. In the latter case, the connection is
  ##  created and closed automatically.
) {
  ##seealso<<
  ##\code{\link{infoNcdfDims}}
  if (inherits(fileCon, 'character')) {
    if (!file.exists(fileCon))
      stop('Specified file not existent!')
    fileCon <- open.nc(fileCon)
    on.exit(close.nc(fileCon))
  }
  dimNames <- infoNcdfDims(fileCon)$name
  results <- list()
  for (dimNameT in dimNames) {
    if (is.element(dimNameT, infoNcdfVars(fileCon, dimvars = TRUE)$name)) {
      results[[dimNameT]] <- var.get.nc(fileCon, dimNameT)
    } else {
      results[[dimNameT]] <- NULL
    }
  }
  ##value<< A list with the coordinate values (if available) for all dimensions.
  return(results)
}
bgctw/ncdfTools documentation built on Jan. 29, 2020, 1:16 p.m.