#' @export
#'
#' @title Open a GOES AOD netCDF file
#'
#' @description Opens a NetCDF file and returns the \code{nc} handle. By
#' default, the directory specified with \code{setSatelliteDataDir()} is
#' searched for the file.
#'
#' @details
#' Errors generated by `nc_open()` are not handled and will pass through to the
#' console or calling function.
#'
#' @param filename Name of scan file.
#' @param dataDir Data directory containing \code{filename}. Defaults to
#' \code{getSatelliteDataDir()}.
#'
#' @return NetCDF \code{ncdf4} handle.
#'
#' @seealso \code{\link{getSatelliteDataDir}}
#'
#' @examples
#' \dontrun{
#' library(MazamaSatelliteUtils)
#' setSatelliteDataDir("~/Data/Satellite")
#'
#' scanFile <- goesaodc_downloadScanFiles(
#' satID = "G16",
#' datetime = "2019-09-06 18:00",
#' timezone = "UTC"
#' )
#'
#' goesaodc_openScanFile(scanFile)
#' }
goesaodc_openScanFile <- function(
filename = NULL,
dataDir = getSatelliteDataDir()
) {
# ----- Validate parameters --------------------------------------------------
MazamaCoreUtils::stopIfNull(filename)
if ( length(filename) == 0 )
stop("Param 'filename' must be a single filename or a list of filenames")
# ----- Open nc handle -------------------------------------------------------
if ( length(filename) == 1 ) {
nc <- goesaodc_openSingleScanFile(filename, dataDir)
} else {
nc <- list()
for ( fn in filename ) {
# Create a label for this file in the list
label <-
fn %>%
goesaodc_convertFilenameToDatetime() %>%
MazamaCoreUtils::timeStamp(unit = "sec", timezone = "UTC")
nc[[label]] <- goesaodc_openSingleScanFile(fn, dataDir)
}
}
# ----- Return ---------------------------------------------------------------
return(nc)
}
# Internal helper function
goesaodc_openSingleScanFile <- function(
filename = NULL,
dataDir = getSatelliteDataDir()
) {
# Check if internal package file is being used
if ( stringr::str_detect(filename, "extdata") ) {
fullPath <- filename
} else {
fullPath <- file.path(dataDir, filename)
}
if ( !file.exists(fullPath) )
stop(paste0("Cannot find ", fullPath))
nc <- ncdf4::nc_open(fullPath)
return(nc)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.