R/echoIBM.setCalibration.R

Defines functions echoIBM.setCalibration

Documented in echoIBM.setCalibration

#*********************************************
#*********************************************
#' Writes calibration 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.setCalibration <- 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="calibration", beforeUnderscore=TRUE)

		# Read the calibration file if there was a match:
		if(length(files)){
			calibration <- read.TSD(files[1], t="all", drop.out=FALSE)
		}
		else{
			warning(paste0("No calibration files matching the specified system (", esnm, "). Available files are the following:", paste(basename(files), collapse="\n")))
		}
		
		# Add data:
		calibration <- replaceKeepDim(calibration, dotList, esnm)
		
		tofile <- file.path(path, paste0(event$name, "_", esnm, "_calibration.tsd"))
		write.TSD(calibration, tofile, numt=1)
		return(tofile)
	}

	## Get available calibration files:
	#if(length(files)==0){
	#	files <- list.files(system.file("extdata", "calibration", package="echoIBM"), full.names=TRUE)
	#}

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