library(ggplot2)
library(urbanR)
# -----  NO2 data:
output_path <- "/esarchive/scratch/nperez/git/Flor/Jan4Nuria/"
results <- readRDS(paste0(output_path, "bcn_NonUniformGrid.rds"))

n_receptors <- nrow(results[results$date == results$date[1],])
crs_sim <- "+proj=utm +zone=31 +ellps=intl +units=m +no_defs"

# data.frame and SpatialPointsDataFrame:
mean_map <- results[results$date == results$date[1],]
mean_map_sp <- sp::SpatialPointsDataFrame(mean_map[,c("X", "Y")], mean_map)
ggplot(data = mean_map_sp@data, aes(x = X, y = Y, colour = NO2, shape = '.')) + 
  geom_point() +
  ggtitle("Original CALIOPE-Urban output data") +
  #scale_colour_gradientn(colours = rainbow(64), breaks = seq(0, 100, 100/64))
  scale_colour_gradientn(colours = rainbow(64))
 dev.new()

# ----  Districts:
path <- "/esarchive/scratch/jbenavides/data/geom/";
districts <- sf::read_sf(paste0(path, "BCN_Districte_ED50_SHP.shp"))
ggplot() + geom_sf(data = districts) +
 geom_point(data = mean_map, aes(x = X, y = Y, colour = NO2))
dev.new()

# ---- Create uniform grid:I
spatial_context <- context_sp(geom_path = '/esarchive/scratch/jbenavides/data/geom/')
config <- NULL
config$crs_sim <- "+proj=utm +zone=31 +ellps=intl +units=m +no_defs"
receptor_grid <- sf::st_sf(geom=sf::st_make_grid(spatial_context,
                           #offset = c(420819, 4574709), square = TRUE,
                     cellsize = 10, what = "centers"), crs=config$crs_sim)
# add coordinates required for plotting
receptor_grid$data <- cbind(receptor_grid$data, 
                            Coord_X = sf::st_coordinates(receptor_grid)[,1],
                            Coord_Y = sf::st_coordinates(receptor_grid)[,2])
receptor_grid$Coord_X <- sf::st_coordinates(receptor_grid)[,1]
receptor_grid$Coord_Y <- sf::st_coordinates(receptor_grid)[,2]

receptor <- data.frame(receptor_grid$data[,c("Coord_X", "Coord_Y")],
                     NO2 = rep(NA, 2383800))
receptor <- sp::SpatialPointsDataFrame(receptor[,c("Coord_X", "Coord_Y")],
             receptor)
sp::proj4string(receptor) <- crs_sim
sp::proj4string(mean_map_sp) <- crs_sim
 
regular <- gstat::idw(NO2 ~ 1, mean_map_sp, newdata = receptor, idp = 2.0,
                      nmax = 4)

regular@data <- cbind(regular@data, regular@coords)
ggplot(data = regular@data, aes(x = Coord_X, y = Coord_Y, pwd = 0.5, colour = var1.pred)) +
  geom_point() +
  ggtitle("IDW with 4 nearest neighbours") +
  scale_colour_gradientn(colours = rainbow(10))

regular_z <- regular@data$var1.pred
dim(regular_z) <- c(x = 1450, y = 1644)
x <- unique(regular@data$Coord_X)
y <- unique(regular@data$Coord_Y)
fields::image.plot(x, y, regular_z, main = "IDW 4 nearest neighbours",
                   legend.lab = "NO2 concentration",
                   breaks = seq(0, 100, 100/64), 
                   col = RPMG::rainbow.colors(64))
fields::image.plot(x, y, regular_z, main = "IDW 4 nearest neighbours",
                   legend.lab = "NO2 concentration",
                   breaks = seq(0, 100, 100/64))
# points on the are of BCN
ref <- sf::st_intersection(receptor_grid, districts)
ggplot() + geom_point(data = ref, aes(x = Coord_X, y = Coord_Y, colour = Coord_X))

ref_sp <- data.frame(ref$data[,c("Coord_X", "Coord_Y")],
                     NO2 = rep(NA, 1020625))
ref_sp <- sp::SpatialPointsDataFrame(ref_sp[,c("Coord_X", "Coord_Y")], ref_sp)

sp::proj4string(ref_sp) <- crs_sim
sp::proj4string(mean_map_sp) <- crs_sim

res <- gstat::idw(NO2 ~ 1, mean_map_sp, newdata = ref_sp, idp = 2.0)

res@data <- cbind(res@data, res@coords) 
ggplot(data = res@data, aes(x = Coord_X, y = Coord_Y, colour = var1.pred)) +
  geom_point()






mean_map_sf <- mean_map %>% sf::st_as_sf(coords = c("X", "Y"), crs = crs_sim)

bcn_mean_map <- mean_map_sf
bcn_mean_map$Coord_X <- sf::st_coordinates(bcn_mean_map)[,1]
bcn_mean_map$Coord_Y <- sf::st_coordinates(bcn_mean_map)[,2]

bcn_mean_map <- sf::st_transform(bcn_mean_map,crs("+proj=longlat"))
bcn_mean_map$X <- sf::st_coordinates(bcn_mean_map)[,1]
bcn_mean_map$Y <- sf::st_coordinates(bcn_mean_map)[,2]

points <- sp::SpatialPoints(methods::as(bcn_mean_map[,c('X','Y')], "Spatial"),
              proj4string = sp::CRS("+proj=longlat"))
mean_map_sp <- SpatialPointsDataFrame(mean_map[,c("X", "Y")], mean_map)
ggplot(data=mean_map_sp@data, aes(x = X, y = Y, colour = NO2)) + geom_point()
