#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # PART 2: Epidemiological model #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Run the script file in the folder "./Ash_dieback_model" # Required data inputs: # * "Hymenoscyphus_fraxineus_paper2/Ash_Dieback.asc": MaxEnt-predicted distribution of H. fraxineus # * "NPP_mean_MOD17A3HGF.tif": Net primary productivity data # * "Fraxinus_excelsior.tif": modelled distribution of host trees # * "LC2001_MCD12Q1_LC_Type1.tif": land cover data # * "p_death_defoliated_model.RData": parameters for describing tree mortality and defoliation curves (output from Part 1 epidemiological model) # Outputs: # * "predicted_first_incidence.tif": map showing the year when ash dieback firstly occurs # * "../New_model_20260806/p_death": a collection of raster files of predicted tree mortality rate (input for u model) # * "../New_model_20260806/NPP/forest_NPP_loss_highres.nc": predicted NPP loss fraction (input for u model) # * "../New_model_20260806/NPP/NPP_from_MODIS.nc": baseline NPP data (input for u model) # # * "forest_NPP_loss_SD.nc": standard deviation of predicted tree mortality rate (sensitivity analysis for u model) # * "temp_p_death_sd": standard deviation of predicted tree mortality rate (sensitivity analysis for u model) library(tidyverse) library(raster) library(rnaturalearth) library(rnaturalearthdata) library(sf) library(sp) # for coordinates handling (可选) library(geosphere) # for great-circle distances setwd("D:/DPhil/Paper 3/Ash_dieback_model") #RUN STEP1-5 FOR DATA LOADING====== model_res <- 0.05 # resolution of disease spread model ext <- extent(c(xmn=-12, xmx=60, ymn=30, ymx=76)) # extent of study 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)) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP1: Define Disease Range ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Criteria: # (1) Valid extent should locate within the distribution range of Hymenoscyphus fraxineus # (2) Valid extent should should not include water bodies # Disease range data disease_range <- raster("Hymenoscyphus_fraxineus_paper2/Ash_Dieback.asc") %>% crop(ext) %>% # cut to the extent calc(function(x) ifelse(x > 0.05, 1, 0)) # let pathogen occurrence probability > 0.05 plot(disease_range) # Resample disease data if(model_res < res(disease_range)[1]){ r1 <- raster(ext, res = model_res) %>% { crs(.) <- CRS("+proj=longlat +datum=WGS84 +no_defs"); . } disease_range_coarse <- resample( disease_range, r1, method = "bilinear" ) } else{ disease_range_coarse <- disease_range %>% aggregate(fact = model_res/res(disease_range)[1], fun = sum) %>% # calculation fraction of disease range when enlarging cell size { . / ( (model_res / res(disease_range)[1]) ^ 2 ) } %>% { . > 0.001} %>% # disease range: fraction > 0.001 { .[.==0] <- NA; .} } # Create land mask if(model_res > 0.5){ # Create empty raster r1 <- raster(ext, res = 0.5) %>% { crs(.) <- CRS("+proj=longlat +datum=WGS84 +no_defs"); . } # Generate land mask land_mask <- ne_countries(scale = "medium", returnclass = "sf") %>% # Get country border shapefile (as 'sf' object) rasterize(r1, field = 1) %>% # convert vector to raster data aggregate(fact = model_res/res(r1)[1], fun = sum) %>% # calculate land fraction (0-1) { . / ( (model_res / res(r1)[1]) ^ 2 ) } %>% { . > 0.4} %>% # define lands: land fraction > 0.4 { .[.==0] <- NA; .} } else{ # Create empty raster r1 <- raster(ext, res = model_res) %>% { crs(.) <- CRS("+proj=longlat +datum=WGS84 +no_defs"); . } # Generate land mask land_mask <- ne_countries(scale = "medium", returnclass = "sf") %>% rasterize(r1, field = 1) } plot(land_mask) # Final disease range mask disease_mask <- (disease_range_coarse & land_mask) plot(disease_mask, main = "Disease range") # Draw country boundary world <- ne_countries(scale = "medium", returnclass = "sf") %>% st_transform(crs = crs(disease_mask)) plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP2: Define short-distance dispersal ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # It is assumed that, for every single point of disease outbreak, disease spread # to neighborhood cells at a constant rate spread_rate <- 51 # km/yr # annual apread of ash dieback in the range between 25 km and 78 km, and a mean annual spread of 51 km # https://balticforestry.lammc.lt/bf/PDF_Articles/2017-23%5B1%5D/Baltic%20Forestry%202017.1_144-149.pdf # Ash dieback first detected in north-eastern Poland in 1992 # Here we use the coordinate of Warsaw as an approximate warsaw_lon <- 21.0122 warsaw_lat <- 52.2297 short_dispersal <- function(init_lon, init_lat, mask, base_year = 1992, spread_rate = 25){ # Arguments: # * init_lon longitude of the initial disease outbreak point # * init_lat latitude of the initial disease outbreak point # * mask a mask that defines the disease affection range # * base_year year when the disease break out initially (default 1992) # * spread_rate disease spread rate in km/yr # find cell index for the initial disease outbreak point init_cell <- cellFromXY(mask, cbind(init_lon, init_lat)) # decide whether the initial point locates within the study extent if(is.na(init_cell)){ stop("point: (", init_lon, ", ", init_lat, "not locates within the study extent.") } if(extract(mask, init_cell) != 1){ stop("point: (", init_lon, ", ", init_lat, "not locates within the disease range mask.") } # find index for all cells in the raster all_coords <- xyFromCell(mask, 1:ncell(mask)) # retuns a ncell x 2 maxtrix: lon, lat as columns # calculate distances from all cells to the initial disease outbreak point d_m <- distGeo(c(init_lon, init_lat), all_coords) # in meters d_km <- d_m / 1000 # calculate first disease outbreak year for all cells beta <- 1/spread_rate # time spend per km spread years <- base_year + beta * d_km # output raster of first disease outbreak year result <- raster(extent(mask), res = res(mask)) %>% { crs(.) <- crs(mask); . } result[mask == 1] <- years[which(mask[]==1)] result } disease_coll <- stack() %>% addLayer(short_dispersal(warsaw_lon, warsaw_lat, disease_mask, spread_rate = spread_rate)) # visualise initial disease outbreak short distance dispersal plot(disease_coll[[1]]) plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP3: Define long-distance dispersal ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # international transport and trade leads to long distance dispersal of ash dieback # disease. Multiple initial break out points can be resulted from introductions # of the pathogen # # it is assumed that the chance of successful long distance dispersal is # proportional to the area of infection areas, but inversely proportional to # the areas that are not infected (align with MacArthur and Wilsons's # biogeographical theory and Levin's metapopulation model; however, the ash # dieback pathogen is unlikely extinct locally, so the extinction process is # ignored). cmax <- 0.3 # maxmimum number of new introductions (when half of the region is colonised) # function to get actual disease first incidence year pathogen_first_incidence <- function(disease_coll){ if(nlayers(disease_coll)==1){ disease_coll[[1]] } else{ disease_coll %>% calc(fun = min) } } # function to get proportion of areas infected pathogen_p <- function(year, disease_coll){ infection_map <- (pathogen_first_incidence(disease_coll) <= year) total_cells <- cellStats(!is.na(infection_map), stat = sum) # total disease range infected_cells <- cellStats(infection_map, stat = sum) # infected range infected_cells/total_cells } # simulate long distance dispersal long_dispersal <- function(simulation_years = 1992:2025, warsaw_lon = 21.0122, warsaw_lat = 52.2297, spread_rate = 75, cmax = 0.3){ # initial disease outbreak in Poland 1992 disease_coll <- stack() %>% addLayer(short_dispersal(warsaw_lon, warsaw_lat, disease_mask, spread_rate = spread_rate)) # simulate transport and trade for(year in simulation_years){ # calculate the current number of new colonisations c_current <- 4 * cmax * pathogen_p(year, disease_coll) * (1 - pathogen_p(year, disease_coll)) # simulate new colonisations if(round(c_current)>0){ for(i in 1:round(c_current)){ # find uninfected areas uninfected_area <- (pathogen_first_incidence(disease_coll) > year) candidate_cells <- which(!is.na(uninfected_area[]) & uninfected_area[] == 1) if(length(candidate_cells) > 0){ # determine new infection point chosen_cell_coords <- sample(candidate_cells, 1) %>% xyFromCell(uninfected_area, .) disease_coll <- disease_coll %>% addLayer(short_dispersal(chosen_cell_coords[1], chosen_cell_coords[2], disease_mask, spread_rate = spread_rate)) } else{ # do nothing, as all areas have already been infected } } } else{ # do nothing, as there is no new colonisation } } disease_coll } if(!file.exists("predicted_first_incidence.tif")){ # simulate long distance dispersal disease_coll <- long_dispersal(simulation_years = 1992:2025, warsaw_lon, warsaw_lat,spread_rate = 75, cmax = 0.3) # where cmax = 0.3 is the optimal fitted value (see STEP4) pathogen_first_incidence(disease_coll) %>% plot plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) # Save result writeRaster( pathogen_first_incidence(disease_coll), "predicted_first_incidence.tif", overwrite = TRUE ) } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP4: Fit Model with Real Incidence Data ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Real disease incidence year: # In 2008, ash dieback was reported in Haute-Saône department, Franche-Comté region), # France (47.7570° N, 6.1556° E) # Ref: https://gd.eppo.int/reporting/article-388 # In 2005, ash dieback was reported in federal states of Brandenburg, # Mecklenburg-Western Pomerania and Saxony-Anhalt (north-eastern Germany) # (52.4125° N, 12.5316° E) # Ref: https://www.nw-fva.de/fileadmin/nwfva/publikationen/pdf/enderle_2017_ash_dieback_in_germany_-.pdf # In 2012, ash dieback was firstly found in South East England (approximately # (51.2000° N, 0.2700° E) # Ref: https://www.britishecologicalsociety.org/first-occurrence-of-ash-dieback-in-britain/ # In 2008, ash dieback pathogen was isolated in Østfold County, Norway # (59.2558° N, 11.3279° E) # Ref: https://pubmed.ncbi.nlm.nih.gov/30764156/ # In 2010, ash dieback was first noticed in Kharkiv in Ukraine # (50.0020° N, 36.3074° E) # Ref: https://gd.eppo.int/reporting/article-2675 # In 2009, the first case of ash dieback in Italy was noticed in a 1.8-ha ash-maple forest # Fusine, UD; 46°30'N, 13°37'E; 782 m above sea level (46.5000° N, 13.6167° E) # Ref: https://pubmed.ncbi.nlm.nih.gov/30754429/#:~:text=Abstract,ash%20and%2070%25%20of%20seedlings. if(FALSE){ # this part is only for fitting the model, not for running and testing the model. # So it will not be run by default first_record_data <- tibble( year = c(2008, 2005, 2012, 2008, 2010, 2009), country = c("France", "Germany", "United Kingdom", "Norway", "Ukraine", "Italy"), longitude = c(6.1556, 12.5316, 0.2700, 11.3279, 36.3074, 13.6167), latitude = c(47.7570, 52.4125, 51.2000, 59.2558, 50.0020, 46.5000) ) # define quadratic loss function L_quad <- function(result, first_record_data){ # result: a raster that describes the predicted first incidence of disease # first_record_data: real data on disease incidence on several geographical points # Get cell index for all real data points cells <- cellFromXY(result, cbind(first_record_data$longitude, first_record_data$latitude)) # Extract values predicted_values <- extract(result, cells) sum((first_record_data$year - predicted_values)^2) } # fit cmax and spread_rate parameter potential_cmax <- seq(0.1, 1, 0.1) potential_spread_rate <- c(75) repetion <- 3 loss_data <- tibble(cmax = c(), spread_rate = c(), repetition = c(), L = c()) for(cmax in potential_cmax){ for(spread_rate in potential_spread_rate){ for(i in 1:repetion){ cat("cmax =", cmax, "; repetition =", i, ", spread_rate =", spread_rate, "\n") disease_coll <- long_dispersal(simulation_years = 1992:2025, warsaw_lon, warsaw_lat,spread_rate, cmax) L <- L_quad( pathogen_first_incidence(disease_coll), first_record_data ) loss_data <- rbind(loss_data, tibble(cmax = cmax, spread_rate = spread_rate, repetition = i, L = L)) } } } # visualise loss function save(loss_data, file = "loss_data_fit_spread_model.RData") loss_data %>% group_by(cmax, spread_rate) %>% summarise(Lmean = mean(L), Lsd = sd(L)) %>% mutate(spread_rate=factor(spread_rate)) %>% ggplot(aes(x = cmax, y = Lmean)) + geom_smooth() + geom_pointrange(aes(ymin = Lmean - Lsd, ymax = Lmean + Lsd)) + theme1 + ylab("Loss function") +xlab(expression(C[max])) # visualise the optimal model disease_coll <- long_dispersal(simulation_years = 1992:2025, warsaw_lon, warsaw_lat,spread_rate = 75, cmax = 0.3) pathogen_first_incidence(disease_coll) %>% plot plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) points( first_record_data$longitude, first_record_data$latitude, pch = 19, col = "red", cex = 1.2 ) offset_x <- c(-17, 0, -17, 0, 0, 0) offset_y <- c(0, 0, 0, -0, 0, 0) text( first_record_data$longitude + offset_x, first_record_data$latitude + offset_y, labels = first_record_data$year, pos = 4, cex = 0.9, col = "blue" ) } # Actual year of first disease incidence #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP5: Calculate NPP ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Remaining NPP = NPP * D (host density in %) * (1 - p_death) * (1 - p_defoliated) # * forested_fraction (if the focus is forest carbon stock) # Import data first_yr <- raster("predicted_first_incidence.tif") NPP <- raster("NPP_mean_MOD17A3HGF.tif") D <- raster("Fraxinus_excelsior.tif") %>% crop(ext) LC <- raster("LC2001_MCD12Q1_LC_Type1.tif") load("p_death_defoliated_model.RData") # Unify scales to align with the disease model (first_yr) (forest_fraction <- (LC %in% 1:5) %>% # find forested range aggregate(fact = res(first_yr)[1]/res(LC)[1], fun = sum) %>% # calculation fraction of forest range { . / ( (res(first_yr)[1]/res(LC)[1]) ^ 2 ) }) %>% plot(main = "Forest fraction") forest_fraction[][forest_fraction[]==0] <- NA plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) if(model_res >= res(D)[1]){ (D <- D %>% aggregate(fact = res(first_yr)[1]/res(D)[1], fun = mean)) %>% plot(main = "Host Density") } (NPP <- NPP %>% aggregate(fact = res(first_yr)[1]/res(NPP)[1], fun = mean)) %>% plot(main = "NPP") # Resampling NPP <- resample(NPP, first_yr, "bilinear") D <- resample(D, first_yr, "bilinear") forest_fraction <- resample(forest_fraction, first_yr, "bilinear") # added on 15/07/2026 - output p_death raster for an updated version of u model if(dir.exists("../New_model_20260806/p_death")) dir.create("../New_model_20260806/p_death") if(!file.exists("../New_model_20260806/p_death/p_death_year_1992.tiff")){ p_death_folder <- "../New_model_20260806/p_death" ## create folder if (!dir.exists(p_death_folder)) { dir.create(p_death_folder, recursive = TRUE) } simulation_years <- 1992:2025 for(year in simulation_years){ # calculate time since the first incidence of the pathogen since_infected <- first_yr %>% calc(function(x) ifelse(year - x > 0, year - x, 0)) p_death <- since_infected p_death[] <- ifelse( since_infected[]==0, 0, predict(fit_mortality, newdata = tibble(Since.Infected = since_infected[])) ) out_file <- paste0(p_death_folder, "/p_death_D_year_", year, ".tiff") writeRaster( p_death * D, filename = out_file, overwrite = TRUE, # 覆盖旧文件 filetype = "GTiff" # 标准GeoTIFF ) } } # calculate NPP loss calc_NPP_loss <- function(simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity, new_Asym_death = NULL, new_Asym_defo = NULL, attribute_death = FALSE, use_severity = TRUE){ NPP_loss <- stack() for(year in simulation_years){ # calculate time since the first incidence of the pathogen since_infected <- first_yr %>% calc(function(x) ifelse(year - x > 0, year - x, 0)) # calculate p_death (in percentage) p_death <- since_infected if(is.null(new_Asym_death)){ p_death[] <- ifelse( since_infected[]==0, 0, predict(fit_mortality, newdata = tibble(Since.Infected = since_infected[])) ) } else{ p_death[] <- ifelse( since_infected[]==0, 0, SSlogis(since_infected[], new_Asym_death, coef(fit_mortality)[2], coef(fit_mortality)[3]) ) } # calculate p_defoliated (in percentage) if(!attribute_death){ p_defoliated <- since_infected if(is.null(new_Asym_defo)){ p_defoliated[] <- ifelse( since_infected[]==0, 0, predict(fit_severity, newdata = tibble(Since.Infected = since_infected[])) ) } else{ p_defoliated[] <- ifelse( since_infected[]==0, 0, SSlogis(since_infected[], new_Asym_defo, coef(fit_severity)[2], coef(fit_severity)[3]) ) } } # calculate the remaining NPP if(!attribute_death){ NPP_loss <- NPP_loss %>% addLayer(NPP * D - NPP * D * (1 - p_death * 0.01) * (1 - p_defoliated * 0.01)) } else{ NPP_loss <- NPP_loss %>% addLayer(NPP * D - NPP * D * (1 - p_death * 0.01)) } } NPP_loss } NPP_loss <- calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity ) NPP_loss_from_death <- calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity, attribute_death = TRUE ) forest_NPP_loss <- NPP_loss * forest_fraction # outputs for u model if(!file.exists("../New_model_20260806/NPP/forest_NPP_loss_highres.nc")){ # percentage of NPP loss writeRaster(forest_NPP_loss/NPP, filename = "../New_model_20260806/NPP/forest_NPP_loss_highres.nc", format = "CDF", varname = "npp", # 变量名 varunit = "%", # 变量单位 KgC/m2/yr longname = "Forest NPP Loss", # 长名称 xname = "longitude", # x坐标名称 yname = "latitude", # y坐标名称 zname = "year", overwrite = TRUE) # NPP_ref writeRaster(NPP * 0.0001, filename = "../New_model_20260806/NPP/NPP_from_MODIS.nc", format = "CDF", varname = "npp", # 变量名 varunit = "KgC/m2", # 变量单位 KgC/m2/yr longname = "NPP reference", # 长名称 xname = "longitude", # x坐标名称 yname = "latitude", # y坐标名称 overwrite = TRUE) } #FURTHER ANALYSIS================= #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP6: Visualise Simulation Results ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Loss of NPP in 2000, 2005, 2010, 2015, 2020, 2025 (NPP_loss[[2000-1992+1]] * 0.0001) %>% plot(main = "Loss of NPP 2000 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (NPP_loss[[2005-1992+1]] * 0.0001) %>% plot(main = "Loss of NPP 2005 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (NPP_loss[[2010-1992+1]] * 0.0001) %>% plot(main = "Loss of NPP 2010 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (NPP_loss[[2015-1992+1]] * 0.0001) %>% plot(main = "Loss of NPP 2015 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (NPP_loss[[2020-1992+1]] * 0.0001) %>% plot(main = "Loss of NPP 2020 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (NPP_loss[[2025-1992+1]] * 0.0001) %>% plot(main = "Loss of NPP 2025 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) ##Fig 2========== # Loss of NPP in forested areas. (forest_NPP_loss[[2000-1992+1]] * 0.0001) %>% plot(main = "Loss of forest NPP 2000 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (forest_NPP_loss[[2005-1992+1]] * 0.0001) %>% plot(main = "Loss of forest NPP 2005 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (forest_NPP_loss[[2010-1992+1]] * 0.0001) %>% plot(main = "Loss of forest NPP 2010 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (forest_NPP_loss[[2015-1992+1]] * 0.0001) %>% plot(main = "Loss of forest NPP 2015 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (forest_NPP_loss[[2020-1992+1]] * 0.0001) %>% plot(main = "Loss of forest NPP 2020 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (forest_NPP_loss[[2025-1992+1]] * 0.0001) %>% plot(main = "Loss of forest NPP 2025 (kg C/m²)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) ## Fig 3========= # Attribution to tree mortality (((NPP_loss_from_death) / NPP_loss * 100)[[2000-1992+1]]) %>% plot(main = "NPP loss attributed to tree mortality 2000 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (((NPP_loss_from_death) / NPP_loss * 100)[[2005-1992+1]]) %>% plot(main = "NPP loss attributed to tree mortality 2005 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (((NPP_loss_from_death) / NPP_loss * 100)[[2010-1992+1]]) %>% plot(main = "NPP loss attributed to tree mortality 2010 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (((NPP_loss_from_death) / NPP_loss * 100)[[2015-1992+1]]) %>% plot(main = "NPP loss attributed to tree mortality 2015 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (((NPP_loss_from_death) / NPP_loss * 100)[[2020-1992+1]]) %>% plot(main = "NPP loss attributed to tree mortality 2020 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) (((NPP_loss_from_death) / NPP_loss * 100)[[2025-1992+1]]) %>% plot(main = "NPP loss attributed to tree mortality 2025 (%)") plot(world, add = TRUE, border = "black", col = NA, lwd = 0.5) # Size: 5.94 x 3.86 ## Fig 1A ====== # Forest NPP loss change over year plot_NPP_loss_year <- function(NPP_loss, simulation_years, return_data = FALSE){ #summarise NPP loss data across all cells series <- cellStats( area(NPP_loss) * 1e6 * # km2 to m2 NPP_loss * 0.0001, # kg*C/m^2, scaling factor 0.0001 'sum' ) theme1 <- theme_bw()+ 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)) series <- series/cellStats(area(NPP_loss) * 1000000, 'sum') #divide the area (in m2) - NPP unit: kg C/m2 if(return_data){ tibble( year = simulation_years, NPP_loss_kgC = series ) } else{ tibble( year = simulation_years, NPP_loss_kgC = series ) %>% ggplot(aes(x = year, y = NPP_loss_kgC)) + geom_point(size = 2) + geom_line(linewidth = 0.7) + theme1 + ylab("NPP loss (kg C/yr/m²)") } } plot_NPP_loss_year(forest_NPP_loss, 1992:2025) ## Fig 1B========= # Density distribution of pixels lapply(c(2005, 2010, 2015, 2020, 2025), function(year){ start_year <- 1992 index <- year - start_year + 1 forest_NPP_loss[[index]][] %>% {.[!is.na(.)]} %>% #remove NAs tibble(value = .) %>% mutate(Year = factor(year)) }) %>% do.call(rbind, .) %>% ggplot(aes(x = value * 0.0001, color = Year, fill = Year)) + geom_density(linewidth = 0.7, alpha = 0.05) + theme1 + scale_color_manual(values = c( "#F2AA9D","#E75E46","#B93018","#681B0D","#170603" )) + scale_fill_manual(values = c( "#F2AA9D","#E75E46","#B93018","#681B0D","#170603" )) +xlab("NPP Loss (kg C/m²)") + theme(legend.key = element_blank()) + xlim(0, 0.2) # Pick color: https://htmlcolorcodes.com/zh/yanse-xuanze-qi/ # Summarise by country calc_loss_by_country <- function(loss_raster, country = "United Kingdom"){ # Get country boundary country_sp <- ne_countries(scale = "large", country = country, returnclass = "sf") %>% as("Spatial") # transform to vector data # Crop raster raster_country <- loss_raster %>% crop(country_sp) %>% # crop raster to specific region to make computation faster mask(country_sp) # pixels not located in the specified country should be NA # Calculate pixel area cell_area_m2 <- raster_country %>% area() %>% # calculate the ara for each pixel {. * 1e6} # convert km2 to m2 # Calculate NPP loss of the country sum(cellStats(raster_country * cell_area_m2, 'sum')/cellStats(cell_area_m2, 'sum')) } ## Table S1======= calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "United Kingdom") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "United Kingdom") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "United Kingdom") calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "Sweden") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "Sweden") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "Sweden") calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "Norway") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "Norway") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "Norway") calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "Germany") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "Germany") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "Germany") calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "France") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "France") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "France") calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "Poland") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "Poland") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "Poland") calc_loss_by_country(forest_NPP_loss[[2005-1992+1]] * 0.0001, "Estonia") calc_loss_by_country(forest_NPP_loss[[2015-1992+1]] * 0.0001, "Estonia") calc_loss_by_country(forest_NPP_loss[[2025-1992+1]] * 0.0001, "Estonia") # Comparsion with fossil fuel emission # Emission data source: https://openclimatedata.net/emissions/gcb-fossil/2024v18/united-kingdom/ # Area of countries obtained from Google search "United Kingdom, 313.8 Mt CO2, area 243,610 km²; Sweden, 37.47 Mt CO2, area 450,295 km²; Norway, 40.81 Mt CO2, area 385,207 km²; Germany, 678.8 Mt CO2, area 357,022 km²; France, 293.5 Mt CO2, area 632,702 km²; Poland, 315.0 Mt CO2, area 312,685 km²; Estonia, 11.76 Mt CO2, area 45,335 km²" %>% strsplit(";") %>% { .[[1]] } %>% str_remove("\n") %>% tibble(raw = .) %>% mutate( emission_Mt = str_extract(raw, "\\d+\\.?\\d*(?= Mt CO2)") %>% as.numeric(), area_km2 = str_extract(raw, "\\d{1,9}(?:,\\d{1,9})*(?= km²)") %>% str_remove(",") %>% as.numeric(), kgC_m2 = (emission_Mt/44*12 * 1e9) / (area_km2 * 1e6) #CO2 convert to C, Mt C/km2 to kg C/m2 ) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP7: Explore Sensitivity ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## Figure 4===== # Effect of increased spread rate (spread_rate) NPP_spread_rate <- tibble(year=c(), NPP_loss_kgC=c(), spread_rate = c()) for(spread_rate in c(seq(20, 70, 10), 75)){ cat("spread_rate =", spread_rate, "\n") first_yr <- long_dispersal(simulation_years = 1992:2005, spread_rate = spread_rate) %>% pathogen_first_incidence() NPP_loss_per_yr <- (calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity ) * forest_fraction) %>% plot_NPP_loss_year(1992:2025, return_data = TRUE) NPP_spread_rate <- rbind( NPP_spread_rate, NPP_loss_per_yr %>% mutate(spread_rate = spread_rate) ) } ggplot() + geom_point(data = NPP_spread_rate %>% filter(spread_rate != 75), aes(x = year, y = NPP_loss_kgC, color = factor(spread_rate)), size = 2) + geom_line(data = NPP_spread_rate %>% filter(spread_rate != 75), aes(x = year, y = NPP_loss_kgC, color = factor(spread_rate)), linewidth = 0.7) + geom_line(data = NPP_spread_rate %>% filter(spread_rate == 75), aes(x = year, y = NPP_loss_kgC), linewidth = 1) + theme1 + ylab("NPP loss (kg C/yr/m²)") + labs(color = "Spread rate (km/yr)") # Effect of increasing trade (cmax) NPP_cmax <- tibble(year=c(), NPP_loss_kgC=c(), cmax = c()) for(cmax in c(seq(0.5, 4.5, 0.5),0.3)){ cat("cmax =", cmax, "\n") first_yr <- long_dispersal(simulation_years = 1992:2005, cmax = cmax) %>% pathogen_first_incidence() NPP_loss_per_yr <- (calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity ) * forest_fraction) %>% plot_NPP_loss_year(1992:2025, return_data = TRUE) NPP_cmax <- rbind( NPP_cmax, NPP_loss_per_yr %>% mutate(cmax = cmax) ) } ggplot() + geom_point(data = NPP_cmax %>% filter(cmax != 0.3), aes(x = year, y = NPP_loss_kgC, color = factor(cmax)), size = 2) + geom_line(data = NPP_cmax %>% filter(cmax != 0.3), aes(x = year, y = NPP_loss_kgC, color = factor(cmax)), linewidth = 0.7) + geom_line(data = NPP_cmax %>% filter(cmax == 0.3), aes(x = year, y = NPP_loss_kgC), linewidth = 1) + theme1 + ylab(expression(c[max])) + labs(color = "Spread rate (km/yr)") # Effect of tree mortality (p_death, changing Asym or phi1 in the mortality model) NPP_Asym <- tibble(year=c(), NPP_loss_kgC=c(), cmax = c()) for(Asym in c(seq(40, 90, 10),54.65)){ cat("Asym =", Asym, "\n") first_yr <- long_dispersal(simulation_years = 1992:2005) %>% pathogen_first_incidence() NPP_loss_per_yr <- (calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity, new_Asym_death = Asym ) * forest_fraction) %>% plot_NPP_loss_year(1992:2025, return_data = TRUE) NPP_Asym <- rbind( NPP_Asym, NPP_loss_per_yr %>% mutate(Asym = Asym) ) } ggplot() + geom_point(data = NPP_Asym %>% filter(Asym != 54.65), aes(x = year, y = NPP_loss_kgC, color = factor(Asym)), size = 2) + geom_line(data = NPP_Asym %>% filter(Asym != 54.65), aes(x = year, y = NPP_loss_kgC, color = factor(Asym)), linewidth = 0.7) + geom_line(data = NPP_Asym %>% filter(Asym == 54.65), aes(x = year, y = NPP_loss_kgC), linewidth = 1) + theme1 + ylab("NPP loss (kg C/yr/m²)") + labs(color = "Maximal Mortality (%)") # Effect of tree defoliation (p_defoliation) NPP_Asym_defo <- tibble(year=c(), NPP_loss_kgC=c(), cmax = c()) for(Asym_defo in c(seq(40, 90, 10),51.98)){ cat("Asym_defo =", Asym_defo, "\n") first_yr <- long_dispersal(simulation_years = 1992:2005) %>% pathogen_first_incidence() NPP_loss_per_yr <- (calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity, new_Asym_defo = Asym_defo ) * forest_fraction) %>% plot_NPP_loss_year(1992:2025, return_data = TRUE) NPP_Asym_defo <- rbind( NPP_Asym_defo, NPP_loss_per_yr %>% mutate(Asym_defo = Asym_defo) ) } ggplot() + geom_point(data = NPP_Asym_defo %>% filter(Asym_defo != 51.98), aes(x = year, y = NPP_loss_kgC, color = factor(Asym_defo)), size = 2) + geom_line(data = NPP_Asym_defo %>% filter(Asym_defo != 51.98), aes(x = year, y = NPP_loss_kgC, color = factor(Asym_defo)), linewidth = 0.7) + geom_line(data = NPP_Asym_defo %>% filter(Asym_defo == 51.98), aes(x = year, y = NPP_loss_kgC), linewidth = 1) + theme1 + ylab("NPP loss (kg C/yr/m²)") + labs(color = "Maximal Defoliation (%)") #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #==== STEP8: Sensitivity (proposed by Nick) ==== #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # PART1: Generate SD file for forest NPP loss fraction # parameter sets sensitivity_range <- 0.15 # ±15% spread_rate <- 75 * c(1-sensitivity_range,1+sensitivity_range) cmax <- 0.3 * c(1-sensitivity_range,1+sensitivity_range) NPP_Asym <- 54.65 * c(1-sensitivity_range,1+sensitivity_range) NPP_Asym_defo <- 51.98 * c(1-sensitivity_range,1+sensitivity_range) # monte carlo simulation for(isim in 1:100){ cat("simulation = ", isim,"\n") spread_rate_i <- runif(n = 1, min = spread_rate[1], max = spread_rate[2]) cmax_i <- runif(n = 1, min = cmax[1], max = cmax[2]) NPP_Asym_i <- runif(n = 1, min = NPP_Asym[1], max = NPP_Asym[2]) NPP_Asym_defo_i <- runif(n = 1, min = NPP_Asym_defo[1], max = NPP_Asym_defo[2]) first_yr <- long_dispersal(simulation_years = 1992:2005, spread_rate = spread_rate_i, cmax = cmax_i) %>% pathogen_first_incidence() forest_NPP_loss <- (calc_NPP_loss( simulation_years = 1992:2025, first_yr, NPP, D, fit_mortality, fit_severity, new_Asym_death = NPP_Asym_i, new_Asym_defo = NPP_Asym_defo_i ) * forest_fraction) if(!dir.exists("temp_NPP_sd")) dir.create("temp_NPP_sd") out_file <- file.path("temp_NPP_sd", paste0("sensitivity_forest_NPP_isim", isim, ".tif")) writeRaster(forest_NPP_loss, filename = out_file, format = "GTiff", overwrite = TRUE) } # calculate NPP sd per year for(ilayer in 1:nlayers(forest_NPP_loss)){ cat("ilayer = ", ilayer, "\n") st <- list.files("temp_NPP_sd/", pattern = "sensitivity_forest_NPP_isim", full.names = TRUE) %>% lapply(raster, band = ilayer) %>% stack() sd_r <- calc(st, fun = sd, na.rm = TRUE) out_tif <- file.path("temp_NPP_sd", paste0("SD_forest_NPP_layer", ilayer, ".tif")) writeRaster(sd_r, filename = out_tif, overwrite = TRUE) rm(st, sd_r) } # combine NPP sd files files <- list.files( "temp_NPP_sd/", pattern = "SD_forest_NPP_layer", full.names = TRUE ) files <- files[order(as.numeric( sub(".*layer(\\d+)\\.tif$", "\\1", files) ))] sd_brick <- files %>% lapply(raster) %>% stack() writeRaster(sd_brick/NPP, filename = "forest_NPP_loss_SD.nc", format = "CDF", varname = "npp", # 变量名 varunit = "%", # 变量单位 KgC/m2/yr longname = "Forest NPP Loss", # 长名称 xname = "longitude", # x坐标名称 yname = "latitude", # y坐标名称 zname = "year", overwrite = TRUE) # PART2: Generate SD file for p_death # added on 15/07/2026 - output p_death raster for an updated version of u model sensitivity_range <- 0.15 # ±15% NPP_Asym <- 54.65 * c(1-sensitivity_range,1+sensitivity_range) first_yr <- raster("predicted_first_incidence.tif") if(!dir.exists("temp_p_death_sd")) dir.create("temp_p_death_sd") new_NPP_Asym <- runif(n = 100, min = NPP_Asym[1], max = NPP_Asym[2]) for(year in 1992:2025){ cat("year =", year, "\n") # calculate time since the first incidence of the pathogen since_infected <- first_yr %>% calc(function(x) ifelse(year - x > 0, year - x, 0)) year_p_death_stack <- lapply(new_NPP_Asym, function(new_NPP_Asym_i){{ p_death <- since_infected p_death[] <- ifelse( since_infected[]==0, 0, SSlogis(since_infected[], new_NPP_Asym_i, coef(fit_mortality)[2], coef(fit_mortality)[3]) ) return(p_death) }}) %>% stack() year_p_death_sd <- calc(year_p_death_stack, fun = sd, na.rm = TRUE) out_file <- paste0("temp_p_death_sd/p_death_D_sd_year_", year, ".tiff") writeRaster( year_p_death_sd * D, filename = out_file, overwrite = TRUE, # 覆盖旧文件' filetype = "GTiff" # 标准GeoTIFF ) }