meireles / spectrolab

Dealing with spectral data in R
16 stars 11 forks source link

'Comment' variable in metadata of .sed is not read by read_spectra() #20

Open ikotzur opened 1 year ago

ikotzur commented 1 year ago

Hi, thanks for this handy package. .sed files sometimes have a 'Comment' line inserted in the start of the metadata section at top of file, if a comment is added during spectrum collection.

I tried adding to the psr_meta_tags list in a version of read_spectra() with "Comment:". This works if the .sed being read has comments but it fails for files that don't have comments, with this:

Error in split(y, s)[[pick]] : subscript out of bounds

traceback() 16: is.factor(x) 15: sub(re, "", x, perl = TRUE) 14: mysub(paste0("^", whitespace, "+"), x) 13: is.factor(x) 12: sub(re, "", x, perl = TRUE) 11: mysub(paste0(whitespace, "+$"), mysub(paste0("^", whitespace, "+"), x)) 10: trimws(split(y, s)[[pick]]) 9: FUN(X[[i]], ...) 8: lapply(meta_tags, function(x) { y = f_lines[grep(paste0("^", x), f_lines)] if (length(y) == 0) { return(NULL) } y = strsplit(gsub(x, "", y), sep_char)[[1]] s = sort(rep(c(1, 2), length.out = length(y))) if (length(s) == 1) { return(trimws(y)) } else { return(trimws(split(y, s)[[pick]])) } }) 7: FUN(X[[i]], ...) 6: lapply(X = X, FUN = FUN, ...) 5: sapply(file_paths, function(x) { f_lines = trimws(readLines(x, n = max_lines)) pick = ifelse(test = sample_type == "target", yes = 2, no = 1) meta_tags = setNames(meta_tags, meta_tags) data = lapply(meta_tags, function(x) { y = f_lines[grep(paste0("^", x), f_lines)] if (length(y) == 0) { return(NULL) } y = strsplit(gsub(x, "", y), sep_char)[[1]] s = sort(rep(c(1, 2), length.out = length(y))) if (length(s) == 1) { return(trimws(y)) } else { return(trimws(split(y, s)[[pick]])) } }) names(data) = gsub(tag_sep, "", names(data)) unlist(data) ... 4: spectrolab:::i_read_ascii_metadata(file_paths = names(x), sample_type = sample_type, sep_char = ",", max_lines = 40, meta_tags = psr_meta_tags, tag_sep = ":") at #90 3: FUN(X[[i]], ...) 2: lapply(result, function(x) { spectrolab:::i_read_ascii_metadata(file_paths = names(x), sample_type = sample_type, sep_char = ",", max_lines = 40, meta_tags = psr_meta_tags, tag_sep = ":") }) at #89 1: read_spectra2(path = dir_path1, type = "target_reflectance", format = "sed", extract_metadata = TRUE)