Split original git repo by subdirectory.

master
Taha Ahmed 13 years ago
parent 5ee85d580e
commit a6466ddc02

2
.gitignore vendored

@ -1,3 +1,5 @@
*/
*.info
*.RData
*.Rdeprecated
*.Rhistory

@ -1,7 +0,0 @@
# AUTOLAB.R
# Functions to read and manipulate data from AUTOLAB potentiostats/galvanostats
# Taha Ahmed, June 2011
# CONTENTS
# >>>> amp2df
# >>>> ocp2df

@ -1,89 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
#################### amp2df ######################
##################################################
amp2df <- function(datafilename, wearea = 1) {
## Description:
## Reads current-time data (from AUTOLAB potentiostat)
## and returns a dataframe with the data and some
## calculated quantities based on the data.
## Usage:
## amp2df(datafilename, wearea)
## Arguments:
## datafilename: text string with full path to experimental file
## wearea: (optional) area of working electrode (in square centimeters)
## Value:
## Dataframe with the following columns:
## $ sampleid : chr
## $ time : num
## $ current : num
## $ currentdensity : num
## $ timediff : num
## $ dIdt : num
## $ didt : num
## $ charge : num
## $ chargedensity : num
#
datafile <- file(datafilename, "r")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\d+\\.\\d+"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers _without_ a negative sign (hyphen),
# followed by one or more digits before the decimal, a decimal point,
# and an one or more digits after the decimal point.
# Note that backslashes are escaped.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(sampleid,
stringsAsFactors = FALSE,
matrix(scan(zz, what = numeric(), sep = ""),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "time", "current")
# Calculate current density
currentdensity <- ff$current / wearea
ff <- cbind(ff, currentdensity = currentdensity)
# Calculate time diff and current diff
timediff <- c(ff$time[1], diff(ff$time))
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)
#
return(ff)
}

@ -1,61 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
#################### ocp2df ######################
##################################################
ocp2df <- function(datafilename) {
## Description:
## Reads voltage-time data (from AUTOLAB potentiostat)
## and returns a dataframe with the data.
## Usage:
## ocp2df(datafilename)
## Arguments:
## datafilename: text string with full path to experimental file
## Value:
## Dataframe with the following columns:
## $ sampleid : chr
## $ time : num
## $ potential : num
#
datafile <- file(datafilename, "r")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\d+\\.\\d+"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers _without_ a negative sign (hyphen),
# followed by one or more digits before the decimal, a decimal point,
# and an one or more digits after the decimal point.
# Note that backslashes are escaped.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(sampleid,
stringsAsFactors = FALSE,
matrix(scan(zz, what = numeric(), sep = ""),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "time", "potential")
#
return(ff)
}

@ -1,12 +0,0 @@
# CHI.R
# Functions to read and manipulate data from the CHI760 potentiostat/galvanostat
# Taha Ahmed, Jan 2011 - Feb 2011
# CONTENTS
# >>>> mps2df
# >>>> ocp2df
# >>>> chronocm2df
# >>>> chronoamp2df
# >>>> amperometry2df
# >>>> cv2df
# >>>> lsv2df

@ -1,123 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
############### amperometry2df ###################
##################################################
amperometry2df <- function(datafilename, wearea = 1) {
## Description:
## Reads current-time data (from CHI 760 potentiostat)
## and returns a dataframe with the data,
## 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")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by either the letter 'e' or a comma.
# Note that backslashes are escaped due to the way R handles strings.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(stringsAsFactors = FALSE,
sampleid, matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "time", "current")
# Calculate current density
currentdensity <- ff$current / wearea
ff <- cbind(ff, currentdensity = currentdensity)
# Calculate time and current diffs
timediff <- c(ff$time[1], diff(ff$time))
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
# InitE (volt)
position.InitE <- regexpr("^Init\\sE\\s\\(V\\)", chifile)
InitE <- as.numeric(strsplit(chifile[which(position.InitE == 1)], "\\s=\\s")[[1]][2])
ff$InitE <- InitE
# SampleInterval (volt)
position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(s\\)", chifile)
SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
ff$SampleInterval <- SampleInterval
# Run time (seconds)
position.RunTime <- regexpr("^Run\\sTime\\s\\(sec\\)", chifile)
RunTime <- as.numeric(strsplit(chifile[which(position.RunTime == 1)], "\\s=\\s")[[1]][2])
ff$RunTime <- RunTime
# Quiet time (seconds)
position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\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)
}

