library(ncdf4)
library(dplyr)
library(tidyr)
library(mapview)
library(raster)
library(sf)
library(leaflet)

obs_file <- nc_open("/home/abarrant/esarchive/obs/eea/eionet/hourly/sconcno2/sconcno2_202504.nc")


station_code <- ncvar_get(obs_file, "station_code")
times <- ncvar_get(obs_file, "time")
no2_conc <- ncvar_get(obs_file, "sconcno2") 

station_lat <- ncvar_get(obs_file, "lat")
station_lon <- ncvar_get(obs_file, "lon")
station_class <- ncvar_get(obs_file, "station_classification")


time_units <- ncatt_get(obs_file, "time", "units")$value

#"hours since YYYY-MM-DD"
if(grepl("since", time_units)) {
  ref_date <- strsplit(time_units, "since ")[[1]][2]
  ref_date <- as.POSIXct(ref_date, format="%Y-%m-%d %H:%M:%S", tz="UTC")
  dates <- ref_date + times * 3600  #conversion when units are hours
} else {
  dates <- times  #keep it the way it comes
}

#df creation length
no2_df <- expand.grid(
  time_index = 1:length(dates),
  station_index = 1:length(station_code)
)

#adding date and station id
no2_df$time <- dates[no2_df$time_index]
no2_df$station_id <- station_code[no2_df$station_index]

#adding no2
no2_df$no2_concentration <- no2_conc[cbind(no2_df$station_index, no2_df$time_index)]

#adding metadata if available
if(inherits(station_lat, "numeric")) {
  no2_df$station_lat <- station_lat[no2_df$station_index]
}
if(inherits(station_lon, "numeric")) {
  no2_df$station_lon <- station_lon[no2_df$station_index]
}
if(inherits(station_class, "character")) {
  no2_df$station_class <- station_class[no2_df$station_index]
}

#cleaning dataframe 
no2_df <- no2_df %>% 
  select(-time_index, -station_index) %>%
  #replace missings
  mutate(no2_concentration = ifelse(no2_concentration < -900, NA, no2_concentration))

#closing nc file
nc_close(obs_file)


#head(no2_df)
#summary(no2_df)

# Count of observations per station
station_counts <- no2_df %>%
  group_by(station_id) %>%
  summarize(
    count = n(),
    mean_no2 = mean(no2_concentration, na.rm = TRUE),
    max_no2 = max(no2_concentration, na.rm = TRUE)
  )
head(station_counts)

#stations dataframe ------------------------------------------------------------
# Create a simple dataframe with just the station information
stations_df <- data.frame(
  station_index = 1:length(station_code),
  station_id = station_code,
  latitude = station_lat,
  longitude = station_lon,
  class = station_class
)

stations_df <- stations_df %>% 
  select(-station_index)

saveRDS(stations_df, "/home/abarrant/esarchive/scratch/abarrant/eionet/no2_stations.rds")

stations_sf <- st_as_sf(stations_df, 
                        coords = c("longitude", "latitude"), 
                        crs = 4326) 
st_write(stations_sf, "/home/abarrant/esarchive/scratch/abarrant/eionet/no2_stations.shp", delete_layer = TRUE)

leaflet_map <- leaflet(stations_sf) %>%
  addTiles() %>%  # Add default OpenStreetMap tiles
  addCircleMarkers(
    radius = 6,
    color = ~colorFactor(palette = "Set1", domain = stations_sf$class)(class),
    stroke = FALSE,
    fillOpacity = 0.7,
    popup = ~paste0("<strong>Station ID:</strong> ", station_id, 
                    "<br><strong>Class:</strong> ", class)
  ) %>%
  addLegend(
    position = "bottomright",
    pal = colorFactor(palette = "Set1", domain = stations_sf$class),
    values = stations_sf$class,
    title = "Station Class",
    opacity = 0.7
  )

# Display the interactive map
leaflet_map