==================================================== Installing and running TTS simulations (version 4.x) ==================================================== These instructions assume you have installed R for TTS raw data plotting Download the file "simulate4.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 "simulate4.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("simulate4.txt") Quit R and save workspace: > q(save="yes") 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, "llm3523" llm3523 <- read.campbell("LM3523A.dat") # Create a set of rising and falling thresholds called up2000 and dn2000 # Both functions add 0 to the threshold sset # get.upthresh() also adds 9999 to prevent the array index going out of bounds up2000 <- get.upthresh(20,1850,10) [1] 0 20 77 170 300 467 670 910 1187 1500 1850 9999 dn2000 <- get.dnthresh(30,1900,17) [1] 0 30 62 105 159 225 302 391 491 602 724 858 1004 1160 1328 [16] 1507 1698 1900 # First simulation, using the above thresholds and minimum stage = 0.90 sim1 <- tts.sample(llm3523,up2000,dn2000,minstg=0.90) # sim1 now contains a list of the record numbers that were sampled and condition codes # 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(llm3523,sim1) plot.sim(llm3523,sim1,021227) # The latter command plots only data on or after Dec. 27, 2002 # Create some new thresholds using a log scale instead of the default square root up.new <- get.upthresh(20,950,8,log,exp) up.new [1] 0 20 35 60 105 182 315 547 950 9999 dn.new <- get.dnthresh(15,900,12,log,exp) dn.new [1] 0 15 22 32 46 66 96 140 203 295 428 620 900 # Run a second simulation with the new thresholds # And try changing some of the subroutine 7 parameters sim2 <- tts.sample(llm3523,up.new,dn.new,stay=4,repwait=72,minstg=0.90) # Examples of different ways to call plot.sim plot.sim(llm3523,sim2) plot.sim(llm3523,sim2,021227) plot.sim(llm3523,sim2,021227,2000) plot.sim(llm3523,sim2,021227,2000,021230,1500) plot.sim(llm3523,sim2,edate=021230) plot.sim(llm3523,sim2,edate=021230,etime=1500) plot.sim(llm3523,sim2,ylim=c(0,200),title="LLM simulation: new thresholds") ======================================= 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 get.upthresh: function(tstart, tmax, n, f = sqrt, finv = function(x) x^2) # Returns a vector of thresholds plus the required value of 9999 # Same arguments as get.thresh get.dnthresh: function(tstart, tmax, n, f = sqrt, finv = function(x) x^2) # This function is identical to get.thresh 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 tts.sample: function(data, up, dn, stay = 2, revpct = c(10, 20), repwait = 8, minstg = 0, turblim = 2000, limskip = 2, initcode = 3, revval = 5, basewait = 18) # Simulates TTS sampling # Reads a data frame, thresholds and sampling parameters # Returns an object containing a list of sampled records and condition codes # 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 before reusing a threshold # (unless another threshold on the same scale is used first) # 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 # basewait: minimum number of intervals between a rising sample and the next startup sample