pierreroudier / opusreader

Reading OPUS binary files in R
GNU General Public License v3.0
4 stars 5 forks source link

Simplified matrix output fails for single channel spectra of the background #11

Closed philipp-baumann closed 3 years ago

philipp-baumann commented 3 years ago

https://github.com/pierreroudier/opusreader/blob/d894ceef533af3b7285b64cfe6db461a9f31b2e2/R/opus_read_file.R#L135

library("opusreader")

fp <- opus_file()
s <- opus_read(fp, extract = c("spc", "ScSm", "ScRf"))
head(s$wavenumbers)
#> [1] 7498.292 7496.860 7495.428 7493.996 7492.565 7491.133
head(s$wavenumbers_sc_sm)
#> [1] 7498.292 7496.860 7495.428 7493.996 7492.565 7491.133
head(s$wavenumbers_sc_rf)
#> [1] 7504.019 7502.587 7501.155 7499.723 7498.292 7496.860

s_simple <- opus_read(rep(fp, 5), extract = "ScRf", simplify = TRUE)
#> Error in xy.coords(x, y, setLab = FALSE): 'x' and 'y' lengths differ

Created on 2021-05-12 by the reprex package (v2.0.0)

My suggestion is to implement matching of spectrum types and corresponding wavenumbers (instead of fixed x$wavenumbers). The wavenumber range of the background measurement (single channel) and the sample spectrum are more narrow.

I have no idea about the technical reasons behind Bruker truncating the range of single channel spectra of the background (maybe something to check with the chief software/chemometrics engineer at Bruker if they allow us insight in their "company secrets" and GMP lock-in. Regardless, we should resample it with the correct wavenumbers stored internally.

Side note: There must be a secret hash in the header to make sure nobody can modify blocks later without getting noticed...Hypothetically, if one figures out the exact location of the hash of the entire OPUS binary together with the type of hash algorithm used, GMP will be replaced by "Open Manufacturing Practice". I hope for this time to come soon. Then, becomes clear that there is no sense anymore to hide all these details in the files without providing any full specification ;-).

I have some functions in simplerspec where I have prototyped some type matching logic we could slightly modify. But have to check and recall first.

pierreroudier commented 3 years ago

My suggestion is to implement matching of spectrum types and corresponding wavenumbers (instead of fixed x$wavenumbers). The wavenumber range of the background measurement (single channel) and the sample spectrum are more narrow.

Fixed in https://github.com/pierreroudier/opusreader/commit/9ed438ebb01c48cad8b4d3f2bd5a00b52f725045.

Not 100% on the wavenumbers that should be affected to interferograms, I have used:

 # Grab correct wavenumbers for interpolation
  wn <- switch(extract,
  spc = x$wavenumbers,
  spc_nocomp = x$wavenumbers,
  ScSm = x$wavenumbers_sc_sm,
  ScRf = x$wavenumbers_sc_rf,
  IgSm = x$wavenumbers_sc_sm,
  IgRf = x$wavenumbers_sc_rf
)

Please re-open if that's not correct.