@ -1,122 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
################# chronoamp2df ###################
##################################################
chronoamp2df <- function(datafilename, wearea = 1) {
## Description:
## Reads current-time data (from CHI 760 potentiostat)
## and returns a dataframe with the data and the
## data attributes (experimental conditions).
## Usage:
## chronoamp2df(datafilename, wearea)
## Arguments:
## datafilename: text string with full path to experimental file
## wearea: (optional) area of working electrode (in square centimeters)
## Value:
## Dataframe with the following columns (and no extra attributes):
## $ sampleid : chr
## $ step : num
## $ time : num
## $ current : num
## $ currentdensity : num
## $ InitE : num
## $ HighE : num
## $ LowE : num
## $ InitPN : chr
## $ Step : num
## $ Pulse width : num
## $ Sample interval : num
## $ Quiet Time : 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")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by either the letter 'e' or a comma.
# Note that backslashes are escaped.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(sampleid, step.current = s,
matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "step.current", "time", "current")
# Calculate current density
currentdensity <- ff$current / wearea
ff <- cbind(ff, currentdensity = currentdensity)
#
### Collect attributes of this experiment
# InitE (volt)
position.InitE <- regexpr("^Init\\sE\\s\\(V\\)", chifile)
InitE <- as.numeric(strsplit(chifile[which(position.InitE == 1)], "\\s=\\s")[[1]][2])
ff$InitE <- InitE
# HighE (volt)
position.HighE <- regexpr("^High\\sE\\s\\(V\\)", chifile)
HighE <- as.numeric(strsplit(chifile[which(position.HighE == 1)], "\\s=\\s")[[1]][2])
ff$HighE <- HighE
# LowE (volt)
position.LowE <- regexpr("^Low\\sE\\s\\(V\\)", chifile)
LowE <- as.numeric(strsplit(chifile[which(position.LowE == 1)], "\\s=\\s")[[1]][2])
ff$LowE <- LowE
# InitPN
position.InitPN <- regexpr("^Init\\sP/N", chifile)
InitPN <- strsplit(chifile[which(position.InitPN == 1)], "\\s=\\s")[[1]][2]
ff$InitPN <- InitPN
# Steps (total number of steps)
position.Steps <- regexpr("^Step\\s", chifile)
Steps <- as.numeric(strsplit(chifile[which(position.Steps == 1)], "\\s=\\s")[[1]][2])
ff$Steps <- Steps
# Pulse width (s)
position.PulseWidth <- regexpr("^Pulse\\sWidth\\s\\(sec\\)", chifile)
PulseWidth <- as.numeric(strsplit(chifile[which(position.PulseWidth == 1)], "\\s=\\s")[[1]][2])
ff$PulseWidth <- PulseWidth
# Sample interval (s)
position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(s\\)", chifile)
SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
ff$SampleInterval <- SampleInterval
# Quiet Time (s)
position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\s=\\s")[[1]][2])
ff$QuietTime <- QuietTime
# Sensitivity (A/V)
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)
}

@ -1,78 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
################# chronocm2df ####################
##################################################
chronocm2df <- function(datafilename) {
# Function description: chronocoulometry data
# CH Instruments potentiostat records all data using standard SI units,
# so all potential values are in volts, currents are in amperes,
# charges in Coulombs, time in seconds, etc.
#
datafile <- file(datafilename, "r")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by either the letter 'e' or a comma.
# Note that backslashes are escaped.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(sampleid, step = factor(s),
matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "step", "time", "charge")
#
### Collect attributes of this experiment
# These attributes are specific for each kind of experiment,
# be careful when adapting to other electrochemical data
rgxp.attr <- c("^Init\\sE\\s\\(V\\)",
"^Final\\sE\\s\\(V\\)",
"^Step\\s",
"^Pulse\\sWidth\\s\\(sec\\)",
"^Sample\\sInterval\\s\\(s\\)",
"^Quiet\\sTime\\s\\(sec\\)",
"^Sensitivity\\s\\(A/V\\)")
names.attr <- c("InitE",
"FinalE",
"Steps",
"PulseWidth",
"SamplingInterval",
"QuietTime",
"Sensitivity")
for (n in 1:length(rgxp.attr)) {
attrow.idx <- regexpr(rgxp.attr[n], chifile)
attrow.len <- attr(attrow.idx, "match.length")
attr(attrow.idx, "match.length") <- NULL
attr(ff, names.attr[n]) <- strsplit(chifile[which(attrow.idx == 1)],
"\\s=\\s")[[1]][2]
}
#
return(ff)
}

