Skip to content

Commit 7da3617

Browse files
authored
fix: Implement fallback to Google Drive for UNSD data download (pypsa-meets-earth#1653)
* fix: Implement fallback to Google Drive for UNSD data download * fix: Update release notes to include hot-fix for UNSD data downtime affecting CI
1 parent 9abed0a commit 7da3617

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

doc/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Upcoming release
1212
This part of documentation collects descriptive release notes to capture the main improvements introduced by developing the model before the next release.
1313

1414
**New Features and Major Changes**
15+
* Added a hot-fix to handle UNSD data downtime causing CI to fail `PR #1653 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1653>`__
1516

1617
* Align PyPSA-Earth costs with the reference units used in `technology-data`, avoiding discrepancies when combining technologies with different original currency years `PR #1604 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1604>`__
1718

scripts/build_base_energy_totals.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import glob
88
import logging
99
import os
10+
import shutil
1011
import sys
1112
from io import BytesIO
1213
from pathlib import Path
@@ -20,6 +21,7 @@
2021
import py7zr
2122
import requests
2223
from _helpers import BASE_DIR, aggregate_fuels, get_conv_factors
24+
from googledrivedownloader import download_file_from_google_drive as download_gdrive
2325

2426
_logger = logging.getLogger(__name__)
2527

@@ -380,14 +382,32 @@ def calc_sector(sector):
380382
os.remove(f)
381383

382384
# Feed the dictionary of links to the for loop, download and unzip all files
383-
for key, value in d.items():
384-
zipurl = value
385-
386-
with urlopen(zipurl) as zipresp:
387-
with ZipFile(BytesIO(zipresp.read())) as zfile:
388-
zfile.extractall(os.path.join(BASE_DIR, "data/demand/unsd/data"))
385+
try:
386+
for key, value in d.items():
387+
zipurl = value
388+
389+
with urlopen(zipurl) as zipresp:
390+
with ZipFile(BytesIO(zipresp.read())) as zfile:
391+
zfile.extractall(
392+
os.path.join(BASE_DIR, "data/demand/unsd/data")
393+
)
394+
395+
path = os.path.join(BASE_DIR, "data/demand/unsd/data")
396+
except:
397+
_logger.warning(
398+
f"Could not open the file from {zipurl}. "
399+
"Using the data stored in google drive."
400+
)
401+
download_gdrive(
402+
file_id="1VUV0X-tTQECi2pHdE5EWXjPI2yeCdk6F",
403+
dest_path=os.path.join(BASE_DIR, "data/demand/unsd/unsd.zip"),
404+
unzip=True,
405+
)
389406

390-
path = os.path.join(BASE_DIR, "data/demand/unsd/data")
407+
# clean up __MACOSX folder if it exists
408+
macosx_root_path = os.path.join(BASE_DIR, "data/demand/unsd/__MACOSX")
409+
if os.path.exists(macosx_root_path):
410+
shutil.rmtree(macosx_root_path)
391411

392412
# Get the files from the path provided in the OP
393413
all_files = list(

0 commit comments

Comments
 (0)