Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move user-based path setting out of Config object #32

Merged
merged 8 commits into from
Oct 31, 2024
Merged
69 changes: 47 additions & 22 deletions message_ix_buildings/chilled/core/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from message_ix_buildings.chilled.util.util import (
get_archs,
get_logger,
get_paths,
load_all_scenarios_data,
load_parametric_analysis_data,
)
Expand All @@ -56,7 +57,12 @@


def create_climate_variables_maps(config: "Config", start_time: datetime.datetime):
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")
isimip_bias_adj_path = get_paths(config, "isimip_bias_adj_path")
isimip_ewembi_path = get_paths(config, "isimip_ewembi_path")

out_path = os.path.join(project_path, "out", "version", config.vstr)
archetype_path = os.path.join(out_path, "rasters")
save_path = os.path.join(out_path, "VDD_ene_calcs")

Expand Down Expand Up @@ -105,10 +111,10 @@ def map_calculated_variables(args):
endstr = ".nc"

if str(clim) == "hist":
isi_folder = config.isimip_ewemib_path
isi_folder = isimip_ewembi_path
filestr = climate_filestr_hist
else:
isi_folder = config.isimip_bias_adj_path
isi_folder = isimip_bias_adj_path
filestr = climate_filestr_future

filepath = os.path.join(
Expand All @@ -135,12 +141,12 @@ def map_calculated_variables(args):
t_oa_gbm = t_out_ave.groupby("time.month")

i_sol_v = xr.open_dataarray(
os.path.join(config.dle_path, "EWEMBI_vert_irrad_1980-2009_avg.nc")
os.path.join(dle_path, "EWEMBI_vert_irrad_1980-2009_avg.nc")
) # Values in daily Wh/m2

# Horizontal irradiation
i_sol_h = xr.open_dataarray(
os.path.join(config.dle_path, "EWEMBI_horiz_irrad_1980-2009_avg.nc")
os.path.join(dle_path, "EWEMBI_horiz_irrad_1980-2009_avg.nc")
) # Values in daily Wh/m2

if config.arch_setting == "regional":
Expand Down Expand Up @@ -756,7 +762,8 @@ def read_netcdf_files(input_args):


def aggregate_urban_rural_files(config: "Config"):
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
out_path = os.path.join(project_path, "out", "version", config.vstr)
save_path = os.path.join(out_path, "VDD_ene_calcs")

output_path_vdd = os.path.join(
Expand Down Expand Up @@ -834,7 +841,8 @@ def aggregate_urban_rural_files(config: "Config"):


def make_vdd_total_maps(config: "Config"):
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
out_path = os.path.join(project_path, "out", "version", config.vstr)
save_path = os.path.join(out_path, "VDD_ene_calcs")

output_path_vdd = os.path.join(
Expand Down Expand Up @@ -1133,7 +1141,9 @@ def make_map(


def process_construction_shares(config: "Config"):
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")
out_path = os.path.join(project_path, "out", "version", config.vstr)
floorarea_path = os.path.join(out_path, "floorarea_country")

output_path = os.path.join(
Expand All @@ -1153,7 +1163,7 @@ def process_construction_shares(config: "Config"):

# If constr_setting == 1, then process construction shares. Otherwise, skip
if config.constr_setting == 1:
input_path = config.dle_path
input_path = dle_path

dsc = xr.Dataset()
for urt in config.urts:
Expand Down Expand Up @@ -1226,8 +1236,10 @@ def process_construction_shares(config: "Config"):


def process_floor_area_maps(config: "Config"):
input_path = config.dle_path
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")
input_path = dle_path
out_path = os.path.join(project_path, "out", "version", config.vstr)
save_path = os.path.join(out_path, "floorarea_country")

output_path = os.path.join(
Expand Down Expand Up @@ -1354,8 +1366,11 @@ def process_floor_area_maps(config: "Config"):


def process_country_maps(config: "Config"):
input_path = config.dle_path
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")

input_path = dle_path
out_path = os.path.join(project_path, "out", "version", config.vstr)
save_path = os.path.join(out_path, "floorarea_country")

output_path = os.path.join(
Expand Down Expand Up @@ -1436,8 +1451,11 @@ def process_country_maps(config: "Config"):


def process_final_maps(config: "Config"):
input_path = config.dle_path
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")

input_path = dle_path
out_path = os.path.join(project_path, "out", "version", config.vstr)
vdd_path = os.path.join(
out_path,
"VDD_ene_calcs",
Expand Down Expand Up @@ -1835,8 +1853,11 @@ def process_final_maps(config: "Config"):
def process_iso_tables(config: "Config"):
start = datetime.datetime.now()

input_path = config.dle_path
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")

input_path = dle_path
out_path = os.path.join(project_path, "out", "version", config.vstr)
vdd_path = os.path.join(out_path, "VDD_ene_calcs", config.gcm, config.rcp)
floorarea_path = os.path.join(out_path, "floorarea_country", config.gcm, config.rcp)
finalmaps_path = os.path.join(out_path, "final_maps", config.gcm, config.rcp)
Expand Down Expand Up @@ -2140,7 +2161,11 @@ def aggregate_ncfile(args: tuple) -> xr.Dataset:


def create_climate_outputs(config: "Config", start_time: datetime.datetime):
out_path = os.path.join(config.project_path, "out", "version", config.vstr)
project_path = get_paths(config, "project_path")
dle_path = get_paths(config, "dle_path")
isimip_bias_adj_path = get_paths(config, "isimip_bias_adj_path")
isimip_ewemib_path = get_paths(config, "isimip_ewemib_path")
out_path = os.path.join(project_path, "out", "version", config.vstr)
archetype_path = os.path.join(out_path, "rasters")
save_path = os.path.join(out_path, "VDD_ene_calcs")

Expand Down Expand Up @@ -2180,10 +2205,10 @@ def create_climate_outputs(config: "Config", start_time: datetime.datetime):
nyrs_clim = int(years_clim[1]) - int(years_clim[0]) + 1

if str(clim) == "hist":
isi_folder = config.isimip_ewemib_path
isi_folder = isimip_ewemib_path
filestr = config.climate_filestr_hist
else:
isi_folder = config.isimip_bias_adj_path
isi_folder = isimip_bias_adj_path
filestr = config.climate_filestr_future

filepath = os.path.join(
Expand Down Expand Up @@ -2226,12 +2251,12 @@ def create_climate_outputs(config: "Config", start_time: datetime.datetime):
# Vertical irradiation
# i_sol_v = xr.open_dataarray(input_folder+'CERES_vert_irrad_2001-13_avg.nc') #Values in daily Wh/m2
i_sol_v = xr.open_dataarray(
os.path.join(config.dle_path, "EWEMBI_vert_irrad_1980-2009_avg.nc")
os.path.join(dle_path, "EWEMBI_vert_irrad_1980-2009_avg.nc")
) # Values in daily Wh/m2

# Horizontal irradiation
i_sol_h = xr.open_dataarray(
os.path.join(config.dle_path, "EWEMBI_horiz_irrad_1980-2009_avg.nc")
os.path.join(dle_path, "EWEMBI_horiz_irrad_1980-2009_avg.nc")
) # Values in daily Wh/m2

# i_sol = i_sol.sel(time=slice(years_clim[0],years_clim[1]))
Expand Down
15 changes: 12 additions & 3 deletions message_ix_buildings/chilled/postprocess/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import pandas as pd

from message_ix_buildings.chilled.util.config import Config
from message_ix_buildings.chilled.util.util import get_logger
from message_ix_buildings.chilled.util.util import get_logger, get_paths

log = get_logger(__name__)
cfg = Config()
cfg = Config(user="MEAS")
dle_path = get_paths(cfg, "dle_path")


def get_sturm_data(input_path, input_version_name):
Expand Down Expand Up @@ -196,6 +197,14 @@ def postprocess_electricity_demand(input_path, input_version_name):
)

# Save files

df_chilled.to_csv(
os.path.join(version_output_path, "chilled_postprocessed.csv"),
index=False,
)

log.info("Saved: " + os.path.join(version_output_path, "chilled_postprocessed.csv"))

df_sturm.to_csv(
os.path.join(version_output_path, "sturm_building_stock_inputs.csv"),
index=False,
Expand Down Expand Up @@ -254,4 +263,4 @@ def postprocess_electricity_demand(input_path, input_version_name):
)


postprocess_electricity_demand(cfg.dle_path, cfg.vstr)
postprocess_electricity_demand(dle_path, cfg.vstr2 + "_" + cfg.vstr)
9 changes: 7 additions & 2 deletions message_ix_buildings/chilled/preprocess/archetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from message_ix_buildings.chilled.util.util import (
get_archs,
get_logger,
get_paths,
read_arch_inputs_df,
read_arch_reg_df,
)
Expand All @@ -23,7 +24,9 @@


def create_archetypes(config: "Config"):
out_path = os.path.join(config.project_path, "out", "version")
project_path = get_paths(config, "dle_path")

out_path = os.path.join(project_path, "out", "version")
archetype_path = os.path.join(out_path, config.vstr, "rasters")

# if archetypes folder does not exist, create it
Expand Down Expand Up @@ -101,7 +104,9 @@ def create_archetypes(config: "Config"):


def create_archetype_variables(config: "Config"):
out_path = os.path.join(config.project_path, "out", "version")
project_path = get_paths(config, "dle_path")

out_path = os.path.join(project_path, "out", "version")
archetype_path = os.path.join(out_path, config.vstr, "rasters")

# get archs
Expand Down
6 changes: 4 additions & 2 deletions message_ix_buildings/chilled/preprocess/message_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import xarray as xr

from message_ix_buildings.chilled.util.config import Config
from message_ix_buildings.chilled.util.util import get_paths


def create_message_raster(config: "Config"):
Expand All @@ -35,11 +36,12 @@ def create_message_raster(config: "Config"):
ISO attributes in a dataframe

"""
input_path = os.path.join(config.dle_path)
input_path = get_paths(config, "dle_path")
message_region_file = get_paths(config, "message_region_map_file")

if config.node == "R11":
msgregions = pd.read_excel(
config.message_region_file,
message_region_file,
sheet_name="regional definition",
)

Expand Down
11 changes: 1 addition & 10 deletions message_ix_buildings/chilled/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import numpy as np

from message_ix_buildings.chilled.functions.user_settings import DICT_USER_SETTINGS


@dataclass
class Config:
Expand All @@ -20,6 +18,7 @@ class Config:
#:
#: This is used to name the output files and directories.
vstr: str = "ALPS2023"
vstr2 = "v19"

#: Select the climate model.
#:
Expand Down Expand Up @@ -128,14 +127,6 @@ class Config:
#: TODO: In the future, support "R12".
node: Literal["R11"] = "R11"

#: Paths settings by user
project_path: str = str(DICT_USER_SETTINGS[user]["project_path"])
dle_path: str = str(DICT_USER_SETTINGS[user]["dle_path"])
message_region_file: str = str(DICT_USER_SETTINGS[user]["message_region_map_file"])
isimip_bias_adj_path: str = str(DICT_USER_SETTINGS[user]["isimip_bias_adj_path"])
isimip_ewemib_path: str = str(DICT_USER_SETTINGS[user]["isimip_ewembi_path"])
chunk_size = DICT_USER_SETTINGS[user]["chunk_size"]

#: NetCDF settings
netcdf4_format = "NETCDF4_CLASSIC"
comp = dict(zlib=True, complevel=5) # Compression between 0 and 9 (highest)
Expand Down
13 changes: 12 additions & 1 deletion message_ix_buildings/chilled/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
from pathlib import Path

import pandas as pd
from util.config import Config # type: ignore

from message_ix_buildings.chilled.functions.user_settings import DICT_USER_SETTINGS
from message_ix_buildings.chilled.util.config import Config # type: ignore


def get_project_root() -> Path:
return Path(__file__).parent.parent.parent


def get_paths(config: "Config", selection):
# if selection is chunk_size, then return chunk_size as is
# else, return string version of the path
if selection == "chunk_size":
return DICT_USER_SETTINGS[config.user][selection]
else:
return str(DICT_USER_SETTINGS[config.user][selection])


def get_logger(name: str):
log = logging.getLogger(name)
log.setLevel(logging.INFO)
Expand Down