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 155 retune planktivores #70

Merged
merged 27 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e815848
First Attempt at Fixing
jcaracappa1 Oct 1, 2020
c126769
PlantivFix 2 at_biology update
jcaracappa1 Oct 2, 2020
357b3aa
Removed bacterial/detritus consumption from planktivores
jcaracappa1 Oct 3, 2020
d350c56
Planktiv Fix 4
jcaracappa1 Oct 4, 2020
c0fd515
Change how parameters are read into plotting function
jcaracappa1 Oct 5, 2020
f148e1a
Reworked Functions to Better Calculate Consumption
jcaracappa1 Oct 5, 2020
c6b9f1f
Updated for new param.ls usage
jcaracappa1 Oct 5, 2020
aa8f2dc
Merge pull request #67 from NOAA-EDAB/master
jcaracappa1 Oct 5, 2020
c14f6d4
Detailed plot for single species biomass by age class
jcaracappa1 Oct 9, 2020
a1bc94f
Sort prey groups alphabetically
jcaracappa1 Oct 9, 2020
8f12ad0
separate consumption plots
jcaracappa1 Oct 9, 2020
07a146a
RetunePlanktiv6
jcaracappa1 Oct 9, 2020
5c00bca
RetunePlanktiv7
jcaracappa1 Oct 10, 2020
20bea08
Function to plot food web from dietcheck.txt
jcaracappa1 Oct 13, 2020
f794baf
More color options
jcaracappa1 Oct 13, 2020
1fa19f8
Spreadsheet to help keep track of groups
jcaracappa1 Oct 14, 2020
1c684b1
Updated for RemovePred3
jcaracappa1 Oct 15, 2020
f5071ba
Pulls fixed recruitment from at_biology.prm
jcaracappa1 Oct 15, 2020
20fcfd1
Updated Recruitment fro RemovePred3
jcaracappa1 Oct 15, 2020
53460a1
Updated for ReducePred4
jcaracappa1 Oct 16, 2020
2444b68
ReducePred5
jcaracappa1 Oct 16, 2020
526a855
RemovePred6
jcaracappa1 Oct 19, 2020
9da2777
Edit example
jcaracappa1 Oct 19, 2020
d90e693
update example
jcaracappa1 Oct 19, 2020
55982ce
Flag to remove "REL*" groups
jcaracappa1 Oct 19, 2020
0812167
Final update to RemovePred
jcaracappa1 Oct 19, 2020
a1a7afb
Final Tuning for Predator Reduction
jcaracappa1 Oct 19, 2020
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
42 changes: 42 additions & 0 deletions R/get_param_kdenr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Script to get table of fixed recruitment (KDENR) values




get_param_kdenr = function(bio.file){

#Read lines
bio.lines = readLines(bio.file)
closeAllConnections()

#get kdenr lines
kdenr.line.id = grep('^KDENR',bio.lines)
kdenr.line.name = bio.lines[kdenr.line.id]
kdenr.line.vals = bio.lines[grep('^KDENR',bio.lines)+1]

#Get group names, repeat out inverts (juv,adult)
group.names = unlist(lapply(kdenr.line.name,function(x){
char.split = strsplit(x,' ')[[1]]
group = strsplit(char.split[1],'KDENR_')[[1]][2]
if(length(char.split) == 2){
return(c(paste0(group,'j'),paste0(group,'a')))
}else{
return(group)
}
}))

#Get group values
group.vals = unlist(lapply(kdenr.line.vals,function(x){
val.split = strsplit(x,' ')[[1]]
return(val.split)
}))

return(data.frame(code = group.names,KDENR = group.vals))
}

#Test
orig.kdner = get_param_kdenr(bio.file = 'C:/Users/joseph.caracappa/Documents/Atlantis/Parameter_Files/at_biol_neus_v15_scaled_diet_20181126_3.prm')
removePred2.kdner = get_param_kdenr(bio.file = here::here('currentVersion','at_biology.prm'))
write.csv(removePred2.kdner, file = 'C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Diagnostic_Data/removePred3_KDNER.csv')


