forked from seanlhodges/WQualityStateTrend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackageDataForDelivery - NAs added in for trend.R
89 lines (65 loc) · 3.19 KB
/
PackageDataForDelivery - NAs added in for trend.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
#requestData(vendor[tss],tss_url,"service=Hilltop&request=Reset")
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")
# build template file of all possible results for Trend - Sites x Parameter x Five/Ten Yr
# specify a matrix of appropriate size
# building blank data file
if(!exists("templateSeason")){
library(readr)
seasons <- read_csv("//file/herman/R/OA/08/02/2017/Water Quality/R/lawa_state/seasons.csv")
for(j in 1:nrow(l)){
for(i in 1:length(wqparam)){
if(!exists("templateSeason")){
templateSeason <- seasons
templateSeason$Parameter <- wqparam[i]
templateSeason$LawaSiteID <- l$LawaSiteID[j]
} else {
tmpSeason <- seasons
tmpSeason$Parameter <- wqparam[i]
tmpSeason$LawaSiteID <- l$LawaSiteID[j]
templateSeason <- rbind(templateSeason,tmpSeason)
}
}
}
rm(tmpSeason)
save(templateSeason,file = "templateSeason.Rda")
}
#build blank trend results file
trend_fordelivery <- read.csv("//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trend_fordelivery.csv")
trend_fordelivery <- trend_fordelivery[,c(2:length(trend_fordelivery))]
#/* -===Local variable/constant definitions===- */
l <- read.csv("LAWA_Site_Table1.csv",stringsAsFactors=FALSE)
l$SWQLanduse[l$SWQLanduse=="Native"|l$SWQLanduse=="Exotic"|l$SWQLanduse=="Natural"] <- "Forest"
wqparam <- c("BDISC","TURB","NH4","TON","TN","DRP","TP","ECOLI")
landuse <- unique(l$SWQLanduse)
altitude <- unique(l$SWQAltitude)
blank.trend.rows <- nrow(l)*length(wqparam)*2
data.blank <- matrix(data=NA, nrow=blank.trend.rows, ncol=length(trend_fordelivery), byrow=TRUE)
counter<-1
for(i in 1:nrow(l)){
for(j in 1:length(wqparam)){
#for(k in 1:length(landuse)){
#for(m in 1:length(altitude)){
data.blank[counter,] <- c(l$LawaSiteID[i],wqparam[j],l$SWQLanduse[i],l$SWQAltitude[i],NA,l$SWQFrequencyLast5[i],l$Region[i],5)
counter<-counter+1
data.blank[counter,] <- c(l$LawaSiteID[i],wqparam[j],l$SWQLanduse[i],l$SWQAltitude[i],NA,l$SWQFrequencyAll[i],l$Region[i],10)
counter<-counter+1
#}
#}
}
}
data.blank <- as.data.frame(data.blank)
names(data.blank) <- c("Location","Parameter","Altitude","Landuse","TrendScore","Frequency","Region","period")
tmp <- merge(data.blank,trend_fordelivery,by=c("Location","Parameter","Altitude","Landuse","period"),all=TRUE)
tmp$Frequency.x[complete.cases(tmp$Frequency.y)] <- tmp$Frequency.y[complete.cases(tmp$Frequency.y)]
tmp$Region.x[complete.cases(tmp$Region.y)] <- tmp$Region.y[complete.cases(tmp$Region.y)]
tmp <- tmp[,c(1:4,9,7,8,5)]
names(tmp) <- c("Location","Parameter","Altitude","Landuse","TrendScore","Frequency","Region","period")
tmp$TrendScore <- as.character(tmp$TrendScore)
tmp$TrendScore[is.na(tmp$TrendScore)] <- "NA"
trend_fordelivery <- write.csv(tmp, "//file/herman/R/OA/08/02/2017/Water Quality/ROutput/trend_fordelivery_with_NA.csv")