==================================================== Installing and running TTS simulations (version 5.0) ==================================================== These instructions assume you have installed R for TTS raw data plotting Download the file "simulate5.txt" from the TTS web page by right-clicking on the "TTS Simulation Functions" link and selecting "Save Target As" in Internet Explorer or "Save Link As" in Netscape. Store "simulate5.txt" in the Startup location for raw data plotting, i.e. the location where you originally placed .Rdata Start R in the usual way. Press ESC to break out of plotting. You will see the ">" prompt for entering commands in R. Read in the functions: > source("simulate5.txt") Save the workspace: > save.image() Each time you start R to do a simulation, press ESC to get the prompt ===================================== Example R session (prompts not shown) ===================================== # Read in the data and assign it to a data frame called, for example, "eagdata" eagdata <- read.campbell("eag.dat") # Create a set of rising and falling thresholds called up2000 and dn2000 up2000 <- get.thresh(20,1850,10) up2000 [1] 20 77 170 300 467 670 910 1187 1500 1850 dn2000 <- get.thresh(30,1900,17) dn2000 [1] 30 62 105 159 225 302 391 491 602 724 858 1004 1160 1328 1507 [16] 1698 1900 # First simulation, using the above thresholds and minimum stage = 0.20 sim1 <- tts5.sample(eagdata,up2000,dn2000,minstg=0.20) # sim1 now contains a list of the record numbers that were sampled along with # threshold codes and sampled thresholds # To plot the results, pass the data and simulation results to the function plot.sim() # Turbidity is plotted in red at or above minimum stage and in black below minimum stage plot.sim(eagdata,sim1) plot.sim(eagdata,sim1,051222,0000,051223,0000,numbers=T) # The latter command plots only data on Dec. 22, 2002, and shows bottle numbers # Create some new thresholds using a log scale instead of the default square root up.new <- get.thresh(20,1850,10,log,exp) up.new [1] 20 33 55 90 150 247 409 676 1119 1850 dn.new <- get.thresh(30,1900,17,log,exp) dn.new [1] 30 39 50 65 85 110 142 184 239 309 401 520 674 873 1131 [16] 1466 1900 # Run a second simulation with the new thresholds # And try changing some of the subroutine 7 parameters. See command syntax below. sim2 <- tts5.sample(eagdata,up.new,dn.new,stay=4,repwait=72,minstg=0.30) # Examples of different ways to call plot.sim # Turbidity data collected below the minimum stage are plotted in black plot.sim(eagdata,sim2) plot.sim(eagdata,sim2,051222) # Plot starts at 051222 plot.sim(eagdata,sim2,051222,0600) # Plot starts at 6am on 051222 plot.sim(eagdata,sim2,edate=051223) # Plot ends at midnight ending 051223 plot.sim(eagdata,sim2,edate=051223,etime=1100) # Plot ends at 11am on 051223 plot.sim(eagdata,sim2,ylim=c(30,100),title="EAG simulation: new thresholds") # Sets y axis range to 30-100 and adds a title ======================================= Command syntax ======================================= -------------------------- A (very) few general rules -------------------------- R is case-sensitive Function arguments can be specified in sequential order without names Alternatively, function arguments can be specified by name in any order. Arguments that are shown as 'name = value' in the function prototype will default to the given value if omitted from the function call If a function returns an object, the result is usually assigned to a new object using the "<-" operator. Otherwise, e.g. with plotting functions, no assignment is necessary ------------------------- The functions ------------------------- read.campbell: function(file,path="C:\\newdata") # Reads a Campbell TTS file located at folder specified by 'path' # Returns a data frame containing the data # Arguments: # file - filename in quotes # path - the file location; must use "\\" to delimit subfolders get.thresh: function(tstart, tmax, n, f = sqrt, finv = function(x) x^2) # Returns a vector of thresholds # Arguments: # tstart - lowest threshold # tmax - highest threshold # n - number of thresholds # f - function designating the type of scale # Thresholds will be equally spaced after applying function f # finv - inverse function of f tts5.sample: function(data, up, dn, stay = 2, revpct = c(10, 20), repwait = 18, minstg = 0, turblim = 2000, limskip = 2, initcode = 3, revval = 5, startwait = 72) # Simulates TTS sampling at Rev 5.0 # Reads a data frame, thresholds and sampling parameters # Returns an object containing a list of sampled records, threshold codes, and thresholds # Arguments: # data - a data frame created by read.campbell() that has stg and turb columns # up - vector of ascending thresholds for rising turbidities # dn - vector of ascending thresholds for falling turbidities # stay - number of intervals required before a threshold # or reversal condition is deemed to be met # revpct - percent change required for detection of reversal # revpct[1] is for peaks and revpct[2] is for troughs # (An absolute change of revval (usually 5) is always required however) # repwait - number of intervals that must pass before reusing a threshold # minstg - minimum stage for sampling # turblim - sensor limit # limskip - number of intervals skipped between samples when turblim exceeded # initcode - initial value of threshold code # initcode=1 starts simulation in rising condition # initcode=2 starts simulation in falling condition # initcode=3 (default) starts simulation in unknown condition # revval - minimum change in turbidity units required for detection of reversal # reversal occurs only when both revval and revpct criteria are met # startwait - minimum number of intervals between a rising sample and the next startup sample plot.sim: function(df, simout, sdate, stime, edate, etime, ylim, title = "", numbers=F) # Plots a TTS sampling simulation # Arguments: # df - data frame created by read.campbell() or read.flo() function # simout - object created by tts.sample() simulation function # sdate - optional start date (yymmdd) # stime - optional start time (hhmm) # edate - optional end date (yymmdd) # etime - optional end time (hhmm) # ylim - optional ylimits, vector of length 2, e.g. c(0,500) # title - optional title for plot in quotes # numbers - show bottle numbers on plot if numbers=T; # can be very slow if plotting time periods of weeks or longer