@ -1,94 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
#################### cv2df #######################
##################################################
cv2df <- function(datafilename, wearea = 1) {
# Function description:
# CH Instruments potentiostat records all data using standard SI units,
# so all potential values are in volts, currents are in amperes,
# charges in Coulombs, time in seconds, etc.
#
cvfile <- file(datafilename, "r")
chifile <- readLines(cvfile, n = -1) #read all lines of input file
close(cvfile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+,"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by a comma.
# Note that backslashes are escaped.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(sampleid, cycle = as.integer(ceiling(s/2)), segment = s,
matrix(scan(zz, what = numeric(), sep = ","),
ncol = 3, byrow = T)))
close(zz)
}
# Column names after initial assignment
names(ff) <- c("sampleid", "cycle", "segment", "potential", "current", "charge")
# Calculate current density
currentdensity <- ff$current / wearea
ff <- cbind(ff[, 1:5], currentdensity = currentdensity, charge = ff[, 6])
## Collect attributes of this experiment
# InitE (volt)
position.InitE <- regexpr("^Init\\sE\\s\\(V\\)", chifile)
InitE <- as.numeric(strsplit(chifile[which(position.InitE == 1)], "\\s=\\s")[[1]][2])
ff$InitE <- InitE
# HighE (volt)
position.HighE <- regexpr("^High\\sE\\s\\(V\\)", chifile)
HighE <- as.numeric(strsplit(chifile[which(position.HighE == 1)], "\\s=\\s")[[1]][2])
ff$HighE <- HighE
# LowE (volt)
position.LowE <- regexpr("^Low\\sE\\s\\(V\\)", chifile)
LowE <- as.numeric(strsplit(chifile[which(position.LowE == 1)], "\\s=\\s")[[1]][2])
ff$LowE <- LowE
# InitPN (positive or negative)
position.InitPN <- regexpr("^Init\\sP/N", chifile)
InitPN <- strsplit(chifile[which(position.InitPN == 1)], "\\s=\\s")[[1]][2]
ff$InitPN <- InitPN
# ScanRate (volt per second)
position.ScanRate <- regexpr("^Scan\\sRate\\s\\(V/s\\)", chifile)
ScanRate <- as.numeric(strsplit(chifile[which(position.ScanRate == 1)], "\\s=\\s")[[1]][2])
ff$ScanRate <- ScanRate
# Segments, number of
position.Segments <- regexpr("^Segment\\s=", chifile)
Segments <- as.numeric(strsplit(chifile[which(position.Segments == 1)], "\\s=\\s")[[1]][2])
ff$Segments <- Segments
# SampleInterval (volt)
position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(V\\)", chifile)
SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
ff$SampleInterval <- SampleInterval
# Quiet time (seconds)
position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\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)
}

@ -1,114 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
################### lsv2df #######################
##################################################
lsv2df <- function(datafilename, wearea = 1) {
## Description:
## Reads LSV datafiles from CHI 760 potentiostat
## (potential, current, and charge)
## 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 (id)
## $ segment : num (id)
## $ potential : num (measure)
## $ current : num (measure)
## $ charge : num (measure)
## $ currentdensity : num (measure)
## $ chargedensity : num (measure)
## $ InitE : num (id)
## $ FinalE : num (id)
## $ ScanRate : num (id)
## $ SampleInterval : num (id)
## $ QuietTime : num (id)
## $ Sensitivity : num (id)
## 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")
chifile <- readLines(lsvfile, n = -1) #read all lines of input file
close(lsvfile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+,"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by a comma.
# Note that backslashes are escaped.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(stringsAsFactors = FALSE,
sampleid, segment = s,
matrix(scan(zz, what = numeric(), sep = ","),
ncol = 3, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "segment", "potential", "current", "charge")
# Calculate current density
currentdensity <- ff$current / wearea
ff <- cbind(ff, currentdensity = currentdensity)
# Calculate charge density
chargedensity <- ff$charge / wearea
ff <- cbind(ff, chargedensity = chargedensity)
#
### Collect attributes of this experiment
# InitE (volt)
position.InitE <- regexpr("^Init\\sE\\s\\(V\\)", chifile)
InitE <- as.numeric(strsplit(chifile[which(position.InitE == 1)], "\\s=\\s")[[1]][2])
ff$InitE <- InitE
# FinalE (volt)
position.FinalE <- regexpr("^Final\\sE\\s\\(V\\)", chifile)
FinalE <- as.numeric(strsplit(chifile[which(position.FinalE == 1)], "\\s=\\s")[[1]][2])
ff$FinalE <- FinalE
# ScanRate (volt per second)
position.ScanRate <- regexpr("^Scan\\sRate\\s\\(V/s\\)", chifile)
ScanRate <- as.numeric(strsplit(chifile[which(position.ScanRate == 1)], "\\s=\\s")[[1]][2])
ff$ScanRate <- ScanRate
# SampleInterval (volt)
position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(V\\)", chifile)
SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
ff$SampleInterval <- SampleInterval
# Quiet time (seconds)
position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\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)
}

