144 lines
5.1 KiB
R
144 lines
5.1 KiB
R
|
|
library(here)
|
|
library(sf)
|
|
library(terra)
|
|
library(tidyverse)
|
|
library(lubridate)
|
|
library(exactextractr)
|
|
library(readxl)
|
|
|
|
|
|
# Vang alle command line argumenten op
|
|
args <- commandArgs(trailingOnly = TRUE)
|
|
|
|
|
|
# Converteer het tweede argument naar een string waarde
|
|
project_dir <- as.character(args[1])
|
|
|
|
# Controleer of data_dir een geldige waarde is
|
|
if (!is.character(project_dir)) {
|
|
project_dir <- "sony"
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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")
|
|
|
|
|
|
pivot_stats2 <- readRDS(here(cumulative_CI_vals_dir,"combined_CI_data.rds")) %>%
|
|
ungroup() %>%
|
|
group_by(field, sub_field) %>%
|
|
summarise(across(everything(), ~ first(na.omit(.))), .groups = "drop")
|
|
|
|
#%>% drop_na(pivot_quadrant)
|
|
|
|
# gather data into long format for easier calculation and visualisation
|
|
pivot_stats_long <- pivot_stats2 %>%
|
|
tidyr::gather("Date", value, -field, -sub_field ) %>%
|
|
mutate(#Date = right(Date, 8),
|
|
Date = lubridate::ymd(Date)
|
|
) %>%
|
|
drop_na(c("value","Date")) %>%
|
|
mutate(value = as.numeric(value))%>%
|
|
filter_all(all_vars(!is.infinite(.))) %>%
|
|
# rename(field = pivot_quadrant,
|
|
# sub_field = field) %>%
|
|
unique()
|
|
|
|
|
|
# #2021
|
|
# pivot_select_model_Data_2021 <- harvesting_data %>% filter(Year == 2021) %>% pull(field)
|
|
#
|
|
# pivot_select_model_Data_2022 <- harvesting_data %>% filter(Year == 2022) %>% pull(field)
|
|
|
|
# pivot_select_model_Data_2023 <- harvesting_data %>% filter(year == 2023) %>% filter(!is.na(season_start)) %>% pull(sub_field)
|
|
|
|
pivot_select_model_Data_2024 <- harvesting_data %>% filter(year == 2024)%>% filter(!is.na(season_start)) %>% pull(sub_field)
|
|
message(pivot_select_model_Data_2024)
|
|
# pivots_dates_Data_2022 <- pivots_dates0 %>% filter(!is.na(season_end_2022))
|
|
# pivot_select_model_Data_2022 <- unique(pivots_dates_Data_2022$pivot_quadrant )
|
|
#
|
|
# pivots_dates_Data_2023 <- pivots_dates0 %>% filter(!is.na(season_start_2023))
|
|
# pivot_select_model_Data_2023 <- unique(pivots_dates_Data_2023$pivot_quadrant)
|
|
#
|
|
# pivots_dates_Data_2024 <- pivots_dates0 %>% filter(!is.na(season_start_2024))
|
|
# pivot_select_model_Data_2024 <- unique(pivots_dates_Data_2024$pivot_quadrant)
|
|
|
|
extract_CI_data <- function(field_names, harvesting_data, field_CI_data, season) {
|
|
# field_names = "Nandi A1a"
|
|
# field_names = "1.13A"
|
|
# harvesting_data = harvesting_data
|
|
# field_CI_data = pivot_stats_long
|
|
# season= 2024
|
|
|
|
filtered_harvesting_data <- harvesting_data %>%
|
|
# na.omit() %>%
|
|
filter(year == season, sub_field %in% field_names)
|
|
|
|
filtered_field_CI_data <- field_CI_data %>%
|
|
filter(sub_field %in% field_names)
|
|
if (nrow(filtered_field_CI_data) == 0) {
|
|
return(data.frame()) # Return an empty data frame if no data is found
|
|
}
|
|
|
|
# CI <- map_df(field_names, ~ {
|
|
ApproxFun <- approxfun(x = filtered_field_CI_data$Date, y = filtered_field_CI_data$value)
|
|
Dates <- seq.Date(ymd(min(filtered_field_CI_data$Date)), ymd(max(filtered_field_CI_data$Date)), by = 1)
|
|
LinearFit <- ApproxFun(Dates)
|
|
|
|
CI <- data.frame(Date = Dates, FitData = LinearFit) %>%
|
|
left_join(., filtered_field_CI_data, by = "Date") %>%
|
|
filter(Date > filtered_harvesting_data$season_start & Date < filtered_harvesting_data$season_end) %>%
|
|
mutate(DOY = seq(1, n(), 1),
|
|
model = paste0("Data", season, " : ", field_names),
|
|
season = season,
|
|
sub_field = field_names)
|
|
# }) #%>%
|
|
|
|
return(CI)
|
|
}
|
|
|
|
## Extracting the correct CI values
|
|
# Data_2021 <- map(pivot_select_model_Data_2021, ~ extract_CI_data(.x, harvesting_data = harvesting_data, field_CI_data = pivot_stats_long, season = 2021)) %>% list_rbind()
|
|
# message('2021')
|
|
# Data_2022 <- map(pivot_select_model_Data_2022, ~ extract_CI_data(.x, harvesting_data = harvesting_data, field_CI_data = pivot_stats_long, season = 2022)) %>% list_rbind()
|
|
# message('2022')
|
|
# Data_2023 <- map(pivot_select_model_Data_2023, ~ extract_CI_data(.x, harvesting_data = harvesting_data, field_CI_data = pivot_stats_long, season = 2023)) %>% list_rbind()
|
|
# message('2023')
|
|
Data_2024 <- map(pivot_select_model_Data_2024, ~ extract_CI_data(.x, harvesting_data = harvesting_data, field_CI_data = pivot_stats_long, season = 2024)) %>% list_rbind()
|
|
message('2024')
|
|
head(Data_2024)
|
|
|
|
|
|
#CI_all <- rbind(Data_2023, Data_2024)
|
|
CI_all <- Data_2024
|
|
message('CI_all created')
|
|
#CI_all <- Data_2023
|
|
|
|
CI_all <- CI_all %>% group_by(model) %>% mutate(CI_per_day = FitData - lag(FitData),
|
|
cumulative_CI = cumsum(FitData))
|
|
message('CI_all cumulative')
|
|
|
|
head(CI_all)
|
|
message('show head')
|
|
|
|
saveRDS(CI_all, here(cumulative_CI_vals_dir,"All_pivots_Cumulative_CI_quadrant_year_v2.rds"))
|
|
message('rds saved')
|
|
|