Skip to content

Commit

Permalink
Merge pull request #180 from nismod/feature/merge_admin_levels_disrup…
Browse files Browse the repository at this point in the history
…tion

Merge disruption results for admin levels of varying scale
  • Loading branch information
thomas-fred authored Apr 12, 2024
2 parents 88dc522 + 7ce415e commit e7c469c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
67 changes: 66 additions & 1 deletion workflow/power-tc/disruption.smk
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def disruption_summaries_for_storm_set(wildcards):
affects and return paths to their summary files.
"""

json_file = checkpoints.countries_intersecting_storm_set.get(**wildcards).output.country_set
json_file = f"{wildcards.OUTPUT_DIR}/power/by_storm_set/{wildcards.STORM_SET}/countries_impacted.json"
country_set = cached_json_file_read(json_file)

return expand(
Expand Down Expand Up @@ -471,3 +471,68 @@ rule disruption_by_admin_region_for_storm_set:
Test with:
snakemake --cores 1 -- results/power/by_storm_set/IBTrACS/disruption/EAPA_admin-level-2.gpq
"""


def EAPA_all_coarser_admin_levels(wildcards) -> list[str]:
"""
Given an admin level, return a list of paths to the EAPA file at that admin
level and all the coarser levels down to and including level 0 (national).
"""
max_level = int(wildcards.ADMIN_SLUG.split("-")[-1])
return expand(
"{OUTPUT_DIR}/power/by_storm_set/{STORM_SET}/disruption/EAPA_admin-level-{i}.gpq",
OUTPUT_DIR=wildcards.OUTPUT_DIR,
STORM_SET=wildcards.STORM_SET,
i=range(max_level + 1),
)

rule merge_disruption_admin_levels:
"""
Take results at target admin level and gap fill with coarser admin levels
where appropriate.
"""
input:
admin_levels = EAPA_all_coarser_admin_levels,
output:
merged_admin_levels = "{OUTPUT_DIR}/power/by_storm_set/{STORM_SET}/disruption/EAPA_{ADMIN_SLUG}-0.gpq"
run:
import logging

import geopandas as gpd

from open_gira.admin import merge_gadm_admin_levels

logging.basicConfig(format="%(asctime)s %(process)d %(filename)s %(message)s", level=logging.INFO)

if len(input.admin_levels) == 1:
# already at national level, no other region level data to merge
gpd.read_parquet(input.admin_levels).to_parquet(output.merged_admin_levels)

else:

def read_and_label_ISO_A3(path: str) -> gpd.GeoDataFrame:
"""Requires a GID_n column with e.g. ZWE.9.3_1 or AFG.3_1 data."""
df = gpd.read_parquet(path)
GID_col, = df.columns[list(map(lambda s: s.startswith("GID_"), df.columns))]
if "ISO_A3" in df.columns:
raise RuntimeError("Will not overwrite ISO_A3 column, quitting.")
df["ISO_A3"] = df[GID_col].apply(lambda s: s.split(".")[0])
return df

# e.g. admin-level-3, admin-level-2, [admin-level-1, admin-level-0]
# or admin-level-1, admin-level-0, []
primary, secondary, *others = [read_and_label_ISO_A3(path) for path in sorted(input.admin_levels, reverse=True)]

logging.info(f"Starting with: {set(primary.ISO_A3)}")
merged = merge_gadm_admin_levels(primary, secondary)

for other in others:
merged = merge_gadm_admin_levels(merged, other)

merged.reset_index(drop=True).sort_index(axis=1).to_parquet(output.merged_admin_levels)
logging.info("Done")

"""
Test with:
snakemake -c1 -- results/power/by_storm_set/IBTrACS/disruption/EAPA_admin-level-2-0.gpq
"""
7 changes: 6 additions & 1 deletion workflow/power-tc/exposure.smk
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def exposure_summaries_for_storm_set(wildcards):
affects and return paths to their summary files.
"""

json_file = checkpoints.countries_intersecting_storm_set.get(**wildcards).output.country_set
json_file = f"{wildcards.OUTPUT_DIR}/power/by_storm_set/{wildcards.STORM_SET}/countries_impacted.json"
country_set = cached_json_file_read(json_file)

return expand(
Expand Down Expand Up @@ -234,3 +234,8 @@ rule merge_exposure_admin_levels:

merged.reset_index(drop=True).sort_index(axis=1).to_parquet(output.merged_admin_levels)
logging.info("Done")

"""
Test with:
snakemake -c1 -- results/power/by_storm_set/IBTrACS/exposure/EAE_admin-level-2-0.gpq
"""

0 comments on commit e7c469c

Please sign in to comment.