library(plantecophys)
library(tidyverse)

#Create a DF with Photosyn keeping VPD and Vc and Vj constant and varying gsw
my_list <- seq(from = 0.01, to = 1, by = 0.001)

df400=data.frame()
# Loop through the list and print each element
for (i in seq_along(my_list)) {
  current_element <- my_list[[i]]
  
 output=plantecophys::Photosyn(
    Ca = 400,
    Patm = 101.1,
    RH = NULL,
    GS = current_element,
    Rd = 1.1
  )
 df400=rbind(df400,output)
}

df2000=data.frame()
# Loop through the list and print each element
for (i in seq_along(my_list)) {
  current_element <- my_list[[i]]
  
  output=plantecophys::Photosyn(
    Ca = 2000,
    Patm = 101.1,
    RH = NULL,
    GS = current_element,
    Rd = 1.1
  )
  df2000=rbind(df2000,output)
}


df100=data.frame()
# Loop through the list and print each element
for (i in seq_along(my_list)) {
  current_element <- my_list[[i]]
  
  output=plantecophys::Photosyn(
    Ca = 100,
    Patm = 101.1,
    RH = NULL,
    GS = current_element,
    Rd = 1.1
  )
  df100=rbind(df100,output)
}


df<-rbind(df400,df2000,df100)
#Create a DF with same as above but correcting for gcw
my_list <- seq(from = 0.01, to = 1, by = 0.001)
gcw=0.0056 #gcw estimate for citrus
gcc=gcw/20
gsw=.1
gsc=gsw/1.6

#function to recalculate gcc then spit it back out as calibrated to gsw/1.6
duursma_gsw <- function(gsw) {
  gcw=0.0056
  gsw_true=gsw-gcw
  gtc=gsw_true/1.6+gcw/20 #re-estimate gtc using new_gsw for gcw
  gsw_calib=gtc*1.6
  return(gsw_calib)
}

corrected_list<-duursma_gsw(my_list)

df_cor400=data.frame()
# Loop through the list and print each element
for (i in seq_along(corrected_list)) {
  current_element <- corrected_list[[i]]
  
  output=plantecophys::Photosyn(
    Ca = 400,
    Patm = 101.1,
    RH = NULL,
    GS = current_element,
    Rd = 1.1
  )
  df_cor400=rbind(df_cor400,output)
}

df_cor2000=data.frame()
# Loop through the list and print each element
for (i in seq_along(corrected_list)) {
  current_element <- corrected_list[[i]]
  
  output=plantecophys::Photosyn(
    Ca = 2000,
    Patm = 101.1,
    RH = NULL,
    GS = current_element,
    Rd = 1.1
  )
  df_cor2000=rbind(df_cor2000,output)
}

df_cor100=data.frame()
# Loop through the list and print each element
for (i in seq_along(corrected_list)) {
  current_element <- corrected_list[[i]]
  
  output=plantecophys::Photosyn(
    Ca = 100,
    Patm = 101.1,
    RH = NULL,
    GS = current_element,
    Rd = 1.1
  )
  df_cor100=rbind(df_cor100,output)
}

df_cor<-rbind(df_cor400,df_cor2000,df_cor100)

df$GS_cor=df_cor$GS
df$A_cor=df_cor$ALEAF
df$Ci_cor=df_cor$Ci
df<-df%>%
  group_by(Ca)%>%
  mutate(
         prop_Ci=Ci/(Ci[GS==1.00]), #new Ci as a proportion of its value as gtw=1.00
         prop_new_Ci=Ci_cor/(Ci_cor[GS==1.00]), #new Ci as a proportion of its value as gtw=1.00
         err_Ci=abs(Ci-Ci_cor), #the error of the Ci estimate (in ppm) when not accounting for gcw
         prop_err=ifelse(prop_new_Ci>0,
                         (prop_Ci-prop_new_Ci)/prop_new_Ci,
                         NA),
         prop_gsw=gcw/GS
         )

