Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atlnts 289 mortality #121

Merged
merged 5 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Imports:
angstroms,
atlantistools,
abind,
bgmfiles,
data.table,
Expand Down Expand Up @@ -48,4 +49,4 @@ Imports:
visNetwork,
zoo
Remotes:
alketh/atlantistools
andybeet/atlantistools
6 changes: 5 additions & 1 deletion R/get_atl_paramfiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ get_atl_paramfiles = function(param.dir,atl.dir,include_catch){
dietcheck = paste0(atl.dir,run.prefix,'DietCheck.txt')
yoy = paste0(atl.dir,run.prefix,'YOY.txt')
ssb = paste0(atl.dir,run.prefix,'SSB.txt')
mort <- paste0(atl.dir,run.prefix,'Mort.txt')
specificmort <- paste0(atl.dir,run.prefix,'SpecificMort.txt')

#If catch is turned on all generate paths for CATCH and TOTCATCH
if(include_catch){
Expand Down Expand Up @@ -79,7 +81,9 @@ get_atl_paramfiles = function(param.dir,atl.dir,include_catch){
yoy =yoy,
ssb = ssb,
catch = ifelse(include_catch,catch,NA),
catchtot = ifelse(include_catch,catchtot,NA)
catchtot = ifelse(include_catch,catchtot,NA),
mort = mort,
specificmort = specificmort
)

return(param.list)
Expand Down
93 changes: 91 additions & 2 deletions R/make_atlantis_diagnostic_figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#'@plot.spatial.biomass logical. Plots showing the spatial (box/level) structure of groups' biomass
#'@plot.LTL logical. Plots comparing LTL groups (domain-wide) to data
#'@plot.catch logical. Plots annual catch(mt) age based catch (numbers) and age based %ages
#'@plot.mortality logical. Plots Mortality (F, M1, M2) from two output sources (Mort, SpecificMort)

#'
#'@return A series of figures and tables based on output grouping flags
Expand Down Expand Up @@ -136,7 +137,8 @@ make_atlantis_diagnostic_figures = function(
plot.consumption,
plot.spatial.biomass,
plot.LTL,
plot.catch
plot.catch,
plot.mortality
){

`%>%` = dplyr::`%>%`
Expand Down Expand Up @@ -182,6 +184,7 @@ make_atlantis_diagnostic_figures = function(
rm(biomass.spatial.stanza)
}

# Catch Timeseries ------------------------------------------------------

if(plot.catch|plot.all) {

Expand Down Expand Up @@ -217,6 +220,92 @@ make_atlantis_diagnostic_figures = function(
rm(totcatch,catchmt)
}

# Mortality Timeseries ------------------------------------------------------

if(plot.mortality|plot.all) {

# plot mortality from Mort.txt
mort = readRDS(paste0(out.dir,'mort.rds'))
itype <- 1
plotMort <- list()
# Annual Mortality time series M, F by species on same plot
temp.plot.1 = atlantistools::plot_line(mort,col="source")
temp.plot.1 = ggplot2::update_labels(temp.plot.1,labels = list(x='Time (years)', y = 'Mortality'))
temp.plot.1 = add.title(temp.plot.1,'Mortality (F & M2)')
plotMort[[itype]] <- temp.plot.1

# plot mortaliy from specificMort.txt
specificmort <- readRDS(paste0(out.dir,'specificmort.rds'))

# plot absolute mortality. 3 pages, one page for F,M1,M2
for (atype in rev(unique(specificmort$mort))) {
itype <- itype + 1
mort <- specificmort %>%
dplyr::filter(mort == atype)
temp.plot = atlantistools::plot_line(mort, col = 'agecl')
temp.plot = ggplot2::update_labels(p = temp.plot, labels = c(list(x='Time (years)', y = 'Mortality'), list(colour = 'Ageclas')))
temp.plot = add.title(temp.plot,paste0('Mortality at Age (',atype,')'))

plotMort[[itype]] <- temp.plot

}

# plot relative mortality by age class
# select species with 10 age classes
for (iage in 1:max(specificmort$agecl)) {
mortality <- specificmort %>%
dplyr::filter(code %in% atlantistools::get_cohorts(param.ls$fgs,numCohorts = 10)) %>%
dplyr::filter(agecl == iage)

pct = atlantistools::agg_perc(mortality, groups = c('time','species'))
temp.plot = atlantistools::plot_bar(pct, fill = 'mort', wrap = 'species')
temp.plot = ggplot2::update_labels(temp.plot,labels = list(x='Time (years)', y = 'Rate (proportion)'))+
ggplot2::scale_y_continuous(labels = scales::label_number(accuracy = 0.01))
temp.plot = add.title(temp.plot, paste0("Relative Mortality Rates for species with 10 age classes (Age ",iage, ")"))

plotMort[[itype + iage]] <- temp.plot
}

# select species with 2 age classes
for (i2age in 1:2) {
mortality <- specificmort %>%
dplyr::filter(code %in% atlantistools::get_cohorts(param.ls$fgs,numCohorts = 2)) %>%
dplyr::filter(agecl == i2age)

pct = atlantistools::agg_perc(mortality, groups = c('time','species'))
temp.plot = atlantistools::plot_bar(pct, fill = 'mort', wrap = 'species')
temp.plot = ggplot2::update_labels(temp.plot,labels = list(x='Time (years)', y = 'Rate (proportion)'))+
ggplot2::scale_y_continuous(labels = scales::label_number(accuracy = 0.01))
temp.plot = add.title(temp.plot, paste0("Relative Mortality Rates for species with 2 age classes (Age ",i2age, ")"))

plotMort[[itype + iage + i2age]] <- temp.plot
}

# select species with 1 age classes (Biomass pool)
mortality <- specificmort %>%
dplyr::filter(code %in% atlantistools::get_cohorts(param.ls$fgs,numCohorts = 1)) %>%
dplyr::filter(agecl == 1)

pct = atlantistools::agg_perc(mortality, groups = c('time','species'))
temp.plot = atlantistools::plot_bar(pct, fill = 'mort', wrap = 'species')
temp.plot = ggplot2::update_labels(temp.plot,labels = list(x='Time (years)', y = 'Rate (proportion)'))+
ggplot2::scale_y_continuous(labels = scales::label_number(accuracy = 0.01))
temp.plot = add.title(temp.plot, paste0("Relative Mortality Rates for species with 1 age class (Age 1) "))

plotMort[[itype + iage + i2age + 1]] <- temp.plot



pdf(paste0(fig.dir,run.name,' Specific Mortality Timeseries.pdf'),width = 20, height = 20, onefile = T)
for (iplot in 1:(itype+iage+i2age+1)) {
gridExtra::grid.arrange(plotMort[[iplot]])
}
dev.off()


rm(mort,specificmort)
}

# Overall biomass ---------------------------------------------------------


Expand Down Expand Up @@ -368,7 +457,7 @@ make_atlantis_diagnostic_figures = function(
temp.plot.2 = atlantistools::custom_grid(temp.plot.2,grid_x = 'polygon',grid_y = 'species')
temp.plot.2 = add.title(temp.plot.2, 'Invert Biomass by Box')

pdf(file = paste0(out.dir,run.name,' Biomass Box Timeseries.pdf'),width = 48, height = 60, onefile =T)
pdf(file = paste0(fig.dir,run.name,' Biomass Box Timeseries.pdf'),width = 48, height = 60, onefile =T)
gridExtra::grid.arrange(temp.plot.1)
gridExtra::grid.arrange(temp.plot.2)
dev.off()
Expand Down
12 changes: 11 additions & 1 deletion R/process_atl_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ process_atl_output = function(param.dir,

# memory.limit(size = 56000)
source(here::here('R','load_nc_temp.R'))

#Utility function
bind.save = function(x,name){
x2 = dplyr::bind_rows(x)
Expand Down Expand Up @@ -581,4 +581,14 @@ process_atl_output = function(param.dir,

rm(catch,totcatch,catchmt)
}

# Do mortality -------------------------------------------------------------------
mortality <- atlantistools::load_mort(param.ls$mort,prm_run=param.ls$run.prm,fgs=param.ls$groups.file,convert_names = T) #%>%
# dplyr::group_by(species,time) %>%
# dplyr::summarise(atoutput = atoutput[source == "F"]/atoutput[source == "M"],.groups="drop") %>%
# dplyr::mutate(atoutput = ifelse(is.infinite(atoutput),NA,atoutput))
saveRDS(mortality,paste0(out.dir,'mort.RDS'))

specificMortality <- atlantistools::load_spec_mort(param.ls$specificmort,prm_run=param.ls$run.prm,fgs=param.ls$groups.file,convert_names = T,removeZeros = F)
saveRDS(specificMortality,paste0(out.dir,'specificmort.RDS'))
}