R/reportBuildings.R

Defines functions reportBuildings

Documented in reportBuildings

#' Read in GDX and calculate buildings variables, used in convGDX2MIF.R for the reporting
#'
#' Read in (net and gross) demand data from GDX file, information used in convGDX2MIF.R
#' for the reporting
#'
#' @param gdx a GDX object as created by readGDX, or the path to a gdx
#' @param output a magpie object containing all needed variables generated by other report*.R functions
#' @return MAgPIE object - contains buildings module variables
#' @author Sebastian Osorio
#' @seealso \code{\link{convGDX2MIF}}
#' @examples
#' \dontrun{
#' reportBuildings(gdx)
#' }
#'
#' @importFrom gdx readGDX
#' @importFrom magclass mbind setNames dimSums getSets getSets<- as.magpie
#' @importFrom stringr str_replace
#' @export
#'
reportBuildings <- function(gdx, output=NULL) {

  if(is.null(output)){
    stop("please provide a file containing all needed information")
  }

  # Loading parameters from convGDX2MIF parent function
  c_LIMESversion <- readGDX(gdx, name = "c_LIMESversion", field = "l", format = "first_found")

  tmp1 <- NULL
  tmp2 <- NULL
  # Check the version so to choose the electricity-related variables
  if (c_LIMESversion >= 2.38) {

    # Loading sets and switches from convGDX2MIF parent function
    #heating <- .readHeatingCfg(gdx)
    c_buildings <- readGDX(gdx, name = c("c_buildings", "report_c_buildings"),
                           field = "l", format = "first_found") #switch on buildings module


    if (c_buildings == 1) {
      # Loading parameters and variables

      p_bd_heatdem_ue <- readGDX(gdx, name = "p_bd_heatdem_ue", field = "l", format = "first_found", restore_zeros = TRUE) # heat demand per sector
      p_othersec_demDH_sec_ue <- readGDX(gdx, name = "p_othersec_demDH_sec_ue", field = "l", format = "first_found", restore_zeros = T) # heat demand per sector
      p_bd_area <- readGDX(gdx, name = "p_bd_area", field = "l", format = "first_found", restore_zeros = FALSE) # heat demand per sector
      v_bd_heatdem_ESR <- readGDX(gdx, name = "v_bd_heatdem_ESR", field = "l", format = "first_found") # heat that is covered by the ESR [annual data per sector]
      v_bd_heatdem_ETS <- readGDX(gdx, name = "v_bd_heatdem_ETS", field = "l", format = "first_found") # heat that is covered by the ETS [annual data per sector]
      p_othersec_demDH_ue <- readGDX(gdx, name = "p_othersec_demDH_ue", field = "l", format = "first_found") # heat that is provided by DH to other sectors (industry and agriculture) [annual data per sector]
      p_bd_ratio_ue2fe_DH <- readGDX(gdx, name = "p_bd_ratio_ue2fe_DH", field = "l", format = "first_found") # Ratio useful energy to final energy [--] - same for all DH technologies

      # create MagPie object of demand with iso3 regions
      p_bd_heatdem_ue <- limesMapping(p_bd_heatdem_ue)
      p_othersec_demDH_sec_ue <- limesMapping(p_othersec_demDH_sec_ue)
      p_bd_area <- limesMapping(p_bd_area)
      v_bd_heatdem_ESR <- limesMapping(v_bd_heatdem_ESR)
      v_bd_heatdem_ETS <- limesMapping(v_bd_heatdem_ETS)
      p_othersec_demDH_ue <- limesMapping(p_othersec_demDH_ue)
      p_bd_ratio_ue2fe_DH <- limesMapping(p_bd_ratio_ue2fe_DH)

      # Restrict years
      y <- getYears(output)
      p_bd_heatdem_ue <- p_bd_heatdem_ue[,y,]
      p_othersec_demDH_ue <- p_othersec_demDH_ue[,y,]
      p_othersec_demDH_sec_ue <- p_othersec_demDH_sec_ue[,y,]


      # Surface area
      tmp1 <- mbind(tmp1, setNames(p_bd_area[, , "resid"], "Useful Area|Residential (Mm2)"))
      tmp1 <- mbind(tmp1, setNames(p_bd_area[, , "nonresid"], "Useful Area|Non-residential (Mm2)"))

      # Useful energy
      tmp1 <- mbind(tmp1, setNames(p_bd_heatdem_ue[, , "resid.space_heat"] / 1000,
                                   "Useful Energy|Heating|Residential|Space-heating (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(p_bd_heatdem_ue[, , "resid.water_heat"] / 1000,
                                   "Useful Energy|Heating|Residential|Water-heating (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(p_bd_heatdem_ue[, , "nonresid.space_heat"] / 1000,
                                   "Useful Energy|Heating|Non-residential|Space-heating (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(p_bd_heatdem_ue[, , "nonresid.water_heat"] / 1000,
                                   "Useful Energy|Heating|Non-residential|Water-heating (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(p_othersec_demDH_sec_ue[, , "industry"] / 1000,
                                   "Useful Energy|Heating|Industry|Heat (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(p_othersec_demDH_sec_ue[, , "agric"] / 1000,
                                   "Useful Energy|Heating|Agriculture|Heat (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS, dim = 3) / 1000,
                                   "Useful Energy|Heating|Buildings|ETS-covered (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ESR, dim = 3) / 1000,
                                   "Useful Energy|Heating|Buildings|non-ETS-covered (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames((dimSums(v_bd_heatdem_ETS, dim = 3) + dimSums(v_bd_heatdem_ESR, dim = 3)) / 1000,
                                   "Useful Energy|Heating|Buildings (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames(p_othersec_demDH_ue / 1000,
                                   "Useful Energy|Heating|Other sectors|Heat (TWh/yr)"))
      tmp1 <- mbind(tmp1, setNames((dimSums(v_bd_heatdem_ETS, dim = 3) + p_othersec_demDH_ue) / 1000,
                                   "Useful Energy|Heating|ETS-covered (TWh/yr)"))

      #Heat demand covered by DH and decentral P2H (formulation as of October 2023)
      #Check if the new variables/equations already exist in the model
      v_bd_heatdem_ETS_DH <- readGDX(gdx, name = "v_bd_heatdem_ETS_DH", field = "l", format = "first_found",  restore_zeros  =  FALSE) #in GWh
      if(is.null(v_bd_heatdem_ETS_DH)) { #No split of heat demand,
        #There is only information regarding generation, so need to assume generation as a proxy for demand (there could be some wasted heat)

        #Estimate derived heat (DH) that is used in buildings
        o_demDH_ue <- output[, , "Useful Energy|Heating|Heat (TWh/yr)"]
        o_bd_demDH_ue <- o_demDH_ue - p_othersec_demDH_ue / 1000 #'p_othersec_demDH_ue' in GWh

        #Report variables
        tmp1 <- mbind(tmp1, setNames(o_bd_demDH_ue,
                                     "Useful Energy|Heating|Buildings|Heat (TWh/yr)"))


      } else { #split of heat demand between DH and decentral P2H
        #Load variables
        v_bd_heatdem_ETS_decP2H <- readGDX(gdx, name = "v_bd_heatdem_ETS_decP2H", field = "l", format = "first_found",  restore_zeros  =  FALSE) #in GWh

        # create MagPie object of demand with iso3 regions
        v_bd_heatdem_ETS_DH <- limesMapping(v_bd_heatdem_ETS_DH)
        v_bd_heatdem_ETS_decP2H <- limesMapping(v_bd_heatdem_ETS_decP2H)

        ##report
        #DH
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_DH[,,"resid.space_heat"] / 1000,
                                     "Useful Energy|Heating|Residential|Space-heating|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_DH[,,"resid.water_heat"] / 1000,
                                     "Useful Energy|Heating|Residential|Water-heating|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_DH[,,"nonresid.space_heat"] / 1000,
                                     "Useful Energy|Heating|Non-residential|Space-heating|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_DH[,,"nonresid.water_heat"] / 1000,
                                     "Useful Energy|Heating|Non-residential|Water-heating|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_DH[,,"space_heat"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Buildings|Space-heating|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_DH[,,"water_heat"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Buildings|Water-heating|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_DH[,,"resid"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Residential|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_DH[,,"nonresid"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Non-residential|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_DH[,,] / 1000, dim = 3),
                                     "Useful Energy|Heating|Buildings|Heat (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_DH[,,] / 1000, dim = 3) + p_othersec_demDH_ue / 1000,
                                     "Useful Energy|Heating|Heat (TWh/yr)"))

        #Decentral P2H
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_decP2H[,,"resid.space_heat"] / 1000,
                                     "Useful Energy|Heating|Residential|Space-heating|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_decP2H[,,"resid.water_heat"] / 1000,
                                     "Useful Energy|Heating|Residential|Water-heating|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_decP2H[,,"nonresid.space_heat"] / 1000,
                                     "Useful Energy|Heating|Non-residential|Space-heating|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(v_bd_heatdem_ETS_decP2H[,,"nonresid.water_heat"] / 1000,
                                     "Useful Energy|Heating|Non-residential|Water-heating|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_decP2H[,,"space_heat"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Buildings|Space-heating|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_decP2H[,,"water_heat"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Buildings|Water-heating|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_decP2H[,,"resid"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Residential|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_decP2H[,,"nonresid"] / 1000, dim = 3),
                                     "Useful Energy|Heating|Non-residential|Decentral|Electricity (TWh/yr)"))
        tmp1 <- mbind(tmp1, setNames(dimSums(v_bd_heatdem_ETS_decP2H[,,] / 1000, dim = 3),
                                     "Useful Energy|Heating|Buildings|Decentral|Electricity (TWh/yr)"))


      }


      ## Final energy
      #Take estimations from above, divide by the ue2fe factor, and change name
      items <- getItems(tmp1, dim = 3)[grep("Useful Energy Available for Final Consumption", getItems(tmp1, dim = 3))]
      for(var_name in items) {
        #tmp2 <- mbind(tmp2, setNames(tmp1[, , var_name] / p_bd_ratio_ue2fe_DH, str_replace(var_name, "Useful Energy Available for Final Consumption", "Final Energy")))
      }

    }

  }

  # concatenating net and gross demand data
  tmp <- mbind(tmp1, tmp2)

  return(tmp)
}
pik-piam/limes documentation built on April 12, 2025, 5:44 p.m.