Skip to content

Commit 33e0d9e

Browse files
authored
Merge pull request #28 from RLumSK/master
Add new support for Kromek SPE files
2 parents d7dabaf + e602482 commit 33e0d9e

File tree

10 files changed

+4353
-79
lines changed

10 files changed

+4353
-79
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Authors@R: c(
1313
comment = c(ORCID = "0000-0003-2364-5621")),
1414
person("Christelle", "Lahaye", , "[email protected]", role = "ctb",
1515
comment = c(ORCID = "0000-0003-2215-9015")),
16+
person("Sebastian", "Kreutzer", , "[email protected]", role = "aut",
17+
comment = c(ORCID = "0000-0002-0734-2199")),
1618
person("Archéosciences Bordeaux", , , "[email protected]", role = "cre"),
1719
person("Université Bordeaux Montaigne", role = c("cph", "fnd")),
1820
person("CNRS", role = "fnd"),
@@ -52,7 +54,7 @@ Config/testthat/edition: 3
5254
Encoding: UTF-8
5355
LazyData: false
5456
Roxygen: list(markdown = TRUE)
55-
RoxygenNote: 7.3.1
57+
RoxygenNote: 7.3.2
5658
Collate:
5759
'AllClasses.R'
5860
'AllGenerics.R'

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# gamma 1.0.5.9000
22

33
# gamma 1.0.5
4+
## Bugfixes & changes
5+
* Add support for Kromek SPE files to `read()`(#28 by @RLumSK)
6+
47
## Internals
58
* Fix unicode character in plot axis labels.
69

R/AllGenerics.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,10 @@ if (!isGeneric("plot")) {
604604
#' Reads a gamma ray spectrum file.
605605
#' @param file A [`character`] string giving the path of files to be imported.
606606
#' @param extensions A [`character`] vector specifying the possible
607-
#' file extensions. It must be one or more of "`cnf`", "`tka`".
607+
#' file extensions. It must be one or more of "`cnf`", "`tka`", "`spe`".
608608
#' @param ... Extra parameters to be passed to [rxylib::read_xyData()].
609609
#' @note
610-
#' Only supports Canberra CNF and TKA files.
610+
#' Supports Canberra CNF and TKA and Kromek SPE files.
611611
#' @return
612612
#' A [GammaSpectra-class] object if more than one spectrum are imported
613613
#' at once, else a [GammaSpectrum-class] object.

R/read.R

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ NULL
88
setMethod(
99
f = "read",
1010
signature = signature(file = "character"),
11-
definition = function(file, extensions = c("cnf", "tka"), ...) {
11+
definition = function(file, extensions = c("cnf", "tka", "spe"), ...) {
1212
# Validation
1313
extensions <- match.arg(extensions, several.ok = TRUE)
1414
extensions <- c(extensions, toupper(extensions))
@@ -28,7 +28,8 @@ setMethod(
2828
switch(
2929
extension,
3030
cnf = readCanberraCNF(file = x, ...),
31-
tka = readCanberraTKA(file = x, ...)
31+
tka = readCanberraTKA(file = x, ...),
32+
spe = readKromekSPE(file = x, ...)
3233
)
3334
}, ...)
3435

@@ -132,3 +133,81 @@ readCanberraTKA <- function(file, ...) {
132133
real_time = real_time
133134
)
134135
}
136+
137+
#' Read Kromek SPE file
138+
#'
139+
#' @param file A [`character`] string giving the path and file to be imported.
140+
#' @param ... currently not used
141+
#' @return
142+
#' An object of class [GammaSpectrum-class].
143+
#' @keywords internal
144+
#' @noRd
145+
readKromekSPE <- function(file, ...) {
146+
## import entire file
147+
tmp <- readLines(con = file)
148+
149+
## search for KROMEK_INFO
150+
if (length(grep(pattern = "$KROMEK_INFO", x = tmp, fixed = TRUE)) == 0)
151+
stop("Kromek SPE does not follow implemented definition!", call. = FALSE)
152+
153+
## get elements and their position
154+
el_id <- which(grepl(pattern = "\\$[a-zA-Z].+[^:]", x = tmp))
155+
156+
## now read all information into a list
157+
el_l <- lapply(seq_along(el_id), function(x) {
158+
## if limit is reached or the element length is 0 nothing was stored
159+
if (length(tmp) == x && (el_id[x] + 1) == (el_id[x + 1] - 1))
160+
return(NA)
161+
162+
tmp[(el_id[x] + 1):(min(length(tmp), el_id[x + 1] - 1, na.rm = TRUE))]
163+
164+
})
165+
166+
## assign element names but remove the dollar and the :
167+
names(el_l) <- gsub(pattern = "[$:]", replacement = "", x = tmp[el_id])
168+
169+
## extract data
170+
## $DATA
171+
m <- matrix(c(1:length(el_l[["DATA"]][-1]), as.numeric(el_l[["DATA"]][-1])), ncol = 2)
172+
colnames(m) <- c("CHN", "CNTS")
173+
el_l[["DATA"]] <- m
174+
175+
## $KROMEK_INFO
176+
l <- el_l[["KROMEK_INFO"]][seq(2, length(el_l[["KROMEK_INFO"]]), 2)]
177+
names(l) <- gsub(pattern = ":", replacement = "", x = el_l[["KROMEK_INFO"]][seq(1, length(el_l[["KROMEK_INFO"]]), 2)], fixed = TRUE)
178+
el_l[["KROMEK_INFO"]] <- as.list(l)
179+
180+
## get metadata (here we follow the template from before)
181+
date <- as.POSIXct(el_l[["DATE_MEA"]], format = c("%m/%d/%Y %H:%M:%S"))
182+
tmp_time <- as.numeric(strsplit(el_l[["MEAS_TIM"]], " ")[[1]])
183+
live_time <- tmp_time[1]
184+
real_time <- tmp_time[2]
185+
186+
## get data
187+
spc_data <- as.data.frame(el_l[["DATA"]])
188+
189+
## add a column to store the channel number
190+
spc_data[["channel"]] <- as.integer(seq_len(nrow(spc_data)))
191+
colnames(spc_data) <- c("energy", "count", "channel")
192+
193+
# Get instrument name (remove the last word)
194+
instrument_name <- paste("Kromek", el_l[["KROMEK_INFO"]][["DETECTOR_TYPE"]])
195+
196+
# Compute 32-bytes MD5 hash
197+
hash <- as.character(tools::md5sum(file))
198+
199+
.GammaSpectrum(
200+
hash = hash,
201+
name = tools::file_path_sans_ext(basename(file)),
202+
date = date,
203+
instrument = instrument_name,
204+
file_format = "SPE",
205+
channel = as.integer(spc_data$channel),
206+
energy = spc_data$energy,
207+
count = as.integer(spc_data$count),
208+
live_time = live_time,
209+
real_time = real_time
210+
)
211+
}
212+
213+

0 commit comments

Comments
 (0)