fig1<-df %>%
  ggplot(aes(x=GS,y=prop_Ci,group=Ca))+
  coord_cartesian(xlim=c(0.02,.12), ylim=c(0,0.9))+
  facet_grid(Ca~., scales="free")+
  geom_line(aes(),linetype=8,size=.2) +
  geom_line(aes(y=prop_new_Ci),linetype=1, size=.2) +
  geom_line(aes(y=prop_err),linetype=1, color="darkgreen", size=0.5) +
  geom_hline(yintercept=0.00, linetype=1, color="black", size=0.2)+
  geom_hline(yintercept=0.05, linetype=2, color="blue")+
  geom_ribbon(aes(ymin = prop_new_Ci, ymax = prop_Ci),fill="darkgreen", alpha=0.3) +
  
  #geom_hline(yintercept=0.00, linetype=1, size=0.2, color="black")+
  #geom_hline(yintercept=0.05, linetype=1, size=0.2, color="red")+
  #geom_vline(xintercept=0.05, linetype=2, color="black")+
  #geom_line(aes(),linetype=1,  size=0.5) +
  #geom_line(aes(),linetype=1,size=1) +
  #geom_line(aes(y=prop_err),color="darkgreen",linetype=8, size=.5) +
  #geom_ribbon(aes(ymin = prop_new_Ci, ymax = prop_Ci),fill="pink") +
  xlab(expression(paste(italic("g")["lw"], " (mol m" ^"-2", " sec"^"-1", ")" )))+
  ylab(expression(paste("Proportional values" )))+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        text = element_text(size=12),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"),
        axis.text.x = element_text(angle = 45, hjust = 1))
fig1


setwd("/Users/civince/Dropbox (UFL)/Writing/RACiR/RACiR Manuscript/Major update/Photosynthesis Research")


ggsave(filename = "Figure 1 overall model Duursma.png", fig1,width = 4.5, height = 7, dpi = 300, units = "in", device='png')



magmin=0.09-0.02
papmin=0.17-0.02
pepmin=0.33-0.11
citmin=0.12-0.01
magmax=0.09+0.02
papmax=0.17+0.02
pepmax=0.33+0.11
citmax=0.12+0.01
offsety1=.04

a<-df %>%
  #subset(E==0.01&Ca==400)%>%
  subset()%>%
  ggplot(aes(x=GS,y=prop_Ci, group=Ca))+
  coord_cartesian(xlim=c(0.025,0.5), ylim=c(0,0.9))+
  geom_line(aes(),linetype=8,size=.2) +
  geom_line(aes(y=prop_new_Ci),linetype=1, size=.2) +
  geom_line(aes(y=prop_err, color=as.factor(Ca)),linetype=1,  size=0.5) +
  geom_hline(yintercept=0.00, linetype=1, color="black", size=0.2)+
  geom_hline(yintercept=0.05, linetype=2, color="blue")+
  geom_errorbarh(aes(xmin=magmin,xmax=magmax,y=magy), size=0.4,height=0.02,color="darkcyan")+
  annotate("text", label= "Magnolia",x=(magmin+magmax)/2, y=magy+offsety1, color="darkcyan")+
  geom_errorbarh(aes(xmin=pepmin,xmax=pepmax,y=pepy), size=0.4,height=0.02,color="brown1")+
  annotate("text", label= "Bell Pepper",x=(pepmin+pepmax)/2, y=pepy+offsety1,color="brown1")+
  geom_errorbarh(aes(xmin=citmin,xmax=citmax,y=city), size=0.4,height=0.02, color="darkolivegreen")+
  annotate("text", label= "Citrus",x=(citmin+citmax)/2, y=city+offsety1, color="darkolivegreen")+
  geom_errorbarh(aes(xmin=papmin,xmax=papmax,y=papy), size=0.4,height=0.02, color="blueviolet")+
  annotate("text", label= "Papaya",x=(papmin+papmax)/2, y=papy+offsety1, color="blueviolet")+
  
  geom_ribbon(aes(ymin = prop_new_Ci, ymax = prop_Ci, fill=as.factor(Ca)),alpha=0.3) +
  annotate("text", label= "A",x=0.025, y=0.86, fontface =2)+
  xlab(expression(paste(italic("g")["lw"], " (mol m" ^"-2", " sec"^"-1", ")" )))+
  #xlab(expression(paste("")))+
  ylab(expression(paste("Proportional values" )))+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        text = element_text(size=12),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"),
        axis.text.x = element_text(angle = 45, hjust = 1))+
  guides(fill=guide_legend(title=expression(paste(italic("C")["a"]))))

