R/echoIBM.setNoise.R

Defines functions echoIBM.setNoise

Documented in echoIBM.setNoise

#*********************************************
#*********************************************
#' Writes noise 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.setNoise <- function(
	event, 
	files = NULL, 
	nr0a = list(), 
	...){

	
	############### LOG: ###############
	# Start: 2017-03-29 - Clean version.

	# Save the input variables:
	dotList <- list(...)
	
	copyForOneEsnm <- function(i, event, files, nr0a, 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="noise", beforeUnderscore=TRUE)

		# Read the noise file if there was a match:
		numt <- 1
		if(length(files)){
			noise <- read.TSD(files[1], t="all", drop.out=FALSE)
			numt <- read.TSD(files[1], var="numt")$numt
		}
		else{
			warning(paste0("No noise files matching the specified system (", esnm, "). Available files are the following:", paste(basename(files), collapse="\n")))
		}
		
		# Add data:
		noise <- replaceKeepDim(noise, dotList, esnm)
		
		# Special method for generating close range noise, particularly for omnidirectional fishery sonar:
		if(length(nr0a[[esnm]])){
			beams <- echoIBM.readFile(path, ext="beams", t="all")
			#beams <- lapply(path, echoIBM.readFile, ext="beams", t="all")
			nr0aPar <- c(list(beams=beams), nr0a[[esnm]])
			noise$nr0a <- do.call(omnisonar_nr0a, nr0aPar)
		}
		
		tofile <- file.path(path, paste0(event$name, "_", esnm, "_noise.tsd"))
		
		write.TSD(noise, tofile, numt=numt)
		return(tofile)
	}

	outfiles <- sapply(seq_along(event$path), copyForOneEsnm, event=event, files=files, nr0a=nr0a, dotList=dotList)
	
	names(outfiles) <- event$esnm
	return(list(files=outfiles))
}
arnejohannesholmin/echoIBM documentation built on April 14, 2024, 11:37 p.m.