205 lines
6.7 KiB
R
205 lines
6.7 KiB
R
# nolint start: commented_code_linter, line_length_linter,object_usage_linter.
|
|
library(here)
|
|
library(sf)
|
|
library(terra)
|
|
library(tidyverse)
|
|
library(lubridate)
|
|
library(exactextractr)
|
|
library(readxl)
|
|
|
|
|
|
# Vang alle command line argumenten op
|
|
args <- commandArgs(trailingOnly = TRUE)
|
|
|
|
# Controleer of er ten minste één argument is doorgegeven
|
|
if (length(args) == 0) {
|
|
stop("Geen argumenten doorgegeven aan het script")
|
|
}
|
|
|
|
# Converteer het eerste argument naar een numerieke waarde
|
|
end_date <- as.Date(args[1])
|
|
|
|
|
|
offset <- as.numeric(args[2])
|
|
# Controleer of weeks_ago een geldig getal is
|
|
if (is.na(offset)) {
|
|
# stop("Het argument is geen geldig getal")
|
|
offset <- 7
|
|
}
|
|
|
|
# Converteer het tweede argument naar een string waarde
|
|
project_dir <- as.character(args[3])
|
|
|
|
# Controleer of data_dir een geldige waarde is
|
|
if (!is.character(project_dir)) {
|
|
project_dir <- "chemba"
|
|
}
|
|
|
|
|
|
laravel_storage_dir <- here("laravel_app/storage/app", project_dir)
|
|
#preparing directories
|
|
planet_tif_folder <- here(laravel_storage_dir, "merged_tif")
|
|
merged_final <- here(laravel_storage_dir, "merged_final_tif")
|
|
|
|
new_project_question = FALSE
|
|
planet_tif_folder <- here(laravel_storage_dir, "merged_tif")
|
|
merged_final <- here(laravel_storage_dir, "merged_final_tif")
|
|
|
|
data_dir <- here(laravel_storage_dir, "Data")
|
|
extracted_CI_dir <- here(data_dir, "extracted_ci")
|
|
daily_CI_vals_dir <- here(extracted_CI_dir, "daily_vals")
|
|
cumulative_CI_vals_dir <- here(extracted_CI_dir, "cumulative_vals")
|
|
|
|
weekly_CI_mosaic <- here(laravel_storage_dir, "weekly_mosaic")
|
|
daily_vrt <- here(data_dir, "vrt")
|
|
harvest_dir <- here(data_dir, "HarvestData")
|
|
|
|
source("parameters_project.R")
|
|
source("ci_extraction_utils.R")
|
|
source("mosaic_creation_utils.R")
|
|
|
|
dir.create(here(laravel_storage_dir))
|
|
dir.create(here(data_dir))
|
|
dir.create(here(extracted_CI_dir))
|
|
dir.create(here(daily_CI_vals_dir))
|
|
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)
|
|
|
|
# end_date <- lubridate::dmy("20-6-2024")
|
|
week <- week(end_date)
|
|
|
|
|
|
#weeks_ago = 0
|
|
# Creating weekly mosaic
|
|
#dates <- date_list(weeks_ago)
|
|
dates <- date_list(end_date, offset)
|
|
print(dates)
|
|
#load pivot geojson
|
|
# pivot_sf_q <- st_read(here(data_dir, "pivot.geojson")) %>% dplyr::select(pivot, pivot_quadrant) %>% vect()
|
|
|
|
# raster_files <- list.files(planet_tif_folder,full.names = T, pattern = ".tif")
|
|
|
|
# filtered_files <- map(dates$days_filter, ~ raster_files[grepl(pattern = .x, x = raster_files)]) %>%
|
|
# compact() %>%
|
|
# flatten_chr()
|
|
# head(filtered_files)
|
|
|
|
raster_files <- list.files(planet_tif_folder,full.names = T, pattern = ".tif")
|
|
|
|
filtered_files <- map(dates$days_filter, ~ raster_files[grepl(pattern = .x, x = raster_files)]) %>%
|
|
compact() %>%
|
|
flatten_chr()
|
|
|
|
# filtered_files <- raster_files #for first CI extraction
|
|
|
|
# create_mask_and_crop <- function(file, field_boundaries) {
|
|
# message("starting ", file)
|
|
# CI <- rast(file)
|
|
# # names(CI) <- c("green","nir")
|
|
# message("raster loaded")
|
|
#
|
|
# CI <- CI[[2]]/CI[[1]]-1
|
|
# # CI <- CI$nir/CI$green-1
|
|
# message("CI calculated")
|
|
# CI <- terra::crop(CI, field_boundaries, mask = TRUE) #%>% CI_func()
|
|
#
|
|
# new_file <- here(merged_final, paste0(tools::file_path_sans_ext(basename(file)), ".tif"))
|
|
# writeRaster(CI, new_file, overwrite = TRUE)
|
|
#
|
|
# vrt_file <- here(daily_vrt, paste0(tools::file_path_sans_ext(basename(file)), ".vrt"))
|
|
# terra::vrt(new_file, vrt_file, overwrite = TRUE)
|
|
#
|
|
# return(CI)
|
|
# }
|
|
|
|
|
|
vrt_list <- list()
|
|
|
|
# for (file in raster_files) {
|
|
# v_crop <- create_mask_and_crop(file, field_boundaries)
|
|
# message(file, " processed")
|
|
# gc()
|
|
# }
|
|
|
|
for (file in filtered_files) {
|
|
v_crop <- create_mask_and_crop(file, field_boundaries)
|
|
emtpy_or_full <- global(v_crop, "notNA")
|
|
|
|
vrt_file <- here(daily_vrt, paste0(tools::file_path_sans_ext(basename(file)), ".vrt"))
|
|
|
|
if(emtpy_or_full[1,] > 10000){
|
|
vrt_list[vrt_file] <- vrt_file
|
|
|
|
}else{
|
|
file.remove(vrt_file)
|
|
|
|
}
|
|
|
|
message(file, " processed")
|
|
gc()
|
|
}
|
|
|
|
# Extracting CI
|
|
|
|
|
|
# pivot_sf_q <- st_read(here("..", "Data", "pivot_20210625.geojson")) %>% dplyr::select(pivot, pivot_quadrant) %>% vect()
|
|
# pivot_sf <- st_read(here(data_dir, "pivot.geojson")) %>% dplyr::select(pivot, pivot_quadrant) %>% group_by(pivot) %>% summarise() %>% vect()
|
|
# message("pivot loaded")
|
|
|
|
raster_files_NEW <- list.files(merged_final,full.names = T, pattern = ".tif")
|
|
|
|
# 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")
|
|
# )
|
|
|
|
# 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") %>%
|
|
# separate(Year, into = c("name", "Year"), sep = "(?<=season_start|season_end)\\_", remove = FALSE) %>%
|
|
# mutate(name = str_to_title(name)) %>%
|
|
# pivot_wider(names_from = name, values_from = value) %>%
|
|
# rename(field = pivot_quadrant)
|
|
|
|
#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.
|
|
|
|
|
|
# Define the path to the file
|
|
file_path <- here(cumulative_CI_vals_dir,"combined_CI_data.rds")
|
|
|
|
# Check if the file exists
|
|
if (!file.exists(file_path)) {
|
|
# Create the file with columns "column1" and "column2"
|
|
data <- data.frame(sub_field=NA, field=NA)
|
|
saveRDS(data, file_path)
|
|
}
|
|
|
|
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= field_boundaries, 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(sub_field)
|
|
|
|
|
|
combined_CI_data <- readRDS(here(cumulative_CI_vals_dir,"combined_CI_data.rds")) #%>% drop_na(pivot_quadrant)
|
|
head(combined_CI_data)
|
|
pivot_stats2 <- bind_rows(pivot_stats, combined_CI_data)
|
|
# pivot_stats2 <- combined_CI_data
|
|
print("All CI values extracted from latest image.")
|
|
saveRDS(pivot_stats2, here(cumulative_CI_vals_dir,"combined_CI_data.rds")) #used to save the rest of the data into one file
|