-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copy and source proj functions locally
- Loading branch information
1 parent
9ef3e55
commit 5d18cad
Showing
5 changed files
with
399 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#get projection results | ||
|
||
get_proj_res<-function(dir1="foo",dir2="foo",proj_out="Model_Proj_out/",data_file="proj_res_summ.out",FY=2018, k=1,nproj=2,species="foo") | ||
{ | ||
|
||
#Proj output files | ||
alt2=read.table(file=file.path(dir1,proj_out,"alt2_proj.out"),header=TRUE) | ||
print("Read alt2_proj.out") | ||
|
||
#Check skip lines equation. Not sure if percentiles prints the same for everyone...it should though | ||
ssb_ref=as.data.frame(matrix(scan(file=file.path(dir1,proj_out,"percentiles.out"),skip=(23+(nproj*5)),nlines=nproj),nrow=nproj, | ||
ncol=9,byrow=TRUE)) | ||
print("Read ssb from percentiles .out") | ||
|
||
names(ssb_ref)=c("Year", "SSB100", "SSBabc", "SSBofl", "LowCI_SSB", "Median_SSB", "Mean_SSB", "UpperCI_SSB", "Stdev_SSB") | ||
print("attaching names to ssb_ref") | ||
print("first row of ssb_ref") | ||
print(ssb_ref[1,]) | ||
|
||
#See above comment | ||
f_ref=as.data.frame(matrix(scan(file=file.path(dir1,proj_out,"percentiles.out"),skip=(26+(nproj*6)),nlines=nproj),nrow=nproj,ncol=9,byrow=TRUE)) | ||
print("Read f from percentiles .out") | ||
|
||
names(f_ref)=c("Year", "F0", "Fabc", "Fofl", "LowCI_F", "Median_F", "Mean_F", "UpperCI_F", "Stdev_F") | ||
print("attaching names to f_ref") | ||
print("first row of f_ref") | ||
print(f_ref[1,]) | ||
|
||
|
||
res=data.frame(Spp=species, | ||
Year=round(alt2$Year[alt2$Year>=FY & alt2$Year<=(FY+15)],0), | ||
TotBiom=round(alt2$TotBiom[alt2$Year>=FY & alt2$Year<=(FY+15)],0), | ||
SSB=round(alt2$SSB[alt2$Year>=FY & alt2$Year<=(FY+15)],0), | ||
SSB100=round(ssb_ref[ssb_ref$Year>=FY & ssb_ref$Year<=(FY+15),"SSB100"],0), | ||
SSBabc=round(ssb_ref[ssb_ref$Year>=FY & ssb_ref$Year<=(FY+15),"SSBabc"],0), | ||
SSBofl=round(ssb_ref[ssb_ref$Year>=FY & ssb_ref$Year<=(FY+15),"SSBofl"],0), | ||
Fofl=round(f_ref$Fofl[f_ref$Year>=FY & f_ref$Year<=(FY+15)],2), | ||
Fabc=round(f_ref$Fabc[f_ref$Year>=FY & f_ref$Year<=(FY+15)],2), | ||
OFL=round(alt2$OFL[alt2$Year>=FY & alt2$Year<=(FY+15)],0), | ||
ABC=round(alt2$ABC[alt2$Year>=FY & alt2$Year<=(FY+15)],0)) | ||
names(res)=c("Spp","Year","TotBiom","SSB","SSB100","SSBabc","SSBofl","Fofl","Fabc","OFL","ABC") | ||
res$SSB_SSBofl=round(alt2$SSB[alt2$Year>=FY & alt2$Year<=(FY+15)],0)/round(ssb_ref[ssb_ref$Year>=FY & ssb_ref$Year<=(FY+15),"SSBofl"],0) | ||
|
||
print("Summary of results") | ||
print(res) | ||
|
||
print("writing summary projection results") | ||
write.table(res,file.path(dir1,data_file), row.names=FALSE, col.names=FALSE) | ||
print("finished writing summary projection results") | ||
|
||
return(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
write_proj<-function(dir="foo", | ||
sdir="data/", | ||
data_file="projection_data.dat", | ||
data=Models[[1]],NSEX=2,NAGE=30,Nfishery=2,fleets=1:2 | ||
,rec_age=3, max_age=36,FY=1977,rec_FY=1977,rec_LY_decrement=2, | ||
spawn_month=1, Fratios=0.5) | ||
{ | ||
#Writing data file required by proj | ||
|
||
LY=data$Retro_year # last year in the report file is the last year of full model; | ||
# need this calc to get end year for the retros | ||
Y5<-LY-5 ## start year to calculate 5-year mean apical F | ||
|
||
rec_LY=LY-rec_LY_decrement ## last year of recruitment used to get mean rec | ||
|
||
write_file=paste0(dir,"/",data_file) | ||
|
||
T1<-noquote(paste(data_file)) | ||
write(T1,paste(write_file),ncolumns = 1 ) | ||
|
||
T1<-noquote(" 0 # SSL Species???") | ||
write(T1,paste(write_file),ncolumns = 1,append=T) | ||
|
||
T1<-noquote(" 0 # Constant Buffer Dorn") | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote(paste(Nfishery, "# Number of fisheries", sep=" ")) | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote(paste(NSEX, "# Number of Sexes",sep=" ")) | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote(paste(mean(data$sprseries$sum_Apical_F[data$sprseries$Yr>Y5 & data$sprseries$Yr<=LY]),"# 5 year average F ",sep="")) | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote("1 # Author f") | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote("0.4 # SPR ABC") | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote("0.35 # SPR MSY") | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote(paste(spawn_month, "# Spawning month",sep=" ")) | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote(paste(NAGE,"# number of ages",sep=" ")) | ||
write(T1,paste(write_file),append = T) | ||
|
||
T1<-noquote(paste(Fratios,"# Fratio",sep=" ")) | ||
write(T1,paste(write_file),append = T) | ||
|
||
if(NSEX<2) | ||
{ | ||
print("In NSEX <2") | ||
T1<-noquote("# natural mortality at age females, males") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing natural mortality") | ||
if(rec_age==0) | ||
{ | ||
print("In rec_age==0") | ||
write(as.numeric(c(subset(data$M_at_age,data$M_at_age$Yr==(LY-1)& data$M_at_age$Sex==NSEX)[,as.character(seq(rec_age,(max_age-1),1))], | ||
subset(data$M_at_age,data$M_at_age$Yr==(LY-1)&data$M_at_age$Sex==NSEX)[,as.character((max_age-1))])), | ||
paste(write_file),append = T,ncolumns = 45) | ||
} | ||
if(rec_age>0){ | ||
print("In rec_age>0") | ||
write(as.numeric(subset(data$M_at_age,data$M_at_age$Yr==(LY-1)&data$M_at_age$Sex==NSEX)[,as.character(seq((rec_age-1),(max_age-1),1))]), | ||
paste(write_file),append = T,ncolumns = 45) | ||
} | ||
|
||
T1<-noquote("# Maturity females") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing maturity") | ||
if(spp=="GT")write(rep(1,NAGE),paste(write_file),append = T,ncolumns = 45) | ||
if(spp!="GT") write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==NSEX & data$endgrowth$int_Age %in% rec_age:max_age)[,"Age_Mat"]),4),paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote("# wt spawn females") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing wt spawn females") | ||
if(spp=="GT")write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==NSEX & data$endgrowth$int_Age %in% rec_age:max_age)[,"Mat*Fecund"]),4),paste(write_file),append = T,ncolumns = 45) | ||
if(spp!="GT")write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==NSEX & data$endgrowth$int_Age %in% rec_age:max_age)[,"Wt_Beg"]),4),paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote("# WtAge females by fishery") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing WtAge by sex and fishery") | ||
for (i in 1:length(fleets)){ | ||
write(round(as.numeric(subset(data$ageselex,data$ageselex$Fleet==fleets[i] & data$ageselex$Yr==LY & | ||
data$ageselex$Factor=="bodywt")[NSEX,as.character(seq(rec_age,max_age,1))]),4), | ||
paste(write_file),append = T,ncolumns = 45) | ||
|
||
} | ||
|
||
T1<-noquote("# Selectivity by fishery") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing Selectivity by sex and fishery") | ||
for (i in 1:length(fleets)) | ||
{ | ||
write(round(as.numeric(subset(data$ageselex,data$ageselex$Fleet==fleets[i] & data$ageselex$Yr==LY & data$ageselex$Factor=="Asel2")[NSEX,as.character(seq(rec_age,max_age,1))]),4),paste(write_file),append = T,ncolumns = 45) | ||
} | ||
|
||
T1<-noquote(paste("# Numbers at age",rec_age,"-",max_age,"females males",LY)) | ||
write(T1,paste(write_file),append = T) | ||
print("Writing Numbers at age by sex") | ||
write(as.numeric(subset(data$natage,data$natage[,"Beg/Mid"]=="B"&data$natage$Yr==LY)[NSEX,as.character(seq(rec_age,max_age,1))]),paste(write_file),append = T,ncolumns = 45) | ||
|
||
Rec_1<-as.numeric(data$natage[,as.character(rec_age)][data$natage$Yr<=rec_LY & data$natage$Yr>=rec_FY & data$natage$Sex==(NSEX) & data$natage[,"Beg/Mid"]=="B"]) | ||
N_rec<-length(Rec_1) | ||
T1<-noquote("# No Recruitments") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing number of recruit observations") | ||
write(N_rec,paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote("# Recruitment") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing recruits") | ||
write(round(Rec_1,1),paste(write_file),append = T,ncolumns = 45) | ||
|
||
} | ||
|
||
if(NSEX>1) | ||
{ | ||
T1<-noquote("# natural mortality") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing natural mortality") | ||
for(i in 1:NSEX) | ||
{ | ||
if(rec_age==0) | ||
{ | ||
write(as.numeric(c(subset(data$M_at_age,data$M_at_age$Yr==(LY-1)&data$M_at_age$Sex==NSEX)[,as.character(seq(rec_age,(max_age-1),1))], | ||
subset(data$M_at_age,data$M_at_age$Yr==(LY-1)&data$M_at_age$Sex==NSEX)[,as.character((max_age-1))])), | ||
paste(write_file),append = T,ncolumns = 45) | ||
} | ||
if(rec_age>0){ | ||
write(as.numeric(subset(data$M_at_age,data$M_at_age$Yr==(LY-1)&data$M_at_age$Sex==NSEX)[,as.character(seq((rec_age-1),(max_age-1),1))]), | ||
paste(write_file),append = T,ncolumns = 45) | ||
} | ||
} | ||
|
||
T1<-noquote("# Maturity females") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing female maturity") | ||
if(spp=="GT")write(rep(1,NAGE),paste(write_file),append = T,ncolumns = 45) | ||
if(spp!="GT") write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==1 & data$endgrowth$int_Age %in% rec_age:max_age)[,"Age_Mat"]),4),paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote("# Maturity males") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing male maturity") | ||
if(spp=="GT")write(rep(1,NAGE),paste(write_file),append = T,ncolumns = 45) | ||
if(spp!="GT") write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==2 & data$endgrowth$int_Age %in% rec_age:max_age)[,"Age_Mat"]),4),paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote("# wt spawn females") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing wt spawn females") | ||
if(spp=="GT")write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==1 & data$endgrowth$int_Age %in% rec_age:max_age)[,"Mat*Fecund"]),4),paste(write_file),append = T,ncolumns = 45) | ||
if(spp!="GT")write(round(as.numeric(subset(data$endgrowth,data$endgrowth$Sex==1 & data$endgrowth$int_Age %in% rec_age:max_age)[,"Wt_Beg"]),4),paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote("# WtAge by sex and fishery") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing wtAge by sex and fishery") | ||
for(j in 1:NSEX) | ||
{ | ||
for (i in 1:length(fleets)) | ||
{ | ||
write(round(as.numeric(subset(data$ageselex,data$ageselex$Fleet==fleets[i] & data$ageselex$Yr==LY & data$ageselex$Factor=="bodywt")[j,as.character(seq(rec_age,max_age,1))]),4),paste(write_file),append = T,ncolumns = 45) | ||
} | ||
} | ||
|
||
T1<-noquote("# Selectivity by sex and fishery") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing selectivity by sex and fishery") | ||
for(j in 1:NSEX) | ||
{ | ||
for (i in 1:length(fleets)) | ||
{ | ||
write(round(as.numeric(subset(data$ageselex,data$ageselex$Fleet==fleets[i] & data$ageselex$Yr==LY & data$ageselex$Factor=="Asel2")[j,as.character(seq(rec_age,max_age,1))]),4),paste(write_file),append = T,ncolumns = 45) | ||
} | ||
} | ||
|
||
T1<-noquote(paste("# Numbers at age",rec_age,"-",max_age,"females males",LY)) | ||
write(T1,paste(write_file),append = T) | ||
print("Writing Numbers at age females males") | ||
for(j in 1:NSEX) | ||
{ | ||
write(as.numeric(subset(data$natage,data$natage[,"Beg/Mid"]=="B"&data$natage$Yr==LY)[j,as.character(seq(rec_age,max_age,1))]),paste(write_file),append = T,ncolumns = 45) | ||
} | ||
|
||
print("Getting number of recruits") | ||
Rec_1<-as.numeric(data$natage[,as.character(rec_age)][data$natage$Yr<=rec_LY & data$natage$Yr>=rec_FY & data$natage$Sex==(NSEX-1) & data$natage[,"Beg/Mid"]=="B"] | ||
+data$natage[,as.character(rec_age)][data$natage$Yr<=rec_LY & data$natage$Yr>=rec_FY & data$natage$Sex==NSEX & data$natage[,"Beg/Mid"]=="B"]) | ||
print("Getting number of recruit obs") | ||
N_rec<-length(Rec_1) | ||
T1<-noquote("# No Recruitments") | ||
write(T1,paste(write_file),append = T) | ||
print("Writing number of recruit obs") | ||
write(N_rec,paste(write_file),append = T,ncolumns = 45) | ||
|
||
T1<-noquote(paste("# Recruitment age ",rec_age,"+ ", rec_FY,"-",rec_LY,sep="")) | ||
write(T1,paste(write_file),append = T) | ||
print("Writing number of recruits") | ||
write(round(Rec_1,1),paste(write_file),append = T,ncolumns = 45) | ||
|
||
|
||
} | ||
|
||
T1<-noquote(paste("# SSB ", FY,"-",LY,sep="")) | ||
write(T1,paste(write_file),append = T) | ||
print("Writing SSB") | ||
if(spp!="EBS_Pcod") write(as.numeric(data$sprseries$SSB[data$sprseries$Yr<=LY&data$sprseries$Yr>=FY]),paste(write_file),append = T,ncolumns = 45) | ||
if(spp=="EBS_Pcod") write(as.numeric(data$sprseries$SSB[data$sprseries$Yr<=LY&data$sprseries$Yr>=FY])/2,paste(write_file),append = T,ncolumns = 45) | ||
# file.copy(write_file, file.path(dir, sdir),overwrite=TRUE) | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
write_setup <-function(dir="foo",sdir="data/",data_file="setup.dat",data=Models[[1]],nsims=100,nproj=20) | ||
{ | ||
LY=data$Retro_year | ||
|
||
write_file=paste0(dir,"/",data_file) | ||
#write set-up.dat required to run projection | ||
write(paste(noquote("std"), noquote("#No idea what this is"),sep=" "),file=write_file) | ||
write(paste(7, noquote("#Number of alternatives???"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(1, noquote("#Vector of alt numbers"),sep=" "),file=write_file,append=TRUE) | ||
write(2,file=write_file,append=TRUE) | ||
write(3,file=write_file,append=TRUE) | ||
write(4,file=write_file,append=TRUE) | ||
write(5,file=write_file,append=TRUE) | ||
write(6,file=write_file,append=TRUE) | ||
write(7,file=write_file,append=TRUE) | ||
write(paste(1,noquote("#TAC_ABC"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(1,noquote("#SRType"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(1,noquote("#Rec_Gen"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(0,noquote("#Fmsy_F35"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(0,noquote("#Rec_Cond"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(1,noquote("#Write_Big"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(nproj,noquote("#NProj"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(nsims,noquote("#Nsims"),sep=" "),file=write_file,append=TRUE) | ||
write(paste(LY,noquote("#Styr"),sep=" "),file=write_file,append=TRUE) | ||
|
||
#create copy in data folder | ||
file.copy(write_file, file.path(dir, sdir),overwrite=TRUE) | ||
} | ||
|
Oops, something went wrong.