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

Use absolute path for config.default.yaml in _helpers.py #1137

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ E.g. if a new rule becomes available describe how to use it `make test` and in o

* Include a dedicated cutout for Europe in bundle_config.yaml `PR #1125 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1125>`_

* Use absolute path for `config.default.yaml` in `_helpers.py` for facilitate smooth module import `PR #1137 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1137>`_

PyPSA-Earth 0.4.1
=================

Expand Down
8 changes: 7 additions & 1 deletion scripts/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
# filename of the regions definition config file
REGIONS_CONFIG = "regions_definition_config.yaml"

# absolute path to base directory (pypsa-earth)
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))

def check_config_version(config, fp_config="config.default.yaml"):
# absolute path to config.default.yaml
CONFIG_DEFAULT_PATH = os.path.join(BASE_DIR, "config.default.yaml")


def check_config_version(config, fp_config=CONFIG_DEFAULT_PATH):
"""
Check that a version of the local config.yaml matches to the actual config
version as defined in config.default.yaml.
Expand Down
17 changes: 7 additions & 10 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import requests
import xarray as xr
from _helpers import (
BASE_DIR,
configure_logging,
create_logger,
three_2_two_digits_country,
Expand Down Expand Up @@ -85,7 +86,7 @@ def download_GADM(country_code, update=False, out_logging=False):
GADM_url = f"https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/{GADM_filename}.gpkg"

GADM_inputfile_gpkg = os.path.join(
os.getcwd(),
BASE_DIR,
"data",
"gadm",
GADM_filename,
Expand Down Expand Up @@ -489,7 +490,7 @@ def download_WorldPop_standard(
]

WorldPop_inputfile = os.path.join(
os.getcwd(), "data", "WorldPop", WorldPop_filename
BASE_DIR, "data", "WorldPop", WorldPop_filename
) # Input filepath tif

if not os.path.exists(WorldPop_inputfile) or update is True:
Expand Down Expand Up @@ -543,7 +544,7 @@ def download_WorldPop_API(
WorldPop_filename = f"{two_2_three_digits_country(country_code).lower()}_ppp_{year}_UNadj_constrained.tif"
# Request to get the file
WorldPop_inputfile = os.path.join(
os.getcwd(), "data", "WorldPop", WorldPop_filename
BASE_DIR, "data", "WorldPop", WorldPop_filename
) # Input filepath tif
os.makedirs(os.path.dirname(WorldPop_inputfile), exist_ok=True)
year_api = int(str(year)[2:])
Expand Down Expand Up @@ -580,12 +581,10 @@ def convert_GDP(name_file_nc, year=2015, out_logging=False):
name_file_tif = name_file_nc[:-2] + "tif"

# path of the nc file
GDP_nc = os.path.join(os.getcwd(), "data", "GDP", name_file_nc) # Input filepath nc
GDP_nc = os.path.join(BASE_DIR, "data", "GDP", name_file_nc) # Input filepath nc

# path of the tif file
GDP_tif = os.path.join(
os.getcwd(), "data", "GDP", name_file_tif
) # Input filepath nc
GDP_tif = os.path.join(BASE_DIR, "data", "GDP", name_file_tif) # Input filepath nc

# Check if file exists, otherwise throw exception
if not os.path.exists(GDP_nc):
Expand Down Expand Up @@ -628,9 +627,7 @@ def load_GDP(

# path of the nc file
name_file_tif = name_file_nc[:-2] + "tif"
GDP_tif = os.path.join(
os.getcwd(), "data", "GDP", name_file_tif
) # Input filepath tif
GDP_tif = os.path.join(BASE_DIR, "data", "GDP", name_file_tif) # Input filepath tif

if update | (not os.path.exists(GDP_tif)):
if out_logging:
Expand Down
19 changes: 11 additions & 8 deletions scripts/retrieve_databundle_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,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.relpath(config["destination"])
destination = os.path.join(PREFIX, config["destination"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you propose PREFIX here instead of BASE_DIR as in the other cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @davide-f. That is the valid point. I have used BASE_DIR as the first try. And it worked fine as standalone for rule execution. But to make sure rules see each other and trigger, outputs and inputs needs to match. So submodules/pypsa-earth/data/eez/eez_v11.gpkg is not equal to absolute path /home/yerbol/efuels/.../submodules/pypsa-earth/data/eez/eez_v11.gpkg when it comes to input outputs of snakemake.

With BASE_DIR instead build_shapes were not triggering retrieve_databundle_light. But retrieve_databundle_light worked just fine separately when calling it. CI test actually helped me to figure that out (https://github.com/pypsa-meets-earth/pypsa-earth/actions/runs/11314101704/job/31463655978)

BASE_DIR is good for handling files inside of the script, that are not tight to input/outputs of snakemake. If it is input/output, only PREFIX makes the full workflow intact and preserve rule connections.

url = config["urls"]["zenodo"]

if hot_run:
Expand Down Expand Up @@ -188,7 +188,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.relpath(config["destination"])
destination = os.path.join(PREFIX, config["destination"])
url = config["urls"]["gdrive"]

# retrieve file_id from path
Expand Down Expand Up @@ -266,7 +266,7 @@ def download_and_unzip_protectedplanet(
"""
resource = config["category"]
file_path = os.path.join(rootpath, "tempfile_wpda.zip")
destination = os.path.relpath(config["destination"])
destination = os.path.join(PREFIX, config["destination"])
url = config["urls"]["protectedplanet"]

def get_first_day_of_month(date):
Expand Down Expand Up @@ -438,7 +438,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.relpath(config["destination"])
destination = os.path.join(PREFIX, config["destination"])
url = config["urls"]["direct"]

file_path = os.path.join(destination, os.path.basename(url))
Expand Down Expand Up @@ -492,7 +492,7 @@ def download_and_unzip_hydrobasins(
True when download is successful, False otherwise
"""
resource = config["category"]
destination = os.path.relpath(config["destination"])
destination = os.path.join(PREFIX, config["destination"])
url_templ = config["urls"]["hydrobasins"]["base_url"]
suffix_list = config["urls"]["hydrobasins"]["suffixes"]

Expand Down Expand Up @@ -543,7 +543,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.relpath(config["destination"])
destination = os.path.join(PREFIX, config["destination"])

# load data for post method
postdata = config["urls"]["post"]
Expand Down Expand Up @@ -792,8 +792,8 @@ def datafiles_retrivedatabundle(config):


def merge_hydrobasins_shape(config_hydrobasin, hydrobasins_level):
basins_path = config_hydrobasin["destination"]
output_fl = config_hydrobasin["output"][0]
basins_path = os.path.join(PREFIX, config_hydrobasin["destination"])
output_fl = os.path.join(PREFIX, config_hydrobasin["output"][0])

files_to_merge = [
"hybas_{0:s}_lev{1:02d}_v1c.shp".format(suffix, hydrobasins_level)
Expand Down Expand Up @@ -824,6 +824,9 @@ def merge_hydrobasins_shape(config_hydrobasin, hydrobasins_level):
countries = snakemake.params.countries
logger.info(f"Retrieving data for {len(countries)} countries.")

# get prefix (exists only if importing rule as import, else empty string)
PREFIX = snakemake.log[0].split("log")[0]

# load enable configuration
config_enable = snakemake.config["enable"]
# load databundle configuration
Expand Down
Loading