# Read in the original text file glbtemp <- read.fwf("GLB.Ts.txt", skip=8, widths=c(4, 6, rep(5, 11), 7, 4, 7, rep(5, 3), 6), na.strings=c(" *****", "****")) header <- readLines("GLB.Ts.txt", n=8) names <- strsplit(header[8], " +")[[1]] colnames(glbtemp) <- names # Design new database: # # period_table (ID [PK], description) # # obs_table (year [PK], # period [PK] [FK period_table.ID], # temperature) periods <- data.frame(ID=1:(dim(glbtemp)[2] - 2), label=colnames(glbtemp)[c(-1, -20)]) write.table(periods, "periodStart.txt", row.names=FALSE, quote=FALSE) # THEN hand edit to add further descriptions # obs <- data.frame(year=rep(glbtemp$Year, dim(glbtemp)[2] - 2), # period=rep(periods$ID, each=dim(glbtemp[1])), # temperature=as.numeric(data.matrix(glbtemp[, c(-1, -20)]))) library(reshape) obs <- melt(glbtemp[, -20], id="Year") colnames(obs) <- c("year", "period", "temperature") obs$period <- as.numeric(factor(obs$period, levels=colnames(glbtemp)[c(-1, -20)])) write.table(obs, "obs.txt", row.names=FALSE, quote=FALSE) # Read in tables and combine them periods <- read.table("period.txt", header=TRUE) obs <- read.table("obs.txt", header=TRUE) temps <- merge(periods, obs, by.x="ID", by.y="period") glbdb <- cast(melt(temps[, -1], measure="temperature"), year ~ label) # Check load("GLB.rda") all(mapply(identical, glbModel, glbdb[c("year", as.character(periods$label))]))