Main Page/Research/MSB/Scripts/read.msb.files.R

From phurvitz
< Main Page‎ | Research‎ | MSB‎ | Scripts
Revision as of 02:45, 3 October 2007 by Phil Hurvitz (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
# libraries
library(gmt)

# functions
source ("http://gis.washington.edu/phurvitz/R/functions.R") 

#indir <- "C:/users/phurvitz/htdocs/phurvitz/msb/processed_data/research/projects/ubicomp3/phurvitz/gis.washington.edu/phurvitz/msb/data/pmh04_20070921/"
indir <- "C:/users/phurvitz/htdocs/phurvitz/msb/processed_data/research/projects/ubicomp3/phurvitz/gis.washington.edu/phurvitz/msb/data/pmh04_20070921a"
setwd(indir)

# get a list of directories
list.allfiles <- list.files(indir)
list.dirs <- NULL
for (f in list.allfiles) {
    if (file.info(f)$isdir) {
        list.dirs <- c(list.dirs, f)
    }
}
count.list.dirs <- length(list.dirs)

# file list from one of the dirs
filelist0 <- list.files(list.dirs[1], pattern=".*csv")

# initialize master lists
for (f in filelist0) {
    command.init.csv <- paste(f, "<- NULL")
    eval(parse(text=command.init.csv))
}

# timestamp adjuster
time.adj <- -16 * 3600

# for each directory
for (d in list.dirs) {
    # a list of CSV files in the directory
    filelist <- list.files(d, pattern=".*csv")
    # for each CSV file in the directory
    for (f in filelist) {
        # get the relative path to the CSV
        fn <- (paste(d, f, sep="/"))
        # formulate this as a data frame name in the R session
        tn <- (paste(d, f, sep="."))
        # create a command to read the filename into the data frame
        command.read.csv <- paste(tn, "<- read.csv(", quote(fn), ", head=F)", sep="")
        # print the command (for testing)
        #print(command.read.csv)
        # evaluate the command
        as.data.frame(eval(parse(text=command.read.csv)))
        # fix the colnames and timestamps on these
        if (attr(regexpr("class", fn), "match.length") > 0 ) {
            command.set.colnames <- paste("colnames(", tn, ") <- c(\"longitude\", \"latitude\", \"unixtime\", \"updown\", \"moving\")", sep="")
        }
        else if (attr(regexpr("Output.csv", fn), "match.length") > 0 ) {
            command.set.colnames <- paste("colnames(", tn, ") <- c(\"longitude\", \"latitude\", \"elevation\", \"unixtime\")", sep="")
        }
        else if (attr(regexpr("timestamp", fn), "match.length") > 0 ) {
            command.set.colnames <- paste("colnames(", tn, ") <- c(\"msbtime\", \"unixtime\")", sep="")
        }
        # set the colnames
        eval(parse(text=command.set.colnames))
        
        # set the timestamp
        command.set.timestamp <- paste(tn, "$date <- ISOdatetime(1970,1,1,0,0,0) + ", tn, "$unixtime + time.adj", sep="")
        eval(parse(text=command.set.timestamp))
        command.set.timestamp <- paste(tn, "$reftime <- ISOdatetime(1970,1,1,0,0,0) + ", tn, "$unixtime", sep="")
        eval(parse(text=command.set.timestamp))
        
        # set the MSB seconds
        if (attr(regexpr("timestamp", fn), "match.length") > 0 ) {
            command.set.msbtime <- paste(tn, "$msbseconds <- round(", tn, "$msbtime / 1000, 0)", sep="")
            #print(command.set.msbtime)
            eval(parse(text=command.set.msbtime)) 
        }
        
        # concatenate with the master list
        command.concatenate.table <- paste(f, "<- rbind (", f, ",", tn, ")", sep="")
        print(command.concatenate.table)
        eval(parse(text=command.concatenate.table))
        
        # write out the files
        command.write.table <- paste("write.table 
          (", tn, ", file=\"", tn, "\", col.names=T, row.names=F, sep=\",\")", sep="")
        print(command.write.table)
        eval(parse(text=command.write.table)) 
    }
}

# dump out concatenated tables
for (f in filelist0) {
    command.write.table <- paste("write.table (", f, ", file=\"", f, "\", col.names=T, row.names=F, sep=\",\")", sep="")
    print(command.write.table)
    eval(parse(text=command.write.table))
}
write.table (gps_class.csv, file="class.csv", col.names=T, row.names=F, sep=",")
write.table (sirfGPSOutput_timestamp.csv, file="timestmp.csv", col.names=T, row.names=F, sep=",")
write.table (sirfGPSOutput.csv, file="gps.csv", col.names=T, row.names=F, sep=",")

# read phone log
phone.log <- read.csv("P:/public_html/msb/processed_data/phonelog.csv", as.is=T)
phone.log <- phone.log[phone.log$time.phone!="",]
phone.log$time.phone <- as.character(phone.log$time.phone)
phone.log$date <- as.character(strptime(phone.log$time.phone, "%I:%M:%S %p %m/%d/%Y"))
write.table(phone.log, file="phone_log.csv", col.names=T, row.names=F, sep=",")