-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_op_species_indicator.R
214 lines (156 loc) · 7.12 KB
/
plot_op_species_indicator.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
Portrait<-F # graphical output orientation
first.year<- 1963 #first year on plot, negative value means value defined by data
last.year<- 2100 #last year on plot
HindcastModel<-T; # plot values from the hindcast
ForecastModel<-F; # plot values from the scenarios
redefine.scenario.manually<-F # Define the scenario dir used explecitely in this script or do it externally
op.dir<-data.path
if (ForecastModel==T & redefine.scenario.manually) {
scenario<-"Opti-2_HCR_4_stoch_penBlim_atAgeW__";
output.dir<-data.path
op.dir<-file.path(data.path,scenario)
} else if (ForecastModel==T ) {
output.dir<-scenario.dir
op.dir<-scenario.dir
}
##########################################################################
my.dev<-'png' # output device: 'screen', 'wmf', 'png', 'pdf'
#my.dev<-'screen'
cleanup()
file.name<-'summary'
nox<-3; noy<-1;
noxy<-nox*noy
ref<-Read.reference.points()
Init.function()
MSFD<-read.FLOP.MSFD.control(file=file.path(op.dir,"op_msfd.dat"),n.VPA=nsp-first.VPA+1,n.other.pred=first.VPA-1)
if (HindcastModel) {
dat1<-subset(Read.summary.data(dir=data.path),Year<=last.year &Species.n>=first.VPA)
if (first.year>0) dat1<-subset(dat1,Year>=first.year )
# mean weight in the sea and in tha catch
w<-subset(dat1,select=c(Species.n,Year,Quarter,Age,weca,west, N,C.obs ))
incl<-data.frame(Species.n=first.VPA:nsp,incl=c(MSFD@mean.weight.C.sp))
w<-subset(merge(incl,w),incl==1)
w[w$weca<-0,'weca']<-0
w$N1<-w$N
w[w$Age==0,'N1']<-0
w$WC<-w$C.obs*w$weca
w$WN<-w$N*w$west
w$WN1<-w$N1*w$west
w<-aggregate(cbind(WC,WN,WN1,N,N1,C.obs)~Species.n+Year,data=w,sum,na.rm=T)
w$west<-w$WN/w$N
w$west1<-w$WN1/w$N1
w$weca<-w$WC/w$C.obs
w<-subset(w,select=c( Species.n, Year, west,west1,weca))
#Species average M2 and F
mf<-subset(dat1,select=c(Species.n,Year,Quarter,Age, N.bar,M2,C.hat ))
mf$deadM2<-mf$N.bar*mf$M2
inclM<-data.frame(Species.n=first.VPA:nsp,faM=c(MSFD@M2.bar.ages[1,]),laM=c(MSFD@M2.bar.ages[2,]) )
inclF<-data.frame(Species.n=first.VPA:nsp,faC=c(SMS.control@avg.F.ages[,1]),laC=c(SMS.control@avg.F.ages[,2]) )
incl<-merge(inclM,inclF)
mf<-merge(incl,mf)
mf$inclF<-F
mf$inclM<-F
mf[mf$faM>=0 & mf$laM>=0 & mf$Age>=mf$faM & mf$Age<=mf$laM,'inclM']<-T
mf[mf$faC>=0 & mf$laC>=0 & mf$Age>=mf$faC & mf$Age<=mf$laC,'inclF']<-T
m<-droplevels(subset(mf,inclM==T))
m$deadM2<-m$N.bar*m$M2
m<-aggregate(deadM2~Species.n+Year+Age,sum,data=m)
mn<-subset(dat1,(Age>0 & Quarter==1) | (Age==0 & Quarter==3),select=c(Species.n,Year,Age,N))
mz<-aggregate(Z~Species.n+Year+Age,sum,data=dat1)
mm<-merge(mn,mz)
mm$N.bar<-mm$N*(1-exp(-mm$Z))/mm$Z
m<-merge(mm,m)
m$M2<- m$deadM2/m$N.bar
m<-aggregate(M2~Species.n+Year,mean,data=m)
f<-droplevels(subset(mf,inclF==T))
f<-aggregate(C.hat~Species.n+Year+Age,sum,data=f)
fn<-subset(dat1,(Age>0 & Quarter==1) | (Age==0 & Quarter==3),select=c(Species.n,Year,Age,N))
fz<-aggregate(Z~Species.n+Year+Age,sum,data=dat1)
ff<-merge(fn,fz)
ff$N.bar<-ff$N*(1-exp(-ff$Z))/ff$Z
f<-merge(ff,f)
f$F<- f$C.hat/f$N.bar
f<-aggregate(F~Species.n+Year,mean,data=f)
fm<-merge(f,m,all=T)
life.expectancy<-function(fa,la) {
# life expectancy
bio<-subset(dat1,select=c(Species.n,Year,Quarter,Age,Z))
incl<-data.frame(Species.n=first.VPA:nsp,incl=c(MSFD@community.life.expectancy.options['first.age',]))
incl<-NULL
for (s in (first.VPA:nsp)) {
fa.i<-fa[s-first.VPA+1]
la.i<-la[s]
if (la.i>=fa.i) incl<-rbind(incl,data.frame(Species.n=s,Age=seq(fa.i,la.i)))
}
bio<-merge(bio,incl)
# a<-data.frame(Species.n=first.VPA:nsp,w=c(MSFD@community.life.expectancy.options['weighting',]))
# bio<-merge(bio,a)
bio<-bio[order(bio$Species.n,bio$Year,bio$Age,bio$Quarter),]
min.Year<-min(bio$Year)
bio$first<- !duplicated(paste(bio$Species.n,bio$Year))
a<-unique(subset(bio,first,select=c(Species.n,Age,Quarter)))
a$ini.age<-a$Age+(a$Quarter-1)*0.25
a<-subset(a,select=c(Species.n,ini.age))
bio<-merge(bio,a)
bio$sumZ<-bio$Z
bio<-bio[order(bio$Species.n,bio$Year,bio$Age,bio$Quarter),]
for (x in (1:dim(bio)[1])) {
if (!bio[x,'first']) bio[x,'sumZ']<-bio[x,'sumZ']+(bio[x-1,'sumZ'])
}
bio$p<-exp(-bio$sumZ)
a<-aggregate(p~Year+Species.n+ini.age,sum,na.rm=T,data=bio)
a$Expectancy<-a$p/4 + a$ini.age+0.25/2 # to expected life in years (from quarters)
a$p<-NULL
a$ini.age<-NULL
return(a)
}
a<-life.expectancy(fa=MSFD@community.life.expectancy.options['first.age',],la=SMS.control@species.info[,'last-age'])
aa<-MSFD@community.life.expectancy.options['first.age',]
aa[]<-1
a1<-life.expectancy(fa=aa,la=SMS.control@species.info[,'last-age'])
a1$Expectancy1<-a1$Expectancy
a1$Expectancy<-NULL
a<-merge(a,a1)
}
add.set<-strmacro(in1,in2,out,
expr={
if (HindcastModel & ForecastModel) out<-c(in1,in2) else if (HindcastModel) out<-c(in1) else if (ForecastModel) out<-c(in2)
} )
## HER SKAL HISTORISKE DATA SAMLES MED SCENARIO DATA
a<-merge(a,w,all = T)
a<-merge(a,fm,all=T)
# a includes all data
a$Species<-sp.names[a$Species.n]
my.sp<-sort(unique(a$Species.n))
cleanup()
plotfile<-function(dev='screen',out) {
if (dev=='screen') X11(width=8, height=12, pointsize=12)
if (dev=='wmf') win.metafile(filename = paste(out,'.wmf',sep=''), width=8, height=10, pointsize=12)
if (dev=='png') png(filename =paste(out,'.png',sep=''), width = 1000, height = 1400,units = "px", pointsize = 30, bg = "white")
if (dev=='pdf') pdf(file =paste(out,'.pdf',sep=''), width = 8, height = 10,pointsize = 12,onefile=FALSE)
}
for (sp in my.sp) {
#sp<-18
Species<-sp.names[sp]
b<-droplevels(subset(a,Species.n==sp))
plotfile(dev=my.dev,out=file.path(op.dir,paste('Species_indicator',sp,sep='-')));
par(mfcol=c(nox,noy))
par(mar=c(3,4,3,2)) # bottom, left, top, right
par(mar=c(2,4,3,5)+.1) #bottom, left, top, right
# F and M2
min.d<-min(min(b$M2,na.rm=T),min(b$F,na.rm=T),na.rm=T)
max.d<-max(max(b$M2,na.rm=T),max(b$F,na.rm=T),na.rm=T)
plot(b$Year,b$F, ylim=c(min.d,max.d),type='b',xlab='Year',ylab='',lty=1,pch='F',lwd=2,main=paste(Species,' Mean F and M2',sep=':'))
lines(b$Year,b$M2,lty=2,pch='M',lwd=2,type='b',col=2)
# life.expect
min.d<-min(min(b$Expectancy,na.rm=T),min(b$Expectancy1,na.rm=T),na.rm=T)
max.d<-max(max(b$Expectancy,na.rm=T),max(b$Expectancy1,na.rm=T),na.rm=T)
plot(b$Year,b$Expectancy,lty=1,pch='0',lwd=2,ylab='year',xlab=' ',type='b',col=1,ylim=c(min.d,max.d),main="Life expectancy for age 0 (third quarter) and age 1")
lines(b$Year,b$Expectancy1,lty=2,pch='1',lwd=2,type='b',col=2)
min.d<-min(min(b$west,na.rm=T),min(b$weca,na.rm=T),na.rm=T)
max.d<-max(max(b$west,na.rm=T),max(b$weca,na.rm=T),na.rm=T)
plot(b$Year,b$weca, ylim=c(min.d,max.d),type='b',xlab='Year',col='blue',ylab='kg',lty=1,pch='c',lwd=2,main=paste('Mean weight in the sea (for age>=0 and age>=1) and in the catch (c)',sep=':'))
lines(b$Year,b$west,lty=2,pch='0',lwd=2,type='b',col=1)
lines(b$Year,b$west1,lty=2,pch='1',lwd=2,type='b',col=2)
if (my.dev %in% c('png','wmf','pdf')) dev.off()
}