2
2
# '
3
3
# ' Use this function to import a series of associated ESR spectra into R.
4
4
# '
5
- # ' This is a wrapper function for \code{read.table}. The function should be
5
+ # ' This is a wrapper function for \code{read.table} and \code{readBin}.
6
+ # ' The function should be
6
7
# ' used to read in a series of associated ESR spectrum files. A list with all
7
8
# ' spectrum data is returned, which can be passed to \code{plot_Spectrum}
8
9
# ' for plotting the spectra.
9
10
# '
10
- # ' @param file \code{\link{character}} (required): file path or directory where
11
- # ' the spectra files are stored.
12
- # ' @param ... further arguments (e.g., \code{n} to specify the number of datapoints).
11
+ # ' **Binary formats**
12
+ # '
13
+ # ' This function is able to read in binary spectrum files produced by Bruker
14
+ # ' ESR devices. By default (\code{device = 'auto'}), the function assumes the
15
+ # ' proper mode of the vector (integer or numeric), endianness and number of
16
+ # ' bytes per element based on the file extension. Currently, the following
17
+ # ' devices are supported:
18
+ # '
19
+ # ' - **Bruker ESP300-E** (*.SPC* files): `what = 'int', endian = 'big', size = 4`
20
+ # ' - **Bruker ELEXSYS500** (*.DTA* files): `what = 'numeric', endian = 'big', size = 8`
21
+ # ' - **Bruker EMXplus** (*.SPC* files): `what = 'numeric', endian = 'little', size = 4`
22
+ # '
23
+ # ' Note that the Bruker ESP300-E and EMXplus devices share a common file
24
+ # ' extension (.SPC) and that `device = 'auto'` (the default) will always
25
+ # ' assume that the SPC file is from a ESP300-E. If your SPC file is
26
+ # ' from a EMXplus, however, you should manually specify this using
27
+ # ' `device = 'EMXplus'`.
28
+ # '
29
+ # '
30
+ # ' @param file \code{\link{character}} (**required**):
31
+ # ' file path or directory where the spectra files are stored.
32
+ # '
33
+ # ' @param device [character] (*with default*):
34
+ # ' Manually specify the device the spectrum files were produced by.
35
+ # ' By default, the proper binary format is deduced from the file
36
+ # ' extension, which may however fail in case of ambiguous file
37
+ # ' endings. See details.
38
+ # '
39
+ # ' Allowed options are:
40
+ # ' - `"auto"` (the default)
41
+ # ' - `"ESP300-E"` (.SPC)
42
+ # ' - `"ELEXSYS500"` (.DTA)
43
+ # ' - `"EMXplus"` (.SPC)
44
+ # '
45
+ # ' @param ... further arguments
46
+ # ' (e.g., \code{n} to specify the number of datapoints; \code{sw} to specify
47
+ # ' the sweep width).
48
+ # '
13
49
# ' @return Returns a terminal output. In addition an
14
50
# ' \code{\link{R6Class}} object is returned. \cr
51
+ # '
15
52
# ' @export
53
+ # '
16
54
# ' @author Christoph Burow, University of Cologne (Germany)
55
+ # '
17
56
# ' @seealso \code{\link{read.table}}, \code{\link{readBin}}, \code{\link{read.csv}}
57
+ # '
18
58
# ' @references In progress
59
+ # '
19
60
# ' @examples
20
61
# '
21
62
# ' # Import ASCII text file
38
79
# ' file5 <- system.file("extdata", "quartz.DTA", package = "ESR")
39
80
# ' spec5 <- read_Spectrum(file5)
40
81
# '
82
+ # ' # Import Bruker EMXplus raw binary spectrum
83
+ # ' file6 <- system.file("extdata", "DL_alanine.spc", package = "ESR")
84
+ # ' spec6 <- read_Spectrum(file6, device = "EMXplus")
85
+ # '
41
86
# ' # Import all example data sets at once by providing only the directory
42
87
# ' dir <- system.file("extdata", package = "ESR")
43
88
# ' specs <- read_Spectrum(dir)
44
89
# '
90
+ # ' @md
45
91
# ' @export read_Spectrum
46
- read_Spectrum <- function (file , ... ) {
92
+ read_Spectrum <- function (file , device = " auto" , ... ) {
93
+
94
+ # # SUPPORTED DEVICES ----
95
+ devices <- list (bruker = c(" auto" , " ESP300-E" , " ELEXSYS500" , " EMXplus" ))
96
+ if (! is.null(device ))
97
+ if (! device %in% unlist(devices ))
98
+ stop(" Unknown device. Only the following are supported: " , paste(unlist(devices ), collapse = " , " ), call. = FALSE )
47
99
48
100
# # ADDITIONAL ARGS ----
49
101
extraArgs <- list (... )
@@ -66,7 +118,7 @@ read_Spectrum <- function(file, ...) {
66
118
if (is.na(val )) {
67
119
file_list <- list.files(f , paste0(valid_ext , collapse = " |" ), ignore.case = TRUE )
68
120
if (length(file_list ) == 0 ) {
69
- stop(paste(" sInvalid file extension:" , ext ), call. = FALSE )
121
+ stop(paste(" Invalid file extension:" , ext ), call. = FALSE )
70
122
}
71
123
ext <- list (file_list )
72
124
}
@@ -128,7 +180,12 @@ read_Spectrum <- function(file, ...) {
128
180
# # SPC
129
181
# # -----------------------------------------------
130
182
if (type == " spc" ) {
131
- df <- as.data.table(readBin(f , " int" , n = file.info(f )$ size , endian = " big" , size = 4 ))
183
+
184
+ if (device == " auto" || device == " ESP300-E" )
185
+ df <- as.data.table(readBin(f , " int" , n = file.info(f )$ size , endian = " big" , size = 4 ))
186
+ else if (device == " EMXplus" )
187
+ df <- as.data.table(readBin(f , " numeric" , n = file.info(f )$ size , endian = " little" , size = 4 ))
188
+
132
189
133
190
par <- tryCatch(
134
191
read.table(gsub(" .spc" , " .par" , f , ignore.case = TRUE ), stringsAsFactors = FALSE ),
0 commit comments