Skip to content

Commit a3d0c12

Browse files
authored
Merge pull request #1106 from CLIMADA-project/feature/fix-1105-get_countries_per_region
Suggested fix for ImpfSetTropCyclone.get_countries_per_region() #1105
2 parents cf24ba4 + 31c9886 commit a3d0c12

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

climada/entity/impact_funcs/test/test_tc.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import numpy as np
2525
import pandas as pd
2626

27-
from climada.entity.impact_funcs.trop_cyclone import ImpfSetTropCyclone, ImpfTropCyclone
27+
from climada.entity.impact_funcs.trop_cyclone import (
28+
CountryCode,
29+
ImpfSetTropCyclone,
30+
ImpfTropCyclone,
31+
)
2832

2933

3034
class TestEmanuelFormula(unittest.TestCase):
@@ -168,6 +172,16 @@ def test_get_countries_per_region(self):
168172
self.assertListEqual(out[2], [124, 840])
169173
self.assertListEqual(out[3], ["CAN", "USA"])
170174

175+
def test_get_countries_per_region_all_or_none(self):
176+
ifs = ImpfSetTropCyclone()
177+
out = ifs.get_countries_per_region()
178+
out2 = ifs.get_countries_per_region("all")
179+
self.assertEqual(out, out2)
180+
for reg in CountryCode.REGION_NAME.value:
181+
out_reg = ifs.get_countries_per_region(reg)
182+
for i in range(4):
183+
self.assertEqual(out[i][reg], out_reg[i])
184+
171185
def test_get_imf_id_regions_per_countries(self):
172186
"""Test get_impf_id_regions_per_countries()"""
173187
ifs = ImpfSetTropCyclone()

climada/entity/impact_funcs/trop_cyclone.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import logging
2525
from enum import Enum
26+
from typing import Literal, overload
2627

2728
import numpy as np
2829
import pandas as pd
@@ -92,7 +93,7 @@ class CountryCode(Enum):
9293
"NAM", "NER", "NGA", "NLD", "NOR", "POL", "PRK", "PRT", "PSE", "REU", "ROU",
9394
"RUS", "RWA", "SDN", "SEN", "SGP", "SGS", "SJM", "SLE", "SMR", "SPM", "SRB",
9495
"SSD", "STP", "SVK", "SVN", "SWE", "SYC", "TCD", "TGO", "TUN", "TUR", "UKR",
95-
"UMI", "VAT", "XKX", "ZMB",
96+
"UMI", "VAT", "XKO", "ZMB",
9697
],
9798
}
9899

@@ -363,10 +364,47 @@ def calibrated_regional_vhalf(
363364
reg_v_half[regions_short[-1]] = np.round(df_reg["v_half"].values[0], 5)
364365
return reg_v_half
365366

367+
@overload
368+
@staticmethod
369+
def get_countries_per_region(
370+
region: Literal["all"] = "all",
371+
) -> tuple[
372+
dict[str, str], # region_name
373+
dict[str, int], # impf_id
374+
dict[str, list[int]], # numeric
375+
dict[str, list[str]], # alpha3
376+
]: ...
377+
378+
@overload
379+
@staticmethod
380+
def get_countries_per_region(
381+
region: None,
382+
) -> tuple[
383+
dict[str, str], dict[str, int], dict[str, list[int]], dict[str, list[str]]
384+
]: ...
385+
386+
@overload
387+
@staticmethod
388+
def get_countries_per_region(
389+
region: str,
390+
) -> tuple[
391+
str, int, list[int], list[str] # region_name # impf_id # numeric # alpha3
392+
]: ...
393+
366394
@staticmethod
367395
def get_countries_per_region(region=None):
368-
"""Returns dictionaries with numerical (numeric) and alphabetical (alpha3) ISO3 codes
369-
of all countries associated to a calibration region.
396+
"""Returns countries within a TC calibration region and associated impact functions.
397+
398+
This method returns a tuple with numerical (numeric) and alphabetical (alpha3)
399+
ISO3 codes of all countries associated to a calibration region.
400+
401+
If no region or "all" is provided as argument, the method return a tuple of
402+
dictionaries with short name of the tropical cyclone calibration regions as
403+
keys and the values for each of those.
404+
405+
Notes
406+
-----
407+
370408
Only contains countries that were affected by tropical cyclones
371409
between 1980 and 2017 according to EM-DAT.
372410
@@ -395,9 +433,12 @@ def get_countries_per_region(region=None):
395433
return (
396434
CountryCode.REGION_NAME.value,
397435
CountryCode.IMPF_ID.value,
398-
coordinates.country_to_iso(
399-
CountryCode.ALPHA3.value, representation="numeric"
400-
),
436+
{
437+
reg: coordinates.country_to_iso(
438+
CountryCode.ALPHA3.value[reg], representation="numeric"
439+
)
440+
for reg in CountryCode.REGION_NAME.value
441+
},
401442
CountryCode.ALPHA3.value,
402443
)
403444

0 commit comments

Comments
 (0)