Skip to content

Commit 39064c0

Browse files
authored
Merge pull request #7 from SWFSC/extract-uctd
Added Valeport UCTD processing
2 parents 202bd69 + 3eb09f1 commit 39064c0

File tree

3 files changed

+94
-42
lines changed

3 files changed

+94
-42
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ License: GPL-2
1212
Encoding: UTF-8
1313
LazyData: true
1414
Roxygen: list(markdown = TRUE)
15-
RoxygenNote: 7.3.1
15+
RoxygenNote: 7.3.2
1616
Suggests:
1717
testthat,
1818
knitr,

R/ctd.R

+90-40
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,46 @@ extract_ctd_header <- function(header.filename, type) {
1010
# Read header text
1111
header.txt <- readLines(header.filename)
1212

13-
# Extract cast date as dttm
14-
cast.date <- lubridate::dmy_hms(
15-
stringr::str_extract(
13+
# If Oceansciences UCTD
14+
if(stringr::str_detect(header.filename, ".asc")) {
15+
# Extract cast date as dttm
16+
cast.date <- lubridate::dmy_hms(
17+
stringr::str_extract(
18+
unlist(
19+
stringr::str_extract_all(header.txt,
20+
pattern = '\\*Cast[\\s\\S]*\\d{2}\\s\\w{3}\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}')),
21+
'\\d{2}\\s\\w{3}\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}'))
22+
23+
# Extract probe serial number
24+
sn <- as.numeric(stringr::str_extract(
25+
unlist(stringr::str_extract_all(header.txt,
26+
pattern = '\\*SerialNumber=\\d{8}'))[1],"\\d{8}"))
27+
28+
# Extract cast name from file name
29+
cast <- tail(stringr::str_split(header.filename, "/")[[1]], n = 1) %>%
30+
stringr::str_replace(".asc", "")
31+
32+
# If Valeport UCTD
33+
} else {
34+
# Extract cast date as dttm
35+
cast.date <- lubridate::ymd_hms(
36+
stringr::str_extract(
37+
unlist(
38+
stringr::str_extract_all(header.txt,
39+
pattern = 'DataStartTime=\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}')),
40+
'\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}'))
41+
42+
# Extract probe serial number
43+
sn <- as.numeric(stringr::str_extract(
1644
unlist(
1745
stringr::str_extract_all(header.txt,
18-
pattern = '\\*Cast[\\s\\S]*\\d{2}\\s\\w{3}\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}')),
19-
'\\d{2}\\s\\w{3}\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}'))
20-
21-
# if (length(cast.date) == 0) {
22-
# cast.date <- lubridate::dmy_hms(
23-
# stringr::str_extract(
24-
# unlist(stringr::str_extract_all(header.txt,
25-
# pattern = '\\*Cast[\\s\\S]*stop')),
26-
# '\\d{2}\\s\\w{3}\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}'))
27-
# }
28-
29-
# Extract probe serial number
30-
sn <- as.numeric(stringr::str_extract(
31-
unlist(stringr::str_extract_all(header.txt,
32-
pattern = '\\*SerialNumber=\\d{8}'))[1],"\\d{8}"))
33-
34-
# Extract cast name from file name
35-
cast <- tail(stringr::str_split(header.filename, "/")[[1]], n = 1) %>%
36-
stringr::str_replace(".asc", "")
46+
pattern = 'SerialNumber=\\d{5}'))[1],
47+
'\\d{5}'))
3748