a


magmin1=0.05932811
papmin1=0.03927273
pepmin1=0.03603693
citmin1=0.04129358
magmax1=0.2833155
papmax1=0.1306047
pepmax1=0.1441477
citmax1=0.1552069
magy=0.1
papy=0.3
pepy=0.4
city=0.2

offsety=.03
offsetx=0.03

b<-df %>%
  #subset(Emax==0.01&Ca==400)%>%
  ggplot(aes(x=prop_gsw,y=prop_new_Ci,group=Ca))+
  coord_cartesian(xlim=c(0.0,0.3), ylim=c(0,0.8))+
  geom_hline(yintercept=0.00, linetype=1, color="black", size=0.2)+
  geom_hline(yintercept=0.05, linetype=2, color="blue")+
  geom_ribbon(aes(ymin = prop_new_Ci, ymax = prop_Ci, fill=as.factor(Ca)), alpha=0.3) +
  geom_line(aes(y=prop_err, color=as.factor(Ca)),linetype=1,  size=0.5) +
  geom_line(aes(),linetype=8,size=.2) +
  geom_line(aes(y=prop_Ci),linetype=1, size=.2) +
  geom_errorbarh(aes(xmin=magmin1,xmax=magmax1,y=magy), size=0.4,height=0.02,color="darkcyan")+
  annotate("text", label= "Magnolia",x=(magmin1+magmax1)/2, y=magy+offsety, color="darkcyan")+
  geom_errorbarh(aes(xmin=pepmin1,xmax=pepmax1,y=pepy), size=0.4,height=0.02,color="brown1")+
  annotate("text", label= "Bell Pepper",x=(pepmin1+pepmax1)/2, y=pepy+offsety,color="brown1")+
  geom_errorbarh(aes(xmin=citmin1,xmax=citmax1,y=city), size=0.4,height=0.02, color="darkolivegreen")+
  annotate("text", label= "Citrus",x=(citmin1+citmax1)/2, y=city+offsety, color="darkolivegreen")+
  geom_errorbarh(aes(xmin=papmin1,xmax=papmax1,y=papy), size=0.4,height=0.02, color="blueviolet")+
  annotate("text", label= "Papaya",x=(papmin1+papmax1)/2, y=papy+offsety, color="blueviolet")+
  annotate("text", label= "B",x=0, y=0.76, fontface =2)+
  #geom_ribbon(aes(ymin = prop_new_Ci, ymax = prop_Ci),fill="darkgreen", alpha=0.3) +
  xlab(expression(paste(italic("g")["cw"],":", italic("g")["lw"], )))+
  ylab(expression(paste("Proportional values" )))+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        text = element_text(size=12),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"),
        axis.text.x = element_text(angle = 45, hjust = 1))+
  guides(fill=guide_legend(title=expression(paste(italic("C")["a"]))))

library(patchwork)
layout <- "
AA
BB
"

figure2<-a+  b+
  plot_layout(design = layout,guides = 'collect')& theme(legend.position = 'top')
setwd("/Users/civince/Dropbox (UFL)/Writing/RACiR/RACiR Manuscript/Major update/Photosynthesis Research")

ggsave(filename = "Figure 2 gcw corrected Ci Duursma.png", figure2,width = 5.1, height = 7, dpi = 300, units = "in", device='png')



