@ -250,10 +250,38 @@ chronoamp2df <- function(datafilename, wearea = 1) {
############### amperometry2df ###################
############### amperometry2df ###################
##################################################
##################################################
amperometry2df <- function ( datafilename , wearea = 1 ) {
amperometry2df <- function ( datafilename , wearea = 1 ) {
# Function description: for recorded amperometric i-T curves
## Description:
# CH Instruments potentiostat records all data using standard SI units,
## Reads current-time data (from CHI 760 potentiostat)
# so all potential values are in volts, currents are in amperes,
## and returns a dataframe with the data,
# charges in Coulombs, time in seconds, etc.
## the data attributes (experimental conditions),
## and some calculated parameters (charge, didt, etc.)
## Usage:
## amperometry2df(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
## $ time : num
## $ current : num
## $ currentdensity : num
## $ timediff : num
## $ dIdt : num
## $ didt : num
## $ charge : num
## $ chargedensity : num
## $ InitE : num
## $ SampleInterval : num
## $ RunTime : 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.
#
#
datafile <- file ( datafilename , " r" )
datafile <- file ( datafilename , " r" )
chifile <- readLines ( datafile , n = -1 ) #read all lines of input file
chifile <- readLines ( datafile , n = -1 ) #read all lines of input file
@ -288,39 +316,54 @@ amperometry2df <- 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 , matrix ( scan ( zz , what = numeric ( ) , sep = " ," ) ,
data.frame ( stringsAsFactors = FALSE ,
ncol = 2 , byrow = T ) ) )
sampleid , matrix ( scan ( zz , what = numeric ( ) , sep = " ," ) ,
ncol = 2 , byrow = T ) ) )
close ( zz )
close ( zz )
}
}
names ( ff ) <- c ( " sampleid" , " time" , " current" )
names ( ff ) <- c ( " sampleid" , " time" , " current" )
# Calculate current density
# Calculate current density
currentdensity <- ff $ current / wearea
currentdensity <- ff $ current / wearea
ff <- cbind ( ff , currentdensity = currentdensity )
ff <- cbind ( ff , currentdensity = currentdensity )
# Calculate charge densities and differentials
# Calculate time and current diffs
charge.df <- It2charge ( ff $ currentdensity , ff $ time )
timediff <- c ( ff $ time [1 ] , diff ( ff $ time ) )
ff <- cbind ( ff , charge.df )
currentdiff <- c ( ff $ current [1 ] , diff ( ff $ current ) )
currentdensitydiff <- c ( ff $ currentdensity [1 ] , diff ( ff $ currentdensity ) )
# Calculate differential of current and current density
dIdt <- currentdiff / timediff
didt <- currentdensitydiff / timediff
# Calculate charge and charge density
charge <- cumsum ( ff $ current )
chargedensity <- cumsum ( ff $ currentdensity )
# Update ff dataframe
ff <- cbind ( ff ,
timediff = timediff ,
dIdt = dIdt ,
didt = didt ,
charge = charge ,
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 ] )
" ^Sample\\sInterval\\s\\(s\\)" ,
ff $ InitE <- InitE
" ^Run\\sTime\\s\\(sec\\)" ,
# SampleInterval (volt)
" ^Quiet\\sTime\\s\\(sec\\)" ,
position.SampleInterval <- regexpr ( " ^Sample\\sInterval\\s\\(s\\)" , chifile )
" ^Sensitivity\\s\\(A/V\\)" )
SampleInterval <- as.numeric ( strsplit ( chifile [which ( position.SampleInterval == 1 ) ] , " \\s=\\s" ) [ [1 ] ] [2 ] )
names.attr <- c ( " InitE" ,
ff$ SampleInterval <- SampleInterval
" SamplingInterval" ,
# Run time (seconds)
" RunTime" ,
position.RunTime <- regexpr ( " ^Run\\sTime\\s\\(sec\\)" , chifile )
" QuietTime" ,
RunTime <- as.numeric ( strsplit ( chifile [which ( position.RunTime == 1 ) ] , " \\s=\\s" ) [ [1 ] ] [2 ] )
" Sensitivity" )
ff $ RunTime <- RunTime
for ( n in 1 : length ( rgxp.attr ) ) {
# Quiet time (seconds)
attrow.idx <- regexpr ( rgxp.attr [n ] , chifile )
position.QuietTime <- regexpr ( " ^Quiet\\sTime\\s\\(sec\\)" , chifile )
attrow.len <- attr ( attrow.idx , " match.length" )
QuietTime <- as.numeric ( strsplit ( chifile [which ( position.QuietTime == 1 ) ] , " \\s=\\s" ) [ [1 ] ] [2 ] )
attr ( attrow.idx , " match.length" ) <- NULL
ff $ QuietTime <- QuietTime
# attrow.idx should now contain only one matching row
# Sensitivity (ampere per volt)
attr ( ff , names.attr [n ] ) <- strsplit ( chifile [which ( attrow.idx == 1 ) ] ,
position.Sensitivity <- regexpr ( " ^Sensitivity\\s\\(A/V\\)" , chifile )
" \\s=\\s" ) [ [1 ] ] [2 ]
Sensitivity <- as.numeric ( strsplit ( chifile [which ( position.Sensitivity == 1 ) ] , " \\s=\\s" ) [ [1 ] ] [2 ] )
}
ff $ Sensitivity <- Sensitivity
#
#
return ( ff )
return ( ff )
}
}