@ -1,147 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
################### mps2df #######################
##################################################
mps2df <- function(datafilename, wearea = 1) {
## Description:
## Reads time vs current data (from CHI 760 potentiostat)
## and returns a dataframe with the data and
## the data attributes (experimental conditions).
## Usage:
## mps2df(datafilename, wearea)
## Arguments:
## datafilename: text string with full path to experimental file
## wearea: working electrode area in square centimeters (optional)
## 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
## $ PotentialSteps : num
## $ TimeSteps : num
## $ Cycle : 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.
#
datafile <- file(datafilename, "r")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d+\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by either the letter 'e' or a comma.
# Note that backslashes are escaped due to the way R handles strings.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(stringsAsFactors = FALSE,
sampleid, matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "time", "current")
# Calculate current density
currentdensity <- ff$current / wearea
ff <- cbind(ff, currentdensity = currentdensity)
# Calculate time diff and current diff
timediff <- c(ff$time[1], diff(ff$time))
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
# Potential steps
PotentialSteps <- ""
positions.PotentialSteps <- regexpr("^E\\d+\\s\\(V\\)", chifile)
PotentialSteps <- paste("\\num{", strsplit(chifile[which(positions.PotentialSteps == 1)],
"\\s=\\s")[[1]][2], "}", sep = "")
if (length(which(positions.PotentialSteps == 1)) > 1) {
for (i in 2:length(which(positions.PotentialSteps == 1))) {
PotentialSteps <-
paste(PotentialSteps,
paste("\\num{", strsplit(chifile[which(positions.PotentialSteps == 1)],
"\\s=\\s")[[i]][2], "}", sep = ""), sep = ", ")
}
}
ff$PotentialSteps <- PotentialSteps
# Time steps
TimeSteps <- ""
positions.TimeSteps <- regexpr("^T\\d+\\s\\(s\\)", chifile)
TimeSteps <- paste("\\num{", strsplit(chifile[which(positions.TimeSteps == 1)],
"\\s=\\s")[[1]][2], "}", sep = "")
if (length(which(positions.TimeSteps == 1)) > 1) {
for (i in 2:length(which(positions.TimeSteps == 1))) {
TimeSteps <-
paste(TimeSteps,
paste("\\num{", strsplit(chifile[which(positions.TimeSteps == 1)],
"\\s=\\s")[[i]][2], "}", sep = ""), sep = ", ")
}
}
ff$TimeSteps <- TimeSteps
# Cycles
position.Cycles <- regexpr("^Cycle", chifile)
Cycles <- as.numeric(strsplit(chifile[which(position.Cycles == 1)], "\\s=\\s")[[1]][2])
ff$Cycles <- Cycles
# Sample interval (s)
position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(s\\)", chifile)
SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
ff$SampleInterval <- SampleInterval
# Quiet Time (s)
position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\s=\\s")[[1]][2])
ff$QuietTime <- QuietTime
# Sensitivity (A/V)
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)
}

@ -1,85 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
##################################################
################### ocp2df #######################
##################################################
ocp2df <- function(datafilename) {
## Description:
## Reads time vs potential data (from CHI 760 potentiostat)
## and returns a dataframe with the data and
## the data attributes (experimental conditions).
## Usage:
## ocp2df(datafilename)
## Arguments:
## datafilename: text string with full path to experimental file
## 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")
chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile)
#
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen),
# followed by one digit before the decimal, a decimal point,
# and an arbitrary number of digits after the decimal point,
# immediately followed by either the letter 'e' or a comma.
# Note that backslashes are escaped due to the way R handles strings.
#
numrow.idx <- regexpr(rgxp.number, chifile)
# Save the match length attribute to another variable,
numrow.len <- attr(numrow.idx, "match.length")
# then scrap the attribute of the original variable.
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start indices of data ranges
starts <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1) + 1
# End indices, except for the last
ends <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1)
# Fix the last index of end indices
ends <- c(ends, length(numrow.idx))
#
ff <- data.frame(NULL)
for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff,
data.frame(stringsAsFactors = FALSE,
sampleid, matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T)))
close(zz)
}
names(ff) <- c("sampleid", "time", "potential")
#
### Collect attributes of this experiment
# RunTime (sec)
position.RunTime <- regexpr("^Run\\sTime\\s\\(sec\\)", chifile)
RunTime <- as.numeric(strsplit(chifile[which(position.RunTime == 1)], "\\s=\\s")[[1]][2])
ff$RunTime <- RunTime
#
return(ff)
}

@ -1,8 +0,0 @@
# INCA.R
# Functions to read and manipulate data from the Oxford INCA EDS
# Taha Ahmed, April 2011
# CONTENTS
source("/home/taha/chepec/chetex/common/R/common.R")
# >>>> eds2df
# >>>> edspk