49+
# Extract cast name from file name
50+
cast <- tail(stringr::str_split(header.filename, "/")[[1]], n = 1) %>%
51+
stringr::str_replace(".vp2", "")
52+
}
3853
} else if (type == "CTD") {
3954
# Process CTD header file -----------------------------------------------------
4055
# Read header text
@@ -72,30 +87,65 @@ extract_ctd_header <- function(header.filename, type) {
7287
#'
7388
#' @param cast.filename Name of cast file.
7489
#' @param type Cast type (CTD or UCTD).
90+
#' @param skip Number of lines to skip (default = 79 for Valeport files)
7591
#' @return A data frame containing cast data.
7692
#' @export
77-
extract_ctd_cast <- function(cast.filename, type) {
93+
extract_ctd_cast <- function(cast.filename, type, skip = 79) {
94+
7895
# Extract cast name from file path
79-
cast <- tail(stringr::str_split(cast.filename, "/")[[1]], n = 1) %>%
80-
stringr::str_replace("_processed.asc", "")
96+
if(stringr::str_detect(cast.filename, ".asc")) {
97+
# If Oceansciences UCTD
98+
cast <- tail(stringr::str_split(cast.filename, "/")[[1]], n = 1) %>%
99+
stringr::str_replace("_processed.asc", "")
100+
101+
} else {
102+
# If Valeport UCTD
103+
cast <- tail(stringr::str_split(cast.filename, "/")[[1]], n = 1) %>%
104+
stringr::str_replace(".vp2", "")
105+
}
81106

82107
if (type == "UCTD") {
83108
# Process UCTD cast -------------------------------------------------------
84-
# Read cast data and rename columns
85-
read.table(cast.filename, header = TRUE) %>%
86-
dplyr::rename(scan = Scan, C = C0S.m, 'T' = Tnc90C, P = PrM,
87-
Z = DepSM, S = Sal00, Sv = SvCM, avgsVCM = AvgsvCM,
88-
Dens = Density00, Flag = Flag) %>%
89-
dplyr::mutate(
90-
scan = scan - min(scan),
91-
s = scan/16 - min(scan/16), # Calculate time (s) from scan (scan rate is 16 Hz)
92-
dt = c(0, diff(s)), # Calculate time interval
93-
dZ = c(1, diff(Z)), # Calculate change in depth (dZ, m)
94-
dZt = as.numeric(forecast::ma(dZ/dt, order = 5)),
95-
dZt = na_if(dZt, Inf),
96-
Z = -Z,
97-
cast = cast,
98-
path = cast.filename)
109+
110+
# If Oceansciences UCTD
111+
if(stringr::str_detect(cast.filename, ".asc")) {
112+
# Read cast data and rename columns
113+
read.table(cast.filename, header = TRUE) %>%
114+
dplyr::rename(scan = Scan, C = C0S.m, 'T' = Tnc90C, P = PrM,
115+
Z = DepSM, S = Sal00, Sv = SvCM, avgsVCM = AvgsvCM,
116+
Dens = Density00, Flag = Flag) %>%
117+
dplyr::mutate(
118+
scan = scan - min(scan),
119+
s = scan/16 - min(scan/16), # Calculate time (s) from scan (scan rate is 16 Hz)
120+
dt = c(0, diff(s)), # Calculate time interval
121+
dZ = c(1, diff(Z)), # Calculate change in depth (dZ, m)
122+
dZt = as.numeric(forecast::ma(dZ/dt, order = 5)),
123+
dZt = na_if(dZt, Inf),
124+
Z = -Z,
125+
cast = cast,
126+
path = cast.filename)
127+
128+
# If Valeport UCTD
129+
} else {
130+
# Read cast data and rename columns
131+
read.table(cast.filename, skip = skip,
132+
col.names = c("date","time","Z","P","T","C","S","Sv","Dens","ChlA","Ticks")) %>%
133+
dplyr::mutate(
134+
scan = seq_along(date),
135+
t = lubridate::ymd_hms(paste(date, time)),
136+
# Calculate time interval
137+
dt = as.numeric(difftime(t, dplyr::lag(t, 1, default = t[1]), units = "secs")),
138+
s = cumsum(dt),
139+
# Calculate change in depth (dZ, m)
140+
dZ = c(1, diff(Z)),
141+
dZt = as.numeric(forecast::ma(dZ/dt, order = 5)),
142+
dZt = dplyr::na_if(dZt, Inf),
143+
Z = -Z,
144+
cast = cast,
145+
path = cast.filename) %>%
146+
# Include only downcast data
147+
dplyr::slice(which(Z < -2)[1]:which.min(Z))
148+
}
99149

100150
} else if (type == "CTD") {
101151
# Process CTD cast --------------------------------------------------------

man/extract_ctd_cast.Rd

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)