#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # PART 3: Calculate % of Underestimate C #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # After running the U model "../New_model_20260806/umodel_core_v20260720_necromass_version.m", # run the present script to summarise results library(tidyverse) library(R.matlab) library(raster) library(rnaturalearth) library(rnaturalearthdata) library(sf) library(sp) library(rhdf5) setwd("D:/DPhil/Paper 3/New_model_20260806/output") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # ---- STEP1: DATA IMPORT ===== # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Read necromass respiration and land sink data Necromass_respiration <- list.files(pattern = "constant_temperature_Necromass_respiration") %>% lapply(function(file_name){ h5read(file_name, "/Necromass_respiration") %>% raster( xmn = -12, xmx = 60, ymn = 30, ymx = 76, crs = CRS("+proj=longlat +datum=WGS84 +no_defs") ) }) %>% stack() Land_sink <- list.files(pattern = "constant_temperature_Land_sink") %>% lapply(function(file_name){ h5read(file_name, "/Land_sink") %>% raster( xmn = -12, xmx = 60, ymn = 30, ymx = 76, crs = CRS("+proj=longlat +datum=WGS84 +no_defs") ) }) %>% stack() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # ---- STEP2: CALCULATE UNDERESTIMATED C ===== # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Calculate cumulative land sink cumulative <- function(rasterstack){ result <- stack() %>% addLayer(rasterstack[[1]]) for(i in 2:nlayers(rasterstack)){ result <- result %>% addLayer( result[[nlayers(result)]] + rasterstack[[i]] ) } result } Land_sink_accu <- cumulative( Land_sink ) Land_sink_accu[abs(Land_sink_accu) < 1e-6] <- NA Land_sink_accu[Land_sink_accu > 0.01] <- NA Necromass_respiration_accu <- cumulative( Necromass_respiration ) Necromass_respiration_accu[abs(Necromass_respiration_accu) < 1e-6] <- NA #calculate underestimate percentage Underestimate <- (abs(Land_sink_accu) - Necromass_respiration_accu)/abs(Land_sink_accu) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # ---- STEP3: VISUALISATION ===== # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #visualisation world <- ne_countries(scale = "medium", returnclass = "sf") %>% st_transform(crs = crs(raster("../disease_mask.tif"))) plot(Land_sink_accu[[34]], col = colorRampPalette(c("green", "blue", "red"))(100), main = "Land sink accumulation in 2025 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) plot(Necromass_respiration_accu[[34]], col = colorRampPalette(c("green", "blue", "red"))(100), main = "Necromass respiration accumulation in 2025 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) # Figure 5A plot(Underestimate[[34]] * 100, col = colorRampPalette(c("green", "blue", "red"))(100), main = "Percent of underestimated C loss in 2025 (%)", zlim=c(0,100)) plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) theme1 <- theme_bw()+ # visualisation parameters theme(axis.text.x=element_text(size=16,angle=0,colour="black"), axis.text.y=element_text(size=16,angle=0,colour="black"), axis.title=element_text(size=18), axis.line=element_line(linetype=1,color="black",size=0.1), axis.ticks = element_line(colour="black"), panel.grid.major = element_blank(), #change the major and minor grid lines, panel.grid.minor = element_blank(), #if want to change, check this parameters, I think it's easier to dao that #strip.background = element_rect(colour = "black",size = 0.8), #panel.background = element_rect(colour="black", fill="white"), #panel.border = element_blank(), panel.border = element_rect(colour = "black",fill=NA,size = 1.2), plot.title=element_text(size=14,angle=0,colour="black", face = "italic"), plot.tag=element_text(size=14,angle=0,colour="black", face = "bold"), plot.caption=element_text(size=14,angle=0,colour="black",face = "italic"), axis.title.y=element_text(vjust=1.9), axis.title.x=element_text(vjust=0.5), legend.text=element_text(colour="black",size=14), legend.background= element_rect(fill = "transparent", color = NA), #legend.position = "bottom", legend.title = element_text(colour="black", size=14,angle=0)) ## Figure 5B ========== den <- (Underestimate[[34]][] *100)%>% {.[!is.na(.)]} %>% {.[.>=0]} %>% density() ggplot() + geom_line(aes(x = den$x, y = den$y), linewidth = 0.7) + theme1 + xlab("% of underestimated C") + ylab("Density") ## Table 1 (by country) =========== res <- Underestimate[[34]] * 100 country_boundary <- ne_countries( country = "Ukraine", scale = "medium", returnclass = "sf" ) %>% as("Spatial") %>% spTransform(CRS(crs(res)@projargs)) country_values <- extract(res, country_boundary) country_values <- unlist(country_values) country_values <- country_values[!is.na(country_values)] country_values <- country_values[country_values>=0] mean(country_values, na.rm = TRUE) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # ---- STEP4: SENSITIVITY ===== # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #input data: 'MonteCarlo_outputs' - U model outputs for sensitivity analysis setwd("D:\\DPhil\\Paper 3\\MonteCarlo_outputs") # Read necromass respiration and land sink data h5ls("Annual_record_necromass_run_001.mat") Underestimate_2025 <- c() for(file in list.files(pattern = "\\.mat$")){ Land_sink_accu <- file%>% h5read("/#refs#/b/Land_sink") %>% .[90:123] %>% cumsum() Necromass_respiration_accu <- file %>% h5read("/#refs#/b/Necromass_respiration") %>% .[90:123] %>% cumsum() Underestimate <- (abs(Land_sink_accu) - Necromass_respiration_accu)/abs(Land_sink_accu) Underestimate_2025 <- c( Underestimate_2025, Underestimate[34] ) } Underestimate_2025 #> Underestimate_2025 #[1] 0.4172819 0.4746528 0.4549923 0.3830200 0.4075149 0.3960090 0.4450754 0.3871332 0.4039132 0.4093042 0.3466723 0.4260966 0.3967899 0.4422706 #[15] 0.3724652 0.3466627 0.3725994 0.3797386 0.4249615 0.4577073 0.3878235 0.3984798 0.3217811 0.3969297 0.3439695 0.3822174 0.3543938 0.3871348 #[29] 0.4495392 0.4050457 0.4117229 0.3063144 0.3523400 0.4367080 0.3872859 0.3550538 0.3925361 0.4072736 0.4615737 0.3780021 0.4191894 0.3782043 #[43] 0.4333955 0.3899235 0.4206740 0.4428882 0.3653359 0.4025713 0.3816610 0.4380375 0.4091967 0.3163883 0.4480589 0.4148236 0.4213803 0.4184249 #[57] 0.3629609 0.3938146 0.4518957 0.3965797 0.4440969 0.3907262 0.4462798 0.4262538 0.3818772 0.4477773 0.4304008 0.3227546 0.4020752 0.4074414 #[71] 0.3399024 0.4307023 0.3492056 0.3903836 0.3927039 0.3818149 0.3911773 0.5015975 0.3622753 0.4200112 0.3834715 0.3016087 0.4003746 0.3070139 #[85] 0.4280339 0.4065172 0.4389700 0.4400880 0.4279232 0.3851801 0.4594937 0.3785585 0.4886943 0.4040845 0.3843501 0.4122066 0.3032100 0.4052567 #[99] 0.4525295 0.3090275 mean(Underestimate_2025) #[1] 0.4877302 sd(Underestimate_2025) #[1] 0.1714617