-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.R
More file actions
178 lines (167 loc) · 6.26 KB
/
Copy pathdata.R
File metadata and controls
178 lines (167 loc) · 6.26 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Prepare data, write CSV data tables
# Before: cn.dat, cw.dat, dw.dat, lf.dat, lw.dat, mo_raw.dat, nm.dat, pf.dat,
# pm.dat, survey.dat, sw.dat (boot/data)
# After: catage.csv, catage_full.csv, catch_sop.csv, datage.csv,
# datage_full.csv, ibts1.csv, ibts3.csv, landfrac.csv, latage.csv,
# latage_full.csv, maturity.csv, maturity_full.csv, natmort.csv,
# propf.csv, propm.csv, sam.dat, surveytime.csv, wcatch.csv,
# wcatch_full.csv, wdiscards.csv, wdiscards_full.csv, wlandings.csv,
# wlandings_full.csv, wstock.csv, wstock_full.csv (data)
library(icesTAF)
suppressMessages(library(mgcv))
source("utilities_data.R")
mkdir("data")
# Get data
catch.no <- read.ices("boot/data/cn.dat")
catch.mean.weight <- read.ices("boot/data/cw.dat")
dis.mean.weight <- read.ices("boot/data/dw.dat")
land.mean.weight <- read.ices("boot/data/lw.dat")
stock.mean.weight <- read.ices("boot/data/sw.dat")
prop.mature <- read.ices("boot/data/mo_raw.dat")
natural.mortality <- read.ices("boot/data/nm.dat")
surveys <- read.surveys("boot/data/survey.dat")
land.no <- read.ices("boot/data/lf.dat")
dis.no <- catch.no - land.no
prop.f <- read.ices("boot/data/pf.dat")
prop.m <- read.ices("boot/data/pm.dat")
# full datasets, including all ages
catch.no.full <- catch.no
land.no.full <- land.no
land.mean.weight.full <- land.mean.weight
dis.mean.weight.full <- dis.mean.weight
catch.mean.weight.full <- catch.mean.weight
stock.mean.weight.full <- stock.mean.weight
# Smooth maturity
skipYears <- c(1:10, 54)
columnsToSmooth <- 1:5
mo <- prop.mature[-c(skipYears),]
for(cc in columnsToSmooth){
ww <- mo[,cc]
tt <- 1:length(ww)
tmp <- gam(ww ~ s(tt))
mo[,cc] <- predict(tmp)
}
prop.mature[-c(skipYears),] <- mo
prop.mature[54,] <- prop.mature[53,]
prop.mature.full <- prop.mature
# Modify to 6+ data
cutage <- 6
low <- 1
GE <- which(as.numeric(colnames(catch.no)) >= cutage)
E <- which(as.numeric(colnames(catch.no)) == cutage)
w <- catch.no[,GE] / rowSums(catch.no[,GE])
wex <- rbind(w, w[nrow(w),])
wD <- dis.no[,GE] / ifelse(rowSums(dis.no[,GE])>0, rowSums(dis.no[,GE]), 1)
wL <- land.no[,GE] / rowSums(land.no[,GE])
catch.no[,E] <- rowSums(catch.no[,GE])
catch.no <- catch.no[,low:E]
prop.mature[,E] <- rowSums(prop.mature[,GE] * wex)
prop.mature <- prop.mature[,low:E]
stock.mean.weight[,E] <- rowSums(stock.mean.weight[,GE] * wex)
stock.mean.weight <- stock.mean.weight[,low:E]
catch.mean.weight[,E] <- rowSums(catch.mean.weight[,GE] * w)
catch.mean.weight <- catch.mean.weight[,low:E]
dis.mean.weight[,E] <- rowSums(dis.mean.weight[,GE] * wD)
dis.mean.weight <- dis.mean.weight[,low:E]
land.mean.weight[,E] <- rowSums(land.mean.weight[,GE] * wL)
land.mean.weight <- land.mean.weight[,low:E]
natural.mortality[,E] <- rowSums(natural.mortality[,GE] * wex)
natural.mortality <- natural.mortality[,low:E]
land.no[,E] <- rowSums(land.no[,GE])
land.no <- land.no[,low:E]
land.frac <- ifelse(catch.no>0, land.no/catch.no, 1)
prop.f <- prop.f[,low:E]
prop.m <- prop.m[,low:E]
# Catch as sum of products
sop <- data.frame(Landings=rowSums(land.no * land.mean.weight),
Discards=rowSums((catch.no-land.no) * dis.mean.weight))
sop$Catch <- sop$Landings + sop$Discards
# sop$Catch2 <- rowSums(catch.no * catch.mean.weight)
# Prepare tables for export
latage <- xtab2taf(land.no)
datage <- xtab2taf(catch.no - land.no)
catage <- xtab2taf(catch.no)
wlandings <- xtab2taf(land.mean.weight)
wdiscards <- xtab2taf(dis.mean.weight)
wcatch <- xtab2taf(catch.mean.weight)
wstock <- xtab2taf(stock.mean.weight)
maturity <- xtab2taf(prop.mature)
natmort <- xtab2taf(natural.mortality)
ibts1 <- xtab2taf(surveys[[1]])
ibts3 <- xtab2taf(surveys[[2]])
surveytime <- as.data.frame(lapply(surveys, attr, "time"),
col.names=c("ibts1","ibts3"))
catch_sop <- xtab2taf(sop)
landfrac <- xtab2taf(land.frac)
propf <- xtab2taf(prop.f)
propm <- xtab2taf(prop.m)
# full datasets, including all ages
latage_full <- xtab2taf(land.no.full)
datage_full <- xtab2taf(catch.no.full - land.no.full)
catage_full <- xtab2taf(catch.no.full)
wlandings_full <- xtab2taf(land.mean.weight.full)
wdiscards_full <- xtab2taf(dis.mean.weight.full)
wcatch_full <- xtab2taf(catch.mean.weight.full)
wstock_full <- xtab2taf(stock.mean.weight.full)
maturity_full <- xtab2taf(prop.mature.full)
# Rename plus group
latage <- plus(latage)
datage <- plus(datage)
catage <- plus(catage)
wlandings <- plus(wlandings)
wdiscards <- plus(wdiscards)
wcatch <- plus(wcatch)
wstock <- plus(wstock)
landfrac <- plus(landfrac)
maturity <- plus(maturity)
natmort <- plus(natmort)
propf <- plus(propf)
propm <- plus(propm)
# Write tables to data directory
write.taf(catage, dir="data")
write.taf(catch_sop, dir="data")
write.taf(datage, dir="data")
write.taf(ibts1, dir="data")
write.taf(ibts3, dir="data")
write.taf(landfrac, dir="data")
write.taf(latage, dir="data")
write.taf(maturity, dir="data")
write.taf(natmort, dir="data")
write.taf(propf, dir="data")
write.taf(propm, dir="data")
write.taf(surveytime, dir="data")
write.taf(wcatch, dir="data")
write.taf(wdiscards, dir="data")
write.taf(wlandings, dir="data")
write.taf(wstock, dir="data")
# full datasets, including all ages
write.taf(catage_full, dir="data")
write.taf(datage_full, dir="data")
write.taf(latage_full, dir="data")
write.taf(maturity_full, dir="data")
write.taf(wcatch_full, dir="data")
write.taf(wdiscards_full, dir="data")
write.taf(wlandings_full, dir="data")
write.taf(wstock_full, dir="data")
# Prepare model input file
taf2sam <- function(x)
{
y <- as.matrix(taf2xtab(x))
colnames(y) <- sub("\\+", "", colnames(y))
y
}
surveys <- list(ibts1=taf2sam(ibts1), ibts3=taf2sam(ibts3))
attr(surveys$ibts1, "time") <- surveytime$ibts1
attr(surveys$ibts3, "time") <- surveytime$ibts3
input <- write.records(surveys=surveys,
residual.fleet=taf2sam(catage),
prop.mature=taf2sam(maturity),
stock.mean.weight=taf2sam(wstock),
catch.mean.weight=taf2sam(wcatch),
dis.mean.weight=taf2sam(wdiscards),
land.mean.weight=taf2sam(wlandings),
prop.f=taf2sam(propf),
prop.m=taf2sam(propm),
natural.mortality=taf2sam(natmort),
land.frac=taf2sam(landfrac),
file="data/sam.dat")