R/readcurvi.R

Defines functions readcurvi

Documented in readcurvi

#' Read curvilinear semi-landmarks file
#' 
#' @description a wrapper function to read the individual semi-landmark files 
#'  generated by \code{\link{img2landmark}} function, an alternative from reading 
#'  the \code{.tps} file generated from \code{img2landmark}.
#' @param rfolder Path of folder containing the individual semi-landmark files 
#'  (the 'curvillinearLand_' files)
#' @param write logical. whether to write the array of landkmark configurations
#'  into a \code{.tps} file
#' @param scale scale in the unit of um/px, 
#'  i.e. 1 pixel of the image equal to how many um?
#' @return array of semi-lamdmark configurations
#' @seealso \code{\link{img2landmark}}
#' @export

readcurvi <- function(rfolder, write=FALSE, scale=NULL) {
  setwd(rfolder)
  landlist <- list.files(pattern= "curvillinearLand_")
  landlist <- landlist[grep(".csv", landlist)] 
  imgname <- gsub("curvillinearLand_", "", landlist)
  imgname <-  gsub(".csv", "", imgname)
  temp <- read.csv(paste(rfolder, landlist[1], sep="/"), 
          comment.char="", header=TRUE)
  A <- array(NA, dim= c(dim(temp)[1], 2, length(imgname)), 
       dimnames= list(paste0("LM", 1:dim(temp)[1]), c("X","Y"), imgname))
  cat("------Reading semi-landmark files------\n")
  for (i in 1: length(imgname)) {
    tempp <- read.csv(paste(rfolder, landlist[i], sep="/"), 
             comment.char="", header=TRUE)
    A[, 1, i] <- tempp[, 1]  
    A[, 2, i] <- tempp[, 2]  
    cat(paste(i, landlist[i], "\n"))
    flush.console()
  }
  cat("---Reading semi-landmark files ended---\n\n")
  if (write == TRUE) {
    writeland.tps(A, file="semilandmarks.tps")
    cat("\nThe combined semilandmark file is saved at:", 
        paste(rfolder, "semilandmarks.tps", sep="/"), "\n")
  }
  if (!is.null(scale))
    A <- A * scale
  return(A)
}
jinyung/otolith documentation built on May 19, 2019, 10:36 a.m.