@ -1,88 +0,0 @@
## This function should be renamed to EMSA2df(). The file it reads is a so-called EMSA spectral data file.
# /TA, 111110
eds2df <- function(edstxtfile) {
## Description:
## Reads EDS textfile from INCA EDS.
## Stores data in data frame.
## Usage:
## eds2df(edstxtfile)
## Arguments:
## edstxtfile: character string, the full filename
## (with path) to one txt file.
## Value:
## A dataframe
#
incatxt <- file(edstxtfile, "r")
edsfile <- readLines(incatxt, n = -1) #read all lines of input file
close(incatxt)
#
sampleid <- ProvideSampleId(edstxtfile)
#
rgxp.comment <- "^\\#"
#
numrow.idx <- regexpr(rgxp.comment, edsfile)
# scrap the match.length attribute
attr(numrow.idx, "match.length") <- NULL
#
i <- seq(1, length(numrow.idx) - 1, 1)
j <- seq(2, length(numrow.idx), 1)
# Start index of data range
start.idx <- which(numrow.idx[i] == 1 & numrow.idx[j] != 1) + 1
# End index of the data range
end.idx <- which(numrow.idx[i] != 1 & numrow.idx[j] == 1)
#
zz <- textConnection(edsfile[start.idx:end.idx], "r")
#
ff <- data.frame()
ff <- data.frame(stringsAsFactors = FALSE,
sampleid = sampleid,
matrix(scan(zz, what = numeric(), sep = ","), ncol = 2, byrow = T))
close(zz)
names(ff) <- c("sampleid", "energy", "counts")
#
### Collect attributes of this experiment
# XUnit
position.XUnit <- regexpr("^\\#XUNITS", edsfile)
XUnit <- as.character(strsplit(edsfile[which(position.XUnit == 1)], ":\\s")[[1]][2])
ff$XUnit <- XUnit
# YUnit
position.YUnit <- regexpr("^\\#YUNITS", edsfile)
YUnit <- as.character(strsplit(edsfile[which(position.YUnit == 1)], ":\\s")[[1]][2])
ff$YUnit <- YUnit
# Date
position.Date <- regexpr("^\\#DATE", edsfile)
Date <- strsplit(edsfile[which(position.Date == 1)], ":\\s")[[1]][2]
ff$Date <- Date
# Time
position.Time <- regexpr("^\\#TIME", edsfile)
Time <- strsplit(edsfile[which(position.Time == 1)], ":\\s")[[1]][2]
ff$Time <- Time
# XPerChannel
position.XPerChannel <- regexpr("^\\#XPERCHAN", edsfile)
XPerChannel <- as.numeric(strsplit(edsfile[which(position.XPerChannel == 1)], ":\\s")[[1]][2])
ff$XPerChannel <- XPerChannel
# Offset
position.Offset <- regexpr("^\\#OFFSET", edsfile)
Offset <- as.numeric(strsplit(edsfile[which(position.Offset == 1)], ":\\s")[[1]][2])
ff$Offset <- Offset
# ChOffset
position.ChOffset <- regexpr("^\\#CHOFFSET", edsfile)
ChOffset <- as.numeric(strsplit(edsfile[which(position.ChOffset == 1)], ":\\s")[[1]][2])
ff$ChOffset <- ChOffset
# LiveTime
position.LiveTime <- regexpr("^\\#LIVETIME", edsfile)
LiveTime <- as.numeric(strsplit(edsfile[which(position.LiveTime == 1)], ":\\s")[[1]][2])
ff$LiveTime <- LiveTime
# DeadTime is calculated from: REALTIME - LIVETIME
position.RealTime <- regexpr("^\\#REALTIME", edsfile)
RealTime <- as.numeric(strsplit(edsfile[which(position.RealTime == 1)], ":\\s")[[1]][2])
DeadTime <- RealTime - LiveTime
ff$DeadTime <- DeadTime
# BeamEnergy
position.BeamEnergy <- regexpr("^\\#BEAMKV", edsfile)
BeamEnergy <- as.numeric(strsplit(edsfile[which(position.BeamEnergy == 1)], ":\\s")[[1]][2])
ff$BeamEnergy <- BeamEnergy
#
return(ff)
}

