From 0f784cbc8eeade356490b412fa0d6171b163229c Mon Sep 17 00:00:00 2001 From: = Date: Thu, 24 Oct 2024 14:00:11 +0000 Subject: [PATCH] revert to BASE_DIR usage --- scripts/_helpers.py | 6 ++---- scripts/build_demand_profiles.py | 4 ++-- scripts/build_renewable_profiles.py | 4 ++-- scripts/retrieve_databundle_light.py | 18 +++++++++--------- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/scripts/_helpers.py b/scripts/_helpers.py index d8395fd32..253f7b9d1 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -36,13 +36,11 @@ # filename of the regions definition config file REGIONS_CONFIG = "regions_definition_config.yaml" -# prefix when running pypsa-earth rules in different directories (if running in pypsa-earth directory PREFIX='.') +# prefix when running pypsa-earth rules in different directories (if running in pypsa-earth as subworkflow) BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) -RUN_DIR = os.path.abspath(os.getcwd()) -PREFIX = os.path.relpath(BASE_DIR, RUN_DIR) # absolute path to config.default.yaml -CONFIG_DEFAULT_PATH = os.path.join(PREFIX, "config.default.yaml") +CONFIG_DEFAULT_PATH = os.path.join(BASE_DIR, "config.default.yaml") def check_config_version(config, fp_config=CONFIG_DEFAULT_PATH): diff --git a/scripts/build_demand_profiles.py b/scripts/build_demand_profiles.py index ed030f808..6e843e104 100644 --- a/scripts/build_demand_profiles.py +++ b/scripts/build_demand_profiles.py @@ -50,7 +50,7 @@ import pypsa import scipy.sparse as sparse import xarray as xr -from _helpers import configure_logging, create_logger, read_csv_nafix, read_osm_config, PREFIX +from _helpers import configure_logging, create_logger, read_csv_nafix, read_osm_config, BASE_DIR from shapely.prepared import prep from shapely.validation import make_valid @@ -121,7 +121,7 @@ def get_load_paths_gegis(ssp_parentfolder, config): for continent in region_load: sel_ext = ".nc" for ext in [".nc", ".csv"]: - load_path = os.path.join(PREFIX, str(load_dir), str(continent) + str(ext)) + load_path = os.path.join(BASE_DIR, str(load_dir), str(continent) + str(ext)) if os.path.exists(load_path): sel_ext = ext break diff --git a/scripts/build_renewable_profiles.py b/scripts/build_renewable_profiles.py index 1ebf220b4..4fcc80d9a 100644 --- a/scripts/build_renewable_profiles.py +++ b/scripts/build_renewable_profiles.py @@ -202,7 +202,7 @@ import pandas as pd import progressbar as pgb import xarray as xr -from _helpers import configure_logging, create_logger +from _helpers import configure_logging, create_logger, BASE_DIR from add_electricity import load_powerplants from dask.distributed import Client from pypsa.geo import haversine @@ -559,7 +559,7 @@ def create_scaling_factor( # filter plants for hydro if snakemake.wildcards.technology.startswith("hydro"): country_shapes = gpd.read_file(paths.country_shapes) - hydrobasins = gpd.read_file(resource["hydrobasins"]) + hydrobasins = gpd.read_file(os.path.join(BASE_DIR, resource["hydrobasins"])) ppls = load_powerplants(snakemake.input.powerplants) hydro_ppls = ppls[ppls.carrier == "hydro"] diff --git a/scripts/retrieve_databundle_light.py b/scripts/retrieve_databundle_light.py index 307fb5883..ed5ea9055 100644 --- a/scripts/retrieve_databundle_light.py +++ b/scripts/retrieve_databundle_light.py @@ -93,7 +93,7 @@ create_country_list, create_logger, progress_retrieve, - PREFIX + BASE_DIR ) from google_drive_downloader import GoogleDriveDownloader as gdd from tqdm import tqdm @@ -144,7 +144,7 @@ def download_and_unzip_zenodo(config, rootpath, hot_run=True, disable_progress=F """ resource = config["category"] file_path = os.path.join(rootpath, "tempfile.zip") - destination = os.path.join(PREFIX, config["destination"]) + destination = os.path.join(BASE_DIR, config["destination"]) url = config["urls"]["zenodo"] if hot_run: @@ -189,7 +189,7 @@ def download_and_unzip_gdrive(config, rootpath, hot_run=True, disable_progress=F """ resource = config["category"] file_path = os.path.join(rootpath, "tempfile.zip") - destination = os.path.join(PREFIX, config["destination"]) + destination = os.path.join(BASE_DIR, config["destination"]) url = config["urls"]["gdrive"] # retrieve file_id from path @@ -267,7 +267,7 @@ def download_and_unzip_protectedplanet( """ resource = config["category"] file_path = os.path.join(rootpath, "tempfile_wpda.zip") - destination = os.path.join(PREFIX, config["destination"]) + destination = os.path.join(BASE_DIR, config["destination"]) url = config["urls"]["protectedplanet"] def get_first_day_of_month(date): @@ -439,7 +439,7 @@ def download_and_unzip_direct(config, rootpath, hot_run=True, disable_progress=F True when download is successful, False otherwise """ resource = config["category"] - destination = os.path.join(PREFIX, config["destination"]) + destination = os.path.join(BASE_DIR, config["destination"]) url = config["urls"]["direct"] file_path = os.path.join(destination, os.path.basename(url)) @@ -493,7 +493,7 @@ def download_and_unzip_hydrobasins( True when download is successful, False otherwise """ resource = config["category"] - destination = os.path.join(PREFIX, config["destination"]) + destination = os.path.join(BASE_DIR, config["destination"]) url_templ = config["urls"]["hydrobasins"]["base_url"] suffix_list = config["urls"]["hydrobasins"]["suffixes"] @@ -544,7 +544,7 @@ def download_and_unzip_post(config, rootpath, hot_run=True, disable_progress=Fal True when download is successful, False otherwise """ resource = config["category"] - destination = os.path.join(PREFIX, config["destination"]) + destination = os.path.join(BASE_DIR, config["destination"]) # load data for post method postdata = config["urls"]["post"] @@ -793,8 +793,8 @@ def datafiles_retrivedatabundle(config): def merge_hydrobasins_shape(config_hydrobasin, hydrobasins_level): - basins_path = os.path.join(PREFIX, config_hydrobasin["destination"]) - output_fl = os.path.join(PREFIX, config_hydrobasin["output"][0]) + basins_path = os.path.join(BASE_DIR, config_hydrobasin["destination"]) + output_fl = os.path.join(BASE_DIR, config_hydrobasin["output"][0]) files_to_merge = [ "hybas_{0:s}_lev{1:02d}_v1c.shp".format(suffix, hydrobasins_level)