forked from seanlhodges/WQualityStateTrend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackageDataForDelivery.R
563 lines (424 loc) · 24.4 KB
/
PackageDataForDelivery.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
# =============================================================
# PREPARE STATE, TREND & RAW DATA FOR DELIVERY TO IT EFFECT =
# =============================================================
# ======================
# Created 8-Oct-2015
# Based on the process Maree follows to prepare data, the following
# script attempts to emulate the steps for preparing data for
# delivery and outputing to csv and xlsx files.
# ======================
ANALYSIS<-"DELIVERY"
# Set working directory
od<-getwd()
setwd("//file/herman/r/oa/08/02/2017/Water Quality/R/lawa_state")
#/* -===Include required function libraries===- */
source("scripts/WQualityStateTrend/lawa_state_functions.R")
wqparam <- c("BDISC","TURB","PH","NH4","TON","TN","DRP","TP","ECOLI")
wqparam <- as.data.frame(as.matrix(wqparam))
names(wqparam)<-c("Parameter")
# ======================
# TRENDS
# ---------------------
# [2015-10-08] Load final trend file
trends<-read.csv(file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trend_sites_catchment_region.csv",stringsAsFactors = FALSE)
# ---------------------
# [2015-10-08] Remove DIN results - not required for LAWA upload - only there for River Awards & Morgan Foundation
trends <- trends[trends$Parameter!="DIN",]
trends <- trends[trends$Parameter!="PH",]
trends$X <- c(1:nrow(trends)) # updating row counter in data - only needed for debugging code
# ---------------------
# [2017-09-07] Sort by Location,Parameter, m, period and keep Max values for 5 and 10 year months (var=m)
trend_1 <- aggregate(x=trends,by=list(trends$Location,trends$Parameter,trends$period),FUN=max)[,c(4:(4+length(trends)-1))]
# ---------------------
# 10 year TRENDS
# [2015-10-08] Load data to determine frequency's by region, catchment and site and retain those data that match
StartYear <- 2007
EndYear <- 2016
StartMonth <- 1
EndMonth <- 12
load(file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trenddata2007-2016.RData")
lawadata <- samplesByInterval(StartYear,EndYear,StartMonth,EndMonth,lawadata)
# Determine counts by site/parameter/year
df_minfreq_length <- summaryBy(Value~Region+Catchment+SiteName+parameter+year,data=lawadata,
id=~LAWAID+FrequencyAll,
FUN=c(length), keep.name=TRUE)
# Determine annual medians counts by site/parameter
df_minfreq_Parameter <- summaryBy(Value~Region+Catchment+SiteName+parameter,data=df_minfreq_length,
id=~LAWAID,
FUN=c(median), keep.name=TRUE)
# # Determine annual medians counts by site
# df_minfreq_Site <- summaryBy(Value~Region+Catchment+SiteName,data=df_minfreq_length,
# id=~LAWAID,
# FUN=c(median), keep.name=TRUE)
# Determine annual medians counts by catchment
df_minfreq_Catchment <- summaryBy(Value~Region+Catchment+parameter,data=df_minfreq_length,
FUN=c(median), keep.name=TRUE)
# Determine annual medians counts by region
df_minfreq_Region <- summaryBy(Value~Region+parameter,data=df_minfreq_length,
FUN=c(median), keep.name=TRUE)
# Determining minimum frequency's
# Region
df_minfreq_Region$Frequency<-"Quarterly"
df_minfreq_Region$Frequency[df_minfreq_Region$Value==12]<-"Monthly"
# recode value here to 12 or 4 as well
df_minfreq_Region$Value[df_minfreq_Region$Value!=12]<-4
df_minfreq_Region <- df_minfreq_Region[is.na(df_minfreq_Region$Region)==FALSE,]
n <- names(df_minfreq_Region)
n[1]<-"Location"
names(df_minfreq_Region)<-n
df_minfreq_Region$Region<-df_minfreq_Region$Location
df_minfreq_Region$Catchment<-""
df_minfreq_Region$SiteName<-""
# Cross-join regions with parameters to have each parameter for each region
#df_minfreq_Region <- merge(x = df_minfreq_Region, y = wqparam, by = NULL)
# Catchment
df_minfreq_Catchment$Frequency<-"Quarterly"
df_minfreq_Catchment$Frequency[df_minfreq_Catchment$Value>=10]<-"Monthly"
# recode value here to 12 or 4 as well
df_minfreq_Catchment$Value[df_minfreq_Catchment$Value>=10]<-12
df_minfreq_Catchment$Value[df_minfreq_Catchment$Value<10]<-4
df_minfreq_Catchment <- df_minfreq_Catchment[is.na(df_minfreq_Catchment$Region)==FALSE,]
n <- names(df_minfreq_Catchment)
n[2] <-"Location"
names(df_minfreq_Catchment)<-n
df_minfreq_Catchment$Catchment<-""
df_minfreq_Catchment$SiteName<-""
# Cross-join regions with parameters to have each parameter for each region
#df_minfreq_Catchment <- merge(x = df_minfreq_Catchment, y = wqparam, by = NULL)
# Reordering columns
df_minfreq_Catchment <- df_minfreq_Catchment[,c(2,3,4,1,5,6,7)]
# Converting location column to character
df_minfreq_Catchment$Location <- as.character(df_minfreq_Catchment$Location)
# # Parameter
# df_minfreq_Parameter$Frequency<-"Quarterly"
# df_minfreq_Parameter$Frequency[df_minfreq_Parameter$Value>=10]<-"Monthly"
# # recode value here to 12 or 4 as well
# df_minfreq_Parameter$Value[df_minfreq_Parameter$Value>=10]<-12
# df_minfreq_Parameter$Value[df_minfreq_Parameter$Value<10]<-4
#
# Parameter
df_minfreq_Parameter$Frequency<-"Quarterly"
df_minfreq_Parameter$Frequency[df_minfreq_Parameter$Value>=12]<-"Monthly"
# recode value here to 12 or 4 as well
df_minfreq_Parameter$Value[df_minfreq_Parameter$Value>=12]<-12
df_minfreq_Parameter$Value[df_minfreq_Parameter$Value<12]<-4
df_minfreq_Parameter <- df_minfreq_Parameter[is.na(df_minfreq_Parameter$Region)==FALSE,]
n <- names(df_minfreq_Parameter)
n[4]<-"parameter"
n[6]<-"Location"
names(df_minfreq_Parameter) <- n
# [2015-10-14] Reordering columns
df_minfreq_Parameter <- df_minfreq_Parameter[,c(6,5,7,1:4)]
df_minfreq_Parameter <- df_minfreq_Parameter[,c(1,7,2,3:6)]
df_minfreq_Catchment <- df_minfreq_Catchment[,c(1:3,5,4,6:7)]
# [2015-10-14] Binding tables together in order to join results to TRENDS table
df_minfreq <- rbind.data.frame(df_minfreq_Region,df_minfreq_Catchment,df_minfreq_Parameter)
n <- names(df_minfreq)
n[2]<-"Parameter"
names(df_minfreq) <- n
#issue with join below
tmp <- merge(trend_1,df_minfreq,by=c("Location","Parameter"),all.x=TRUE)
# [2015-10-14] Joining data to select min frequency matches
#tmp<-dplyr::left_join(trends,df_minfreq,by=c("Location","Parameter"))
#tmp1<-tmp[tmp$m==tmp$Value,]
tmp1<-tmp[,c(1:6,8:16)]
# ---------------------
# [2015-10-14] Drop columns
# Columns to keep
# Location Parameter Altitude Landuse TrendScore Frequency Region period
# ARC-00014 DRP Lowland Urban -2 Monthly Auckland 5
# ARC-00014 ECOLI Lowland Urban 0 Monthly Auckland 5
# ARC-00014 TN Lowland Urban 0 Monthly Auckland 5
tmp1<-tmp1[,c(1,2,7,8,4,10,9,6)]
names(tmp1) <-c("Location", "Parameter", "Altitude", "Landuse", "TrendScore", "Frequency", "Region", "period")
# remove any NA rows
tmp1 <- tmp1[!is.na(tmp1$Location),]
tmp1 <- tmp1[tmp1$period==10,]
# ---------------------
# 5 year TRENDS
# [2015-10-08] Load data to determine frequency's by region, catchment and site and retain those data that match
StartYear <- 2012
EndYear <- 2016
StartMonth <- 1
EndMonth <- 12
load(file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trenddata2012-2016.RData")
lawadata <- samplesByInterval(StartYear,EndYear,StartMonth,EndMonth,lawadata)
# Determine counts by site/parameter/year
df_minfreq_length <- summaryBy(Value~Region+Catchment+SiteName+parameter+year,data=lawadata,
id=~LAWAID+FrequencyAll,
FUN=c(length), keep.name=TRUE)
# Determine annual medians counts by site/parameter
df_minfreq_Parameter <- summaryBy(Value~Region+Catchment+SiteName+parameter,data=df_minfreq_length,
id=~LAWAID,
FUN=c(median), keep.name=TRUE)
# # Determine annual medians counts by site
# df_minfreq_Site <- summaryBy(Value~Region+Catchment+SiteName,data=df_minfreq_length,
# id=~LAWAID,
# FUN=c(median), keep.name=TRUE)
# Determine annual medians counts by catchment
df_minfreq_Catchment <- summaryBy(Value~Region+Catchment+parameter,data=df_minfreq_length,
FUN=c(median), keep.name=TRUE)
# Determine annual medians counts by region
df_minfreq_Region <- summaryBy(Value~Region+parameter,data=df_minfreq_length,
FUN=c(median), keep.name=TRUE)
# Determining minimum frequency's
# Region
df_minfreq_Region$Frequency<-"Quarterly"
df_minfreq_Region$Frequency[df_minfreq_Region$Value==12]<-"Monthly"
# recode value here to 12 or 4 as well
df_minfreq_Region$Value[df_minfreq_Region$Value!=12]<-4
df_minfreq_Region <- df_minfreq_Region[is.na(df_minfreq_Region$Region)==FALSE,]
n <- names(df_minfreq_Region)
n[1]<-"Location"
names(df_minfreq_Region)<-n
df_minfreq_Region$Region<-df_minfreq_Region$Location
df_minfreq_Region$Catchment<-""
df_minfreq_Region$SiteName<-""
# Cross-join regions with parameters to have each parameter for each region
#df_minfreq_Region <- merge(x = df_minfreq_Region, y = wqparam, by = NULL)
# Catchment
df_minfreq_Catchment$Frequency<-"Quarterly"
df_minfreq_Catchment$Frequency[df_minfreq_Catchment$Value>=10]<-"Monthly"
# recode value here to 12 or 4 as well
df_minfreq_Catchment$Value[df_minfreq_Catchment$Value>=10]<-12
df_minfreq_Catchment$Value[df_minfreq_Catchment$Value<10]<-4
df_minfreq_Catchment <- df_minfreq_Catchment[is.na(df_minfreq_Catchment$Region)==FALSE,]
n <- names(df_minfreq_Catchment)
n[2] <-"Location"
names(df_minfreq_Catchment)<-n
df_minfreq_Catchment$Catchment<-""
df_minfreq_Catchment$SiteName<-""
# Cross-join regions with parameters to have each parameter for each region
#df_minfreq_Catchment <- merge(x = df_minfreq_Catchment, y = wqparam, by = NULL)
# Reordering columns
df_minfreq_Catchment <- df_minfreq_Catchment[,c(2,3,4,1,5,6,7)]
# Converting location column to character
df_minfreq_Catchment$Location <- as.character(df_minfreq_Catchment$Location)
# # Parameter
# df_minfreq_Parameter$Frequency<-"Quarterly"
# df_minfreq_Parameter$Frequency[df_minfreq_Parameter$Value>=10]<-"Monthly"
# # recode value here to 12 or 4 as well
# df_minfreq_Parameter$Value[df_minfreq_Parameter$Value>=10]<-12
# df_minfreq_Parameter$Value[df_minfreq_Parameter$Value<10]<-4
# Parameter - this section updated 2017-09-04 to be more restrictive - changing criteria from 10 to 12.
# Justification: Site/parameter combos need 60 samples over 5 years - setting value to 10 results in
# having too many sites excluded as they do not meeting this criteria when 10 or 11 samples per year.
df_minfreq_Parameter$Frequency<-"Quarterly"
df_minfreq_Parameter$Frequency[df_minfreq_Parameter$Value>=12]<-"Monthly"
# recode value here to 12 or 4 as well
df_minfreq_Parameter$Value[df_minfreq_Parameter$Value>=12]<-12
df_minfreq_Parameter$Value[df_minfreq_Parameter$Value<12]<-4
df_minfreq_Parameter <- df_minfreq_Parameter[is.na(df_minfreq_Parameter$Region)==FALSE,]
n <- names(df_minfreq_Parameter)
n[4]<-"parameter"
n[6]<-"Location"
names(df_minfreq_Parameter) <- n
# [2015-10-14] Reordering columns
df_minfreq_Parameter <- df_minfreq_Parameter[,c(6,5,7,1:4)]
df_minfreq_Parameter <- df_minfreq_Parameter[,c(1,7,2,3:6)]
df_minfreq_Catchment <- df_minfreq_Catchment[,c(1:3,5,4,6:7)]
# [2015-10-14] Binding tables together in order to join results to TRENDS table
df_minfreq <- rbind.data.frame(df_minfreq_Region,df_minfreq_Catchment,df_minfreq_Parameter)
n <- names(df_minfreq)
n[2]<-"Parameter"
names(df_minfreq) <- n
#issue with join below
tmp <- merge(trend_1,df_minfreq,by=c("Location","Parameter"),all.x=TRUE)
# [2015-10-14] Joining data to select min frequency matches
#tmp<-dplyr::left_join(trends,df_minfreq,by=c("Location","Parameter"))
#tmp2<-tmp[tmp$m==tmp$Value,]
tmp2<-tmp[,c(1:6,8:16)]
# ---------------------
# [2015-10-14] Drop columns
# Columns to keep
# Location Parameter Altitude Landuse TrendScore Frequency Region period
# ARC-00014 DRP Lowland Urban -2 Monthly Auckland 5
# ARC-00014 ECOLI Lowland Urban 0 Monthly Auckland 5
# ARC-00014 TN Lowland Urban 0 Monthly Auckland 5
tmp2<-tmp2[,c(1,2,7,8,4,10,9,6)]
names(tmp2) <-c("Location", "Parameter", "Altitude", "Landuse", "TrendScore", "Frequency", "Region", "period")
# remove any NA rows
tmp2 <- tmp2[!is.na(tmp2$Location),]
tmp2 <- tmp2[tmp2$period==5,]
tmp0 <- rbind.data.frame(tmp1,tmp2)
# ---------------------
# Export to XL workbook
write.csv(tmp0,file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trend_fordelivery.csv") ##
# ---------------------
# Housekeeping
rm(trends,trend_1,tmp,tmp1,df_minfreq,df_minfreq_Region,df_minfreq_Parameter,df_minfreq_Catchment,df_minfreq_length,lawadata,n,wqparam)
# ======================
# STATE
# ---------------------
# 1. Load final state file
state<-read.csv(file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/LAWA_STATE_FINAL_2012-2016.csv",stringsAsFactors = FALSE)
# 2. join the agency information back to the LAWAIDs.
# Reload the site table [should still be loaded if all scripts run together, but in case this script is run in isolation, this will ensure you get the
# the last-saved site table back]
load(file="//file/herman/r/oa/08/02/2017/Water Quality/ROutput/lawa_sitetable.RData")
# Fields have been renamed from WFS feed to match what the code is
# expecting, as code originally written expecting data from Hilltop Site Table.
newFieldNames <- c("X","LAWAID","ID", "CouncilSiteID","UsedInLAWA","AltitudeGroup","LanduseGroup","FrequencyAll","Frequency",
"Region","Agency","ISLAND","CatchID","CatchType",
"NZREACH","Catchment","Comments","LawaCatchm","CatchLbl")
names(l) <- newFieldNames
# keep LAWAID and Agency
la<-l[,c(2,11)]
state <- merge(state, la, by.x="Location",by.y="LAWAID", all.x=TRUE) # Using Hilltop sitenames to match site information
#3. Combinations to keep
state$Filter<-FALSE
## Region combos
# All All Region|All
state$Filter[state$StateGroup=="Region|All" & state$AltitudeGroup=="All" & state$LanduseGroup=="All"] <- TRUE
# Upland All Region|Upland
state$Filter[state$StateGroup=="Region|Upland" & state$AltitudeGroup=="Upland" & state$LanduseGroup=="All"] <- TRUE
# Lowland All Region|Lowland
state$Filter[state$StateGroup=="Region|Lowland" & state$AltitudeGroup=="Lowland" & state$LanduseGroup=="All"] <- TRUE
# All Rural Region|Rural
state$Filter[state$StateGroup=="Region|Rural" & state$AltitudeGroup=="All" & state$LanduseGroup=="Rural"] <- TRUE
# All Forest Region|Forest
state$Filter[state$StateGroup=="Region|Forest" & state$AltitudeGroup=="All" & state$LanduseGroup=="Forest"] <- TRUE
# All Urban Region|Urban
state$Filter[state$StateGroup=="Region|Urban" & state$AltitudeGroup=="All" & state$LanduseGroup=="Urban"] <- TRUE
# Upland Rural Region|Upland|Rural
state$Filter[state$StateGroup==" Region|Upland|Rural" ] <- TRUE
# Upland Forest Region|Upland|Forest
state$Filter[state$StateGroup=="Region|Upland|Forest"] <- TRUE
# Lowland Rural Region|Lowland|Rural
state$Filter[state$StateGroup=="Region|Lowland|Rural"] <- TRUE
# Lowland Forest Region|Lowland|Forest
state$Filter[state$StateGroup=="Region|Lowland|Forest"] <- TRUE
# Lowland Urban Region|Lowland|Urban
state$Filter[state$StateGroup=="Region|Lowland|Urban"] <- TRUE
## Catchment combos
# All All Catchment|All
state$Filter[state$StateGroup=="Catchment|All" & state$AltitudeGroup=="All" & state$LanduseGroup=="All"] <- TRUE
# Upland All Catchment|Upland
state$Filter[state$StateGroup=="Catchment|Upland" & state$AltitudeGroup=="Upland" & state$LanduseGroup=="All"] <- TRUE
# Lowland All Catchment|Lowland
state$Filter[state$StateGroup=="Catchment|Lowland" & state$AltitudeGroup=="Lowland" & state$LanduseGroup=="All"] <- TRUE
# All Rural Catchment|Rural
state$Filter[state$StateGroup=="Catchment|Rural" & state$AltitudeGroup=="All" & state$LanduseGroup=="Rural"] <- TRUE
# All Forest Catchment|Forest
state$Filter[state$StateGroup=="Catchment|Forest" & state$AltitudeGroup=="All" & state$LanduseGroup=="Forest"] <- TRUE
# All Urban Catchment|Urban
state$Filter[state$StateGroup=="Catchment|Urban" & state$AltitudeGroup=="All" & state$LanduseGroup=="Urban"] <- TRUE
# Upland Rural Catchment|Upland|Rural
state$Filter[state$StateGroup==" Catchment|Upland|Rural" ] <- TRUE
# Upland Forest Catchment|Upland|Forest
state$Filter[state$StateGroup=="Catchment|Upland|Forest"] <- TRUE
# Upland Urban Catchment|Upland|Urban
#state$Filter[state$StateGroup=="Catchment|Upland|Urban"] EXCLUDED AS ONLY ONE SITE
# Lowland Rural Catchment|Lowland|Rural
state$Filter[state$StateGroup=="Catchment|Lowland|Rural"] <- TRUE
# Lowland Forest Catchment|Lowland|Forest
state$Filter[state$StateGroup=="Catchment|Lowland|Forest"] <- TRUE
# Lowland Urban Catchment|Lowland|Urban
state$Filter[state$StateGroup=="Catchment|Lowland|Urban"] <- TRUE
## Site combos
state$AltitudeGroup[state$StateGroup=="Site|All"] <- "All"
state$LanduseGroup[state$StateGroup=="Site|All"] <- "All"
state$Filter[state$StateGroup=="Site|All"] <- TRUE
state$LanduseGroup[state$StateGroup=="Site|Lowland"] <- "All"
state$Filter[state$StateGroup=="Site|Lowland"] <- TRUE
state$LanduseGroup[state$StateGroup=="Site|Upland"] <- "All"
state$Filter[state$StateGroup=="Site|Upland"] <- TRUE
state$AltitudeGroup[state$StateGroup=="Site|Rural"] <- "All"
state$Filter[state$StateGroup=="Site|Rural"] <- TRUE
state$AltitudeGroup[state$StateGroup=="Site|Forest"] <- "All"
state$Filter[state$StateGroup=="Site|Forest"] <- TRUE
state$AltitudeGroup[state$StateGroup=="Site|Urban"] <- "All"
state$Filter[state$StateGroup=="Site|Urban"] <- TRUE
state$Filter[state$StateGroup=="Site|Upland|Forest"] <- TRUE
state$Filter[state$StateGroup=="Site|Upland|Rural"] <- TRUE
# state$Filter[state$StateGroup=="Site|Upland|Urban"] EXCLUDED AS ONLY ONE SITE
state$Filter[state$StateGroup=="Site|Lowland|Forest"] <- TRUE
state$Filter[state$StateGroup=="Site|Lowland|Rural"] <- TRUE
state$Filter[state$StateGroup=="Site|Lowland|Urban"] <- TRUE
## 4. Remove Upland|Urban combination as only one site contributes
p<-grep(pattern = "Upland|Urban",x = state$StateGroup,fixed=TRUE)
#state<-state[-p,]
## 5. Round values to appropriate DP's
# Note that for rounding off a 5, the IEC 60559 standard is expected to be used, ‘go to the even digit’. Therefore round(0.5) is 0 and round(-1.5) is -2
# This is not the desired behaviour here. It is expected that 0.5 rounds to 1, 2.5 rounds, to 3 and so on.
# Therefore for all even values followed by exactly .5 needs to have a small number added (like 0.00001) to get them rounding in the right direction (unless there is
# a flag in the function that provides for this behaviour), or to redefine the round function.
# (see http://theobligatescientist.blogspot.co.nz/2010/02/r-i-still-love-you-but-i-hate-your.html)
# As all values are positive, we'll add a small number, related to the degree of rounding required.
# If I was smarter, I would redefine the round function
# Rounding to 4 digits
state$Q50 <- state$Q50 + 0.000001
state$Q50 <- round(state$Q50,digits = 4)
# Rounding to 2 digits
state$Q50[state$Parameter=="BDISC" | state$Parameter=="PH" | state$Parameter=="TURB"] <- state$Q50[state$Parameter=="BDISC" | state$Parameter=="PH" | state$Parameter=="TURB"] + 0.0001
state$Q50[state$Parameter=="BDISC" | state$Parameter=="PH" | state$Parameter=="TURB"] <- round(state$Q50[state$Parameter=="BDISC" | state$Parameter=="PH" | state$Parameter=="TURB"],2)
# Rounding to 0 digits
state$Q50[state$Parameter=="ECOLI"] <- state$Q50[state$Parameter=="ECOLI"] + 0.01
state$Q50[state$Parameter=="ECOLI"] <- round(state$Q50[state$Parameter=="ECOLI"],0)
## 6. Detection limit correction
# Load detection limit correction data - this should be updated each year
dl<-read.csv(file="//file/herman/r/oa/08/02/2017/Water Quality/R/lawa_state/AgencyDLs.csv",stringsAsFactors = FALSE)
# Removing NA rows i.e. rows without detection limit values
dl<-dl[!is.na(dl$Value),]
#converting Q50 to text to allow less than strings to be added
state$Q50 <- as.character(state$Q50)
for(i in 1:nrow(dl)){
state$Q50[state$Agency==dl$Agency[i] & state$Parameter==dl$Parameter[i] & as.numeric(state$Q50)<dl$Value[i]] <- dl$DL[i]
}
## 7. Tidy up
# Filtering dataset by state$Filter
state <- state[state$Filter==TRUE,]
# Dropping original row counter and agency field
state<-state[,c(1,3:10)]
# Renaming Median column
c<-names(state)
c[5]<-"Median"
names(state)<-c
# ---------------------
# Export to XL workbook
write.csv(state,file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/state_fordelivery.csv",row.names = FALSE) ##
# ---------------------
# Housekeeping
#rm(state)
# ======================
# RAW DATA
# Load 10 year data pull - data.frame "lawadata"
# This is the 10 year data set with any censoring applied to values
load("//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trenddata2007-2016.RData",verbose=TRUE)
raw<-lawadata[,c(1,14,13,5,2,3,4,7,21,20,16,17)]
# [1] "SiteName" "LAWAID" "parameter" "Date" "Value" "Method" "CenType
# [8] "Agency" "Region" "AltitudeGroup" "LanduseGroup"
#rm(lawadata)
raw<-raw[!is.na(raw$LAWAID),]
#head(raw)
names(raw)
raw$Symbol<-""
raw$Symbol[raw$CenType=="Left"]<-"<"
raw$Symbol[raw$CenType=="Right"]<-">"
raw$RawValue <- paste(raw$Symbol,raw$Value,sep="")
raw$QC <- ""
raw$Agency[grepl(pattern = "^NIWA",x=raw$Agency )] <- "NIWA"
raw$Agency[grepl(pattern = "^Christchurch",x=raw$Agency )] <- "Christchurch"
raw$License[!(raw$Agency=="NIWA" | raw$Agency=="Christchurch")] <- "CC BY 4.0"
raw$Ownership[!(raw$Agency=="NIWA" | raw$Agency=="Christchurch")] <- "The Regional Council or Unitary Authority specified under the agency attribute"
raw$Disclaimer[!(raw$Agency=="Canterbury" | raw$Agency=="Horizons" | raw$Agency=="Waikato")] <- "LAWA Partners shall not be liable, whether in contract, tort, equity or otherwise, for any loss or damage of any type (including consequential losses) arising directly or indirectly from the inadequacy, inaccuracy or any other deficiency in information supplied irrespective of the cause. Use of information supplied is entirely at the risk of the recipient and shall be deemed to be acceptance of this liability exclusion."
raw$Disclaimer[raw$Agency=="Canterbury"] <- "http://data.ecan.govt.nz/Catalogue/Agreement"
raw$Disclaimer[raw$Agency=="Horizons"] <- "The enclosed information is supplied, within the framework of the Manawatu-Wanganui Regional Councils quality system, from the best data currently available.
However, as we endeavour to continuously improve our products, we reserve the right to amend the data on which this information is based, where necessary and without notice, at any time."
raw$Disclaimer[raw$Agency=="Waikato"] <- "Waikato Regional Council provides this information in good faith and has exercised all reasonable skill and care in controlling the content of this information, and accepts no liability in contract, tort or otherwise, for any loss, damage, injury or expense (whether direct, indirect or consequential) arising out of the provision of this information or its use by you."
raw<-raw[,c(1:7,9:18)]
c <- c("Site", "SiteName","LAWAID", "Measurement", "DateTime", "Original.Value", "Method", "Agency", "Region", "Altitude", "Landuse", "Symbol","Raw.Value","QC", "License", "Ownership", "Disclaimer")
names(raw) <- c
# c <- c("Site", "Measurement", "DateTime", "Date", "Raw Value", "Symbol", "Value", "Method", "QC",
# "LAWAID", "Region", "Landuse", "Altitude", "WSGS84_X", "WSGS84_Y", "Catchment", "CatchmentNameLAWA", "Agency",
# "License", "Ownership", "Disclaimer")
# Rearrange columns
#raw<-raw[,c(1:4,18,17,6,19,8:16,20:22)]
# +++ WITHOUT NIWA / CCC
raw_sans <- raw[!(grepl("^CCC",x = raw$LAWAID,perl = TRUE) | grepl("^NRWQN",x = raw$LAWAID,perl = TRUE)) ,]
# ---------------------
# Export to XL workbook
write.csv(raw,file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/rawdata.csv",row.names = FALSE) ##
write.csv(raw_sans,file="//file/herman/R/OA/08/02/2017/Water Quality/ROutput/rawdata_minus_niwa_ccc.csv",row.names = FALSE) ## rename to rawdata_download
rm(raw,raw_sans)
setwd(od)