-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraster_qa.R
More file actions
156 lines (107 loc) · 4.65 KB
/
Copy pathraster_qa.R
File metadata and controls
156 lines (107 loc) · 4.65 KB
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
#'
#'
#' Raster plot with WSC quality flags. This produces a plot showing the flow data in grayscale
#' overlain by the Water Survey of Canada quality flags. Colours are consistent with
#' ECDataExplorer. Raster layout lets the use see the flags in a different context than in
#' a hydrograph.
#'
#' @description {
#' Produces a raster plot: years against day of year, showing all the data flags
#' A (Partial) green
#' B (Below Ice) blue
#' D (Dry) yellow
#' E (Estimated) red
#'}
#' @param dframe - a datafile from water survey read by read.wsc()
#' @author Paul Whitfield <paul.h.whitfield@gmail.com>
#' Produces a gray scale plot of observations with colour symbol showing where data has a quality flag
#'
#' @export
#'
#'
#' @examples
#' data(W05AA008)
#' qaplot <-raster_qa(W05AA008)
#'
raster_qa <- function( dframe) {
##### Fixed labels and text strings
DOY <- "Day of Year"
ylabelq=expression(paste("Discharge m" ^{3}, "/sec"))
rastercolours=c("gray90","gray80","gray70","gray60", "gray50", "gray40", "gray30", "gray20", "gray10")
qcols=grDevices::colorRampPalette(rastercolours)
fcols <-c("black","red","green","blue","cyan","magenta","yellow","gray")
station <- as.character(dframe$ID[1])
sname<- get_wscstation(station)
title=sname$Station_lname
date <-as.Date(dframe$Date,"%Y/%m/%d")
Year <- as.numeric(format(date,"%Y"))
doy <- as.numeric(timeDate::dayOfYear(timeDate::as.timeDate(date)))
mYear <-max(Year, na.rm=TRUE)
nYear <-min(Year, na.rm=TRUE)-1
Years <-mYear-nYear
qdata <- array(dim=c(366, Years))
flag <- array(dim=c(366, Years))
dframe$SYM <-as.character(dframe$SYM) ############# change flag codes to colour codes
dframe$SYM[dframe$SYM=="A"] <-3
dframe$SYM[dframe$SYM=="B"] <-5
dframe$SYM[dframe$SYM=="D"] <-7
dframe$SYM[dframe$SYM=="E"] <-2
dframe$SYM <-as.numeric(dframe$SYM) ##### convert to numbers for plotting colour
for (k in 1:length(dframe[,1])) {
qdata[doy[k],(Year[k]-nYear)] <- dframe$Flow[k]
flag[doy[k], (Year[k]-nYear)] <- dframe$SYM[k]
# 1 was no flag, 2 was "A", 3 was "B", 4 was "C", 5 was "D", 6 was "E"
}
flag[flag==1] <-NA
qmax <-max(qdata, na.rm=TRUE)
qmin <-min(qdata, na.rm=TRUE)
################################ raster map of daily flows
qdata <-as.matrix(qdata)
########################################################### start plotting section
graphics::par(oma=c(0,0,3,0))
graphics::layout(matrix(c(1,1,1,1,2,1,1,1,1,3), 2, 5, byrow = TRUE))
graphics::par(mar=c(4,4,1,1))
################################################################# panel one
doys <-c(1:366)
lyears <- c((nYear+1):mYear)
graphics::image(1:366,1:length(lyears), qdata, axes=FALSE, col=qcols(9),
zlim=c(qmin,qmax), xlab="", ylab="")
sdoy <- sub_set_Years(doys,15)
graphics::axis(1, at=sdoy$position,labels=sdoy$label, cex=1.2)
if(length(lyears)>=70) nn<-10 else nn<-5
sYears <- sub_set_Years(lyears,nn)
graphics::axis(2, at=sYears$position,labels=sYears$label, cex.axis=1.2, las=1)
graphics::mtext(DOY,side=1, line =2.2, cex=0.9)
for( ii in 1:366) {
for( jj in 1:length(lyears)){
graphics::points(ii,jj,pch=15,col=flag[ii,jj])
}
}
graphics::box()
################################################################# panel two
graphics::frame()
graphics::par(mar=c(2,2,2,2))
######### scale bar and legend
fields::image.plot(zlim= c(qmin,qmax), col=qcols(9), legend.only=TRUE,
legend.width=4, legend.mar=1,
legend.shrink=1.0,
bigplot=c(0.1,0.2,0.1,0.2),
legend.args=list(text=ylabelq, side=2,line=0.5, cex=0.90))
################################################################# panel three (element #ten)
graphics::frame()
graphics::frame()
graphics::frame()
graphics::frame()
graphics::frame()
graphics::par(mar=c(1,0,0,1))
leg.txt <-c(" (A) Partial", " (B) Ice", " (D) Dry"," (E) Estimate")
lcol <-c("green","cyan","yellow", "red")
graphics::legend("left", leg.txt, pch=c(22,22,22,22), pt.bg=lcol, cex=1.25, bty='n')
############################################################## Add title
tscale=1.7
if(nchar(title)>=45) tscale=1.5
if(nchar(title)>=50) tscale=1.2
graphics::mtext(title, side=3, line=0, cex=tscale,outer=TRUE)
############################################################### end plotting section
return()
}