-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_oceanoptics_converted.R
173 lines (137 loc) · 5.29 KB
/
parse_oceanoptics_converted.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#' Parse OceanInsight converted file
#'
#' Parse OceanInsight (formerly OceanOptics) converted file.
#' <https://www.oceaninsight.com/>
#'
#' @inheritParams lr_parse_generic
#'
#' @inherit lr_parse_generic return details
#'
#' @examples
#' res_jaz <- lr_parse_jaz(system.file("testdata", "jazspec.jaz",
#' package = "lightr"))
#' head(res_jaz$data)
#' res_jaz$metadata
#'
#' res_jazirrad <- lr_parse_jazirrad(system.file("testdata", "irrad.JazIrrad",
#' package = "lightr"))
#' head(res_jazirrad$data)
#' res_jazirrad$metadata
#'
#' res_usb4000 <- lr_parse_jaz(system.file("testdata", "OOusb4000.txt",
#' package = "lightr"))
#' head(res_usb4000$data)
#' res_usb4000$metadata
#'
#' res_transmission <- lr_parse_jaz(
#' system.file("testdata", "FMNH6834.00000001.Master.Transmission",
#' package = "lightr")
#' )
#' head(res_transmission$data)
#' res_transmission$metadata
#'
#' @export
#'
lr_parse_jaz <- function(filename) {
# METADATA
content <- readLines(filename, skipNul = TRUE)
# Convert to ASCII
content <- vapply(content, iconv, to = "ASCII", sub = "", character(1))
# Can be:
# - Spectrometer
# - Spectrometers
# - Spectrometer Serial Number
# - Espectr?metros
specID <- grep("^(Spectrometers?( Serial Number)?|Espectr.metros): [[:graph:]]+$",
content,
value = TRUE)
specID <- gsub("^(Spectrometers?( Serial Number)?|Espectr.metros): ", "", specID)
author <- grep("^(User|Usuario): [[:print:]]*$", content, value = TRUE)
author <- gsub("^(User|Usuario): ", "", author)
savetime <- grep("^(Date|Fecha): .*", content, value = TRUE)
savetime <- gsub("^(Date|Fecha): ", "", savetime)
oo_savetime_regex <- "^(\\w{3} \\w{3} \\d{2} \\d{2}:\\d{2}:\\d{2}) (\\w+ )?(\\d{4})$"
tz <- ""
if (grepl(oo_savetime_regex, savetime)) {
tz <- trimws(gsub(oo_savetime_regex, "\\2", savetime))
savetime <- gsub(oo_savetime_regex, "\\1 \\3", savetime)
}
if (tz == "") {
tz <- "UTC"
}
# OceanOptics files use locale-dependent date formats but it looks like they
# are always using English for this, even when the locale is not set to
# English
orig_locale <- Sys.getlocale("LC_TIME")
on.exit(Sys.setlocale("LC_TIME", orig_locale))
Sys.setlocale("LC_TIME", "C")
savetime <- as.POSIXct(
savetime,
tz = tz,
tryFormats = c("%c", "%m-%d-%Y, %H:%M:%S")
)
savetime <- format(savetime, tz = "UTC")
specmodel <- NA_character_
# For those, be careful, the line ends with '(specID)' so no $
int <- grep("^(Integration Time|Tiempo de integraci.n) (.+): [[:digit:]]+", content, value = TRUE)
inttime <- gsub("^(Integration Time|Tiempo de integraci.n) \\(.+\\): ([[:digit:]]+).*", "\\2", int)
inttime_unit <- gsub("^(Integration Time|Tiempo de integraci.n) \\((.+)\\):.*", "\\2", int)
if (identical(unname(inttime_unit), "usec")) {
inttime <- as.numeric(inttime) / 1000
}
average <- grep("^(Spectra Averaged|Promedio de Espectros Hechos un): [[:digit:]]+", content, value = TRUE)
average <- gsub("^(Spectra Averaged|Promedio de Espectros Hechos un): ([[:digit:]]+).*", "\\2", average)
boxcar <- grep("^(Boxcar Smoothing|El Alisar Del Furg.n): [[:digit:]]+", content, value = TRUE)
boxcar <- gsub("^(Boxcar Smoothing|El Alisar Del Furg.n): ([[:digit:]]+).*", "\\2", boxcar)
dark_inttime <- white_inttime <- scope_inttime <- inttime
dark_average <- white_average <- scope_average <- average
dark_boxcar <- white_boxcar <- scope_boxcar <- boxcar
metadata <- c(author, savetime, specmodel, specID,
dark_inttime, white_inttime, scope_inttime,
dark_average, white_average, scope_average,
dark_boxcar, white_boxcar, scope_boxcar)
# SPECTRA
data_start <- grep(
"^(>>>>>Begin (Processed )?Spectral Data<<<<<|>>>>> Comienza Data<<<<< Espectral Procesado Del EL)$",
content
)
data_end <- grep(
"^(>>>>>End (Processed )?Spectral Data<<<<<|>>>>> Data<<<<< Espectral Procesado Extremo)$",
content
)
# Some files are missing the ending "tag". Let's then assume that data go to
# the end of file.
if (length(data_end) == 0) {
data_end <- length(content)
}
# Some files have an extra header for the data, some don't...
# If they do, it looks like this header will always start with W
has_header <- startsWith(content[data_start + 1], "W")
data <- content[seq(ifelse(has_header, data_start + 2, data_start + 1),
data_end - 1)]
# Depending on the user locale, some files might use ',' as a decimal sep
data <- gsub(",", ".", data, fixed = TRUE)
data <- do.call(rbind, strsplit(data, "\t", fixed = TRUE))
if (has_header) {
colnames(data) <- strsplit(content[data_start + 1], "\t", fixed = TRUE)[[1]]
} else {
colnames(data) <- c("W", "P")
}
storage.mode(data) <- "numeric"
cornames <- c(wl = "W",
dark = "D",
white = "R",
scope = "S",
processed = "P")
data_final <- setNames(
as.data.frame(matrix(NA_real_, nrow = nrow(data), ncol = 5)),
names(cornames)
)
data_final[match(colnames(data), cornames)] <- data
return(list("data" = data_final, "metadata" = unname(metadata)))
}
#' @rdname lr_parse_jaz
#'
#' @export
#'
lr_parse_jazirrad <- lr_parse_jaz