Skip to content

Commit d2aa357

Browse files
create enum class for iso codes and add function get_regions_per_countries
1 parent 8680668 commit d2aa357

File tree

1 file changed

+182
-109
lines changed

1 file changed

+182
-109
lines changed

climada/entity/impact_funcs/trop_cyclone.py

Lines changed: 182 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
__all__ = ["ImpfTropCyclone", "ImpfSetTropCyclone", "IFTropCyclone"]
2323

2424
import logging
25+
from enum import Enum
2526

2627
import numpy as np
2728
import pandas as pd
@@ -34,6 +35,130 @@
3435
LOGGER = logging.getLogger(__name__)
3536

3637

38+
class country_code(Enum):
39+
"""
40+
Enum class that links ISO country codes (both iso3a and iso3n) to specific regions and
41+
associated impact function IDs.
42+
43+
Attributes:
44+
iso3a (dict): A mapping of region names to lists of 3-letter ISO country codes (iso3a).
45+
iso3n (dict): A mapping of region names to lists of numeric ISO country codes (iso3n).
46+
impf_id (dict): A mapping of region names to corresponding impact function IDs.
47+
region_name (dict): A mapping of region names to their descriptive names.
48+
"""
49+
50+
# fmt: off
51+
iso3n = {
52+
"NA1": [
53+
660, 28, 32, 533, 44, 52, 84, 60, 68, 132,
54+
136, 152, 170, 188, 192, 212, 214, 218, 222, 238,
55+
254, 308, 312, 320, 328, 332, 340, 388, 474, 484,
56+
500, 558, 591, 600, 604, 630, 654, 659, 662, 670,
57+
534, 740, 780, 796, 858, 862, 92, 850,
58+
],
59+
"NA2": [124, 840],
60+
"NI": [
61+
4, 51, 31, 48, 50, 64, 262, 232, 231, 268,
62+
356, 364, 368, 376, 400, 398, 414, 417, 422, 462,
63+
496, 104, 524, 512, 586, 634, 682, 706, 144, 760,
64+
762, 795, 800, 784, 860, 887,
65+
],
66+
"OC": [
67+
16, 36, 184, 242, 258, 316, 296, 584, 583, 520,
68+
540, 554, 570, 574, 580, 585, 598, 612, 882, 90,
69+
626, 772, 776, 798, 548, 876,
70+
],
71+
"SI": [174, 180, 748, 450, 454, 466, 480, 508, 710, 834, 716],
72+
"WP1": [116, 360, 418, 458, 764, 704],
73+
"WP2": [608],
74+
"WP3": [156],
75+
"WP4": [344, 392, 410, 446, 158],
76+
"ROW": [
77+
8, 12, 20, 24, 10, 40, 112, 56, 204, 535,
78+
70, 72, 74, 76, 86, 96, 100, 854, 108, 120,
79+
140, 148, 162, 166, 178, 191, 531, 196, 203, 384,
80+
208, 818, 226, 233, 234, 246, 250, 260, 266, 270,
81+
276, 288, 292, 300, 304, 831, 324, 624, 334, 336,
82+
348, 352, 372, 833, 380, 832, 404, 408, 983, 428,
83+
426, 430, 434, 438, 440, 442, 470, 478, 175, 498,
84+
492, 499, 504, 516, 528, 562, 566, 807, 578, 275,
85+
616, 620, 642, 643, 646, 638, 652, 663, 666, 674,
86+
678, 686, 688, 690, 694, 702, 703, 705, 239, 728,
87+
724, 729, 744, 752, 756, 768, 788, 792, 804, 826,
88+
581, 732, 894, 248,
89+
],
90+
}
91+
iso3a = {
92+
"NA1": [
93+
"AIA", "ATG", "ARG", "ABW", "BHS", "BRB", "BLZ", "BMU", "BOL", "CPV",
94+
"CYM", "CHL", "COL", "CRI", "CUB", "DMA", "DOM", "ECU", "SLV", "FLK",
95+
"GUF", "GRD", "GLP", "GTM", "GUY", "HTI", "HND", "JAM", "MTQ", "MEX",
96+
"MSR", "NIC", "PAN", "PRY", "PER", "PRI", "SHN", "KNA", "LCA", "VCT",
97+
"SXM", "SUR", "TTO", "TCA", "URY", "VEN", "VGB", "VIR",
98+
],
99+
"NA2": ["CAN", "USA"],
100+
"NI": [
101+
"AFG", "ARM", "AZE", "BHR", "BGD", "BTN", "DJI", "ERI", "ETH", "GEO",
102+
"IND", "IRN", "IRQ", "ISR", "JOR", "KAZ", "KWT", "KGZ", "LBN", "MDV",
103+
"MNG", "MMR", "NPL", "OMN", "PAK", "QAT", "SAU", "SOM", "LKA", "SYR",
104+
"TJK", "TKM", "UGA", "ARE", "UZB", "YEM",
105+
],
106+
"OC": [
107+
"ASM", "AUS", "COK", "FJI", "PYF", "GUM", "KIR", "MHL", "FSM", "NRU",
108+
"NCL", "NZL", "NIU", "NFK", "MNP", "PLW", "PNG", "PCN", "WSM", "SLB",
109+
"TLS", "TKL", "TON", "TUV", "VUT", "WLF",
110+
],
111+
"SI": [
112+
"COM", "COD", "SWZ", "MDG", "MWI", "MLI", "MUS", "MOZ", "ZAF", "TZA",
113+
"ZWE",
114+
],
115+
"WP1": ["KHM", "IDN", "LAO", "MYS", "THA", "VNM"],
116+
"WP2": ["PHL"],
117+
"WP3": ["CHN"],
118+
"WP4": ["HKG", "JPN", "KOR", "MAC", "TWN"],
119+
"ROW": [
120+
"ALB", "DZA", "AND", "AGO", "ATA", "AUT", "BLR", "BEL", "BEN", "BES",
121+
"BIH", "BWA", "BVT", "BRA", "IOT", "BRN", "BGR", "BFA", "BDI", "CMR",
122+
"CAF", "TCD", "CXR", "CCK", "COG", "HRV", "CUW", "CYP", "CZE", "CIV",
123+
"DNK", "EGY", "GNQ", "EST", "FRO", "FIN", "FRA", "ATF", "GAB", "GMB",
124+
"DEU", "GHA", "GIB", "GRC", "GRL", "GGY", "GIN", "GNB", "HMD", "VAT",
125+
"HUN", "ISL", "IRL", "IMN", "ITA", "JEY", "KEN", "PRK", "XKX", "LVA",
126+
"LSO", "LBR", "LBY", "LIE", "LTU", "LUX", "MLT", "MRT", "MYT", "MDA",
127+
"MCO", "MNE", "MAR", "NAM", "NLD", "NER", "NGA", "MKD", "NOR", "PSE",
128+
"POL", "PRT", "ROU", "RUS", "RWA", "REU", "BLM", "MAF", "SPM", "SMR",
129+
"STP", "SEN", "SRB", "SYC", "SLE", "SGP", "SVK", "SVN", "SGS", "SSD",
130+
"ESP", "SDN", "SJM", "SWE", "CHE", "TGO", "TUN", "TUR", "UKR", "GBR",
131+
"UMI", "ESH", "ZMB", "ALA",
132+
],
133+
}
134+
# fmt: on
135+
impf_id = {
136+
"NA1": 1,
137+
"NA2": 2,
138+
"NI": 3,
139+
"OC": 4,
140+
"SI": 5,
141+
"WP1": 6,
142+
"WP2": 7,
143+
"WP3": 8,
144+
"WP4": 9,
145+
"ROW": 10,
146+
}
147+
148+
region_name = {
149+
"NA1": "Caribbean and Mexico",
150+
"NA2": "USA and Canada",
151+
"NI": "North Indian",
152+
"OC": "Oceania",
153+
"SI": "South Indian",
154+
"WP1": "South East Asia",
155+
"WP2": "Philippines",
156+
"WP3": "China Mainland",
157+
"WP4": "North West Pacific",
158+
"ROW": "Rest of The World",
159+
}
160+
161+
37162
class ImpfTropCyclone(ImpactFunc):
38163
"""Impact functions for tropical cyclones."""
39164

