
#### 0_2 Merging Z-variables ####
#### Import data from previous notice ####
load(file = "./Data/geo_variables.Rdata")
load(file = "./Data/costs_t_2.Rdata")

# Setting pnl.rc (price of network loss) from RC-calculation two years prior to ap.t_2 (area price)
ap.t_2 <- cb.y_ex_cap %>% select (c(id, pnl.rc))
names(ap.t_2)[2] <- "ap.t_2"
ap.t_2$ap.t_2 <- ap.t_2$ap.t_2/1000

# Setter ap.t_2 manuelt for Linja
ap.t_2 <- ap.t_2 %>% 
  mutate(ap.t_2 = if_else(id == 269, 0.537166713, ap.t_2)) 

dat <- merge.data.frame(dat, ap.t_2, by = "id", all.x = TRUE) 

# Defining variables that are summed for all merging companies
harm.var_sum = c("ldz_n.mgc_sum")

# Defining variables weighted with number of map grid cells
dat_geo <- dat_geo %>% dplyr::rename(y = y.cb)
dat_geo$id.y <- paste(dat_geo$id, dat_geo$y, sep="")
ldz_harm.var_gc <- startsWith(colnames(dat_geo), prefix = "ldz")
ldz_harm.var_gc <- colnames(dat_geo)[ldz_harm.var_gc]

# Defining merging companies
fusjon_Linja = c(912631532, 984882114) # Linja AS = Linja AS + MC8renett AS
fusjon_NordvestNett = c(921699905, 980824586) # Nordvest Nett AS = Nordvest Nett AS + SandC8y Nett AS

list <- list() 

list$Linja = merge_ldz_rdz(comps = fusjon_Linja, new.org = 982974011, new.id = 269, new.name = "fusjonert_Linja", sum_variables = harm.var_sum, ldz_weighted.var = ldz_harm.var_gc, df = dat_geo)$mds
list$NordvestNett = merge_ldz_rdz(comps = fusjon_NordvestNett, new.org = 980824586, new.id = 613, new.name = "fusjonert_NordvestNett", sum_variables = harm.var_sum, ldz_weighted.var = ldz_harm.var_gc, df = dat_geo)$mds

# Combining all rows in the list into one data frame 
merged_ldz <- do.call("rbind", list) 
merged_ldz <- merged_ldz[,c(2,3,1,5,6,7,8,9,10,11,12,13,14,15,16,4)]

## Setter inn ap.t_2 manuelt inn i merged_ldz slik at det skal matche dat_geo #### 
# Sl?? inn ap.t_2 p?? id
merged_ldz <- merged_ldz %>%
  left_join(select(ap.t_2, id, ap.t_2), by = "id")


# Deleting companies who have merged (with IDs equal to IDs of merged companies)
dat_geo = dat_geo[!(dat_geo$id %in% merged_ldz$id),] 

# Adding newly merged companies 
# merged_ldz = merged_ldz %>% select(-c(comp, name))
dat_geo <- rbind(dat_geo, merged_ldz) 

# Merging data
dat_geo = dat_geo %>% select(-c(orgn, y, id.y, ap.t_2)) # Fjerner ogs?? ap.t_2 fra dat.geo -> dette gj??r at man ikke f??r flere ap.t_2 i dat i neste kode-linje.####
dat <- merge.data.frame(dat, dat_geo, by = "id", all.x = T) # Her skjer feilen der det blir danna flere ap.t_2. F??r dette er det bare en variabel i dat. Se kommentar over.####

# Creating ldz_prod
ldz_prod = dat[dat$y == y.cb, c("orgn","ldz_innmat_midl","ldz_le")]
ldz_prod$ldz_prod = ldz_prod$ldz_innmat_midl /ldz_prod$ldz_le
ldz_prod$ldz_prod[!is.finite(ldz_prod$ldz_prod)] <- 0 # Removing NaN and Inf values
ldz_prod = ldz_prod %>% select(-c(ldz_innmat_midl, ldz_le))
dat <- merge.data.frame(dat, ldz_prod, by = "orgn", all.x = T )

print(unique(dat[is.na(dat$ldz_mgc), "comp" ]))
length(dat[is.na(dat)])

# Deleting NAs
dat[is.na(dat)] <- 0

# Sorting data
idasvector <- as.vector(dat$id)
dat <- dat[order(idasvector),]
rm(idasvector, ldz_prod, merged_ldz, list)

