Updated lsv2df(). Now includes attributes in dataframe columns.

Added calculation of charge density.
Also updated the description.
master
Taha Ahmed 13 years ago
parent 0f936f1e62
commit 2287b7f57f

89
CHI.R

@ -488,10 +488,38 @@ cv2df <- function(datafilename, wearea = 1) {
################### lsv2df ####################### ################### lsv2df #######################
################################################## ##################################################
lsv2df <- function(datafilename, wearea = 1) { lsv2df <- function(datafilename, wearea = 1) {
# Function description: ## Description:
# CH Instruments potentiostat records all data using standard SI units, ## Reads LSV datafiles from CHI 760 potentiostat
# so all potential values are in volts, currents are in amperes, ## (potential, current, and charge)
# charges in Coulombs, time in seconds, etc. ## and returns a dataframe with the data,
## the data attributes (experimental conditions),
## and calculated current density and charge density.
## Usage:
## lsv2df(datafilename, wearea)
## Arguments:
## datafilename: text string with full path to experimental file
## wearea: (optional) area of working electrode (in square centimeter)
## Value:
## Dataframe with the following columns (and no extra attributes):
## $ sampleid : chr
## $ segment : num
## $ potential : num
## $ current : num
## $ charge : num
## $ currentdensity : num
## $ chargedensity : num
## $ InitE : num
## $ FinalE : num
## $ ScanRate : num
## $ SampleInterval : num
## $ QuietTime : num
## $ Sensitivity : num
## Note:
## The CH Instruments 760 potentiostat records all data
## using standard SI units, therefore this function
## assumes all potential values to be in volts,
## currents to be in amperes, charges in Coulombs,
## time in seconds, and so on.
# #
lsvfile <- file(datafilename, "r") lsvfile <- file(datafilename, "r")
chifile <- readLines(lsvfile, n = -1) #read all lines of input file chifile <- readLines(lsvfile, n = -1) #read all lines of input file
@ -526,7 +554,8 @@ lsv2df <- function(datafilename, wearea = 1) {
for (s in 1:length(starts)) { for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r") zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff, ff <- rbind(ff,
data.frame(sampleid, segment = factor(s), data.frame(stringsAsFactors = FALSE,
sampleid, segment = s,
matrix(scan(zz, what = numeric(), sep = ","), matrix(scan(zz, what = numeric(), sep = ","),
ncol = 3, byrow = T))) ncol = 3, byrow = T)))
close(zz) close(zz)
@ -534,30 +563,36 @@ lsv2df <- function(datafilename, wearea = 1) {
names(ff) <- c("sampleid", "segment", "potential", "current", "charge") names(ff) <- c("sampleid", "segment", "potential", "current", "charge")
# Calculate current density # Calculate current density
currentdensity <- ff$current / wearea currentdensity <- ff$current / wearea
ff <- cbind(ff[, 1:4], currentdensity = currentdensity, charge = ff[, 5]) ff <- cbind(ff, currentdensity = currentdensity)
# Calculate charge density
chargedensity <- ff$charge / wearea
ff <- cbind(ff, chargedensity = chargedensity)
# #
### Collect attributes of this experiment ### Collect attributes of this experiment
# These attributes are specific for each kind of experiment, # InitE (volt)
# be careful when adapting to other electrochemical data position.InitE <- regexpr("^Init\\sE\\s\\(V\\)", chifile)
rgxp.attr <- c("^Init\\sE\\s\\(V\\)", InitE <- as.numeric(strsplit(chifile[which(position.InitE == 1)], "\\s=\\s")[[1]][2])
"^Final\\sE\\s\\(V\\)", ff$InitE <- InitE
"^Scan\\sRate\\s\\(V/s\\)", # FinalE (volt)
"^Sample\\sInterval\\s\\(V\\)", position.FinalE <- regexpr("^Final\\sE\\s\\(V\\)", chifile)
"^Quiet\\sTime\\s\\(sec\\)", FinalE <- as.numeric(strsplit(chifile[which(position.FinalE == 1)], "\\s=\\s")[[1]][2])
"^Sensitivity\\s\\(A/V\\)") ff$FinalE <- FinalE
names.attr <- c("InitE", # ScanRate (volt per second)
"FinalE", position.ScanRate <- regexpr("^Scan\\sRate\\s\\(V/s\\)", chifile)
"ScanRate", ScanRate <- as.numeric(strsplit(chifile[which(position.ScanRate == 1)], "\\s=\\s")[[1]][2])
"SamplingInterval", ff$ScanRate <- ScanRate
"QuietTime", # SampleInterval (volt)
"Sensitivity") position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(V\\)", chifile)
for (n in 1:length(rgxp.attr)) { SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
attrow.idx <- regexpr(rgxp.attr[n], chifile) ff$SampleInterval <- SampleInterval
attrow.len <- attr(attrow.idx, "match.length") # Quiet time (seconds)
attr(attrow.idx, "match.length") <- NULL position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
attr(ff, names.attr[n]) <- strsplit(chifile[which(attrow.idx == 1)], QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\s=\\s")[[1]][2])
"\\s=\\s")[[1]][2] ff$QuietTime <- QuietTime
} # Sensitivity (ampere per volt)
position.Sensitivity <- regexpr("^Sensitivity\\s\\(A/V\\)", chifile)
Sensitivity <- as.numeric(strsplit(chifile[which(position.Sensitivity == 1)], "\\s=\\s")[[1]][2])
ff$Sensitivity <- Sensitivity
# #
return(ff) return(ff)
} }

Loading…
Cancel
Save