@@ -311,118 +436,66 @@ def get_countries_per_region(region=None):
311436
"""
312437
if not region:
313438
region = "all"
314-
# fmt: off
315-
iso3n = {
316-
"NA1": [
317-
660, 28, 32, 533, 44, 52, 84, 60, 68, 132,
318-
136, 152, 170, 188, 192, 212, 214, 218, 222, 238,
319-
254, 308, 312, 320, 328, 332, 340, 388, 474, 484,
320-
500, 558, 591, 600, 604, 630, 654, 659, 662, 670,
321-
534, 740, 780, 796, 858, 862, 92, 850,
322-
],
323-
"NA2": [124, 840],
324-
"NI": [
325-
4, 51, 31, 48, 50, 64, 262, 232, 231, 268,
326-
356, 364, 368, 376, 400, 398, 414, 417, 422, 462,
327-
496, 104, 524, 512, 586, 634, 682, 706, 144, 760,
328-
762, 795, 800, 784, 860, 887,
329-
],
330-
"OC": [
331-
16, 36, 184, 242, 258, 316, 296, 584, 583, 520,
332-
540, 554, 570, 574, 580, 585, 598, 612, 882, 90,
333-
626, 772, 776, 798, 548, 876,
334-
],
335-
"SI": [174, 180, 748, 450, 454, 466, 480, 508, 710, 834, 716],
336-
"WP1": [116, 360, 418, 458, 764, 704],
337-
"WP2": [608],
338-
"WP3": [156],
339-
"WP4": [344, 392, 410, 446, 158],
340-
"ROW": [
341-
8, 12, 20, 24, 10, 40, 112, 56, 204, 535,
342-
70, 72, 74, 76, 86, 96, 100, 854, 108, 120,
343-
140, 148, 162, 166, 178, 191, 531, 196, 203, 384,
344-
208, 818, 226, 233, 234, 246, 250, 260, 266, 270,
345-
276, 288, 292, 300, 304, 831, 324, 624, 334, 336,
346-
348, 352, 372, 833, 380, 832, 404, 408, 983, 428,
347-
426, 430, 434, 438, 440, 442, 470, 478, 175, 498,
348-
492, 499, 504, 516, 528, 562, 566, 807, 578, 275,
349-
616, 620, 642, 643, 646, 638, 652, 663, 666, 674,
350-
678, 686, 688, 690, 694, 702, 703, 705, 239, 728,
351-
724, 729, 744, 752, 756, 768, 788, 792, 804, 826,
352-
581, 732, 894, 248,
353-
],
354-
}
355-
iso3a = {
356-
"NA1": [
357-
"AIA", "ATG", "ARG", "ABW", "BHS", "BRB", "BLZ", "BMU", "BOL", "CPV",
358-
"CYM", "CHL", "COL", "CRI", "CUB", "DMA", "DOM", "ECU", "SLV", "FLK",
359-
"GUF", "GRD", "GLP", "GTM", "GUY", "HTI", "HND", "JAM", "MTQ", "MEX",
360-
"MSR", "NIC", "PAN", "PRY", "PER", "PRI", "SHN", "KNA", "LCA", "VCT",
361-
"SXM", "SUR", "TTO", "TCA", "URY", "VEN", "VGB", "VIR",
362-
],
363-
"NA2": ["CAN", "USA"],
364-
"NI": [
365-
"AFG", "ARM", "AZE", "BHR", "BGD", "BTN", "DJI", "ERI", "ETH", "GEO",
366-
"IND", "IRN", "IRQ", "ISR", "JOR", "KAZ", "KWT", "KGZ", "LBN", "MDV",
367-
"MNG", "MMR", "NPL", "OMN", "PAK", "QAT", "SAU", "SOM", "LKA", "SYR",
368-
"TJK", "TKM", "UGA", "ARE", "UZB", "YEM",
369-
],
370-
"OC": [
371-
"ASM", "AUS", "COK", "FJI", "PYF", "GUM", "KIR", "MHL", "FSM", "NRU",
372-
"NCL", "NZL", "NIU", "NFK", "MNP", "PLW", "PNG", "PCN", "WSM", "SLB",
373-
"TLS", "TKL", "TON", "TUV", "VUT", "WLF",
374-
],
375-
"SI": [
376-
"COM", "COD", "SWZ", "MDG", "MWI", "MLI", "MUS", "MOZ", "ZAF", "TZA",
377-
"ZWE",
378-
],
379-
"WP1": ["KHM", "IDN", "LAO", "MYS", "THA", "VNM"],
380-
"WP2": ["PHL"],
381-
"WP3": ["CHN"],
382-
"WP4": ["HKG", "JPN", "KOR", "MAC", "TWN"],
383-
"ROW": [
384-
"ALB", "DZA", "AND", "AGO", "ATA", "AUT", "BLR", "BEL", "BEN", "BES",
385-
"BIH", "BWA", "BVT", "BRA", "IOT", "BRN", "BGR", "BFA", "BDI", "CMR",
386-
"CAF", "TCD", "CXR", "CCK", "COG", "HRV", "CUW", "CYP", "CZE", "CIV",
387-
"DNK", "EGY", "GNQ", "EST", "FRO", "FIN", "FRA", "ATF", "GAB", "GMB",
388-
"DEU", "GHA", "GIB", "GRC", "GRL", "GGY", "GIN", "GNB", "HMD", "VAT",
389-
"HUN", "ISL", "IRL", "IMN", "ITA", "JEY", "KEN", "PRK", "XKX", "LVA",
390-
"LSO", "LBR", "LBY", "LIE", "LTU", "LUX", "MLT", "MRT", "MYT", "MDA",
391-
"MCO", "MNE", "MAR", "NAM", "NLD", "NER", "NGA", "MKD", "NOR", "PSE",
392-
"POL", "PRT", "ROU", "RUS", "RWA", "REU", "BLM", "MAF", "SPM", "SMR",
393-
"STP", "SEN", "SRB", "SYC", "SLE", "SGP", "SVK", "SVN", "SGS", "SSD",
394-
"ESP", "SDN", "SJM", "SWE", "CHE", "TGO", "TUN", "TUR", "UKR", "GBR",
395-
"UMI", "ESH", "ZMB", "ALA",
396-
],
397-
}
398-
# fmt: on
399-
impf_id = {
400-
"NA1": 1,
401-
"NA2": 2,
402-
"NI": 3,
403-
"OC": 4,
404-
"SI": 5,
405-
"WP1": 6,
406-
"WP2": 7,
407-
"WP3": 8,
408-
"WP4": 9,
409-
"ROW": 10,
410-
}
411-
region_name = dict()
412-
region_name["NA1"] = "Caribbean and Mexico"
413-
region_name["NA2"] = "USA and Canada"
414-
region_name["NI"] = "North Indian"
415-
region_name["OC"] = "Oceania"
416-
region_name["SI"] = "South Indian"
417-
region_name["WP1"] = "South East Asia"
418-
region_name["WP2"] = "Philippines"
419-
region_name["WP3"] = "China Mainland"
420-
region_name["WP4"] = "North West Pacific"
421439

422440
if region == "all":
423-
return region_name, impf_id, iso3n, iso3a
441+
return (
442+
country_code.region_name,
443+
country_code.impf_id,
444+
country_code.iso3n,
445+
country_code.iso3a,
446+
)
447+
448+
return (
449+
country_code.region_name[region],
450+
country_code.impf_id[region],
451+
country_code.iso3n[region],
452+
country_code.iso3a[region],
453+
)
454+
455+
@staticmethod
456+
def get_regions_per_countries(
457+
countries: list = None, code_type: str = "iso3a"
458+
) -> tuple:
459+
"""Return the impact function id and the region corresponding to a list of countries,
460+
or a single country.
461+
462+
Parameters:
463+
-----------
464+
countries : list
465+
List containing the ISO code of the country, which should be either
466+
a string if the code is iso3a or an integer if iso3n. For example, for Switzerland:
467+
the iso3a code is "CHE" and the iso3n is 756.
468+
code_type : str
469+
Either "iso3a" or "iso3n".
470+
471+
Returns:
472+
--------
473+
impf_ids : list
474+
List of impact function ids matching the countries.
475+
regions_ids : list
476+
List of the regions that match the countries.
477+
regions_names : list
478+
List of the regions that match the countries.
479+
"""
424480

425-
return region_name[region], impf_id[region], iso3n[region], iso3a[region]
481+
if code_type not in {"iso3a", "iso3n"}:
482+
raise ValueError("code_type must be either 'iso3a' or 'iso3n'")
483+
484+
country_dict = getattr(country_code, code_type).value
485+
# Find region
486+
regions_ids = [
487+
key
488+
for country in countries
489+
for key, value in country_dict.items()
490+
if country in value
491+
]
492+
# Find impact function id
493+
impf_ids = [country_code.impf_id.value[region] for region in regions_ids]
494+
regions_name = [
495+
country_code.region_name.value[region] for region in regions_ids
496+
]
497+
498+
return impf_ids, regions_ids, regions_name
426499

427500

428501
@deprecated(

0 commit comments

Comments
 (0)