66 changes: 40 additions & 26 deletions R/make_atlantis_diagnostic_figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
#'@run.prefix sring. Prefix specified in Atlantis.bat file that begins each output file
#'@run.name string. Prefered name to use for output file names
#'@result list. Output produced by process_atl_output(). Default name is 'result'. Should be loaded in first
#'@bgm.file string. Full file name for bgm file
#'@group.file string. Full file name for the functional groups file
#'@biol.prm string. Full file name for the biology.prm parameter file
#'@param.ls list. Output from get_atl_paramfiles
#'
#'@benthic.box numeric. Box ID for benthic plots
#'@benthic.level numeric. Level for benthic plots (default is 4 for NEUS model)
Expand Down Expand Up @@ -60,7 +58,7 @@
# # bgm.file = param.ls$bgm,
# # groups.file = param.ls$func.groups,
# # init.file = param.ls$init.nofill,
# # biol.prm = param.ls$biol.prm,
# # param.ls$biol.prm = param.ls$param.ls$biol.prm,
# # run.prm = param.ls$run.prm,
# # main.nc = param.ls$main.nc,
# # prod.nc = param.ls$prod.nc,
Expand All @@ -80,9 +78,9 @@
# benthic.level = 4
#
# bgm.file = param.ls$bgm
# group.file = param.ls$func.groups
# param.ls$func.groups = param.ls$func.groups
# run.prm = param.ls$run.prm
# biol.prm = param.ls$biol.prm
# param.ls$biol.prm = param.ls$biol.prm
# phytopl.history = here::here('R','phytoplankton_timeseries_biomass_tonnes_1998_2016.csv')
# zoopl.history = here::here('R','Zooplankton_total_biomass_tonnes_N_20yrs.csv')
#
Expand Down Expand Up @@ -111,10 +109,8 @@ make_atlantis_diagnostic_figures = function(
run.name,

result,
bgm.file,
group.file,
biol.prm,

param.ls,

# fig.width,
# fig.height,
benthic.box,
Expand All @@ -136,10 +132,12 @@ make_atlantis_diagnostic_figures = function(
plot.growth.cons,
plot.cohort,
plot.diet,
plot.consumption,
plot.spatial.biomass,
plot.LTL
){

`%>%` = dplyr::`%>%`
#Utility function
add.title = function(plot,title){
plot = plot + ggplot2::ggtitle(title)+
Expand All @@ -148,13 +146,13 @@ make_atlantis_diagnostic_figures = function(
}

#Load groups data
group.code = atlantistools::get_age_acronyms(group.file)
group.data = atlantistools::load_fgs(group.file)
group.code = atlantistools::get_age_acronyms(param.ls$func.groups)
group.data = atlantistools::load_fgs(param.ls$func.groups)
group.index = dplyr::select(group.data,c(Code,LongName))


#Load BGM data
box.bgm = atlantistools::load_box(bgm.file)
box.bgm = atlantistools::load_box(param.ls$bgm)

#plot parameters
plot.labels = list(x = 'Time (years)',y = 'Biomass (tonnes)')
Expand Down Expand Up @@ -330,12 +328,12 @@ make_atlantis_diagnostic_figures = function(
#Data processing

#mum by age
mum.age = atlantistools::prm_to_df_ages(biol.prm, group.file,group = group.code,parameter = 'mum')
mum.age = atlantistools::prm_to_df_ages(param.ls$biol.prm, param.ls$func.groups,group = group.code,parameter = 'mum')
mum.age = tidyr::spread(mum.age,agecl,mum)
mum.age = dplyr::left_join(mum.age,group.index, by = c('species'='LongName'))

#C by age
C.age = atlantistools::prm_to_df_ages(biol.prm,group.file,group = group.code,parameter = 'C')
C.age = atlantistools::prm_to_df_ages(param.ls$biol.prm,param.ls$func.groups,group = group.code,parameter = 'C')
C.age = tidyr::spread(C.age,agecl,c)
C.age = dplyr::left_join(C.age,group.index,by = c('species' = 'LongName'))

Expand Down Expand Up @@ -476,13 +474,13 @@ make_atlantis_diagnostic_figures = function(

#SN/RN domain-wide
RN.SN = result$SN.box %>%
rename('SN' = atoutput) %>%
left_join(result$RN.box) %>%
rename('RN' = atoutput) %>%
group_by(species,time) %>%
summarize(SN = sum(SN,na.rm=T),
dplyr::rename('SN' = atoutput) %>%
dplyr::left_join(result$RN.box) %>%
dplyr::rename('RN' = atoutput) %>%
dplyr::group_by(species,time) %>%
dplyr::summarize(SN = sum(SN,na.rm=T),
RN = sum(RN,na.rm=T)) %>%
mutate(RN.SN = RN/SN)
dplyr::mutate(RN.SN = RN/SN)


temp.plot.5 = ggplot2::ggplot(RN.SN, ggplot2::aes(x= time,y = RN.SN))+
Expand All @@ -493,8 +491,8 @@ make_atlantis_diagnostic_figures = function(
ggplot2::theme(plot.title = ggplot2::element_text(size = 14),
axis.title = ggplot2::element_text(size = 14),
axis.text = ggplot2::element_text(size = 12),
strip.text = ggplot2::element_text(size = 14))+
ggsave(filename = paste0(atl.dir,'Figures/','test.pdf'),width = 30, height = 30, units = 'in')
strip.text = ggplot2::element_text(size = 14))
# ggsave(filename = paste0(atl.dir,'Figures/','test.pdf'),width = 30, height = 30, units = 'in')

pdf(file = paste0(out.dir,run.name,' SN RN Timeseries.pdf'),width = 60, height = 60, onefile = T)
gridExtra::grid.arrange(temp.plot.1)
Expand Down Expand Up @@ -732,6 +730,7 @@ make_atlantis_diagnostic_figures = function(

#Diet figures
if(plot.diet){

if(!is.na(result$biomass.consumed)){

diet.plots = atlantistools::plot_diet(result$biomass.consumed, wrap_col = 'agecl', combine_thresh = 3)
Expand All @@ -741,10 +740,25 @@ make_atlantis_diagnostic_figures = function(
gridExtra::grid.arrange(diet.plots[[i]])
}
dev.off()

}

}

if(plot.consumption){
source(here::here('R','plot_overall_predation.R'))
consumption = get_consumption(prod.file = param.ls$prod.nc,
fgs.file = param.ls$func.groups)
data.sub = subset_diet(diet.file = param.ls$dietcheck,
consumption = consumption,
spp.names = group.index$Code)
plot_overall_predation(data = data.sub,
bioindex.file = paste0(atl.dir,'neus_outputBiomIndx.txt'),
min.fract = 0.1,
fig.dir = out.dir,
file.prefix = run.name)
}



# Spatial biomass ---------------------------------------------------------

Expand All @@ -753,15 +767,15 @@ make_atlantis_diagnostic_figures = function(
if(plot.spatial.biomass){

temp.plots = atlantistools::plot_spatial_box(result$biomass.spatial.stanza,
bgm_as_df = atlantistools::convert_bgm(bgm = bgm.file), timesteps = 7)
bgm_as_df = atlantistools::convert_bgm(bgm = param.ls$bgm), timesteps = 7)
pdf(file = paste0(out.dir, run.name, ' Spatial Biomass Box Distribution.pdf'),width = 24, height =18 )
for( i in seq_along(temp.plots)){
gridExtra::grid.arrange(temp.plots[[i]])
}
dev.off()

temp.plots.2 = atlantistools::plot_spatial_ts(result$biomass.spatial.stanza,
bgm_as_df = atlantistools::convert_bgm(bgm = bgm.file), vol = result$volume )
bgm_as_df = atlantistools::convert_bgm(bgm = param.ls$bgm), vol = result$volume )
pdf(file = paste0(out.dir, run.name, ' Spatial Biomass Distribution Timeseries.pdf'),width =24, height =18 )
for(i in seq_along(temp.plots.2)){
gridExtra::grid.arrange(temp.plots.2[[i]])
Expand Down
22 changes: 15 additions & 7 deletions R/plot_detailed_diet.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ plot_detailed_diet = function(data,plot.spp,min.fract,pred.prey,fig.dir,file.pre
data.new = data_prey_detailed_diet(data,min.fract = 0.05)
}

plot.cols = c(RColorBrewer::brewer.pal(12,'Set3'),
RColorBrewer::brewer.pal(8,'Dark2'),
RColorBrewer::brewer.pal(8,'Set2'),
RColorBrewer::brewer.pal(9,'Set1'))

boxes = unique(data.new$Box)

#Loop through species
Expand Down Expand Up @@ -238,6 +243,7 @@ plot_detailed_diet = function(data,plot.spp,min.fract,pred.prey,fig.dir,file.pre
#Plot prey
fig = ggplot2::ggplot(data.spp.box, ggplot2::aes(x= Time, y = pct, fill = Prey))+
ggplot2::geom_area(alpha = 0.6, size = 0.25, color = 'black')+
ggplot2::scale_fill_manual(values = plot.cols[1:length(unique(data.spp.box$Prey))])+
ggplot2::ggtitle(paste0('Box ',boxes[b]))+
ggplot2::xlab('Day')+
ggplot2::ylab('% Consumption')+
Expand Down Expand Up @@ -273,6 +279,7 @@ plot_detailed_diet = function(data,plot.spp,min.fract,pred.prey,fig.dir,file.pre
#Plot prey
fig = ggplot2::ggplot(data.spp.box, ggplot2::aes(x= Time, y = pct, fill = Predator))+
ggplot2::geom_area(alpha = 0.6, size = 0.25, color = 'black')+
ggplot2::scale_fill_manual(values = plot.cols[1:length(unique(data.spp.box$Predator))])+
ggplot2::ggtitle(paste0('Box ',boxes[b]))+
ggplot2::xlab('Day')+
ggplot2::ylab('% Consumption')+
Expand All @@ -290,11 +297,12 @@ plot_detailed_diet = function(data,plot.spp,min.fract,pred.prey,fig.dir,file.pre
# Example -----------------------------------------------------------------

#Example Params
detailed.diet.file = 'C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/Obs_Hindcast_DetailedDiet2/neus_outputDetailedDietCheck.txt'
out.name = 'C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/Obs_Hindcast_DetailedDiet2/neus_outputDetailedDietCheck_2.txt'
run.name = 'Obs_Hindcast_RetunePlanktiv6_DetDiet'
detailed.diet.file = paste0('C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/',run.name,'/neus_outputDetailedDietCheck.txt')
out.name = paste0('C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/',run.name,'/neus_outputDetailedDietCheck_2.txt')

subset_detailed_diet(detailed.diet.file,
spp.names = c('MAK','HER','BO'),
spp.names = c('HER'),
pred.prey = 'both',
boxes = 0:9,
levels = -1:3,
Expand All @@ -307,13 +315,13 @@ gc()
data.orig = data.table::fread(out.name)
data2 = longform_detailed_diet(data.orig,rm.zero = T)
plot_detailed_diet(data = data2,
plot.spp = c('MAK','HER'),
fig.dir = 'C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/Obs_Hindcast_DetailedDiet2/Figures/Detailed_Diet/',
plot.spp = c('HER'),
fig.dir = paste0('C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/',run.name,'/Figures/Detailed_Diet/'),
file.prefix = 'test_',
pred.prey = 'prey')
plot_detailed_diet(data = data2,
plot.spp = c('MAK','HER'),
fig.dir = 'C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/Obs_Hindcast_DetailedDiet2/Figures/Detailed_Diet/',
plot.spp = c('HER'),
fig.dir = paste0('C:/Users/joseph.caracappa/Documents/Atlantis/Obs_Hindcast/Atlantis_Runs/',run.name,'/Figures/Detailed_Diet/'),
file.prefix = 'test_',
pred.prey = 'pred')

Expand Down
Loading