R/plotOperatingStatus.R

Defines functions plotOperatingStatus

Documented in plotOperatingStatus

#' Plot receiver operating status.
#'
#' Plots a summary of the status data generated by \code{getOperatingStatus()}
#'
#' @param site site name
#' 
#' @param proj project name
#'
#' @param year deployment year (defaults to current year)
#'
#' @param pngFile full path to output .png filename
#' 
#' @return a data frame with these columns
#' \describe{
#' \item{files.per.hour}{data frame with at most one record per hour, giving number of raw data files recorded and bootcount}
#' \item{gps.fix}{data frame with at most hourly true GPS fix (i.e. repeated 'stuck' fixes are not reported)}
#' \item{undated.period}{the number of days of data recorded with dates beginning with 1 Jan 2000; these are periods when the GPS had not set the clock}
#' \item{num.boots}{the number of times the system was restarted during the operating period}
#' \item{pulse.counts}{the number of pulses per antenna for each hour}
#'}
#'
#' Plots to the current graphics device.
#'
#' @author John Brzustowski \email{jbrzusto@@REMOVE_THIS_PART_fastmail.fm}

plotOperatingStatus = function(site, proj, year = lubridate::year(Sys.time()), pngFile) {
    library(lubridate)
    a = getOperatingStatus(site, proj, year)

    ## filter out data more than 3 years from start date (eliminate wonky GPS records) and before
    ## the start date
    start_ts = ymd(sprintf("%d-01-01", year))
    a$files.per.hour = subset(a$files.per.hour, ts >= start_ts & ts <= start_ts + 3 * 365.25 * 24 * 3600)
    timerange = range(a$files.per.hour$ts)

    ## make sure we have a non-empty range of dates
    xx = trunc(a$files.per.hour$ts, "day")
    rangex = range(xx)
    if (isTRUE(diff(rangex) == 0)) {
        xx = trunc(a$files.per.hour$ts, "hour")
        rangex = range(xx)
        if (isTRUE(diff(rangex) == 0)) {
            rangex = rangex + c(0, 1)
        }
    }

    if (any(is.na(rangex))) {
        tt = a$pulse.counts$ts
        tt = tt [ tt >= ymd("2010-01-01")]
        rangex = range(tt)
    }
    png(pngFile, width = 300 + diff(rangex) * 30, height = 600, type="cairo-png")
    yy = hour(a$files.per.hour$ts)
    
    plot(xx,
         yy,
         pch = ' ',
         col = "green",
         ylim = c(0, 24),
         xlim = as.numeric(rangex),
         main = c(sprintf("%d %s %s", year, proj, site), "Receiver status based on raw data file timestamps and GPS fixes"),
         xlab = "",
         ylab = "Hour of Day (GMT)",
         sub = sprintf("Date (begins %s GMT)\n Background colour key: Green: Writing Files and GPS working;    Yellow: Writing Files but GPS stuck;    Red: Not Writing Files\nBlack *: reboot;   Black line: reboot count, wrapping at 24; period shown had %d reboots; Antenna bar indicates pulses were detected that hour."
             , dateStem(rangex), a$num.boots)
         )

    rect (min(xx), min(yy), max(xx), max(yy)+1, col="#ff4040")
    rect(xx, yy, xx + 3600*24, yy + 1,
        col="yellow", border="yellow")
     
    xx = trunc(a$gps$ts, "day")
    yy = hour(a$gps$ts)
    rect(xx, yy, xx + 24*3600, yy + 1, col="green", border="green")
    abline(v = seq(from = trunc(timerange[1], "days"), to = trunc(timerange[2], "days"), by = 24 * 3600), lty=3, col="gray")
    rb = a$files.per.hour$ts[which(c(FALSE, diff(a$files.per.hour$bootnum) > 0))]
    points(trunc(rb, "day")+12*3600, hour(rb), pch="*", col="black")
    points(a$files.per.hour$ts, a$files.per.hour$bootnum %% 24, type="s", col="black")

    pc = a$pulse.counts
    cols = c("#0080ff", "#ff7f00", "#00ffff", "#ff00ff", "#106010", "#8080ff", "#ffff80")
    if (! is.null(pc)) {
        pc = pc[grep("^[a-z][0-9]{1,2}$", pc$pcode, perl=TRUE),]
        pc$n = as.integer(as.factor(pc$pcode))
        pc$n[pc$n > 6] = 6
        pc$col = cols[pc$n]
        pc$x1 = trunc(pc$ts, "day") + (pc$n - 1) * 3600 * 4
        pc$y1 = hour(pc$ts)
        rect(pc$x1, pc$y1, pc$x1+3600*4, pc$y1+1, col=pc$col, border=NA)
        legend("topleft", fill=cols, border="black", legend=substring(levels(as.factor(pc$pcode)), 2), title="Ant #", bty="n")
    }
    dev.off()
    
    return(a)
}
jbrzusto/sensorgnome-R-package documentation built on May 18, 2019, 9:19 p.m.