Check WDPA url also a month forward

This commit is contained in:
euronion 2023-12-21 18:39:06 +01:00
parent 64300c26e5
commit 25c5751565
2 changed files with 24 additions and 13 deletions

View File

@ -31,6 +31,8 @@ Upcoming Release
* Rule ``retrieve_irena`` get updated values for renewables capacities. * Rule ``retrieve_irena`` get updated values for renewables capacities.
* Rule ``retrieve_wdpa`` updated to not only check for current and previous, but also potentially next months dataset availability.
* Split configuration to enable SMR and SMR CC. * Split configuration to enable SMR and SMR CC.
* The configuration setting for country focus weights when clustering the * The configuration setting for country focus weights when clustering the

View File

@ -246,22 +246,31 @@ if config["enable"]["retrieve"]:
if config["enable"]["retrieve"]: if config["enable"]["retrieve"]:
current_month = datetime.now().strftime("%b")
current_year = datetime.now().strftime("%Y") # Some logic to find the correct file URL
bYYYY = f"{current_month}{current_year}" # Sometimes files are released delayed or ahead of schedule, check which file is currently available
def check_file_exists(url): def check_file_exists(url):
response = requests.head(url) response = requests.head(url)
return response.status_code == 200 return response.status_code == 200
url = f"https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_{bYYYY}_Public.zip" # Basic pattern where WDPA files can be found
url_pattern = "https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_{bY}_Public.zip"
if not check_file_exists(url): # 3-letter month + 4 digit year for current/previous/next month to test
prev_month = (datetime.now() - timedelta(30)).strftime("%b") current_monthyear = datetime.now().strftime("%b%Y")
bYYYY = f"{prev_month}{current_year}" prev_monthyear = (datetime.now() - timedelta(30)).strftime("%b%Y")
assert check_file_exists( next_monthyear = (datetime.now() + timedelta(30)).strftime("%b%Y")
f"https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_{bYYYY}_Public.zip"
), "The file does not exist." # Test prioritised: current month -> previous -> next
for bY in [current_monthyear, prev_monthyear, next_monthyear]:
if check_file_exists(url := url_pattern.format(bY=bY)):
break
else:
# If None of the three URLs are working
url = False
assert url, f"No WDPA files found at {url_pattern} for bY='{current_monthyear}, {prev_monthyear}, or {next_monthyear}'"
# Downloading protected area database from WDPA # Downloading protected area database from WDPA
# extract the main zip and then merge the contained 3 zipped shapefiles # extract the main zip and then merge the contained 3 zipped shapefiles
@ -269,7 +278,7 @@ if config["enable"]["retrieve"]:
rule download_wdpa: rule download_wdpa:
input: input:
HTTP.remote( HTTP.remote(
f"d1gam3xoknrgr2.cloudfront.net/current/WDPA_{bYYYY}_Public_shp.zip", url,
static=True, static=True,
keep_local=True, keep_local=True,
), ),