#library(RNetCDF)
library(sp)
library(sf)
#library(mapview)
library(ncdf4)
library(dplyr)
library(ggplot2)




path <- "/home/abarrant/esarchive/scratch/ccarnere/RScripts/results_regional_2023/results_gene_no2/output/model_raw_at_AQMS_gene_no2_df.rds"
save_path <- "/home/abarrant/Documents/RStudio/dump_files/df_obs_gene_no2.csv"


path <- "/home/abarrant/esarchive/scratch/ccarnere/RScripts/results_regional_2023/results_gene_no2/output/df_obs_gene_no2.rds"
data <- readRDS(path)


write.csv(data, save_path, row.names = FALSE)



data1 <- read.csv("/home/abarrant/Documents/RStudio/dump_files/no2 2023/df_obs_gene_no2.csv")
data2 <- read.csv("/home/abarrant/Documents/RStudio/dump_files/no2 2023/model_raw_at_AQMS_gene_no2_df.csv")

data1 <- data1 %>%
  mutate(date = paste(date, ZAL, sep = " ")) %>%
  select(-ZAL)



# Convert to POSIXct with proper format (e.g., "%Y-%m-%d %H:%M:%S")
data1$date <- as.POSIXct(data1$date, format = "%Y-%m-%d %H:%M:%S")
data2$date <- as.POSIXct(data2$date, format = "%Y-%m-%d %H:%M:%S")

# Step 3: Check if conversion was successful
if (any(is.na(data1$date)) || any(is.na(data2$date))) {
  stop("Some date-times could not be converted. Check date-time format or missing values.")
}

# Step 4: Find common date-times
common_datetimes <- intersect(data1$date, data2$date)

# Step 5: Filter both datasets to include only rows with common date-times
data1_filtered <- filter(data1, date %in% common_datetimes)
data2_filtered <- filter(data2, date %in% common_datetimes)

# Step 6: Identify columns with the same name (excluding 'date' if needed)
#common_columns <- intersect(names(data1_filtered), names(data2_filtered))

# Step 7: Combine the common columns side-by-side
# Select the matching columns from each dataset
#combined_data <- data.frame(data1_filtered[common_columns], data2_filtered[common_columns])

# Step 8: Rename columns to differentiate (optional)
#names(combined_data) <- c(paste0(common_columns, "_obs"), paste0(common_columns, "_model"))

common_columns <- setdiff(intersect(names(data1_filtered), names(data2_filtered)), "date")

# Step 5: Rename columns in each dataset to distinguish sources
data1_filtered <- data1_filtered %>%
  rename_with(~ paste0(., "_obs"), common_columns)

data2_filtered <- data2_filtered %>%
  rename_with(~ paste0(., "_model"), common_columns)

combined_data <- data1_filtered["date"]

# Step 7: Interleave columns from data1 and data2
for (col in common_columns) {
  combined_data[[paste0(col, "_obs")]] <- data1_filtered[[paste0(col, "_obs")]]
  combined_data[[paste0(col, "_model")]] <- data2_filtered[[paste0(col, "_model")]]
}


# Step 9: Save the combined data as a new CSV
write.csv(combined_data, "/home/abarrant/Documents/RStudio/dump_files/no2 2023/merged_obs_model_no2_2023.csv", row.names = FALSE)
