R/echoIBM.setCTD.R

Defines functions echoIBM.setCTD

Documented in echoIBM.setCTD

#*********************************************
#*********************************************
#' Writes CTD files to simulation events based on resource files.
#'
#' @param event		A list of the following elements: (1) 'path', giving the paths to the sub-events, (2) 'esnm', giving the names of the acoustic instruments in the events (same length as 'path'), and (3) 'name', giving the name of the event.
#' @param files		An optional vector of file names to be read and written to the simulation events. If empty (default) the resource files of the echoIBM package are used.
#' @param ...		Data overriding the variables generated by the funciton.
#'
#' @return
#'
#' @examples
#' \dontrun{}
#'
#' @importFrom TSD read.TSD write.TSD
#'
#' @export
#' @rdname echoIBM.setup
#'
echoIBM.setCTD <- function(
	event, 
	files = NULL, 
	...){
	
	############### LOG: ###############
	# Start: 2017-03-29 - Clean version.

	# Save the input variables:
	dotList <- list(...)
	
	copyForOneEsnm <- function(i, event, files, dotList){
		path <- event$path[[i]]
		esnm <- event$esnm[[i]]
		
		# Match 'esnm' against the pre-defined systems, and get the corresponding file:
		files <- getFileFromEsnm(files, esnm, type="ctd", beforeUnderscore=TRUE)
		
		# Read the resource CTD file if there was a match. These files must have one time step per beam mode 'bmmd'. Use drop.out=FALSE to allow for selecting time steps using 'bmmd' below:
		CTD <- read.TSD(files[1], t="all", drop.out=FALSE)
		
		# Add data:
		CTD <- replaceKeepDim(CTD, dotList, esnm)
		
		# Warning if CTD data not given
		if(length(CTD)==0){
			stop("'data' must be given as a list of CTD data specifying variables such as those named by labl.TSD(\"ctd\").")
		}
		
		# Change UNIX time to the CTD file:
		CTD$utim <- head(utim, ncol(CTD$lonc))
		
		CTDfile <- file.path(path, paste0(event$name, "_", esnm, ".ctd"))
		write.TSD(CTD, CTDfile)
		
		return(CTDfile)
	}
	
	## Get available CTD files:
	#CTDdir <- system.file("extdata", "ctd", package="echoIBM")
	#if(length(files)==0){
	#	files <- list.files(CTDdir, full.names=TRUE)
	#}
	#if(length(files)==0){
	#	warning(paste0("No CTD files available in ", CTDdir))
	#	return(NULL)
	#}
	
	
	# Loop through the acoustic instruments:
	out <- sapply(seq_along(event$path), copyForOneEsnm, event=event, files=files, dotList=dotList)
	
	names(out) <- event$esnm
	return(out)
}
arnejohannesholmin/echoIBM documentation built on April 14, 2024, 11:37 p.m.