R/is.TSD.R

Defines functions is.TSD

Documented in is.TSD

#*********************************************
#*********************************************
#' Tests whether the file(s) given by 'x' has the Time Step Data format (TSD) by reading the first four bytes as character and comparing to the TSD file header "\%TSD".
#'
#' @param x  is a directory or a vector of file paths.
#' @param recursive  is used in list.files() when 'x' is a directory.
#' @param accept.dir  is TRUE to permit giving 'x' as a directory, which is only used for a special reason in read.TSD().
#'
#' @return
#'
#' @examples
#' \dontrun{}
#'
#' @export
#' @rdname is.TSD
#'
is.TSD<-function(x, recursive=TRUE, accept.dir=TRUE){
	
	##### Preparation #####
	# Get file pahts:
	finfo <- file.info(x)
	if(length(x)==1 && is.na(finfo$isdir)){
		warning("Non-existing path")
		return(FALSE)
	}
	else if(length(x)==1 && !identical(finfo$isdir, FALSE)){
		if(accept.dir){
			x <- list.files(x, recursive=recursive, full.names=TRUE)
		}
		else{
			warning(paste(x, "is a directory"))
			return(FALSE)
		}
	}
	
		
	##### Execution and output #####
	out <- rep(NA, length(x))
	for(i in seq_along(x)){
		first4 <- readChar(x[i], nchars=4, useBytes=TRUE)
		out[i] <- identical(first4, "%TSD")
		#if(!out[i]){
		#	print(first4)
		#}
	}
	names(out) <- basename(x)
	out
}
arnejohannesholmin/TSD documentation built on April 14, 2024, 5:29 a.m.