diff --git a/SPAM_Data_Transformation/SPAM_no_agg_spatial.R b/SPAM_Data_Transformation/SPAM_no_agg_spatial.R index ea4f30e71eadb6b10ba3a3fefa3534be524ee9f8..8f2045a5351cc8dafdeaed432557a16b85249e0a 100644 --- a/SPAM_Data_Transformation/SPAM_no_agg_spatial.R +++ b/SPAM_Data_Transformation/SPAM_no_agg_spatial.R @@ -282,24 +282,30 @@ sph.harv = read_sf(dsn = wd, layer = "GADM_treenut_rest_ha") sph.all = st_join(sph.prod, sph.harv, join = st_within) # write_sf(sph.all, dsn = here("Input_data", driver = "ESRI shapefile")) sph.prod.tib = as_tibble(sph.prod) +sph.prod.tib = dplyr::select(sph.prod.tib, UID, GID_0, GID_1, trnt_r_) + sph.harv.tib = as_tibble(sph.harv) +sph.harv.tib = dplyr::select(sph.harv.tib, UID, GID_0, GID_1, trnt_r_) + # loading SPAM-GADM join spam.gadm.link = read_sf(dsn = here("Input_data", "GADMtoSPAM", "GADM_to_SPAM_within"), layer = "GADM_to_SPAM_within") -# Join spam.gadm.link with sph.harv/prd # Production # rbenchmark::benchmark( -dat_join_prod = spam.gadm.link %>% +spam.gadm.link %>% as_tibble() %>% - left_join(., prd_dat, by="alloc_key") %>% left_join(.,sph.prod.tib, by="UID") %>% + left_join(.,prd_dat, by="alloc_key") %>% mutate(tnuts = trnt_r_*rest) %>% - mutate(rest.1 = (1-trnt_r_)*rest) %>% + mutate(rest.1 = (1-trnt_r_)*rest) %>% dplyr::select(alloc_key, UID, GID_0.x, GID_1.x, rec_type, tech_type, unit, - whea:rest, tnuts, rest.1, geometry.x, geometry.y, x, y) + whea:rest, tnuts, rest.1) %>% + write.csv(here("Input_data", "prod_tnuts_spam_gadm")) + + # Approach validation > do the numbers make sense? xy=dat_join_prod %>% @@ -312,24 +318,36 @@ write_csv(dat_join_prod, here("Input_data", "dat_treenut_prod.csv")) rm(prd_dat, spam.gadm.link, sph.prod, sph.prod.tib,x,xy) # Harvested area # rbenchmark::benchmark( -dat_join_harv = spam.gadm.link %>% +spam.gadm.link %>% as_tibble() %>% left_join(., har_dat, by="alloc_key") %>% left_join(.,sph.harv.tib, by="UID") %>% - mutate(tnuts = trnt_pd*rest) %>% - mutate(rest.1 = rest_pd*rest) %>% - mutate(rest.2 = rest-tnuts) #%>% - select(alloc_key, UID, GID_0.x, GID_1.x, rec_type, tech_type, unit, whea:rest, - tnuts, rest.2, geometry.x, geometry.y, x,y) + mutate(tnuts = trnt_r_*rest) %>% + mutate(rest.1 = (1-trnt_r_)*rest) %>% + dplyr::select(alloc_key, UID, GID_0.x, GID_1.x, rec_type, tech_type, unit, + whea:rest, tnuts, rest.1) %>% + write.csv(here("Input_data", "harv_tnuts_spam_gadm")) # ) -write.csv(dat_join_harv, here("Input_data", "dat_treenut_harv.csv"), row.names=T) -save.image("dat_join_prod") -# Join harvested area (sph) with SPAM +# Physical land +# rbenchmark::benchmark( +spam.gadm.link %>% + as_tibble() %>% + left_join(., phy_dat, by="alloc_key") %>% + left_join(.,sph.harv.tib, by="UID") %>% + mutate(tnuts = trnt_r_*rest) %>% + mutate(rest.1 = (1-trnt_r_)*rest) %>% + dplyr::select(alloc_key, UID, GID_0.x, GID_1.x, rec_type, tech_type, unit, + whea:rest, tnuts, rest.1) %>% + write.csv(here("Input_data", "phys_tnuts_spam_gadm")) + + +# # Calculate yields +# 1. join the two dfs +# 2. Gather crops +# 3. Spread harv. area and production >> ha | Prod | ha*prd(conversion from mt to kg) = yield -# Join production files caluclate ratio treenuts/adjust units -# Calculate yields # Validate results @@ -348,7 +366,7 @@ food_cat <- read_csv('foodnonfood.csv') nam_cat <- read_csv('crop_names.csv') # loading FAO country codes FAOcntr <- read_csv('FAO_countries.csv') -FAOcntr <- select(FAOcntr, 'ISO3', 'FAOSTAT_CODE', "GAUL_CODE", 'Continent', 'Subcontinent') +FAOcntr <- dplyr::select(FAOcntr, 'ISO3', 'FAOSTAT_CODE', "GAUL_CODE", 'Continent', 'Subcontinent') # joining all together names(dat.comb)[8] <- "Crop_short"