#Step1: Loadning relevant libraries
library(sf)
library(stars)
library(ggplot2)
sf_use_s2(FALSE)
#Step2: Read country borders
borders <- st_read(dsn="./data/boundaries/all-country-boundaries.shp", quiet = TRUE)
#Step3: Simplifying border to make them easier to use on our machine
borders<-st_simplify(borders, dTolerance=0.1)
#Step4: Creating a folder called "F101992"
target_dir <- "./data/luminosity/F101992"
#Step5: Create the target directory if it doesn't exist
if (!dir.exists(target_dir)) {
dir.create(target_dir)
}
#Step6: Unzip the file
untar("./data/luminosity/F101992.v4.tar", exdir = target_dir)
#Step7: Unzip the file further
R.utils::gunzip("./data/luminosity/F101992/F101992.v4b_web.avg_vis.tif.gz", remove = FALSE, overwrite=TRUE)
#Step8: Read the raster
f1992<-read_stars("./data/luminosity/F101992/F101992.v4b_web.avg_vis.tif")
#Step9: Make sure the two files have the same CRS
st_crs(f1992)<-st_crs(borders)
#Step10: Reduce the resolution of the raster: Higher dx/day values mean lower resolution
f1992b<-st_warp(f1992, st_as_stars(st_bbox(f1992), dx = 0.3, dy = 0.3))
#Step11: Rename the raster
names(f1992b)<-"lights_1992"
#Step12:Defining the countries defining the regions of interest
norway<-subset(borders, SOVEREIGNT == "Norway")
max_lat_y_eu<-st_bbox(norway)["ymax"]
greece<-subset(borders, SOVEREIGNT == "Greece")
min_lat_y_eu<-st_bbox(greece)["ymin"]
ukraine<-subset(borders, SOVEREIGNT == "Ukraine")
max_lon_x_eu<-st_bbox(ukraine)["xmax"]
portugal<-subset(borders, SOVEREIGNT == "Portugal")
min_lon_x_eu<-st_bbox(portugal)["xmin"]
#Step13: Making the map
fig1992<-ggplot()+
geom_stars(data=f1992b)+
scale_fill_gradientn(name = "", colours=c("black","white"))+
geom_sf(data=borders, linewidth = 0.1, fill = NA, color = "white", alpha=0.5)+
coord_sf(xlim = c(min_lon_x_eu-3, max_lon_x_eu+3), ylim = c(min_lat_y_eu-1, max_lat_y_eu-6))+
theme_bw()+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())