# compute the EU map of the anomalies using ERA5Land

# Subset region
lat_min <- 34#59
lat_max <- 72
lon_min <- -11
lon_max <- 40

library(startR)
path <- "/esarchive/recon/ecmwf/era5land/monthly_mean/$var$_f1h/$var$_$year$$month$.nc"

mes <- 6 
year <- 2020
pos_year <- which(1993:2021 == year)
tas <- Start(dat = path,
             var = 'tas',
             year = as.character(1993:2021),
             month = sprintf("%02d", mes),
             latitude = values(list(lat_min, lat_max)),
             latitude_reorder = Sort(),
             longitude = values(list(lon_min, lon_max)),
             longitude_reorder = CircularSort(-180, 180),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(lon = NULL, lat = NULL),
             retrieve = TRUE)

lat <- attributes(tas)$Variable$common$lat
lon <- attributes(tas)$Variable$common$lon

tas_clim <- s2dv::MeanDims(tas, 'year')

library(s2dv)
PlotEquiMap(tas_clim[1,1,1,,]-273.15, lon = as.vector(lon), lat = as.vector(lat), filled.c = FALSE,
            intylat = 4, intxlon = 4, 
            toptitle = "ERA5 Land May Climatology 1993-2021")
dev.new()
PlotEquiMap(tas[1,1,pos_year,1,,]-273.15,
            lon = as.vector(lon), lat = as.vector(lat), filled.c = FALSE,
            intylat = 4, intxlon = 4, brks = seq(0,34, 2), triangle_ends = c(T, F),
            toptitle = paste("ERA5 Land", month.name[mes], year))
dev.new()

PlotEquiMap(tas[1,1,pos_year,1,,]-tas_clim[1,1,1,,], 
            lon = as.vector(lon), lat = as.vector(lat), filled.c = FALSE,
            intylat = 4, intxlon = 4, brks = seq(-7, 7, 1),
            toptitle = paste("ERA5 Land Anomaly", month.name[mes], year))


