######################################################################################################################################################
rm(list = ls()) # Clean the memory
######### DEFINE THE WORKING DIRECTORY ##############################################################################################################
setwd("C:\\Users\\Lenovo\\Desktop\\INITIALCHANGE\\Inputs") # or setwd("C:/Users/Lenovo/Desktop/INITIALCHANGE/Inputs")
######################################################################################################################################################
################################################# READ THE DATABASE ········###########################################################################
######## IMPORT TTHE DATA #############################################################################################################################
Data <- read.csv("Approach1B.csv") # Read the database
AD <- Data[, 1] # Column 1: Activity data (area)
U_AD <- Data[, 2] # Column 2: U_Activity data (area)
Gw_A <- Data[, 3] # Column 3: Average annual above-ground biomass growth_After the conversion
Gw_B <- Data[, 9] # Column 9: Average annual above-ground biomass growth_Before the conversion
U_Gw_A <- Data[, 4] # Column 4: U_Average annual above-ground biomass growth_After the conversion
U_Gw_B <- Data[, 10] # Column 10: U_Average annual above-ground biomass growth_Before the conversion
R_A <- Data[, 5] # Column 5: Ratio of below-ground biomass to above-ground biomass_After the conversion
R_B <- Data[, 11] # Column 11: Ratio of below-ground biomass to above-ground biomass_Before the conversion
U_R_A <- Data[, 6] # Column 6: U_Ratio of below-ground biomass to above-ground biomass_After the conversion
U_R_B <- Data[, 12] # Column 12: U_Ratio of below-ground biomass to above-ground biomass_Before the conversion
CF_A <- Data[, 7] # Column 7: Carbon fraction of dry matter_After the conversion
CF_B <- Data[, 13] # Column 13: Carbon fraction of dry matter_Before the conversion
U_CF_A <- Data[, 8] # Column 8: U_Carbon fraction of dry matter_After the conversion
U_CF_B <- Data[, 14] # Column 14: U_Carbon fraction of dry matter_Before the conversion
n <- length(AD) # or length(Any variable) # Sample size
#######################################################################################################################################################
######################################################################################################################################################
############################## EMISSION FACTORS /U_EMISSION FACTORS ##################################################################################
EF <- (Gw_A * (1 + R_A) * CF_A)-(Gw_B * (1 + R_B) * CF_B)
U_EF <- (sqrt((Gw_A *CF_A * sqrt(U_Gw_A^2 + U_CF_A^2))^2+(Gw_A * R_A *CF_A * sqrt(U_Gw_A^2 + U_R_A^2 + U_CF_A^2))^2+(Gw_B *CF_B * sqrt(U_Gw_B^2 + U_CF_B^2))^2 + (Gw_B * R_B *CF_B * sqrt(U_Gw_B^2 + U_R_B^2 + U_CF_B^2))^2))/((Gw_A*CF_A+Gw_A*R_A*CF_A)-(Gw_B*CF_B+Gw_B*R_B*CF_B))
######################################################################################################################################################
############################## REMOVALS /U_REMOVALS ##################################################################################################
REMOVALS <- AD * EF * (-1) * (44/12)
U_REMOVALS <- sqrt(U_AD^2 + U_EF^2)
######################################################################################################################################################
############################## TOTAL REMOVALS /U_TOTAL REMOVALS ######################################################################################
TOTAL_REMOVALS <- sum(REMOVALS)
U_TOTAL_REMOVALS <- sqrt(sum((REMOVALS * U_REMOVALS)^2))/abs(TOTAL_REMOVALS)
######################################################################################################################################################
### CONCATENATE THE DATABASE AND THE ESTIMATED QUANTITIES ############################################################################################
Data_Extended <- data.frame(AD, U_AD, Gw_A, U_Gw_A, R_A, U_R_A, CF_A, U_CF_A, Gw_B, U_Gw_B, R_B, U_R_B, CF_B, U_CF_B, EF, U_EF, REMOVALS, U_REMOVALS)
######################################################################################################################################################
### ADD A ROW OF TOTAL_REMOVALS AND U_TOTAL_REMOVALS ################################################################################################
TOTAL_REMOVALS_U_TOTAL_REMOVALS <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, TOTAL_REMOVALS, U_TOTAL_REMOVALS)
######################################################################################################################################################
### FINAL DATA FRAME ################################################################################################################################
Final.data.frame <- rbind(Data_Extended,TOTAL_REMOVALS_U_TOTAL_REMOVALS)
#######################################################################################################################################################
#################################### FINAL RESULTS ###################################################################################################
##################################### DEFINE THE OUTPUT DIRECTORY #####################################################################################
setwd("C:\\Users\\Lenovo\\Desktop\\INITIALCHANGE\\Outputs") # or setwd("C:/Users/Lenovo/Desktop/INITIALCHANGE/Outputs")
# SAVE THE FINAL RESULTS
write.csv(Final.data.frame,file="Belize_Land converted to Forest land.csv")
################################################### END ###############################################################################################