-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-compilation.R
151 lines (120 loc) · 5.77 KB
/
data-compilation.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
###############################################################################
#
# Data compilation
# for Shiny App
#
###############################################################################
library(tools) # for toTitleCase
#------------------------------------------------------------------------------
# Read in data
#------------------------------------------------------------------------------
nuseds <- read.csv("data/NuSEDS_20210513.csv")
# Fix Nahlin Chinook POP_ID
nuseds$POP_ID[which(nuseds$POP_ID == 45165)] <- 45164
#------------------------------------------------------------------------------
# Transboundary Streams
#------------------------------------------------------------------------------
# This is from Gottfried's Lookup Table: https://github.com/SOLV-Code/Transboundary_Data_Report/blob/main/DATA_PROCESSING/DATA/0_LookupFiles/TBR_PSF_CU_Site_LookupFile.csv
tbr <- read.csv("data/TBR_PSF_CU_Site_LookupFile.csv")
tbr$POP_ID[tbr$POP_ID == 45165] <- 45164
# Subset NuSEDS data to only include Central Coast streams
nuseds <- nuseds[which(nuseds$POP_ID %in% tbr$POP_ID & !is.na(nuseds$MAX_ESTIMATE)), ]
#------------------------------------------------------------------------------
# Create dataframe for use in app
#------------------------------------------------------------------------------
# Combine even and odd year pink
n <- length(unique(tbr$POP_ID))
tbrDat <- data.frame(
POP_ID = unique(tbr$POP_ID)
)
popInd <- numeric(n)
for(i in 1:n){
popInd[i] <- which(tbr$POP_ID == tbrDat$POP_ID[i])[1]
}
#------------------------------------------------------------------------------
# Species (n = 5)
#------------------------------------------------------------------------------
tbrDat$Species <- tbr$SPECIES_QUALIFIED[popInd]
tbrDat$Species[tbrDat$Species == "PKE"] <- "Pink"
tbrDat$Species[tbrDat$Species == "CO"] <- "Coho"
tbrDat$Species[tbrDat$Species == "CK"] <- "Chinook"
tbrDat$Species[tbrDat$Species == "SEL"] <- "Sockeye"
tbrDat$Species[tbrDat$Species == "SER"] <- "Sockeye"
#------------------------------------------------------------------------------
# Stream Name - make simple case
#------------------------------------------------------------------------------
tbrDat$streamName <- sapply(sapply(tbr$SYSTEM_SITE[popInd], tolower), toTitleCase)
#------------------------------------------------------------------------------
# Match watersheds
#------------------------------------------------------------------------------
wshd <- read.csv("data/TBR_watershed_LookupFile.csv")
tbrDat$watershed <- wshd$watershed[match(tbrDat$POP_ID, wshd$POP_ID)]
#------------------------------------------------------------------------------
# Location
#------------------------------------------------------------------------------
tbrDat$Latitude <- tbr$Y_LAT[popInd]
tbrDat$Longitude <- tbr$X_LONGT[popInd]
#------------------------------------------------------------------------------
# Average abundance
#------------------------------------------------------------------------------
tbrDat$avgSpawners <- NA
for(i in 1:n){
if(tbrDat$POP_ID[i] > 0){
tbrDat$avgSpawners[i] <- mean(nuseds$MAX_ESTIMATE[nuseds$POP_ID == tbrDat$POP_ID[i]])
}
}
#------------------------------------------------------------------------------
# Number of estimates
#------------------------------------------------------------------------------
tbrDat$nYears <- rep(0, n)
for(i in 1:n){
if(tbrDat$POP_ID[i] > 0){
tbrDat$nYears[i] <- sum(!is.na(nuseds$MAX_ESTIMATE[nuseds$POP_ID == tbrDat$POP_ID[i]]))
}
}
#------------------------------------------------------------------------------
# Survey quality
#------------------------------------------------------------------------------
# Clean up Estimate Classification field of nuseds
nuseds$ESTIMATE_CLASSIFICATION[nuseds$ESTIMATE_CLASSIFICATION == "PRESENCE/ABSENCE (TYPE-6)"] <- "PRESENCE-ABSENCE (TYPE-6)"
nuseds$ESTIMATE_CLASSIFICATION[nuseds$ESTIMATE_CLASSIFICATION =="NO SURVEY THIS YEAR"] <- "NO SURVEY"
nuseds$ESTIMATE_CLASSIFICATION[nuseds$ESTIMATE_CLASSIFICATION ==""] <- "UNKNOWN"
# Re-level ESTIMATE_CLASSIFICATION for easy numeric calculations
nuseds$ESTIMATE_CLASSIFICATION <- factor(
nuseds$ESTIMATE_CLASSIFICATION,
levels = c(
"TRUE ABUNDANCE (TYPE-1)",
"TRUE ABUNDANCE (TYPE-2)",
"RELATIVE ABUNDANCE (TYPE-3)",
"RELATIVE ABUNDANCE (TYPE-4)",
"RELATIVE ABUNDANCE (TYPE-5)",
"PRESENCE-ABSENCE (TYPE-6)",
"RELATIVE: CONSTANT MULTI-YEAR METHODS",
"RELATIVE: VARYING MULTI-YEAR METHODS",
"NO SURVEY",
"UNKNOWN"))
nuseds$ESTIMATE_CLASSIFICATION_num <- as.numeric(nuseds$ESTIMATE_CLASSIFICATION)
nuseds$ESTIMATE_CLASSIFICATION_num[nuseds$ESTIMATE_CLASSIFICATION_num == 7] <- 5
nuseds$ESTIMATE_CLASSIFICATION_num[nuseds$ESTIMATE_CLASSIFICATION_num == 8] <- 6
nuseds$ESTIMATE_CLASSIFICATION_num[nuseds$ESTIMATE_CLASSIFICATION_num > 7] <- 7
# range(nuseds$ESTIMATE_CLASSIFICATION_num)
tbrDat$quality <- rep(NA, n)
for(i in 1:n){
nuseds.p <- nuseds[which(nuseds$POP_ID == tbrDat$POP_ID[i] & nuseds$ANALYSIS_YR >= 2000), ]
tbrDat$quality[i] <- mean(nuseds.p$ESTIMATE_CLASSIFICATION_num, na.rm = TRUE)
}
#------------------------------------------------------------------------------
# Add in NuSEDS MAX_ESIMTATES since 1950
#------------------------------------------------------------------------------
tbrDat[ , paste0("X", 1950:max(nuseds$ANALYSIS_YR))] <- NA
yr <- c(1950:max(nuseds$ANALYSIS_YR))
for(i in 1:n){
if(tbrDat$POP_ID[i] > 0){
nuseds.p <- nuseds[which(nuseds$POP_ID == tbrDat$POP_ID[i]), ]
tbrDat[i , paste0("X", 1950:max(nuseds$ANALYSIS_YR))] <- nuseds.p$MAX_ESTIMATE[match(yr, nuseds.p$ANALYSIS_YR)]
}
}
###############################################################################
# Write csv
###############################################################################
write.csv(tbrDat, file = "data/TBREscapement.csv", row.names = FALSE)