User:Phil Hurvitz/Projects/In process/MSB/Scripts/read sms 6.R

From phurvitz
Jump to: navigation, search
# makes "sms.csv" a formatted file from the SMS records

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

# a function to do the process
process.sms <- function(subject.id, timediff) {
    # where are files?
    current.dir <- getwd()
    basedir <- "/home/phurvitz/public_html/msb/processed_data/downloaded_data"
    subject.dir <- paste(basedir, sid, sep="/")
    setwd(subject.dir)
    subject.dir <- paste(basedir, "/", subject.id, "/", sep="") 

    # is the arg a file?
    if (!file.exists(subject.dir)) {
        err.txt <- paste(subject.dir, "does not exist!", "\n", sep=" ")
        cat(err.txt)
        setwd(current.dir)
        return(invisible())
    }
    
    # is the arg a dir?
    if (!file.info(subject.dir)$isdir) {
        err.txt <- paste(subject.dir, "is not a dir!", "\n", sep=" ")
        cat(err.txt)
        setwd(current.dir)
        return(invisible())
    }

    # time difference, if not specified, set to 0
    if (missing(timediff)) {
      timediff <- 0
    }
    
    # parse the sms log file
    if (.Platform$OS.type=="windows") {
        home <- "P:/"
        cmd.parse.sms <- paste(Sys.getenv("COMSPEC"), "/c", "p:\\public_html\\msb\\tools\\msb_parse_sms.pl")
    }
    else {
        home <- "/home/phurvitz/"
        cmd.parse.sms <- paste(home, "public_html/msb/tools/msb_parse_sms.pl", sep="")
    }
    system(cmd.parse.sms)

    # read sms log
    num.subject.id <- as.numeric(gsub("[a-z]", "", subject.id)) 
    if (num.subject.id < 11) {
        csv.file <- "sms6.csv"
    }

    # read in the file
    csv.file.in <- paste(home, "public_html/msb/processed_data/", csv.file, sep="")
    sms <- read.csv(csv.file.in, head=T, as.is=T)

    # leap-seconds offset for GPS
    gps.offset <- -14

    # start of the GPS epoch
    start.epoch <- strptime("1980-01-06", "%Y-%m-%d", "GMT")

    # only take records for this subject
    sms <- sms[sms[,1]==subject.id,]
    
    # convert the sms network time to a unix seconds value
    sms$sms.net.localtime <- as.POSIXct(strptime(sms$sms.net.localtime, "%Y-%b-%d %H:%M:%S"))

    # change the sms time to a formal date string
    sms$sms.phone.localtime <- as.POSIXct(strptime(sms$sms.phone.localtime, "%I:%M:%S %p %m/%d/%Y"))
    
    # sort
    o <- order(sms$sms.phone.localtime)
    sms <- sms[o,]

    # adjust the date for specific subjects
    if (subject.id=="s01") {
        sms$sms.phone.localtime <- sms$sms.phone.localtime - 60 * 60
    }
    if (subject.id=="s03" | subject.id=="s06") {  ## phone date was set incorrectly
        # get the mean difference between the net and phone time
        timediff.03 <- mean(as.numeric(difftime(sms$sms.net.localtime, sms$sms.phone.localtime, units="s")))
        sms$sms.phone.localtime <- sms$sms.phone.localtime + timediff.03
        cat(paste("adjusting", subject.id, "by", timediff.03, "\n"))
    }

    # date
    sms$sms.phone.localdate <- as.POSIXct(strptime(sms$sms.phone.localtime, format="%F"))

    # change the "never" string to "-9"
    sms$gps.lock.last <- ifelse(sms$gps.lock.last=="never", "-9", sms$gps.lock.last)

    # "unique" rows
    sms <- sms[!duplicated(cbind(sms$msb.secs, format(sms$localtime, "%Y-%m-%d %H"))),]

    # reinitialize row numbers
    rownames(sms) <- NULL

    # write out the table
    colnames(sms) <- unfix.colnames(sms)
    outFN <- paste(subject.dir, "sms.csv", sep="")
    write.table(sms, file=outFN, col.names=T, row.names=F, quote=T, sep=",")
    colnames(sms) <- fix.colnames(sms)
    
    setwd(current.dir)
    return(sms)
}