Skip to content
Snippets Groups Projects
Commit c40c815d authored by Simon, Wolfram's avatar Simon, Wolfram
Browse files

Changed the pipe to not using assigments --> like this it prevents from using...

Changed the pipe to not using assigments --> like this it prevents from using too much memory and writes it directly to the drive. I also added the physical land and assumed the tnuts share for the harvested area to be the same for phys area. Yields are excluded.
parent f4841661
No related branches found
No related tags found
No related merge requests found
...@@ -282,24 +282,30 @@ sph.harv = read_sf(dsn = wd, layer = "GADM_treenut_rest_ha") ...@@ -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) sph.all = st_join(sph.prod, sph.harv, join = st_within)
# write_sf(sph.all, dsn = here("Input_data", driver = "ESRI shapefile")) # write_sf(sph.all, dsn = here("Input_data", driver = "ESRI shapefile"))
sph.prod.tib = as_tibble(sph.prod) 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 = as_tibble(sph.harv)
sph.harv.tib = dplyr::select(sph.harv.tib, UID, GID_0, GID_1, trnt_r_)
# loading SPAM-GADM join # loading SPAM-GADM join
spam.gadm.link = read_sf(dsn = here("Input_data", "GADMtoSPAM", "GADM_to_SPAM_within"), spam.gadm.link = read_sf(dsn = here("Input_data", "GADMtoSPAM", "GADM_to_SPAM_within"),
layer = "GADM_to_SPAM_within") layer = "GADM_to_SPAM_within")
# Join spam.gadm.link with sph.harv/prd
# Production # Production
# rbenchmark::benchmark( # rbenchmark::benchmark(
dat_join_prod = spam.gadm.link %>% spam.gadm.link %>%
as_tibble() %>% as_tibble() %>%
left_join(., prd_dat, by="alloc_key") %>%
left_join(.,sph.prod.tib, by="UID") %>% left_join(.,sph.prod.tib, by="UID") %>%
left_join(.,prd_dat, by="alloc_key") %>%
mutate(tnuts = trnt_r_*rest) %>% 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, 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? # Approach validation > do the numbers make sense?
xy=dat_join_prod %>% xy=dat_join_prod %>%
...@@ -312,24 +318,36 @@ write_csv(dat_join_prod, here("Input_data", "dat_treenut_prod.csv")) ...@@ -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) rm(prd_dat, spam.gadm.link, sph.prod, sph.prod.tib,x,xy)
# Harvested area # Harvested area
# rbenchmark::benchmark( # rbenchmark::benchmark(
dat_join_harv = spam.gadm.link %>% spam.gadm.link %>%
as_tibble() %>% as_tibble() %>%
left_join(., har_dat, by="alloc_key") %>% left_join(., har_dat, by="alloc_key") %>%
left_join(.,sph.harv.tib, by="UID") %>% left_join(.,sph.harv.tib, by="UID") %>%
mutate(tnuts = trnt_pd*rest) %>% mutate(tnuts = trnt_r_*rest) %>%
mutate(rest.1 = rest_pd*rest) %>% mutate(rest.1 = (1-trnt_r_)*rest) %>%
mutate(rest.2 = rest-tnuts) #%>% dplyr::select(alloc_key, UID, GID_0.x, GID_1.x, rec_type, tech_type, unit,
select(alloc_key, UID, GID_0.x, GID_1.x, rec_type, tech_type, unit, whea:rest, whea:rest, tnuts, rest.1) %>%
tnuts, rest.2, geometry.x, geometry.y, x,y) write.csv(here("Input_data", "harv_tnuts_spam_gadm"))
# ) # )
write.csv(dat_join_harv, here("Input_data", "dat_treenut_harv.csv"), row.names=T) # Physical land
save.image("dat_join_prod") # rbenchmark::benchmark(
# Join harvested area (sph) with SPAM 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 # Validate results
...@@ -348,7 +366,7 @@ food_cat <- read_csv('foodnonfood.csv') ...@@ -348,7 +366,7 @@ food_cat <- read_csv('foodnonfood.csv')
nam_cat <- read_csv('crop_names.csv') nam_cat <- read_csv('crop_names.csv')
# loading FAO country codes # loading FAO country codes
FAOcntr <- read_csv('FAO_countries.csv') 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 # joining all together
names(dat.comb)[8] <- "Crop_short" names(dat.comb)[8] <- "Crop_short"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment