forked from dankelley/oce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adv.R
1564 lines (1536 loc) · 75.8 KB
/
adv.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Class to Store adv Data
#'
#' This class holds data from acoustic-Doppler velocimeters.
#'
#' A file containing ADV data is usually recognized by Oce, and so
#' [read.oce()] will usually read the data. If not, one may use the
#' general ADV function [read.adv()] or specialized variants
#' [read.adv.nortek()], [read.adv.sontek.adr()] or
#' [read.adv.sontek.text()].
#'
#' ADV data may be plotted with [plot,adv-method()] function, which is a
#' generic function so it may be called simply as `plot(x)`, where
#' `x` is an [adv-class] object.
#'
#' Statistical summaries of ADV data are provided by the generic function
#' [summary,adv-method()].
#'
#' Conversion from beam to xyz coordinates may be done with
#' [beamToXyzAdv()], and from xyz to enu (east north up) may be done
#' with [xyzToEnuAdv()]. [toEnuAdv()] may be used to
#' transfer either beam or xyz to enu. Enu may be converted to other
#' coordinates (e.g. aligned with a coastline) with
#' [enuToOtherAdv()].
#'
#' @templateVar class adv
#'
#' @templateVar dataExample The key items stored in this slot include `time` and `v`.
#'
#' @templateVar metadataExample Examples that are of common interest include `frequency`, `oceCordinate`, and `frequency`.
#'
#' @template slot_summary
#'
#' @template slot_put
#'
#' @template slot_get
#'
#' @examples
#' data(adv)
#' adv[["v"]] <- 0.001 + adv[["v"]] # add 1mm/s to all velocity components
#'
#' @family classes provided by oce
#' @family things related to adv data
setClass("adv", contains="oce")
#' Sample adv (acoustic-doppler velocimeter) dataset
#'
#' This [adv-class] object is a sampling of measurements made with a
#' Nortek Vector acoustic Doppler velocimeter deployed as part of the St Lawrence
#' Internal Wave Experiment (SLEIWEX). Various identifying features have been
#' redacted.
#'
#' @name adv
#'
#' @docType data
#'
#' @usage data(adv)
#'
#' @examples
#'\donttest{
#' library(oce)
#' data(adv)
#'
#' # Velocity time-series
#' plot(adv)
#'
#' # Spectrum of upward component of velocity, with ``turbulent'' reference line
#' s <- spectrum(adv[["v"]][,3],plot=FALSE)
#' plot(log10(s$freq), log10(s$spec), type="l")
#' for (a in seq(-20, 20, by=1))
#' abline(a=a, b=-5/3, col="gray", lty="dotted")
#'}
#'
#' @source This file came from the SLEIWEX-2008 experiment.
#'
#' @family datasets provided with oce
#' @family things related to adv data
NULL
setMethod(f="initialize",
signature="adv",
definition=function(.Object, time, v, a, q, filename, ...) {
.Object <- callNextMethod(.Object, ...)
if (!missing(time)) .Object@data$time <- time
if (!missing(v)) .Object@data$v <- v
if (!missing(a)) .Object@data$a <- a
if (!missing(q)) .Object@data$q <- q
.Object@metadata$filename <- if (missing(filename)) "" else filename
.Object@processingLog$time <- presentTime()
.Object@processingLog$value <- "create 'adv' object"
return(.Object)
})
#' Summarize an ADV object
#'
#' Summarize data in an `adv` object.
#'
#' @param object an object of class `"adv"`, usually, a result of a call to
#' [read.adv()].
#'
#' @param ... further arguments passed to or from other methods.
#'
#' @examples
#' library(oce)
#' data(adv)
#' summary(adv)
#'
#' @author Dan Kelley
#'
#' @family things related to adv data
setMethod(f="summary",
signature="adv",
definition=function(object, ...) {
cat("ADV Summary\n-----------\n\n", ...)
cat(paste("* Instrument: ", object@metadata$instrumentType,
", serial number ``", object@metadata$serialNumber, "``\n", sep=""))
cat(paste("* Source filename: ``", object@metadata$filename, "``\n", sep=""))
if ("latitude" %in% names(object@metadata)) {
cat(paste("* Location: ",
if (is.na(object@metadata$latitude)) "unknown latitude" else sprintf("%.5f N", object@metadata$latitude), ", ",
if (is.na(object@metadata$longitude)) "unknown longitude" else sprintf("%.5f E", object@metadata$longitude), "\n"))
}
invisible(callNextMethod()) # summary
})
#' @title Extract Something from an adv Object
#'
#' @param x an [adv-class] object.
#'
#' @examples
#' data(adv)
#' head(adv[["q"]]) # in raw form
#' head(adv[["q", "numeric"]]) # in numeric form
#'
#' @template sub_subTemplate
#'
#' @section Details of the specialized `adv` method:
#'
#' In addition to the usual extraction of elements by name, some shortcuts
#' are also provided, e.g. `u1` retrieves `v[,1]`, and similarly
#' for the other velocity components. The `a` and `q`
#' data can be retrieved in [raw()] form
#' or numeric form; see \dQuote{Examples}.
#'
#' It is also worth noting that heading, pitch, etc. may be stored in
#' "slow" form in the object (e.g. in `headingSlow` within
#' the `data` slot). In that case, accessing by full name, e.g.
#' `x[["headingSlow"]]` retrieves the item as expected, but
#' `x[["heading"]]` interpolates to the faster timescale, using
#' [`approx`]`(timeSlow,headingSlow,time)`.
#'
#' @author Dan Kelley
#'
#' @family things related to adv data
setMethod(f="[[",
signature(x="adv", i="ANY", j="ANY"),
definition=function(x, i, j, ...) {
haveSlow <- "timeSlow" %in% names(x@data)
if (i == "u1") {
return(x@data$v[, 1])
} else if (i == "u2") {
return(x@data$v[, 2])
} else if (i == "u3") {
return(x@data$v[, 3])
} else if (i == "a") {
if (!missing(j) && j == "numeric") {
res <- x@data$a
dim <- dim(res)
res <- as.numeric(res)
dim(res) <- dim
} else {
res <- x@data$a
}
return(res)
} else if (i == "q") {
if (!missing(j) && j == "numeric") {
res <- x@data$q
dim <- dim(res)
res <- as.numeric(res)
dim(res) <- dim
} else {
res <- x@data$q
}
return(res)
} else if (i %in% c("heading", "pitch", "roll")) {
if (haveSlow) {
## offset the time so approx doesn't yield all NAs
tSlow <- as.numeric(x@data$timeSlow)
t <- as.numeric(x@data$time)
t0 <- t[1]
res <- approx(tSlow-t0, x@data[[i]], t-t0)$y
} else {
res <- x@data[[i]]
}
} else {
callNextMethod() # [[
}
})
#' Replace Parts of an ADV Object
#'
#' @details
#' If the `adv` object holds slow variables (i.e. if `timeSlow` is
#' in the `data` slot), then assigning to .e.g. `heading` will not
#' actually assign to a variable of that name, but instead assigns to
#' `headingSlow`. To catch misapplication of this rule, an error
#' message will be issued if the assigned value is not of the same length
#' as `timeSlow`.
#'
#' @param x an [adv-class] object.
#'
#' @param value The value to be inserted into `x`.
#'
#' @author Dan Kelley
#'
#' @template sub_subTemplate
#'
#' @family things related to adv data
setMethod(f="[[<-",
signature="adv",
definition=function(x, i, j, ..., value) {
## FIXME: use j for e.g. times
haveSlow <- "timeSlow" %in% names(x@data)
if (i %in% names(x@metadata)) {
x@metadata[[i]] <- value
} else if (i %in% names(x@data)) {
x@data[[i]] <- value
} else if (i %in% c("heading", "pitch", "roll")) {
## do not store as indicated; interpolate to the Slow variant
if (haveSlow) {
name <- paste(i, "Slow", sep="")
if (!(name %in% names(x@data)))
stop("no variable named '", name, "' in object's data slot")
if (length(value) != length(x@data[[name]]))
stop("length mismatch; value has ", length(value), " but ", name, " has ", length(x@data[[name]]), " elements")
x@data[[name]] <- value
} else {
x@data[[i]] <- value
}
} else {
x <- callNextMethod(i=i, j=j, ...=..., value=value) # [[<-
}
## Not checking validity because user may want to shorten items one by one, and check validity later.
## validObject(x)
invisible(x)
})
#' Subset an ADV Object
#'
#' Subset an adv (acoustic Doppler profile) object. This function is somewhat
#' analogous to [subset.data.frame()], except that subsets can only be
#' specified in terms of `time`.
#'
#' @param x an [adv-class] object.
#'
#' @param subset a condition to be applied to the `data` portion of `x`.
#' See \sQuote{Details}.
#'
#' @param \dots ignored.
#'
#' @return
#' A new [adv-class] object.
#'
#' @examples
#' library(oce)
#' data(adv)
#' plot(adv)
#' plot(subset(adv, time < mean(range(adv[['time']]))))
#'
#' @author Dan Kelley
#'
#' @family things related to adv data
#' @family functions that subset oce objects
setMethod(f="subset",
signature="adv",
definition=function(x, subset, ...) {
subsetString <- paste(deparse(substitute(expr=subset, env=environment())), collapse=" ")
res <- x
dots <- list(...)
debug <- if (length(dots) && ("debug" %in% names(dots))) dots$debug else getOption("oceDebug")
if (missing(subset))
stop("must give 'subset'")
if (missing(subset))
stop("must specify a 'subset'")
if (length(grep("time", subsetString))) {
oceDebug(debug, "subsetting an adv object by time\n")
## keep <- eval(substitute(subset), x@data, parent.frame(2)) # used for $ts and $ma, but $tsSlow gets another
keep <- eval(expr=substitute(expr=subset, env=environment()), envir=x@data, enclos=parent.frame(2))
sum.keep <- sum(keep)
if (sum.keep < 2)
stop("must keep at least 2 profiles")
oceDebug(debug, "keeping", sum.keep, "of the", length(keep), "time slots\n")
oceDebug(debug, vectorShow(keep, "keeping bins:"))
res <- x
names <- names(x@data)
haveSlow <- "timeSlow" %in% names
##keep <- eval(substitute(subset), x@data, parent.frame(2)) # used for $ts and $ma, but $tsSlow gets another
keep <- eval(expr=substitute(expr=subset, env=environment()), envir=x@data, enclos=parent.frame(2))
if (haveSlow) {
subsetStringSlow <- gsub("time", "timeSlow", subsetString)
keepSlow <- eval(parse(text=subsetStringSlow), x@data, parent.frame(2))
}
if ("timeBurst" %in% names) {
subsetStringBurst <- gsub("time", "timeBurst", subsetString)
keepBurst <-eval(parse(text=subsetStringBurst), x@data, parent.frame(2))
}
for (name in names(x@data)) {
if ("distance" == name)
next
if (length(grep("Burst$", name))) {
res@data[[name]] <- x@data[[name]][keepBurst]
} else if (length(grep("^time", name)) || is.vector(res@data[[name]])) {
if (1 == length(agrep("Slow$", name))) {
oceDebug(debug, "subsetting data$", name, " (using an interpolated subset)\n", sep="")
res@data[[name]] <- x@data[[name]][keepSlow]
} else {
oceDebug(debug, "subsetting data$", name, "\n", sep="")
res@data[[name]] <- x@data[[name]][keep]
}
} else if (is.matrix(res@data[[name]])) {
oceDebug(debug, "subsetting data$", name, ", which is a matrix\n", sep="")
res@data[[name]] <- x@data[[name]][keep, ]
} else if (is.array(res@data[[name]])) {
oceDebug(debug, "subsetting data$", name, ", which is an array\n", sep="")
res@data[[name]] <- x@data[[name]][keep, , ]
}
}
} else {
stop("only 'time' is permitted for subsetting")
}
res@metadata$numberOfSamples <- dim(res@data$v)[1]
res@processingLog <- processingLogAppend(res@processingLog, paste("subset(x, subset=", subsetString, ")", sep=""))
res
})
#' @template readAdvTemplate
#'
#' @param type character string indicating type of file, and used by
#' `read.adv` to dispatch to one of the speciality functions.
#'
#' @param start the time of the first sample, typically created with
#' [as.POSIXct()]. This may be a vector of times,
#' if `filename` is a vector of file names.
#'
#' @param deltat the time between samples. (This is mandatory if
#' `header=FALSE`.)
#'
#' @param header A logical value indicating whether the file starts with a header.
#' (This will not be the case for files that are created by data loggers that
#' chop the raw data up into a series of sub-files, e.g. once per hour.)
read.adv <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
type=c("nortek", "sontek", "sontek.adr", "sontek.text"),
header=TRUE,
longitude=NA, latitude=NA,
start=NULL, deltat=NA,
debug=getOption("oceDebug"), monitor=FALSE, processingLog=NULL)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (!interactive())
monitor <- FALSE
type <- match.arg(type)
## FIXME: all these read.adv variants should have the same argument list
if (type == "nortek")
read.adv.nortek(file=file, from=from, to=to, by=by, tz=tz,
header=header,
longitude=longitude, latitude=latitude,
debug=debug, monitor=monitor, processingLog=processingLog)
else if (type == "sontek") # guess
read.adv.sontek.serial(file=file, from=from, to=to, by=by, tz=tz,
longitude=longitude, latitude=latitude,
start=start, deltat=deltat,
debug=debug, monitor=monitor, processingLog=processingLog)
else if (type == "sontek.adr")
read.adv.sontek.adr(file=file, from=from, to=to, by=by, tz=tz,
longitude=longitude, latitude=latitude,
debug=debug, processingLog=processingLog)
else if (type == "sontek.text")
read.adv.sontek.text(file=file, from=from, to=to, by=by, tz=tz,
longitude=longitude, latitude=latitude,
debug=debug, processingLog=processingLog)
else
stop("read.adv() cannot understand type = \"", type, "\"")
}
#' Plot an adv Object
#'
#' Plot [adv-class] data.
#'
#' Creates a multi-panel summary plot of data measured by an ADV.
#' The panels are controlled by the `which` argument. (Note the
#' gaps in the sequence, e.g. 4 and 8 are not used.)
#'
#' * `which=1` to `3` (or `"u1"` to `"u3"`)
#' yield timeseries of the first, second, and third components of
#' velocity (in beam, xyz or enu coordinates).
#'
#' * `which=4` is not permitted (since ADV are 3-beam devices)
#'
#' * `which=5` to `7` (or `"a1"` to `"a3"`)
#' yield timeseries of the amplitudes of beams 1 to 3. (Note that
#' the data are called `data$a[,1]`, `data$a[,2]` and
#' `data$a[,3]`, for these three timeseries.)
#'
#' * `which=8` is not permitted (since ADV are 3-beam devices)
#'
#' * `which=9` to `11` (or `"q1"` to `"q3"`)
#' yield timeseries of correlation for beams 1 to 3. (Note that the
#' data are called `data$c[,1]`, `data$c[,2]` and
#' `data$c[,3]`, for these three timeseries.)
#'
#' * `which=12` is not permitted (since ADVs are 3-beam devices)
#'
#' * `which=13` is not permitted (since ADVs do not measure salinity)
#'
#' * `which=14` or `which="temperature"` yields a timeseries of temperature.
#'
#' * `which=15` or `which="pressure"` yields a timeseries of pressure.
#'
#' * `which=16` or `which="heading"` yields a timeseries of heading.
#'
#' * `which=17` or `which="pitch"`yields a timeseries of pitch.
#'
#' * `which=18` or `which="roll"`yields a timeseries of roll.
#'
#' * `which=19` to `21` yields plots of correlation versus
#' amplitude, for beams 1 through 3, using [smoothScatter()].
#'
#' * `which=22` is not permitted (since ADVs are 3-beam devices)
#'
#' * `which=23` or `"progressive vector"` yields a
#' progressive-vector diagram in the horizontal plane, plotted with
#' `asp=1`, and taking beam1 and beam2 as the eastward and
#' northward components of velocity, respectively.
#'
#' * `which=28` or `"uv"` yields velocity plot in the
#' horizontal plane, i.e. `u[2]` versus `u[1]`. If the number of data
#' points is small, a scattergraph is used, but if it is large,
#' [smoothScatter()] is used.
#'
#' * `which=29` or `"uv+ellipse"` as the `"uv"`
#' case, but with an added indication of the tidal ellipse,
#' calculated from the eigen vectors of the covariance matrix.
#'
#' * `which=30` or `"uv+ellipse+arrow"` as the
#' `"uv+ellipse"` case, but with an added arrow indicating the
#' mean current.
#'
#' * `which=50` or `"analog1"` plots a time series of the
#' analog1 signal, if there is one.
#'
#' * `which=51` or `"analog2"` plots a time series of the
#' analog2 signal, if there is one.
#'
#' * `which=100` or `"voltage"` plots the voltage as a
#' timeseries, if voltage exists in the dataset.
#'
#' In addition to the above, there are some groupings defined:
#' * `which="velocity"` equivalent to `which=1:3` (three velocity components)
#' * `which="amplitude"` equivalent to `which=5:7` (three amplitude components)
#' * `which="backscatter"` equivalent to `which=9:11` (three backscatter components)
#' * `which="hydrography"` equivalent to `which=14:15` (temperature and pressure)
#' * `which="angles"` equivalent to `which=16:18` (heading, pitch and roll)
#'
#' @param x an [adv-class] object.
#'
#' @param which List of desired plot types. These are graphed in panels running
#' down from the top of the page. See \dQuote{Details} for the meanings of
#' various values of `which`.
#'
#' @param col Optional indication of color(s) to use. If not provided, the
#' default for images is `oce.colorsPalette(128,1)`, and for lines and points
#' is black.
#'
#' @param titles Optional vector of character strings to be used as labels for the
#' plot panels. For images, these strings will be placed in the right hand side
#' of the top margin. For timeseries, these strings are ignored. If this is
#' provided, its length must equal that of `which`.
#'
#' @param lwd If the plot is of a time-series or scattergraph format with lines,
#' this is used in the usual way; otherwise, e.g. for image formats, this is
#' ignored.
#'
#' @param type Type of plot, as for [plot()].
#'
#' @param drawTimeRange Logical value that applies to panels with time as the horizontal
#' axis, indicating whether to draw the time range in the top-left margin of the
#' plot.
#'
#' @param drawZeroLine Logical value indicating whether to draw zero lines on
#' velocities.
#'
#' @param useSmoothScatter Logical value indicating whether to use
#' [smoothScatter()] in various plots, such as `which="uv"`. If
#' not provided a default is used, with [smoothScatter()] being used if
#' there are more than 2000 points to plot.
#'
#' @param mgp 3-element numerical
#' vector to use for `par(mgp)`, and also for `par(mar)`, computed from
#' this. The default is tighter than the R default, in order to use more space
#' for the data and less for the axes.
#'
#' @param mar Value to be used with [`par`]`("mar")`.
#'
#' @param tformat Optional argument passed to [oce.plot.ts()], for plot
#' types that call that function. (See [strptime()] for the format
#' used.)
#'
#' @param marginsAsImage Logical value indicating whether to put a wide margin to
#' the right of time-series plots, matching the space used up by a palette in an
#' [imagep()] plot.
#'
#' @param cex numeric character expansion factor for plot symbols; see [par()].
#'
#' @param cex.axis,cex.lab,cex.main character expansion factors for axis numbers, axis names and plot titles; see [par()].
#'
#' @param xlim Optional 2-element list for `xlim`, or 2-column matrix, in
#' which case the rows are used, in order, for the panels of the graph.
#'
#' @param ylim Optional 2-element list for `ylim`, or 2-column matrix, in
#' which case the rows are used, in order, for the panels of the graph.
#'
#' @param brushCorrelation Optional number between 0 and 100, indicating a
#' per-beam correlation threshold below which data are to be considered suspect.
#' If the plot type is `p`, the suspect points (velocity, backscatter
#' amplitude, or correlation) will be colored red; otherwise, this argument is
#' ignored.
#'
#' @param colBrush Color to use for brushed (bad) data, if
#' `brushCorrelation` is active.
#'
#' @param main Main title for plot, used just on the top panel, if there are
#' several panels.
#'
#' @param debug A flag that turns on debugging. Set to 1 to get a moderate amount
#' of debugging information, or to 2 to get more.
#'
#' @param ... Optional arguments passed to plotting functions.
#'
#' @seealso The documentation for [adv-class] explains the structure
#' of ADV objects, and also outlines the other functions dealing with them.
#'
#' @examples
#' library(oce)
#' data(adv)
#' plot(adv)
#'
#' @author Dan Kelley
#'
#' @family functions that plot oce data
#' @family things related to adv data
#'
#' @aliases plot.adv
setMethod(f="plot",
signature=signature("adv"),
definition=function(x, which=c(1:3, 14, 15),
col,
titles,
type="l",
lwd=par('lwd'),
drawTimeRange=getOption("oceDrawTimeRange"),
drawZeroLine=FALSE,
useSmoothScatter,
mgp=getOption("oceMgp"),
mar=c(mgp[1]+1.5, mgp[1]+1.5, 1.5, 1.5),
tformat,
marginsAsImage=FALSE,
cex=par("cex"),
cex.axis=par("cex.axis"), cex.lab=par("cex.lab"), cex.main=par("cex.main"),
xlim, ylim,
brushCorrelation, colBrush="red",
main="",
debug=getOption("oceDebug"),
...)
{
debug <- min(4, max(0, round(debug)))
oceDebug(debug, "plot.adv(x, which=c(", paste(which, collapse=","), "), type=\"", type, "\", ...) {\n", sep="", unindent=1)
have.brushCorrelation <- !missing(brushCorrelation)
oceDebug(debug, "brushCorrelation", if (have.brushCorrelation) brushCorrelation else "not given", "\n")
oceDebug(debug, argShow(cex))
oceDebug(debug, argShow(cex.axis))
oceDebug(debug, argShow(cex.lab))
oceDebug(debug, argShow(cex.main))
oceDebug(debug, argShow(mar))
opar <- par(no.readonly = TRUE)
dots <- names(list(...))
##if (!all(which %in% c(1:3,5:7,9:11,14:21,23)))
## stop("\"which\" must be in the range c(1:3,5:7,9:11,14:21,23) but it is ", which)
nw <- length(which)
if (nw == 1 && is.character(which)) {
pm <- pmatch(which, c("velocity", "amplitude", "quality", "hydrography", "angles"))
if (!is.na(pm)) {
nbeams <- 3
if (pm == 1)
which <- 0 + seq(1, nbeams)
else if (pm == 2)
which <- 4 + seq(1, nbeams)
else if (pm == 3)
which <- 8 + seq(1, nbeams)
else if (pm == 4)
which <- 14:15
else if (pm == 5)
which <- 16:18
nw <- length(which)
}
}
colPerPoint <- FALSE
if (missing(col)) {
col <- rep("black", length.out=nw)
} else {
colPerPoint <- length(col) == length(x@data$time) # FIXME slow timescale here?
if (!colPerPoint)
col <- rep(col, length.out=nw)
}
if (!missing(titles) && length(titles) != nw)
stop("length of 'titles' must equal length of 'which'")
if (nw > 1)
on.exit(par(opar))
par(mgp=mgp, mar=mar)
dots <- list(...)
## user may specify a matrix for xlim and ylim
gave.ylim <- !missing(ylim)
if (gave.ylim) {
if (is.matrix(ylim)) {
if (dim(ylim)[2] != nw) {
ylim2 <- matrix(ylim, ncol=2, nrow=nw) # FIXME: is this what I want?
}
} else {
ylim2 <- matrix(ylim, ncol=2, nrow=nw) # FIXME: is this what I want?
}
class(ylim2) <- class(ylim)
ylim <- ylim2
}
gave.xlim <- !missing(xlim)
if (gave.xlim) {
if (is.matrix(xlim)) {
if (dim(xlim)[2] != nw) {
xlim2 <- matrix(xlim, ncol=2, nrow=nw) # FIXME: is this what I want?
}
} else {
if (length(xlim) != 2)
stop("xlim must be a vector of length 2, or a 2-column matrix")
xlim2 <- matrix(xlim[1:2], ncol=2, nrow=nw, byrow=TRUE)
}
xlim <- xlim2
}
oceDebug(debug, "before layout, cex=", par('cex'), "\n")
if (nw > 1) {
if (marginsAsImage) {
w <- 1.5
lay <- layout(matrix(1:(2*nw), nrow=nw, byrow=TRUE), widths=rep(c(1, lcm(w)), nw))
} else {
lay <- layout(cbind(1:nw))
}
}
## Translate word-style (FIXME: ugly coding)
oceDebug(debug, "before nickname-substitution, which=c(", paste(which, collapse=","), ")\n")
which2 <- vector("numeric", nw)
if (nw == 1 && is.character(which)) {
wtmp <- char.expand(which,
c("velocity", "amplitude", "backscatter", "hydrography", "angles"), nomatch=NULL)
if (!is.na(wtmp)) {
if ( wtmp == "velocity" ) which <- 1:3
else if (wtmp == "amplitude" ) which <- 5:7
else if (wtmp == "backscatter") which <- 9:11
else if (wtmp == "hydrography") which <- 14:15
else if (wtmp == "angles" ) which <- 16:18
nw <- length(which)
}
}
for (w in 1:nw) {
ww <- which[w]
if (is.numeric(ww)) {
which2[w] <- ww
} else {
if ( ww == "u1") which2[w] <- 1
else if (ww == "u2") which2[w] <- 2
else if (ww == "u3") which2[w] <- 3
## 4 not allowed since ADV is 3-beam
else if (ww == "a1") which2[w] <- 5
else if (ww == "a2") which2[w] <- 6
else if (ww == "a3") which2[w] <- 7
## 4 not allowed since ADV is 3-beam
else if (ww == "q1") which2[w] <- 9
else if (ww == "q2") which2[w] <- 10
else if (ww == "q3") which2[w] <- 11
## 4 not allowed since ADV is 3-beam
else if (ww == "salinity") which2[w] <- 13
else if (ww == "temperature") which2[w] <- 14
else if (ww == "pressure") which2[w] <- 15
else if (ww == "heading") which2[w] <- 16
else if (ww == "pitch") which2[w] <- 17
else if (ww == "roll") which2[w] <- 18
## 19 beam-1 correlation-amplitude diagnostic plot
## 20 beam-2 correlation-amplitude diagnostic plot
## 21 beam-3 correlation-amplitude diagnostic plot
## 22 not allowed, since ADVs have only 3 beams
else if (ww == "progressive vector") which2[w] <- 23
else if (ww == "uv") which2[w] <- 28
else if (ww == "uv+ellipse") which2[w] <- 29
else if (ww == "uv+ellipse+arrow") which2[w] <- 30
else if (ww == "analog1") which2[w] <- 50
else if (ww == "analog2") which2[w] <- 51
else if (ww == "voltage") which2[w] <- 100
else stop("unknown 'which':", ww)
}
}
which <- which2
oceDebug(debug, "after nickname-substitution, which=c(", paste(which, collapse=","), ")\n")
oceDebug(debug, "after layout, cex=", par('cex'), "\n")
## FIXME below here, was using tsSlow
tlim <- range(x@data$time, na.rm=TRUE)
for (w in 1:nw) {
if (w > 1)
main <- ""
oceDebug(debug, "plotting which[", w, "]=", which[w], "\n")
par(mgp=mgp, mar=mar)
if (which[w] %in% 1:3) {
## u1, u2, u3
y <- as.numeric(x@data$v[, which[w]])
if (have.brushCorrelation && type == "p") {
good <- as.numeric(x@data$q[, which[w]]) >= brushCorrelation
oce.plot.ts(x@data$time[good], y[good], ylab=beamName(x, which[w]),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(y, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.lab=cex.lab, cex.main=cex.main,
mgp=mgp,
mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1,
...)
points(x@data$time[!good], x@data$v[!good, which[w]], col=colBrush, ...)
} else {
oce.plot.ts(x@data$time, y, ylab=beamName(x, which[w]),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(y, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp,
mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
if (drawZeroLine)
abline(h=0)
rm(y) # space may be tight
} else if (which[w] %in% 5:7) {
## a1, a2, a3
## FIXME/DRY: alter a1,a2,a3 if alter q1,q2,q3, since both almost the same
oceDebug(debug, "plotting a1, a2, or a3 since which[w] == ", which[w], "\n")
y <- as.numeric(x@data$a[, which[w]-4])
oceDebug(debug, "range(y):", paste(range(y, na.rm=TRUE), sep="-"), "\n")
if (have.brushCorrelation && type == "p") {
good <- as.numeric(x@data$q[, which[w]-4]) >= brushCorrelation
oce.plot.ts(x@data$time[good], y[good],
ylab=c(expression(a[1]), expression(a[2]), expression(a[3]), expression(a[4]))[which[w]-4],
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(y, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1)
points(x@data$time[!good], y[!good], col=colBrush)
} else {
oce.plot.ts(x@data$time, y,
ylab=c(expression(a[1]), expression(a[2]), expression(a[3]), expression(a[4]))[which[w]-4],
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(y, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
rm(y) # space may be tight
} else if (which[w] %in% 9:11) {
## q1, q2, q3 (named c1, c2, and c3 in the object)
y <- as.numeric(x@data$q[, which[w]-8])
if (have.brushCorrelation && type == "p") {
good <- as.numeric(x@data$q[, which[w]-8]) >= brushCorrelation
oce.plot.ts(x@data$time[good], y[good],
ylab=c(expression(q[1]), expression(q[2]), expression(q[3]), expression(q[4]))[which[w]-8],
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(y, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1)
points(x@data$time[!good], y[!good], col=colBrush)
} else {
oce.plot.ts(x@data$time, y,
ylab=c(expression(q[1]), expression(q[2]), expression(q[3]), expression(q[4]))[which[w]-8],
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(y, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
tformat=tformat,
debug=debug-1)
}
rm(y) # space may be tight
} else if (which[w] == 13 || which[w] == "salinity") {
if ("salinity" %in% names(x@metadata)) {
if ("timeSlow" %in% names(x@data)) {
salinity <- rep(x@metadata$salinity, length(x@data$temperatureSlow))
oce.plot.ts(x@data$timeSlow, salinity, ylab=resizableLabel("S", "y"),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else x@metadata$salinity+c(0.5, -0.5),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
} else {
salinity <- rep(x@metadata$salinity, length(x@data$temperature))
oce.plot.ts(x@data$time, salinity, ylab=resizableLabel("S", "y"),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else x@metadata$salinity+c(0.5, -0.5),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
} else {
warning("no salinity in this ADV object")
}
} else if (which[w] == 14 || which[w] == "temperature") {
if ("timeSlow" %in% names(x@data) && "temperatureSlow" %in% names(x@data)) {
oce.plot.ts(x@data$timeSlow, x@data$temperatureSlow, ylab=resizableLabel("T", "y"),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$temperature, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
} else {
oce.plot.ts(x@data$time, x@data$temperature, ylab=resizableLabel("T", "y"),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$temperature, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
} else if (which[w] == 15 || which[w] == "pressure") {
oce.plot.ts(x@data$time, x@data$pressure, ylab=resizableLabel("p", "y"),
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$pressure, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
} else if (which[w] == 16 || which[w] == "heading") {
if ("timeSlow" %in% names(x@data) && "headingSlow" %in% names(x@data)) {
oce.plot.ts(x@data$timeSlow, x@data$headingSlow, ylab="heading",
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$heading, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
} else {
oce.plot.ts(x@data$time, x@data$heading, ylab="heading",
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$heading, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
} else if (which[w] == 17 || which[w] == "pitch") {
## pitch
if ("timeSlow" %in% names(x@data) && "pitchSlow" %in% names(x@data)) {
oce.plot.ts(x@data$timeSlow, x@data$pitchSlow, ylab="pitch",
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$pitch, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
} else {
oce.plot.ts(x@data$time, x@data$pitch, ylab="pitch",
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$pitch, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=if (colPerPoint) col else col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
} else if (which[w] == 18 || which[w] == "roll") {
if ("timeSlow" %in% names(x@data) && "rollSlow" %in% names(x@data)) {
oce.plot.ts(x@data$timeSlow, x@data$rollSlow, ylab="roll",
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$roll, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1)
} else {
oce.plot.ts(x@data$time, x@data$roll, ylab="roll",
drawTimeRange=drawTimeRange,
xlim=if (gave.xlim) xlim[w, ] else tlim,
ylim=if (gave.ylim) ylim[w, ] else range(x@data$roll, na.rm=TRUE),
type=type,
cex=cex, cex.axis=cex.axis, cex.main=cex.main,
mgp=mgp, mar=c(mgp[1], mgp[1]+1.5, 1.5, 1.5),
lwd=lwd[w], col=col[w],
main=main,
tformat=tformat,
debug=debug-1)
}
## FIXME: should plot.adv() be passing mar, cex, etc to smoothScatter?
} else if (which[w] == 19) {
## beam 1 correlation-amplitude diagnostic plot
a <- as.numeric(x@data$a[, 1])
q <- as.numeric(x@data$q[, 1])
n <- length(a)
if (n < 2000 || (!missing(useSmoothScatter) && !useSmoothScatter)) {
plot(a, c,
xlab=gettext("Amplitude", domain="R-oce"),
ylab=gettext("Correlation", domain="R-oce"),
xlim=if (gave.xlim) xlim[w, ] else range(a),
ylim=if (gave.ylim) ylim[w, ] else range(c),
main=main,
debug=debug-1)
} else {
smoothScatter(a, c, nbin=64,
xlab=gettext("Amplitude", domain="R-oce"),
ylab=gettext("Correlation", domain="R-oce"),
xlim=if (gave.xlim) xlim[w, ] else range(a),
ylim=if (gave.ylim) ylim[w, ] else range(c),
main=main,