-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathea_impact_events.py
More file actions
618 lines (507 loc) · 16.8 KB
/
ea_impact_events.py
File metadata and controls
618 lines (507 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/bin/env python3
"""
East Africa Impact Events - EM-DAT Data Processing and Visualization
This script processes EM-DAT disaster data for East Africa (ICPAC region),
assigns admin codes to events, and generates frequency maps as PNG files.
Data sources:
- EM-DAT: public_emdat_custom_request_2026-01-21.xlsx
- Boundaries: icpac_adm1v3.geojson
Output:
- Frequency maps (PNG)
- Yearly extent maps (PNG)
- Updated Excel files with admin codes
"""
import os
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
from pathlib import Path
# =============================================================================
# Configuration
# =============================================================================
# File paths
EMDAT_FILE = "public_emdat_custom_request_2026-01-21.xlsx"
ICPAC_GEOJSON = "icpac_adm1v3.geojson"
OUTPUT_DIR = "output"
# East Africa (ICPAC) countries
EA_COUNTRIES = [
'Djibouti', 'Eritrea', 'Ethiopia', 'Kenya', 'Rwanda',
'Somalia', 'South Sudan', 'Sudan', 'United Republic of Tanzania',
'Uganda', 'Burundi'
]
# ISO code mapping
COUNTRY_ISO_MAP = {
'Djibouti': 'DJI',
'Eritrea': 'ERI',
'Ethiopia': 'ETH',
'Kenya': 'KEN',
'Rwanda': 'RWA',
'Somalia': 'SOM',
'South Sudan': 'SSD',
'Sudan': 'SDN',
'United Republic of Tanzania': 'TZA',
'Uganda': 'UGA',
'Burundi': 'BDI'
}
# Map extent for East Africa
MAP_EXTENT = {
'x_min': 21.84,
'x_max': 51.42,
'y_min': -11.75,
'y_max': 23.15
}
# Year range
YEAR_START = 1990
YEAR_END = 2025
# =============================================================================
# Data Loading Functions
# =============================================================================
def load_emdat_data(filepath):
"""
Load EM-DAT data from Excel file.
Parameters
----------
filepath : str
Path to the EM-DAT Excel file
Returns
-------
pd.DataFrame
Loaded EM-DAT data
"""
print(f"Loading EM-DAT data from {filepath}...")
df = pd.read_excel(filepath, engine='openpyxl', sheet_name='EM-DAT Data')
print(f" Loaded {len(df)} records")
return df
def filter_ea_data(df, countries=EA_COUNTRIES):
"""
Filter data for East Africa countries.
Parameters
----------
df : pd.DataFrame
Full EM-DAT dataset
countries : list
List of country names to filter
Returns
-------
pd.DataFrame
Filtered dataset for East Africa
"""
df_ea = df[df['Country'].isin(countries)].copy()
print(f" Filtered to {len(df_ea)} East Africa records")
return df_ea
def filter_by_disaster_type(df, disaster_type):
"""
Filter data by disaster type.
Parameters
----------
df : pd.DataFrame
EM-DAT dataset
disaster_type : str
'Drought' or 'Flood'
Returns
-------
pd.DataFrame
Filtered dataset
"""
df_filtered = df[df['Disaster Type'] == disaster_type].copy()
print(f" {disaster_type}: {len(df_filtered)} events")
return df_filtered
def filter_by_year_range(df, start_year, end_year):
"""
Filter data by year range.
Parameters
----------
df : pd.DataFrame
EM-DAT dataset
start_year : int
Start year (inclusive)
end_year : int
End year (inclusive)
Returns
-------
pd.DataFrame
Filtered dataset
"""
df_filtered = df[(df['Start Year'] >= start_year) &
(df['Start Year'] <= end_year)].copy()
print(f" Year range {start_year}-{end_year}: {len(df_filtered)} events")
return df_filtered
def load_icpac_boundaries(filepath):
"""
Load ICPAC admin1 boundaries from GeoJSON.
Parameters
----------
filepath : str
Path to GeoJSON file
Returns
-------
gpd.GeoDataFrame
GeoDataFrame with admin1 boundaries
"""
print(f"Loading ICPAC boundaries from {filepath}...")
gdf = gpd.read_file(filepath)
# Extract ISO code from GID_1 (e.g., "ETH.1_1" -> "ETH")
gdf['ISO'] = gdf['GID_1'].str.split('.').str[0]
print(f" Loaded {len(gdf)} admin1 regions")
print(f" Countries: {sorted(gdf['ISO'].unique())}")
return gdf
# =============================================================================
# Admin Code Assignment Functions
# =============================================================================
def assign_admin1_from_country(df, gdf_boundaries):
"""
Assign admin1 regions to events based on country.
For events without specific location, assigns all admin1 regions of that country.
Parameters
----------
df : pd.DataFrame
EM-DAT disaster data
gdf_boundaries : gpd.GeoDataFrame
Admin1 boundaries
Returns
-------
pd.DataFrame
Data with admin1_list column
"""
df = df.copy()
# Create country to admin1 mapping
country_admin1_map = {}
for iso in gdf_boundaries['ISO'].unique():
admin1_list = gdf_boundaries[gdf_boundaries['ISO'] == iso]['GID_1'].tolist()
country_admin1_map[iso] = admin1_list
# Assign admin1 codes based on country ISO
def get_admin1_list(row):
iso = COUNTRY_ISO_MAP.get(row['Country'])
if iso and iso in country_admin1_map:
return country_admin1_map[iso]
return []
df['admin1_list'] = df.apply(get_admin1_list, axis=1)
return df
def count_events_by_admin1(df, gdf_boundaries):
"""
Count disaster events per admin1 region.
Parameters
----------
df : pd.DataFrame
Disaster data with admin1_list column
gdf_boundaries : gpd.GeoDataFrame
Admin1 boundaries
Returns
-------
gpd.GeoDataFrame
Boundaries with event_count column
"""
# Explode admin1_list to get one row per admin1 per event
df_exploded = df.explode('admin1_list')
# Count events per admin1
counts = df_exploded.groupby('admin1_list').size().reset_index(name='event_count')
counts.columns = ['GID_1', 'event_count']
# Merge with boundaries
gdf = gdf_boundaries.merge(counts, on='GID_1', how='left')
gdf['event_count'] = gdf['event_count'].fillna(0).astype(int)
return gdf
def get_affected_admin1_by_year(df, year):
"""
Get list of admin1 regions affected in a specific year.
Parameters
----------
df : pd.DataFrame
Disaster data with admin1_list column
year : int
Year to filter
Returns
-------
list
List of affected admin1 GID codes
"""
df_year = df[df['Start Year'] == year]
# Collect all admin1 codes
affected = []
for admin_list in df_year['admin1_list']:
if isinstance(admin_list, list):
affected.extend(admin_list)
return list(set(affected))
# =============================================================================
# Visualization Functions
# =============================================================================
def get_colormap():
"""
Create custom colormap for frequency maps.
Returns
-------
LinearSegmentedColormap
Custom colormap (yellow to red)
"""
colors = ['#ffffcc', '#ffeda0', '#fed976', '#feb24c',
'#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026']
return LinearSegmentedColormap.from_list('disaster_cmap', colors)
def create_frequency_map(gdf, title, output_path, disaster_type='Disaster'):
"""
Create choropleth frequency map.
Parameters
----------
gdf : gpd.GeoDataFrame
Boundaries with event_count column
title : str
Map title
output_path : str
Output PNG file path
disaster_type : str
Type of disaster for labeling
"""
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
# Set map extent
ax.set_xlim(MAP_EXTENT['x_min'], MAP_EXTENT['x_max'])
ax.set_ylim(MAP_EXTENT['y_min'], MAP_EXTENT['y_max'])
# Plot background (all regions in light gray)
gdf.plot(ax=ax, color='lightgray', edgecolor='white', linewidth=0.5)
# Plot regions with events
gdf_with_events = gdf[gdf['event_count'] > 0]
if len(gdf_with_events) > 0:
max_count = gdf['event_count'].max()
gdf_with_events.plot(
ax=ax,
column='event_count',
cmap=get_colormap(),
edgecolor='black',
linewidth=0.3,
legend=True,
legend_kwds={
'label': f'Number of {disaster_type} Events',
'orientation': 'horizontal',
'shrink': 0.6,
'pad': 0.05
},
vmin=0,
vmax=max_count
)
# Add title
ax.set_title(title, fontsize=16, fontweight='bold', pad=20)
# Add source annotation
ax.annotate(
'Data Source: EM-DAT, https://www.emdat.be/',
xy=(0.99, 0.01), xycoords='axes fraction',
ha='right', va='bottom', fontsize=8, color='gray'
)
# Remove axis labels
ax.set_xlabel('')
ax.set_ylabel('')
ax.tick_params(labelsize=8)
# Save
plt.tight_layout()
plt.savefig(output_path, dpi=200, bbox_inches='tight', facecolor='white')
plt.close()
print(f" Saved: {output_path}")
def create_yearly_extent_map(gdf, affected_admin1, year, output_path, disaster_type='Disaster'):
"""
Create map showing affected regions for a specific year.
Parameters
----------
gdf : gpd.GeoDataFrame
Admin1 boundaries
affected_admin1 : list
List of affected admin1 GID codes
year : int
Year
output_path : str
Output PNG file path
disaster_type : str
Type of disaster
"""
fig, ax = plt.subplots(1, 1, figsize=(10, 12))
# Set map extent
ax.set_xlim(MAP_EXTENT['x_min'], MAP_EXTENT['x_max'])
ax.set_ylim(MAP_EXTENT['y_min'], MAP_EXTENT['y_max'])
# Plot all regions in light gray
gdf.plot(ax=ax, color='lightgray', edgecolor='white', linewidth=0.5)
# Plot affected regions in red
gdf_affected = gdf[gdf['GID_1'].isin(affected_admin1)]
if len(gdf_affected) > 0:
gdf_affected.plot(ax=ax, color='red', edgecolor='darkred', linewidth=0.5, alpha=0.8)
# Add year label
ax.text(
0.95, 0.05, str(year),
transform=ax.transAxes,
fontsize=36, fontweight='bold',
ha='right', va='bottom'
)
# Add disaster type label
ax.text(
0.95, 0.12, f'{disaster_type} Events',
transform=ax.transAxes,
fontsize=14, fontweight='bold',
ha='right', va='bottom'
)
# Remove axis labels
ax.set_xlabel('')
ax.set_ylabel('')
ax.axis('off')
# Save
plt.tight_layout()
plt.savefig(output_path, dpi=150, bbox_inches='tight', facecolor='white')
plt.close()
def create_combined_yearly_grid(image_dir, output_path, disaster_type, year_start, year_end):
"""
Create a grid of yearly maps combined into one image.
Parameters
----------
image_dir : str
Directory containing yearly PNG files
output_path : str
Output combined PNG file path
disaster_type : str
'Drought' or 'Flood'
year_start : int
Start year
year_end : int
End year
"""
from PIL import Image
import glob
# Get list of image files
pattern = os.path.join(image_dir, f"*_{disaster_type.lower()}.png")
files = sorted(glob.glob(pattern))
if not files:
print(f" No images found for combined grid")
return
# Calculate grid dimensions
n_images = len(files)
n_cols = 6
n_rows = (n_images + n_cols - 1) // n_cols
# Load first image to get dimensions
first_img = Image.open(files[0])
img_width, img_height = first_img.size
# Create combined image
combined_width = n_cols * img_width
combined_height = n_rows * img_height
combined = Image.new('RGB', (combined_width, combined_height), 'white')
# Paste images
for i, filepath in enumerate(files):
img = Image.open(filepath)
row = i // n_cols
col = i % n_cols
x = col * img_width
y = row * img_height
combined.paste(img, (x, y))
# Save
combined.save(output_path, dpi=(200, 200))
print(f" Saved combined grid: {output_path}")
# =============================================================================
# Summary Functions
# =============================================================================
def print_summary(df, disaster_type):
"""
Print summary statistics for disaster data.
Parameters
----------
df : pd.DataFrame
Disaster data
disaster_type : str
Type of disaster
"""
print(f"\n{disaster_type} Summary:")
print(f" Total events: {len(df)}")
print(f" Year range: {df['Start Year'].min()} - {df['Start Year'].max()}")
print(f" Countries affected:")
for country in sorted(df['Country'].unique()):
count = len(df[df['Country'] == country])
print(f" {country}: {count} events")
def save_processed_data(df, output_path):
"""
Save processed data with admin codes to Excel.
Parameters
----------
df : pd.DataFrame
Processed data
output_path : str
Output Excel file path
"""
# Convert admin1_list to string for Excel
df_save = df.copy()
df_save['admin1_list'] = df_save['admin1_list'].apply(
lambda x: ','.join(x) if isinstance(x, list) else ''
)
df_save.to_excel(output_path, index=False)
print(f" Saved: {output_path}")
# =============================================================================
# Main Execution
# =============================================================================
def main():
"""Main execution function."""
print("=" * 60)
print("East Africa Impact Events - Data Processing")
print("=" * 60)
# Create output directories
os.makedirs(os.path.join(OUTPUT_DIR, 'drought'), exist_ok=True)
os.makedirs(os.path.join(OUTPUT_DIR, 'flood'), exist_ok=True)
# Load data
print("\n[1/5] Loading data...")
df_emdat = load_emdat_data(EMDAT_FILE)
gdf_icpac = load_icpac_boundaries(ICPAC_GEOJSON)
# Filter for East Africa
print("\n[2/5] Filtering for East Africa...")
df_ea = filter_ea_data(df_emdat)
df_ea = filter_by_year_range(df_ea, YEAR_START, YEAR_END)
# Process Drought events
print("\n[3/5] Processing Drought events...")
df_drought = filter_by_disaster_type(df_ea, 'Drought')
df_drought = assign_admin1_from_country(df_drought, gdf_icpac)
print_summary(df_drought, 'Drought')
# Process Flood events
print("\n[4/5] Processing Flood events...")
df_flood = filter_by_disaster_type(df_ea, 'Flood')
df_flood = assign_admin1_from_country(df_flood, gdf_icpac)
print_summary(df_flood, 'Flood')
# Generate visualizations
print("\n[5/5] Generating visualizations...")
# Drought frequency map
print("\n Creating Drought frequency map...")
gdf_drought_freq = count_events_by_admin1(df_drought, gdf_icpac)
create_frequency_map(
gdf_drought_freq,
f'Drought Event Frequency in East Africa ({YEAR_START}-{YEAR_END})',
os.path.join(OUTPUT_DIR, f'drought_frequency_{YEAR_START}_{YEAR_END}.png'),
disaster_type='Drought'
)
# Flood frequency map
print("\n Creating Flood frequency map...")
gdf_flood_freq = count_events_by_admin1(df_flood, gdf_icpac)
create_frequency_map(
gdf_flood_freq,
f'Flood Event Frequency in East Africa ({YEAR_START}-{YEAR_END})',
os.path.join(OUTPUT_DIR, f'flood_frequency_{YEAR_START}_{YEAR_END}.png'),
disaster_type='Flood'
)
# Yearly drought maps
print("\n Creating yearly Drought maps...")
for year in range(YEAR_START, YEAR_END + 1):
affected = get_affected_admin1_by_year(df_drought, year)
output_path = os.path.join(OUTPUT_DIR, 'drought', f'{year}_drought.png')
create_yearly_extent_map(gdf_icpac, affected, year, output_path, 'Drought')
print(f" Generated {YEAR_END - YEAR_START + 1} yearly drought maps")
# Yearly flood maps
print("\n Creating yearly Flood maps...")
for year in range(YEAR_START, YEAR_END + 1):
affected = get_affected_admin1_by_year(df_flood, year)
output_path = os.path.join(OUTPUT_DIR, 'flood', f'{year}_flood.png')
create_yearly_extent_map(gdf_icpac, affected, year, output_path, 'Flood')
print(f" Generated {YEAR_END - YEAR_START + 1} yearly flood maps")
# Save processed data
print("\n Saving processed data...")
save_processed_data(
df_drought,
os.path.join(OUTPUT_DIR, 'em_dat_ea_drought_with_admin1.xlsx')
)
save_processed_data(
df_flood,
os.path.join(OUTPUT_DIR, 'em_dat_ea_flood_with_admin1.xlsx')
)
print("\n" + "=" * 60)
print("Processing complete!")
print(f"Output files saved to: {OUTPUT_DIR}/")
print("=" * 60)
if __name__ == "__main__":
main()