
library(startR)
# Nord3v2 Interactive:
options(bitmapType = 'cairo')
output_path <- '/esarchive/scratch/nperez/git/Flor/ArticXchange/s2s_workflow/'

ecmwf_path <- paste0('/esarchive/exp/ecmwf/s2s-monthly_ensforhc/',
                     'weekly_mean/$var$_f24h/$sdate$/$var$_$syear$.nc')

forecast.year <- 2020
# Mondays
sdates.seq.mon <- format(seq(as.Date(paste(forecast.year, 04, 13, sep = '-')),
                             as.Date(paste(forecast.year, 06, 30, sep='-')), 
                             by = 'weeks'), format = '%Y%m%d')
# Thursdays (Monday+3days)
sdates.seq.thu <- format(seq(as.Date(paste(forecast.year, 04, 09, sep = '-')),
                         as.Date(paste(forecast.year, 06, 30, sep = '-')),
                         by = 'weeks'), format = '%Y%m%d')
# Joint dates in order
sdates.seq <- c(sdates.seq.mon, sdates.seq.thu)
ind <- order(as.Date(sdates.seq, format = '%Y%m%d'))  # dates in order
sdates.seq <- sdates.seq[ind]

lat_min <- 64
lat_max <- 71
lon_min <- 20
lon_max <- 31

exp <- Start(dat = ecmwf_path, 
              var = 'tas',
              sdate = sdates.seq,
              syear = 'all',     # before hdate
              time = 'all',
              ensemble = "all",
              latitude = values(list(lat_min, lat_max)),
              latitude_reorder = Sort(),
              longitude = values(list(lon_min, lon_max)),
              longitude_reorder = CircularSort(-180, 180),
              transform = CDORemapper,
              transform_extra_cells = 2,
              transform_params = list(grid = 
  '/esarchive/recon/ecmwf/era5land/weekly_mean/tas_f1h/tas_20140102.nc',
                                     method = 'conservative',
                                     crop = c(lon_min, lon_max, lat_min, lat_max)),
              transform_vars = c('latitude', 'longitude'),
              synonims = list(latitude = c('lat', 'latitude'),
                              longitude = c('lon', 'longitude')),
              syear_depends = 'sdate',
              return_vars = list(latitude = NULL,
                                 longitude = NULL,
                                 time = c('sdate', 'syear')), 
              retrieve = TRUE)

#LAUS
mask.file_laus <- paste0("/esarchive/shapefiles/", '/LAU/LAU_RG_01M_2020_4326.shp')
shp_laus <- rgdal::readOGR(mask.file_laus)
shp_laus <- subset(shp_laus, CNTR_CODE == "FI")
Laus_lapland <- c('047', '148', '240', '320', '241', '261', '273', '498', '583', '854',
                  '614', '683', '698', '732', '742', '751', '758', '845', '851', '890',
                  '976')
shp_lapland <- subset(shp_laus, LAU_ID %in% Laus_lapland)

library(maps)
lapland <- SpatialPolygons2map(shp_lapland)
lapland <- list(x = lapland[[1]], y = lapland[[2]])

library(ragg)
agg_png(paste0(output_path, "conservative_regridding.png"),
        width = 800, height = 900, units = 'px', res = 144)
s2dv::PlotEquiMap(exp[1,1,1,1,1,1,,] - 273.15,
                  lon = as.vector(attributes(exp)$Variables$common$lon),
                  lat = as.vector(attributes(exp)$Variables$common$lat),
                  shapefile = lapland, shapefile_col = 'darkgreen',
                  filled.cont = FALSE, intylat = 2, intxlon = 2,
                  brks = seq(-7, 2, 0.5), triangle_ends = c(FALSE, FALSE),
                  topt = 'ECMWF/S2S-ENSFORHC regridded', title_scale = 0.7)
dev.off()

exp_raw <- Start(dat = ecmwf_path,
              var = 'tas',
              sdate = sdates.seq,
              syear = 'all',     # before hdate
              time = 'all',
              ensemble = "all",
              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')),
              syear_depends = 'sdate',
              return_vars = list(latitude = NULL,
                                 longitude = NULL,
                                 time = c('sdate', 'syear')),
              retrieve = TRUE)
agg_png(paste0(output_path, "orginal_grid.png"),
        width = 800, height = 900, units = 'px', res = 144)
s2dv::PlotEquiMap(exp_raw[1,1,1,1,1,1,,] - 273.15,
                  lon = as.vector(attributes(exp_raw)$Variables$common$lon),
                  lat = as.vector(attributes(exp_raw)$Variables$common$lat),
                  shapefile = lapland, shapefile_col = 'darkgreen',
                  filled.cont = FALSE, intylat = 2, intxlon = 2,
                  brks = seq(-7, 2, 0.5), triangle_ends = c(FALSE, FALSE),
                  topt = 'ECMWF/S2S-ENSFORHC resolution', title_scale = 0.7)
dev.off()




## Generate the syears from the sdates.seq
syears <- t(sapply(sdates.seq, function(x) {
                   year <- as.numeric(substr(x, 1, 4))
                   syears <- paste0((year-20):(year-1), substr(x, 5, 8))
       }))
names(dim(syears)) <- c('sdate', 'syear')

## Generate the times from the syears
Sys.setenv(TZ='UTC') # this helps to get UTC times
times <- lapply(syears, function(x) {
                x <- as.Date(x, format = "%Y%m%d", tz = "UTC")
                x <- seq(x, x + 25, by = 'week')
               format(x, "%Y%m%d")
              })
times <- Reduce(c, times)
dim(times) <- c(time = 4, sdate = 24, syear = 20)
times <- s2dv::Reorder(times, c(2,3,1))

obs_path <- paste0("/esarchive/recon/ecmwf/era5land/weekly_mean/",
                   "$var$_f1h/$var$_$file_date$.nc")

obs <- Start(dat = obs_path,
             var = 'tas',
             file_date = times,
             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')),
             split_multiselected_dims = TRUE,
             return_vars = list(latitude = NULL,
                                longitude = NULL,
                                time = 'file_date'),
             retrieve = TRUE)

as.vector(attributes(obs)$Variables$common$lat)
as.vector(attributes(obs)$Variables$common$lon)


