Skip to content

Commit 585e81b

Browse files
committed
Plot updates
1 parent 9cca1f1 commit 585e81b

File tree

1 file changed

+96
-30
lines changed

1 file changed

+96
-30
lines changed

surf.R

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# at http://www.eol.ucar.edu/software/R/, for manipulating
77
# and plotting time-series.
88

9-
surfboard <- function(file=file.path(Sys.getenv("HOME"),"surfboard","surfboard.dat.gz"), modemtz="MST")
9+
surfboard <- function(file=file.path(Sys.getenv("HOME"),"surfboard","surfboard.dat.gz"), modemtz="MST", do_dat=FALSE)
1010
{
1111
surfd <- scan(file=file,sep=",", quiet=TRUE, what=list(
1212
datetime="", channel=1, status="",
@@ -28,9 +28,10 @@ surfboard <- function(file=file.path(Sys.getenv("HOME"),"surfboard","surfboard.d
2828
dx <- c(surfd$power[mx],surfd$SNR[mx],
2929
surfd$CorrCw[mx], surfd$UncorrCw[mx])
3030
tx <- times[mx]
31-
xts <- dat(nts(matrix(dx,ncol=4), tx,
31+
xts <- nts(matrix(dx,ncol=4), tx,
3232
names=c("power","SNR","CorrCw","UncorrCw"),
33-
units=c("dBmV","dB","","")))
33+
units=c("dBmV","dB","",""))
34+
if (do_dat) xts <- dat(xts)
3435

3536
tsl[as.character(freq)] <- list(list(ts=xts,
3637
channels=unique(surfd$channel[mx]),
@@ -42,11 +43,11 @@ surfboard <- function(file=file.path(Sys.getenv("HOME"),"surfboard","surfboard.d
4243

4344
plotsurf <- function(freqs=0,
4445
file=file.path(Sys.getenv("HOME"),"surfboard","surfboard.dat.gz"),
45-
palate="Heat", ncolors=10, modemtz="MST")
46+
palate="Heat", ncolors=10, modemtz="MST", logbad=FALSE, do_dat=FALSE)
4647
{
4748
# not really important, but time on the modem is standard time,
4849
# not adjusted for daylight savings time
49-
surfd <- surfboard(file=file, modemtz=modemtz)
50+
surfd <- surfboard(file=file, modemtz=modemtz, do_dat=do_dat)
5051

5152
allfreqs <- sort(as.integer(names(surfd)))
5253
cat("Frequencies=",paste(allfreqs,collapse=", "),"\n")
@@ -84,7 +85,6 @@ plotsurf <- function(freqs=0,
8485
snr <- NULL
8586
pow <- NULL
8687

87-
logbad <- FALSE
8888

8989
for (freq in calcfreqs) {
9090
dx <- surfd[[as.character(freq)]]
@@ -95,12 +95,15 @@ plotsurf <- function(freqs=0,
9595
# total number of codewords.
9696

9797
# rate (per hour) of codewords that were corrected
98+
# cat('class(dx$ts[,"CorrCw"]=', class(dx$ts[,"CorrCw"]),"\n")
9899
corx <- d_by_dt(dx$ts[,"CorrCw"],dtmax=86400,lag=1,time=1) * 3600
100+
# cat("class(corx)=", class(corx), "\n")
99101
units(corx) <- "hr-1"
100102
corr <- Cbind(corr,corx)
101103

102104
# rate (per hour) of uncorrectable codewords
103105
uncorx <- d_by_dt(dx$ts[,"UncorrCw"],dtmax=86400,lag=1,time=1) * 3600
106+
# cat("class(uncorx)=", class(uncorx), "\n")
104107
units(uncorx) <- "hr-1"
105108
uncorr <- Cbind(uncorr,uncorx)
106109

@@ -109,10 +112,10 @@ plotsurf <- function(freqs=0,
109112
}
110113
# look for modem restarts when the successive difference of
111114
# correctables or uncorrectables are negative.
112-
restart <- dat(nts(apply(corr@data,1,function(x) { any(!is.na(x) & x < 0)}),
113-
positions(corr),names="restarts",units="")) |
114-
dat(nts(apply(uncorr@data,1,function(x) { any(!is.na(x) & x < 0)}),
115-
positions(uncorr),names="restarts",units=""))
115+
restart <- nts(apply(corr@data,1,function(x) { any(!is.na(x) & x < 0)}),
116+
positions(corr),names="restarts",units="") |
117+
nts(apply(uncorr@data,1,function(x) { any(!is.na(x) & x < 0)}),
118+
positions(uncorr),names="restarts",units="")
116119

117120
if (any(restart)) {
118121
corr[restart,] <- NA
@@ -143,27 +146,54 @@ plotsurf <- function(freqs=0,
143146
", ids=", paste(ids,collapse=","),
144147
", channel#=", paste(chans,collapse=","))
145148

149+
zcw <- !is.na(badcw[,nfreq]) & badcw[,nfreq] == 0
146150
if (logbad) {
147151
# make log plot to expand lower values. Plot 0 as 0.1
148-
zcw <- !is.na(badcw[,nfreq]) & badcw[,nfreq] == 0
149152
badcw[zcw,nfreq] <- 0.1
150153
}
151-
plot(badcw[,nfreq], title=titlestr, type="b", xlim=c(t1,t2),
154+
plot(badcw[,nfreq], type="b", xlim=c(t1,t2),
152155
log=ifelse(logbad,"y",""))
156+
if (!do_dat) {
157+
timeaxis(3, labels=TRUE, time.zone=badcw@time.zone, date.too=FALSE,
158+
xlab=FALSE)
159+
axis(4)
160+
}
161+
153162
if (logbad) badcw[zcw,nfreq] <- 0
154163

155164
pcu <- uncorr[,nfreq] / badcw[,nfreq] * 100
156165
pcu[zcw,] <- 0
157166
colnames(pcu) <- "UncorrCW"
158167
units(pcu) <- "%"
159-
plot(pcu,title=titlestr,type="b",xlim=c(t1,t2))
168+
plot(pcu,type="b",xlim=c(t1,t2))
169+
if (!do_dat) {
170+
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone, date.too=FALSE,
171+
xlab=FALSE)
172+
axis(4)
173+
}
174+
175+
plot(snr[,nfreq],type="b",xlim=c(t1,t2))
176+
if (!do_dat) {
177+
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone, date.too=FALSE,
178+
xlab=FALSE)
179+
axis(4)
180+
}
181+
182+
if (!do_dat) {
183+
plot(pow[,nfreq],type="b",xlim=c(t1,t2))
184+
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone, date.too=FALSE,
185+
xlab=FALSE)
186+
axis(4)
187+
title(main=titlestr, line=-par("cex.main"), outer=TRUE)
188+
}
189+
else plot(pow[,nfreq],type="b",xlim=c(t1,t2), title=titlestr)
160190

161-
plot(snr[,nfreq],title=titlestr,type="b",xlim=c(t1,t2))
162-
plot(pow[,nfreq],title=titlestr,type="b",xlim=c(t1,t2))
163191

164192
par(ask=ask)
165193
}
166194

195+
eolts::set_plot_margins()
196+
167197
# heatmaps of x=time, y=frequency, z=variable.
168198
# Need legends, time scale on X
169199
pcu <- uncorr / badcw * 100
@@ -179,31 +209,47 @@ plotsurf <- function(freqs=0,
179209
t2 <- tx[length(tx)]
180210

181211
timeaxis_setup(t1,t2)
212+
# par(mgp=c(1.7,0.7,0))
182213

183214
title <- paste0(unique(colnames(badcw)), " (", unique(units(badcw)),")")
184-
image(z=badcw@data, x=tx-t1, y=allfreqs, col=colors, ylab="MHz",
185-
main=title, xaxt="n", xlab="")
186-
timeaxis(1, time.zone=badcw@time.zone)
187-
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone)
215+
image(z=badcw@data, x=tx-t1, y=allfreqs, col=colors,
216+
ylab="MHz", xaxt="n", xlab="")
217+
timeaxis(1, labels=FALSE, time.zone=badcw@time.zone)
218+
timeaxis(3, labels=TRUE, time.zone=badcw@time.zone, date.too=FALSE,
219+
xlab=FALSE)
220+
axis(side=4)
221+
mtext(title, side=3, line=1.5, cex=0.8)
188222

189223
title <- paste0(unique(colnames(pcu)), " (", unique(units(pcu)),")")
190-
image(z=pcu@data, x=tx-t1, y=allfreqs, zlim=c(0,100), col=colors, ylab="MHz", main=title, xaxt="n", xlab="")
191-
timeaxis(1, time.zone=badcw@time.zone)
224+
set_plot_margins()
225+
image(z=pcu@data, x=tx-t1, y=allfreqs, zlim=c(0,100), col=colors,
226+
ylab="MHz", xaxt="n", xlab="")
227+
timeaxis(1, labels=FALSE, time.zone=badcw@time.zone)
192228
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone)
229+
axis(side=4)
230+
mtext(title, side=3, line=0.5, cex=0.8)
193231

194232
tx <- positions(snr)
195233
t1 <- tx[1]
196234
t2 <- tx[length(tx)]
197235
tscale <- t2 - t1
198236
title <- paste0(unique(colnames(snr)), " (", unique(units(snr)),")")
199-
image(z=snr@data, x=tx - t1, y=allfreqs, col=colors, ylab="MHz", main=title, xaxt="n", xlab="")
200-
timeaxis(1, time.zone=badcw@time.zone, date.too=TRUE)
237+
set_plot_margins()
238+
image(z=snr@data, x=tx - t1, y=allfreqs, col=colors,
239+
ylab="MHz", xaxt="n", xlab="")
240+
timeaxis(1, labels=FALSE, time.zone=badcw@time.zone)
201241
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone)
242+
axis(side=4)
243+
mtext(title, side=3, line=0.5, cex=0.8)
202244

203245
title <- paste0(unique(colnames(pow)), " (", unique(units(pow)),")")
204-
image(z=pow@data, x=tx - t1, y=allfreqs, col=colors, ylab="MHz", main=title, xaxt="n", xlab="")
246+
set_plot_margins()
247+
image(z=pow@data, x=tx - t1, y=allfreqs, col=colors,
248+
ylab="MHz", xaxt="n", xlab="")
205249
timeaxis(1, time.zone=badcw@time.zone, date.too=TRUE)
206250
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone)
251+
axis(side=4)
252+
mtext(title, side=3, line=0.5, cex=0.8)
207253

208254
# par(ask=ask)
209255

@@ -218,12 +264,12 @@ plotsurf <- function(freqs=0,
218264
plot_ly(z=t(pcu@data), x=tx, y=allfreqs, type="heatmap")
219265
tx <- (as.numeric(positions(pow)) - t1) / 86400
220266
plot_ly(z=t(pow@data), x=tx, y=allfreqs, type="heatmap")
221-
plot_ly(z=t(snr@data), x=tx, y=allfreqs, type="heatmap")
267+
plot_ly(z=t(snr@data), x=tx, y=allfreqs, type="heatmap")
222268
}
223269

224270
if (alltoo) {
225271
# Plot total and error %age across all frequencies
226-
titlestr <- "All frequencies"
272+
titlestr <- NULL
227273

228274
badcw[, 1] <- apply(badcw@data, 1, function(x) { sum(x, na.rm=TRUE) })
229275
uncorr[, 1] <- apply(uncorr@data, 1, function(x) { sum(x, na.rm=TRUE) })
@@ -233,21 +279,41 @@ plotsurf <- function(freqs=0,
233279
zcw <- !is.na(badcw[,1]) & badcw[,1] == 0
234280
badcw[zcw,1] <- 0.1
235281
}
236-
plot(badcw[,1], title=titlestr,type="b",xlim=c(t1,t2),
282+
plot(badcw[,1], type="b",xlim=c(t1,t2),
237283
log=ifelse(logbad,"y",""))
284+
if (!do_dat) {
285+
timeaxis(3, time.zone=badcw@time.zone, date.too=FALSE, xlab=FALSE)
286+
axis(side=4)
287+
}
238288
if (logbad) badcw[zcw,1] <- 0
239289

240290
pcu <- uncorr[,1] / badcw[,1] * 100
241291
zcw <- !is.na(badcw[,1]) & badcw[,1] == 0.
242292
pcu[zcw,] <- 0
243293
colnames(pcu) <- "UncorrCW"
244294
units(pcu) <- "%"
245-
plot(pcu,title=titlestr,type="b",xlim=c(t1,t2))
295+
plot(pcu, type="b",xlim=c(t1,t2))
296+
if (!do_dat) {
297+
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone, date.too=FALSE)
298+
axis(side=4)
299+
}
246300

247301
snr@data[,1] <- apply(snr, 1, function(x) { mean(x, na.rm=T) })
302+
303+
plot(snr[,1], type="b", xlim=c(t1,t2))
304+
if (!do_dat) {
305+
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone, date.too=FALSE)
306+
axis(side=4)
307+
}
308+
248309
pow@data[,1] <- apply(pow, 1, function(x) { mean(x, na.rm=T) })
249310

250-
plot(snr[,1], title=titlestr, type="b", xlim=c(t1,t2))
251-
plot(pow[,1], title=titlestr, type="b", xlim=c(t1,t2))
311+
titlestr <- "All frequencies"
312+
plot(pow[,1], type="b", xlim=c(t1,t2))
313+
if (!do_dat) {
314+
timeaxis(3, labels=FALSE, time.zone=badcw@time.zone, date.too=FALSE)
315+
axis(side=4)
316+
title(main=titlestr, line=-par("cex.main"), outer=TRUE)
317+
}
252318
}
253319
}

0 commit comments

Comments
 (0)