@ -1,63 +0,0 @@
edsWrapper <-
function(data.exp, run, override = FALSE,
kerpk = 1, fitmaxiter = 50, gam = 0.6, scl.factor = 0.1, maxwdth=0.20) {
print("... Started edsWrapper")
# check if edspk has already completed successfully for the current job
current.dirname <- getwd()
print(current.dirname)
current.filename <- "eds-peak-data.rda"
edsdatafile <- paste(current.dirname, current.filename, sep = "/")
if (file.exists(edsdatafile) && !override) {
print("... Started if-clause 1")
# File already exists
# return the data using load() or data()
load(file = edsdatafile)
if (run > length(edsres)) {
print("... Started if-clause 1:1")
# then it does not really exist
edsres[[run]] <- edspk(data.exp,
kerpk = kerpk,
fitmaxiter = fitmaxiter,
gam = gam,
scl.factor = scl.factor,
maxwdth = maxwdth)
save(edsres, file = edsdatafile)
print("... Ended if-clause 1:1")
}
print("... Ended if-clause 1")
return(edsres)
} else {
print("... Started else-clause 1")
if (!exists("edsres")) {
edsres <- list()
print("... edsres list created")
}
# Need to call edspk() and save its results to file as above
edsres[[run]] <- edspk(data.exp,
kerpk = kerpk,
fitmaxiter = fitmaxiter,
gam = gam,
scl.factor = scl.factor,
maxwdth = maxwdth)
save(edsres, file = edsdatafile)
print("... Ended else-clause 1")
return(edsres)
}
}

@ -1,92 +0,0 @@
edspk <-
function(eds.exp, kerpk = 1, fitmaxiter = 50, gam = 1.0, scl.factor = 0.1, maxwdth=0.20) {
eds.base <- baselinefit(eds.exp, tau=2.0, gam=gam, scl.factor=scl.factor, maxwdth=maxwdth)
# This loop deals with the output from baselinefit()
# It makes a "melted" dataframe in long form for each
# separated peak for some baseline parameters
eds.pks <- data.frame()
eds.pks.basl <- data.frame()
eds.pks.pmg <- data.frame()
eds.pks.spl <- data.frame()
peaks <- 1:length(eds.base$npks)
for (s in peaks) {
# recorded data in long form by separated peak
eds.pks <- rbind(eds.pks, # column names assigned after loop
data.frame(peak = factor(peaks[s]),
kernel = NA,
eds.exp[eds.base$indlsep[s]:eds.base$indrsep[s], ]))
# the calculated baseline in long form by separated peak
eds.pks.basl <- rbind(eds.pks.basl,
data.frame(peak = factor(peaks[s]),
kernel = NA,
x = eds.exp[eds.base$indlsep[s]:eds.base$indrsep[s]],
y = eds.base$baseline$basisl[eds.base$indlsep[s]:eds.base$indrsep[s]]))
# the taut string estimation in long form by separated peak
eds.pks.pmg <- rbind(eds.pks.pmg,
data.frame(peak = factor(peaks[s]),
kernel = NA,
x = eds.exp[eds.base$indlsep[s]:eds.base$indrsep[s]],
y = eds.base$pmg$fn[eds.base$indlsep[s]:eds.base$indrsep[s]]))
# the weighted smoothed spline in long form by separated peak
eds.pks.spl <- rbind(eds.pks.spl,
data.frame(peak = factor(peaks[s]),
kernel = NA,
x = eds.exp[eds.base$indlsep[s]:eds.base$indrsep[s]],
y = eds.base$spl$reg[eds.base$indlsep[s]:eds.base$indrsep[s]]))
}
# Column names assigned to d.pks
names(eds.pks) <- c("peak", "kernel", "x", "y")
# This loop calls pkdecompint() on each peak separately
# It makes a "melted" dataframe in long form for:
eds.fit <- list() # holds pkdecompint output
eds.fit.fitpk <- data.frame() # contains fitting curves
eds.fit.parpk <- data.frame() # physical parameters by peak and kernel
eds.nobasl <- data.frame() # data with baseline removed
peaks <- 1:length(eds.base$npks)
for (s in peaks) {
######## PKDECOMPINT ########
if (length(kerpk) > 1) {
# set number of kernels per peak manually
eds.fit[[s]] <- pkdecompint(eds.base, intnum = s,
k = kerpk[s], maxiter = fitmaxiter)
} else {
# use number of kernels determined by baselinefit()
eds.fit[[s]] <- pkdecompint(eds.base, intnum = s,
k = eds.base$npks[s], maxiter = fitmaxiter)
}
# Setup the dataframe that makes up the peak table
for (kernel in 1:eds.fit[[s]]$num.ker) {
eds.fit.parpk <- rbind(eds.fit.parpk,
data.frame(peak = factor(eds.fit[[s]]$intnr),
kernel = factor(kernel),
x = eds.fit[[s]]$parpks[kernel, "loc"],
height = eds.fit[[s]]$parpks[kernel, "height"],
area = eds.fit[[s]]$parpks[kernel, "intens"],
fwhm = eds.fit[[s]]$parpks[kernel, "FWHM"],
m = eds.fit[[s]]$parpks[kernel, "m"],
accept = eds.fit[[s]]$accept))
eds.fit.fitpk <- rbind(eds.fit.fitpk,
data.frame(peak = factor(peaks[s]),
kernel = factor(kernel),
x = eds.fit[[s]]$x,
y = eds.fit[[s]]$fitpk[kernel, ]))
}
eds.nobasl <- rbind(eds.nobasl,
data.frame(peak = factor(peaks[s]),
x = eds.fit[[s]]$x,
y = eds.fit[[s]]$y))
}
return(list(eds.base = eds.base,
eds.peaks = eds.pks,
eds.fit.parpk = eds.fit.parpk,
eds.fit.fitpk = eds.fit.fitpk,
eds.nobasl = eds.nobasl))
}

