-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcre_var_functs.R
55 lines (49 loc) · 2.13 KB
/
tcre_var_functs.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
#Create the budget distribution calculation function
calc_budget_dist <- function(warm_thresh,temps,emms,years) {
periods <- c()
budgets <- c()
end_points <- c()
for (i in c(1:length(years))){
start_temp <- temps[i]
start_emms <- emms[i]
if (is.na(start_temp)!=TRUE & is.na(start_emms)!=TRUE){
diff_temps <- temps - start_temp
exceed_year <- years[ (diff_temps>=warm_thresh & years>years[i]) ][1]
if (is.na(exceed_year)!=TRUE) {
#Linearly interpolate exactly when warming threshold crossed
t_exceed <- approx(x=c(diff_temps[years==(exceed_year-1)],diff_temps[years==exceed_year]),y=c(exceed_year-1,exceed_year),xout=warm_thresh)$y
#Calculate the correct cumulative emissions up to this point
d_cumems <- sum(emms[(years>=(years[i]+1) & (years<=(exceed_year-1)))]) + 0.5*emms[years==years[i]] + (t_exceed-(exceed_year-1))*emms[years==(exceed_year-1)]
period <- t_exceed - years[i]
periods <- c(periods,period)
budgets <- c(budgets,d_cumems)
end_points <- c(end_points,exceed_year)
}
}
}
return(list(periods,budgets,end_points))
}
calc_budget_dist_allmods <- function(warm_thresh,df,temps_n,emms_n,years) {
out <- list(c(),c(),c())
for (j in c(1:nrow(df[df$Variable==temps_n,]))){
out_e <- calc_budget_dist(warm_thresh,as.numeric(df[df$Variable==temps_n,][j,-c(1:5)]),as.numeric(df[df$Variable==emms_n,][j,-c(1:5)]),years)
if (length(out_e[[2]]>0)){
out[[1]]<- c(out[[1]],out_e[[1]])
out[[2]]<- c(out[[2]],out_e[[2]])
out[[3]]<- c(out[[3]],out_e[[3]])
}
}
return(out)
}
calc_budget_dist_obsens <- function(warm_thresh,temps_e,emms_e,years) {
out <- list(c(),c(),c())
for (j in c(1:nrow(temps_e))){
out_e <- calc_budget_dist(warm_thresh,temps_e[j,],emms_e[j,],years)
if (length(out_e[[2]]>0)){
out[[1]]<- c(out[[1]],out_e[[1]])
out[[2]]<- c(out[[2]],out_e[[2]])
out[[3]]<- c(out[[3]],out_e[[3]])
}
}
return(out)
}