# Title     : estimate_met_streets
# Objective : estimate interpolated meteorology for each road over building height
# Created by: Jaime Benavides
# Created on: 23/3/20


#weight_roads_wrf <- readRDS(paste0(config$input_static_path, "weight_roads_wrf.rds"))

dates <- '20190101'
config <- NULL
config$k <- 0.4 
config$input_path <- "/esarchive/scratch/jmateu/sim_rline_bcn/coarse_maps2/in/"
library(urbanR)
source("/esarchive/scratch/nperez/git/caliope-urban/urbanR/R/meteo_rline.R")

weight_roads_wrf <- readRDS("/esarchive/scratch/nperez/git/Flor/Jan4Nuria/meteo_rline/weight_roads_wrf.rds")

for (date in dates){

  sim_path <- paste0(config$input_path, date, "/")

  year <- substr(date, 1, 4)
  month <- substr(date, 5, 6)
  day <- substr(date, 7, 8)


  # extract variable values for each day for the squared area of wrf cells

  date_time_begin <- c(as.numeric(year), as.numeric(month), as.numeric(day),0)
  date_time_end <- c(as.numeric(year), as.numeric(month), as.numeric(day),23)
  all.dates <- data_frame_dates(date_time_begin = date_time_begin, date_time_end = date_time_end)

  u <- readRDS(paste0(sim_path, "u_", date, ".rds"))
  v <- readRDS(paste0(sim_path, "v_", date, ".rds"))
  sinalpha <- readRDS(paste0(sim_path, "sinalpha_", date, ".rds"))
  cosalpha <- readRDS(paste0(sim_path, "cosalpha_", date, ".rds"))
  tskin <- readRDS(paste0(sim_path, "tskin_", date, ".rds"))
  hfx <- readRDS(paste0(sim_path, "hfx_", date, ".rds"))
  ustar <- readRDS(paste0(sim_path, "ustar_", date, ".rds"))
  rmol <- readRDS(paste0(sim_path, "rmol_", date, ".rds"))
  pblh <- readRDS(paste0(sim_path, "pblh_", date, ".rds"))
  z0 <- readRDS(paste0(sim_path, "z0_", date, ".rds"))

  # estimate intermediate variables

  ws <- sqrt(u * u + v * v)
  u_rot <- u * cosalpha - v * sinalpha # earth relative components
  v_rot <- v * cosalpha + u * sinalpha

  dir <- atan2(v_rot, u_rot) / (pi/180)  # replicates uv2sd (functions.f90 in MMIF)

  wd <- (270 - dir) %% 360

  met_u <-  t(compute_weighted_data(u, weight_roads_wrf))
  met_v <-  t(compute_weighted_data(v, weight_roads_wrf))
  met_tsk <- data.frame(date_begin = all.dates, t(compute_weighted_data(tskin, weight_roads_wrf)))
  met_hfx <- data.frame(date_begin = all.dates, t(compute_weighted_data(hfx, weight_roads_wrf)))
  met_ust <- t(compute_weighted_data(ustar, weight_roads_wrf))

  met_rmol <- t(compute_weighted_data(rmol, weight_roads_wrf))
  met_pblh <- t(compute_weighted_data(pblh, weight_roads_wrf))
  met_ws <-  data.frame(date_begin = all.dates, t(compute_weighted_data(ws, weight_roads_wrf)))
  met_zrough <-  data.frame(date_begin = all.dates, t(compute_weighted_data(z0, weight_roads_wrf)))
  met_l <-  1/met_rmol

  met_wstar <- multiApply::Apply(list(met_pblh, met_ust, met_l), 
                                 target_dims = NULL,
                                 fun = function(x, y, z, k) {
                                   if (z < 0) {
                                     res <- (x * y ^ 3 / (k * abs(z))) ^ (1/3)
                                   } else {
                                     res <- 0
                                   }
                                   return(res)
                                 }, k = config$k, ncores = 8)$output1        
 met_wstar <- cbind(all.dates, met_wstar)
 met_pblh <- cbind(all.dates, met_pblh)
 met_ust <- cbind(all.dates, met_ust)
 met_l <- cbind(all.dates, met_l)
 met_wd <- multiApply::Apply(list(met_v, met_u), target_dims = NULL, 
                             fun = function(x, y) {
                       (270 - base::atan2(x, y) / (pi /180)) %% 360},
                       ncores = 8)$output1
  met_wd <- as.data.frame(met_wd)
  met_wd <- cbind(all.dates, met_wd)
  # prepare rline input for each day

  met <- list(TSK = met_tsk, HFX = met_hfx, UST = met_ust, wstar = met_wstar, PBLH = met_pblh,
              L = met_l, ws = met_ws, wd = met_wd, zrough = met_zrough)
  meteo_rline(data = met, lat = 41.390205, lon = 2.154007, sim_path = sim_path,
              ncores = 8)
}

rm(all.dates,met,met_hfx,met_l,met_pblh,met_rmol,met_tsk,met_u,met_ust,met_v,met_wd,met_ws,met_wstar,met_zrough)
rm(weight_roads_wrf,c,col,cosalpha,d,date,date_time_begin,date_time_end,day,dir,hfx,month,pblh)
rm(rmol,sim_path,sinalpha,tskin,u,u_rot,ustar,v,v_rot,wd,ws,year,z0)

print(toc(log = TRUE))