@ -1,6 +0,0 @@
# LEO1550.R
# Functions to read and manipulate data from the LEO1550 scanning electron microscope
# Taha Ahmed, March 2011
# CONTENTS
# >>>> tifftags2df

@ -1,121 +0,0 @@
##################################################
################ tifftags2df #####################
##################################################
tifftags2df <- function(tiffimage) {
## Description:
## Extracts all tags from a TIFF image file
## using the tiffinfo tool and stores
## a selection of the tags in a dataframe.
## Usage:
## tifftags2df(fulltiffpath)
## Arguments:
## tiffimage: character string, the full filename
## (with path) to one TIFF file.
## Value:
## A dataframe with three columns:
## sampleid, parameter, value.
#
substrate.id <- strsplit(basename(tiffimage), "\\.")[[1]][1]
tifftags <- system(paste("tiffinfo", tiffimage, sep = " "),
intern = TRUE, ignore.stderr = TRUE)
# Clean certain special characters from the tiff tags strings
# If these strings are left untreated, they cause all sorts of weird errors later
tifftags.clean <-
sub("[ ]+$", "", #trim trailing spaces, if any
sub("\\r$", "", #remove trailing \r
gsub("\\xb9", "", #remove special character
gsub("\\xb8", "", #remove special character
gsub("\\xb7", "", #remove special character
gsub("\\xb6", "", #remove special character
gsub("\\xb5", "", #remove special character
gsub("\\xb4", "", #remove special character
gsub("\\xb3", "", #remove special character
gsub("\\xb2", "", #remove special character
gsub("\\xb1", "", #remove special character
gsub("\\xb0", "", tifftags,
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T),
useBytes=T)))
# These are the tags we are looking for
tags.df <- data.frame(stringsAsFactors = FALSE, substrate.id, matrix(#
# Name # REGEXP # splitchar # unit
c("EHT", "AP\\_ACTUALKV", " = ", "\\kilo\\volt",
"High current", "DP\\_HIGH\\_CURRENT", " = ", "",
"WD", "AP\\_WD", " = ", "\\milli\\metre",
"Magnification", "AP\\_MAG", " = ", "", # could warrant special treatment
"Brightness", "AP\\_BRIGHTNESS", " = ", "\\percent",
"Contrast", "AP\\_CONTRAST", " = ", "\\percent",
"Signal A", "DP\\_DETECTOR\\_CHANNEL", " = ", "",
"SCM status", "DP\\_SCM\\_STATUS", " = ", "",
"Specimen current", "AP\\_SCM", " = ", "\\femto\\ampere",
# Beam
#"Filament curent", "AP\\_ACTUALCURRENT", " = ", "\\ampere",
"Stigmation Y", "AP\\_STIG\\_Y", " = ", "\\percent",
"Stigmation X", "AP\\_STIG\\_X", " = ", "\\percent",
"Aperture align Y", "AP\\_APERTURE\\_ALIGN\\_Y", " = ", "\\percent",
"Aperture align X", "AP\\_APERTURE\\_ALIGN\\_X", " = ", "\\percent",
"Aperture size", "AP\\_APERTURESIZE", " = ", "\\micro\\metre",
"Beam shift Y", "AP\\_BEAMSHIFT\\_Y", " = ", "\\percent",
"Beam shift X", "AP\\_BEAMSHIFT\\_X", " = ", "\\percent",
#"Beam offset Y", "AP\\_BEAM\\_OFFSET\\_Y", " = ", "\\nano\\metre",
#"Beam offset X", "AP\\_BEAM\\_OFFSET\\_X", " = ", "\\nano\\metre",
# Stage
"Track Z", "DP\\_TRACK\\_Z", " = ", "",
"Stage at Z", "AP\\_STAGE\\_AT\\_Z", " = ", "\\milli\\metre",
"Stage at Y", "AP\\_STAGE\\_AT\\_Y", " = ", "\\milli\\metre",
"Stage at X", "AP\\_STAGE\\_AT\\_X", " = ", "\\milli\\metre",
# Tilt
"Stage tilted?", "DP\\_STAGE\\_TILTED", " = ", "",
"Tilt angle", "AP\\_TILT\\_ANGLE", " = ", "",
"Tilt axis", "AP\\_TILT\\_AXIS", " = ", "",
# Image
"Scan speed", "DP\\_SCANRATE", " = ", "",
"Cycle time", "AP\\_FRAME\\_TIME", " = ", "\\second",
"Freeze on", "DP\\_FREEZE\\_ON", " = ", "",
"Dwell time", "DP\\_DWELL\\_TIME", " = ", "\\nano\\second",
"Noise reduction", "DP\\_NOISE\\_REDUCTION", " = ", "",
"Frames to integrate", "AP\\_FRAME\\_INT\\_COUNT", " = ", "",
"Frames to average", "AP\\_FRAME\\_AVERAGE\\_COUNT", " = ", "",
#"Profile width", "AP\\_PROFILE\\_W", " = ", "\\micro\\metre",
"Pixel size", "AP\\_PIXEL\\_SIZE", " = ", "\\nano\\metre",
# System
"Gun vacuum", "AP\\_COLUMN\\_VAC", " = ", "\\milli\\bar",
"System vacuum", "AP\\_SYSTEM\\_VAC", " = ", "\\milli\\bar",
"Filament age", "AP\\_FILAMENT\\_AGE", " = ", "\\hour",
# Misc
"Photo no.", "AP\\_PHOTO\\_NUMBER", " = ", "",
"Date", "AP\\_DATE", " :", "",
"Time", "AP\\_TIME", " :", ""),
ncol = 4, byrow = T))
names(tags.df) <- c("sampleid", "name", "regexp", "splitchar", "unit")
for (i in 1:dim(tags.df)[1]) {
current.tag <- which(regexpr(tags.df$regexp[i], tifftags.clean) == 1) + 1
value.tmp <- strsplit(tifftags.clean[current.tag], split = tags.df$splitchar[i])[[1]][2]
# Remove leading spaces from tags.df$value
tags.df$value[i] <- sub("^\\s+", "", value.tmp, useBytes = T)
if (tags.df$unit[i] != "") {
tags.df$value[i] <- paste("\\SI{", strsplit(tags.df$value[i], split = " ")[[1]][1], "}{", tags.df$unit[i], "}", sep = "")
}
}
tags <- data.frame(stringsAsFactors = FALSE,
sampleid = tags.df$sampleid,
parameter = tags.df$name,
value = tags.df$value)
return(tags)
}

@ -1,157 +0,0 @@
source("/home/taha/chepec/chetex/common/R/common/ProvideSampleId.R")
source("/home/taha/chepec/chetex/common/R/common/int2padstr.R")
##################################################
#################### OO2df #######################
##################################################
OO2df <- function(datafile) {
## Description:
##
##
##
##
## Usage:
## OO2df(datafile)
## Arguments:
## datafile: text string with full path to TXT file
## containing single or multiple data ranges
## Value:
## Dataframe with the following columns:
## $ sampleid : chr
## $ wavelength : num
## $ counts : num
## $
## $
## $
## $
## $
## $
## $
## $
## $
## $
## $ cps ?
#
# range.header.start.rexp <- "^; \\(Data for Range" #regexp
# range.header.end.rexp <- "^_2THETA[^=]" #regexp
range.data.start.rexp <- ">+Begin[\\s\\w]*<+"
range.data.end.rexp <- ">+End[\\s\\w]*<+"
# Read the input file
dfile <- file(datafile, "r")
# Note that readLines apparently completely skips empty lines.
# That causes line numbers to not match between source and f vector.
f <- readLines(dfile, n=-1) # read _all_ lines from data file
close(dfile)
# Fetch a sampleid for the current job
sampleid <- ProvideSampleId(datafile)
# # Look for header start rows
# range.header.start.rows <- which(regexpr(range.header.start.rexp, f) == 1)
# # Look for header end rows
# range.header.end.rows <- which(regexpr(range.header.end.rexp, f) == 1)
# Look for data start marker line
range.data.start.rows <- which(regexpr(range.data.start.rexp, f, perl = TRUE) == 1) + 1
# Look for data end marker line
range.data.end.rows <- which(regexpr(range.data.end.rexp, f, perl = TRUE) == 1) - 1
# Calculate number of ranges
ranges.total <-
ifelse(length(range.data.start.rows) == length(range.data.end.rows),
length(range.data.start.rows),
NA) #why would they not be equal?
if (is.na(ranges.total)) {
# Obviously something bad happened.
# Do something about it. echo an error message perhaps.
# But why would they not be equal?
}
# Header always precedes start of data
range.header.end.rows <- range.data.start.rows - 2
if (ranges.total > 1) {
range.header.start.rows <- c(