diff --git a/r_app/2_CI_data_prep.R b/r_app/2_CI_data_prep.R index fa32198..61d75b3 100644 --- a/r_app/2_CI_data_prep.R +++ b/r_app/2_CI_data_prep.R @@ -33,6 +33,7 @@ laravel_storage_dir <- here("laravel_app","storage","app") #preparing directories project = "chemba" +new_project_question = TRUE planet_tif_folder <- here(laravel_storage_dir, project, "merged_tif") merged_final <- here(laravel_storage_dir, project,"merged_final_tif") @@ -52,6 +53,7 @@ dir.create(here(cumulative_CI_vals_dir)) dir.create(here(weekly_CI_mosaic)) dir.create(here(daily_vrt)) dir.create(merged_final) +dir.create(harvest_dir) @@ -257,27 +259,14 @@ extract_rasters_daily <- function(file, field_geojson, quadrants = TRUE, save_di # pivot_sf_q <- st_read(here("..", "Data", "pivot_20210625.geojson")) %>% dplyr::select(pivot, pivot_quadrant) %>% vect() pivot_sf <- st_read(here(data_dir, "pivot_20210625.geojson")) %>% dplyr::select(pivot, pivot_quadrant) %>% group_by(pivot) %>% summarise() %>% vect() message("pivot loaded") -if (!file.exists(here(cumulative_CI_vals_dir,"combined_CI_data.rds"))) { - - print("combined_CI_data.rds does not exist. Running this part of the script...") -} + raster_files_NEW <- list.files(merged_final,full.names = T, pattern = ".tif") -filtered_files <- map(dates$days_filter, ~ raster_files_NEW[grepl(pattern = .x, x = raster_files_NEW)]) %>% - compact() %>% - flatten_chr() -walk(filtered_files, extract_rasters_daily, field_geojson= pivot_sf_q, quadrants = TRUE, daily_CI_vals_dir) -message("after walk") + pivots_dates0 <- readRDS(here(harvest_dir, "harvest_data_new")) %>% filter( pivot %in% c("1.1", "1.2", "1.3", "1.4", "1.6", "1.7", "1.8", "1.9", "1.10", "1.11", "1.12", "1.13", "1.14" , "1.16" , "1.17" , "1.18" ,"2.1", "2.2", "2.3" , "2.4", "2.5", "3.1", "3.2", "3.3", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "5.1" ,"5.2", "5.3", "5.4", "6.1", "6.2", "DL1.1", "DL1.3") ) - -# pivots_dates_long <- pivots_dates0 %>% -# select(c("pivot_quadrant", "season_start_2021", "season_end_2021", "season_start_2022", "season_end_2022", "season_start_2023", "season_end_2023")) %>% -# pivot_longer(cols = c("season_start_2021", "season_end_2021", "season_start_2022", "season_end_2022", "season_start_2023", "season_end_2023")) %>% -# separate(pivot_quadrant, into = c("name", "Year"), sep = "\\.") - harvesting_data <- pivots_dates0 %>% select(c("pivot_quadrant", "season_start_2021", "season_end_2021", "season_start_2022", "season_end_2022", "season_start_2023", "season_end_2023", "season_start_2024", "season_end_2024")) %>% pivot_longer(cols = starts_with("season"), names_to = "Year", values_to = "value") %>% @@ -285,32 +274,77 @@ harvesting_data <- pivots_dates0 %>% mutate(name = str_to_title(name)) %>% pivot_wider(names_from = name, values_from = value) %>% rename(Field = pivot_quadrant) -# extracted_values <- list.files("C:\\Users\\timon\\Resilience BV\\4002 CMD App - General\\4002 CMD Team\\4002 TechnicalData\\04 WP2 technical\\DetectingSpotsR\\EcoFarm\\planet\\extracted", -# pattern ="_quadrant", full.names = TRUE) -extracted_values <- list.files(here(daily_CI_vals_dir), full.names = TRUE) -#get CI values for this week only + +pivots_data_present <- unique(pivots_dates0$pivot_quadrant) +quadrant_list <- pivots_data_present + +#If run for the firsttime, it will extract all data since the start and put into a table.rds. otherwise it will only add on to the existing table. +if (new_project_question == TRUE) { + + print("combined_CI_data.rds does not exist. Preparing combined_CI_data.rds file for all available images.") + + walk(raster_files_NEW, extract_rasters_daily, field_geojson= pivot_sf_q, quadrants = TRUE, daily_CI_vals_dir) + + extracted_values <- list.files(here(daily_CI_vals_dir), full.names = TRUE) + + pivot_stats <- extracted_values %>% + map(readRDS) %>% list_rbind() %>% + group_by(pivot_quadrant) %>% + summarise(across(everything(), ~ first(na.omit(.)))) + + saveRDS(pivot_stats, here(cumulative_CI_vals_dir,"combined_CI_data.rds")) #used to save the rest of the data into one file + + pivot_stats2 <- pivot_stats + + print("All CI values extracted from allhistoric images") + +} else { + print("combined_CI_data.rds exists, adding the latest image data to the table.") + + filtered_files <- map(dates$days_filter, ~ raster_files_NEW[grepl(pattern = .x, x = raster_files_NEW)]) %>% + compact() %>% + flatten_chr() + + walk(filtered_files, extract_rasters_daily, field_geojson= pivot_sf_q, quadrants = TRUE, daily_CI_vals_dir) + + extracted_values <- list.files(daily_CI_vals_dir, full.names = TRUE) + + extracted_values <- map(dates$days_filter, ~ extracted_values[grepl(pattern = .x, x = extracted_values)]) %>% + compact() %>% + flatten_chr() + + pivot_stats <- extracted_values %>% + map(readRDS) %>% list_rbind() %>% + group_by(pivot_quadrant) %>% + summarise(across(everything(), ~ first(na.omit(.)))) + + combined_CI_data <- readRDS(here(cumulative_CI_vals_dir,"combined_CI_data.rds")) %>% drop_na(pivot_quadrant) + pivot_stats2 <- bind_rows(pivot_stats, combined_CI_data) + print("All CI values extracted from latest 7 images.") + +} + + + + #extracted_values <- map(dates$days_filter, ~ extracted_values[grepl(pattern = .x, x = extracted_values)]) %>% # compact() %>% # flatten_chr() #combine them into one df -pivot_stats <- extracted_values %>% - map(readRDS) %>% list_rbind() %>% - group_by(pivot_quadrant) %>% - summarise(across(everything(), ~ first(na.omit(.)))) + #saveRDS(pivot_stats, here(cumulative_CI_vals_dir,"combined_CI_data.rds")) #used to save the rest of the data into one file #load historic CI data and update it with last week of CI data -combined_CI_data <- readRDS(here(cumulative_CI_vals_dir,"combined_CI_data.rds")) %>% drop_na(pivot_quadrant) -pivot_stats2 <- bind_rows(pivot_stats, combined_CI_data) +# combined_CI_data <- readRDS(here(cumulative_CI_vals_dir,"combined_CI_data.rds")) %>% drop_na(pivot_quadrant) +# pivot_stats2 <- bind_rows(pivot_stats, combined_CI_data) # pivot_stats2 <- purrr::map(list.files(here(daily_CI_vals_dir), full.names = TRUE, pattern = "quadrant"), readRDS) %>% list_rbind() %>% group_by(pivot_quadrant) %>% # summarise(across(everything(), ~ first(na.omit(.)))) -pivots_data_present <- unique(pivots_dates0$pivot_quadrant) -quadrant_list <- pivots_data_present + # gather data into long format for easier calculation and visualisation pivot_stats_long <- pivot_stats2 %>% @@ -321,7 +355,8 @@ pivot_stats_long <- pivot_stats2 %>% drop_na(c("value","Date")) %>% mutate(value = as.numeric(value))%>% filter_all(all_vars(!is.infinite(.)))%>% - rename(Field = pivot_quadrant) + rename(Field = pivot_quadrant) %>% + unique() # #2021 pivots_dates_Data_2021 <- pivots_dates0 %>% filter(!is.na(season_start_2021))