library(CSTools)
library(lubridate)
library(startR)
library(s2dv)
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Interpolation.R')

plotpath <- '/esarchive/scratch/jramon/downscaling/plots/ip'
target_grid <- '/esarchive/recon/ecmwf/era5/monthly_mean/tas_f1h/tas_200002.nc'

#lonmin <- -22
#lonmax <- 45
#latmin <- 27
#latmax <- 72
#lonmin <- -11.5
#lonmax <- 5.35
#latmin <- 35.1
#latmax <- 44.1
lonmin <- -11.5
lonmax <- 5.35
latmin <- 35.1
latmax <- 44.1

sdates <- c('20000201','20010201','20020201','20030201','20040201','20050201','20060201','20070201')

exp1 <- startR::Start(dat = '/esarchive/exp/ecmwf/system5c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
                     var = 'tas', time = indices(1), member = indices(1:3), sdate = sdates,
                     lat = values(list(latmin, latmax)), 
                     lat_reorder = Sort(decreasing = FALSE),
                     lon = values(list(lonmin, lonmax)), 
                     lon_reorder = CircularSort(-180, 180),
                     synonims = list(var = c('var','variable'), 
                                     lon = c('lon', 'longitude'),
                                     lat = c('lat', 'latitude'), 
                                     member = c('member','ensemble')),
                     return_vars = list(lat = 'dat', lon = 'dat'),
                     num_procs = 1, retrieve = TRUE)
exp2 <- s2dv_cube(exp1, lat = attr(exp1, "Variables")$dat1$lat, lon = attr(exp1, "Variables")$dat1$lon,
                 source_files = attr(exp1, "Files")[1,1,])

#attr(exp2$lon,"first_lon") <- exp2$attrs$lon[1]
#attr(exp2$lon,"last_lon") <- exp2$attrs$lon[length(exp2$attrs$lon)]
#attr(exp2$lat,"first_lat") <- exp2$attrs$lat[1]
#attr(exp2$lat,"last_lat") <- exp2$attrs$lat[length(exp2$attrs$lat)]

#--------------------------------
# Downscaling with Interpolation
# If lon_reorder = CircularSort(0, 360) is set, please add the parameter remap_region = c(lonmin,lonmax,latmin,latmax)
#--------------------------------
exp1_con <- Interpolation(exp = exp1, lats = attr(exp1, "Variables")$dat1$lat,
                          lons = attr(exp1, "Variables")$dat1$lon, target_grid = target_grid, method_remap = 'con')
exp1_bil <- Interpolation(exp = exp1, lats = attr(exp1, "Variables")$dat1$lat, 
                          lons = attr(exp1, "Variables")$dat1$lon, target_grid = target_grid, method_remap = 'bil')
exp1_bic <- Interpolation(exp = exp1, lats = attr(exp1, "Variables")$dat1$lat,
                          lons = attr(exp1, "Variables")$dat1$lon, target_grid = target_grid, method_remap = 'bic')
exp1_nn <- Interpolation(exp = exp1, lats = attr(exp1, "Variables")$dat1$lat,
                         lons = attr(exp1, "Variables")$dat1$lon, target_grid = target_grid, method_remap = 'nn')
exp1_con2 <- Interpolation(exp = exp1, lats = attr(exp1, "Variables")$dat1$lat,
                          lons = attr(exp1, "Variables")$dat1$lon, target_grid = target_grid, method_remap = 'con2')

#--------------------------------
# Downscaling with CST_Interpolation
#--------------------------------
exp2_con <- CST_Interpolation(exp = exp2, target_grid = target_grid, method_remap = 'con')
exp2_bil <- CST_Interpolation(exp = exp2, target_grid = target_grid, method_remap = 'bil')
exp2_bic <- CST_Interpolation(exp = exp2, target_grid = target_grid, method_remap = 'bic')
exp2_nn <- CST_Interpolation(exp = exp2, target_grid = target_grid, method_remap = 'nn')
exp2_con2 <- CST_Interpolation(exp = exp2, target_grid = target_grid, method_remap = 'con2')

#--------------------------------
# Downscaling to point locations
#--------------------------------
points <- list(lat = c(36.1, 38.7), lon = c(-1.9, 0.8))
down_points_sbc <- Interpolation(exp = exp1, lats = attr(exp1, "Variables")$dat1$lat, lons = attr(exp1, "Variables")$dat1$lon,
                                 points = points, method_point_interp = 'bilinear', remap_region = c(lonmin,lonmax,latmin,latmax),
                                 source_file = attr(exp1, "Files")[1,1,1])
                                     

#s2dv::PlotEquiMap(var = obs$data[1,1,1,,,1], lat = obs$lat, lon = obs$lon, filled.continents = FALSE, toptitle = 'Observations', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_obs.png"))
#s2dv::PlotEquiMap(var = exp$data[1,1,1,1,1,,], lat = exp$lat, lon = exp$lon, filled.continents = FALSE, toptitle = 'Predictions', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_fcst.png"))
#s2dv::PlotEquiMap(var = exp1_con$data[1,1,1,1,1,,], lat = exp_con$lat, lon = exp_con$lon, filled.continents = FALSE, toptitle = 'Conservative', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_con.png"))
#s2dv::PlotEquiMap(var = exp1_bil$data[1,1,1,1,1,,], lat = exp_bil$lat, lon = exp_bil$lon, filled.continents = FALSE, toptitle = 'Bilinear', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_bil.png"))
#s2dv::PlotEquiMap(var = exp1_bic$data[1,1,1,1,1,,], lat = exp_bic$lat, lon = exp_bic$lon, filled.continents = FALSE, toptitle = 'Bicubic', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_bic.png"))
#s2dv::PlotEquiMap(var = exp1_nn$data[1,1,1,1,1,,], lat = exp_nn$lat, lon = exp_nn$lon, filled.continents = FALSE, toptitle = 'Nearest neighbour', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_nn.png"))
#s2dv::PlotEquiMap(var = exp1_con2$data[1,1,1,1,1,,], lat = exp_con2$lat, lon = exp_con2$lon, filled.continents = FALSE, toptitle = '2nd order conservative', brks = seq(275,288,1), height = 8, width = 10, fileout = file.path(plotpath, "ip_con2.png"))





