8
8
setMethod(
9
9
f = " read" ,
10
10
signature = signature(file = " character" ),
11
- definition = function (file , extensions = c(" cnf" , " tka" ), ... ) {
11
+ definition = function (file , extensions = c(" cnf" , " tka" , " spe " ), ... ) {
12
12
# Validation
13
13
extensions <- match.arg(extensions , several.ok = TRUE )
14
14
extensions <- c(extensions , toupper(extensions ))
@@ -28,7 +28,8 @@ setMethod(
28
28
switch (
29
29
extension ,
30
30
cnf = readCanberraCNF(file = x , ... ),
31
- tka = readCanberraTKA(file = x , ... )
31
+ tka = readCanberraTKA(file = x , ... ),
32
+ spe = readKromekSPE(file = x , ... )
32
33
)
33
34
}, ... )
34
35
@@ -132,3 +133,81 @@ readCanberraTKA <- function(file, ...) {
132
133
real_time = real_time
133
134
)
134
135
}
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