# Written by Cameron Bracken - circa 2010
read_rdf_header <- function(con,end){
#assumes connection is already correctly positioned
obj <- list()
repeat{
line <- readLines(con,n=1)
if(line == end) break
splitLine <- strsplit(line,':')[[1]]
name <- splitLine[1]
if(length(splitLine) > 1){
if(substr(splitLine[2],1,1) == ' ')
splitLine[2] <- substr(splitLine[2],2,nchar(splitLine[2]))
contents <- paste(splitLine[2:length(splitLine)],collapse=':')
} else{
contents <- NA
}
obj[[name]] <- contents
if(end == 1) break
}
#returns the object, but connection is advanced
return(obj)
}
read_rdf_meta <- function(rdf.con,rdf.obj){
rdf.obj[['meta']] <- read_rdf_header(rdf.con,'END_PACKAGE_PREAMBLE')
return(rdf.obj)
}
read_next_line_and_rewind <- function(con){
str <- readLines(con,n=1)
seek(con,seek(con)-nchar(str)-2)
return(str)
}
read_rdf_run <- function(rdf.con,rdf.obj){
this.run <- length(rdf.obj$runs) + 1
rdf.obj$runs[[this.run]] <- read_rdf_header(rdf.con,'END_RUN_PREAMBLE')
#time steps
nts <- as.integer(rdf.obj$runs[[this.run]]$time_steps)
#for non-mrm files
if(length(nts) == 0)
nts <- as.integer(rdf.obj$runs[[this.run]]$timesteps)
rdf.obj$runs[[this.run]][['times']] <- readLines(rdf.con,n=nts)
#Series
nob <- 0
repeat{
nob <- nob + 1
rdf.obj$runs[[this.run]][['objects']][[nob]] <- read_rdf_header(rdf.con,'END_SLOT_PREAMBLE')
#name the objecst after their object.slot name
obj.name <- rdf.obj$runs[[this.run]][['objects']][[nob]]$object_name
slot.name <- rdf.obj$runs[[this.run]][['objects']][[nob]]$slot_name
name <- paste(obj.name,slot.name,sep='.')
names(rdf.obj$runs[[this.run]][['objects']])[nob] <- name
#read in the extr two header pieces
rdf.obj$runs[[this.run]][['objects']][[nob]]$units <- read_rdf_header(rdf.con,1)[[1]]
rdf.obj$runs[[this.run]][['objects']][[nob]]$scale <- read_rdf_header(rdf.con,1)[[1]]
rdf.obj$runs[[this.run]][['objects']][[nob]]$values <- as.numeric(readLines(rdf.con,n=nts))
#END_COLUMN,END_SLOT, table slots need support here
dummy <- readLines(rdf.con,n=2)
if(read_next_line_and_rewind(rdf.con) == 'END_RUN'){
dummy <- readLines(rdf.con,n=1)
break
}
}
return(rdf.obj)
}
#' Read an rdf file into R.
#'
#' \code{read.rdf} reads an rdf file into R and formats it as a multi-level list containing
#' all of the metadata included in the rdf file. Rdf files are generated by RiverWare
#' and are documented in the \href{http://riverware.org/PDF/RiverWare/documentation/}{RiverWare documentation}.
#'
#' @param file The input rdf file that will be read into R.
#' @return A multi-level list containing all metadata and slot data in the original rdf file.
#' @examples
#' zz <- read.rdf(system.file('extdata/Scenario/DNF,CT,IG', "KeySlots.rdf", package = "RWDataPlot"))
#'
#' @export
read.rdf <- function(file){
rdf.obj <- list()
rdf.con <- file(file,'r')
rdf.obj <- read_rdf_meta(rdf.con,rdf.obj)
for(i in 1:as.numeric(rdf.obj$meta$number_of_runs))
rdf.obj <- read_rdf_run(rdf.con,rdf.obj)
close(rdf.con)
return(rdf.obj)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.