|
| 1 | +#' Parse Brucker OPUS binary file |
| 2 | +#' |
| 3 | +#' Parse Brucker OPUS binary file (.0, .1, .2, etc. file |
| 4 | +#' extensions). |
| 5 | +#' <https://www.bruker.com/en/products-and-solutions/infrared-and-raman/opus-spectroscopy-software.html> |
| 6 | +#' |
| 7 | +#' @inheritParams lr_parse_generic |
| 8 | +#' |
| 9 | +#' @inherit lr_parse_generic return |
| 10 | +#' |
| 11 | +#' @importFrom stats setNames |
| 12 | +#' |
| 13 | +#' @examples |
| 14 | +#' res_trm <- lr_parse_brucker_opus( |
| 15 | +#' system.file("testdata", "i1_sc.0", package = "lightr") |
| 16 | +#' ) |
| 17 | +#' head(res_trm$data) |
| 18 | +#' res_trm$metadata |
| 19 | +#' |
| 20 | +#' @export |
| 21 | +#' |
| 22 | +lr_parse_brucker_opus <- function(filename, ...) { |
| 23 | + f <- file(filename, "rb") |
| 24 | + on.exit(close(f)) |
| 25 | + |
| 26 | + # Header: 504 bytes |
| 27 | + magic_number <- readBin(f, "raw", n = 4) |
| 28 | + |
| 29 | + skip <- readBin(f, "raw", n = 20) |
| 30 | + |
| 31 | + # At this stage, we have 480 bytes left in the header. |
| 32 | + # Each block desc is 12 bytes long. |
| 33 | + # So we have at most 40 blocks. |
| 34 | + block_index <- as.data.frame(matrix(nrow = 40, ncol = 5)) |
| 35 | + colnames(block_index) <- c( |
| 36 | + "data_type", |
| 37 | + "channel_type", |
| 38 | + "text_type", |
| 39 | + "block_size", |
| 40 | + "offset" |
| 41 | + ) |
| 42 | + for (i in seq_len(nrow(block_index))) { |
| 43 | + block_index$data_type[i] <- readBin(f, "integer", 1, 1, signed = FALSE) |
| 44 | + block_index$channel_type[i] <- readBin(f, "integer", 1, 1, signed = FALSE) |
| 45 | + block_index$text_type[i] <- readBin(f, "integer", 1, 1, signed = FALSE) |
| 46 | + skip <- readBin(f, "raw", n = 1) |
| 47 | + block_index$block_size[i] <- readBin(f, "integer", 1, 4, endian = "little") |
| 48 | + block_index$offset[i] <- readBin(f, "integer", 1, 4, endian = "little") |
| 49 | + } |
| 50 | + |
| 51 | + # Ref = data_type 11 |
| 52 | + |
| 53 | + # Data = data_type 7 |
| 54 | + |
| 55 | + return(list(data = data, metadata = metadata)) |
| 56 | +} |
0 commit comments