Skip to content

Instantly share code, notes, and snippets.

@brews
Created November 5, 2012 16:05
Show Gist options
  • Save brews/4017999 to your computer and use it in GitHub Desktop.
Save brews/4017999 to your computer and use it in GitHub Desktop.
Read a Dave Meko-style MATLAB RWL tree-ring file into R as a Tucson format object, via dplR
#!/usr/bin/env Rscript
# 2011-11-07
# Convert full-ring .mat files from Meko's velmex script to common Tucson
# format.
suppressMessages(require(R.matlab))
suppressMessages(require(dplR))
args <- commandArgs(trailingOnly = TRUE)
raw <- readMat(file.path(args[1]))
series.names <- as.vector(raw$XT[[1]], mode = 'character')
series <- as.vector(raw$XT[[2]])
names(series) <- series.names
n.series <- length(series.names)
years <- lapply(series, function(x) x[ , 1])
year.min <- min(unlist(years))
year.max <- max(unlist(years))
max.series.l <- year.max - year.min + 1 # Not sure why +1...
target <- data.frame(matrix(NA, nrow = max.series.l, ncol = n.series))
names(target) <- series.names
rownames(target) <- seq(year.max, year.min)
# Some of this should be put into a vector before the loop and then indexed.
for (i in 1:n.series) {
# end <- year.max - series[[i]][length(series[[i]][ , 1]), 1] + 1
end <- year.max - max(years[[i]]) + 1
start <- end + length(years[[i]]) - 1
target[i][start:end, 1] <- series[[i]][ , 2]
}
suppressMessages(write.tucson(rwl.df = target, fname = args[2], prec = 0.001))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment