-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkhtml_continents.py
More file actions
78 lines (58 loc) · 2.76 KB
/
mkhtml_continents.py
File metadata and controls
78 lines (58 loc) · 2.76 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
import csv
# Which version to make?
#iver = 0 # 0=yearly
#timescalestr = 'yearly'
#timescalestr = 'yearly_jj'
#timescalestr = 'monthly'
#timescalestr = 'seasonal_djf'
#timescalestr = 'seasonal_mam'
#timescalestr = 'seasonal_jja'
timescalestr = 'seasonal_son'
#fnhtml = 'table-for-fire-countries-yearly.html'
fnhtml = 'table-for-fire-countries-' + timescalestr + '.html'
# Read list of countries with continent indicators
fnlist = 'continents_table.csv'
print('Reading continents and countries from:')
print(fnlist)
with open(fnlist, 'r') as csvfile:
reader = csv.reader(csvfile)
cnthead = next(reader) # Assuming the first row contains headers
rawdat = {header: [] for header in cnthead}
for row in reader:
for i, header in enumerate(cnthead):
rawdat[header].append(row[i])
# Extract continent names (skip first 3 field names)
cntnm = cnthead[3:]
ncnt = len(cntnm)
# Create html for a list of countries, grouped by continent
# This is not a complete html file, it is meant to be pasted into an existing
# html file that contains the rest of the webpage and the styles etc.
print('Writing to: ' + fnhtml)
with open(fnhtml, 'w') as f:
# Process each continent in turn
cntcount = [0] * ncnt # count how many countries are associated with each continent
for icnt in range(ncnt):
# Find which countries are assigned to this continent
flaglist = [int(x) if x.strip() != '' else 0 for x in rawdat[cntnm[icnt]]]
gotlist = [i for i, flag in enumerate(flaglist) if flag > 0]
print()
print(cntnm[icnt], len(gotlist))
# Extract fields for this subset of countries
if gotlist:
cysubsetno = [rawdat[cnthead[0]][i] for i in gotlist] # country number
cysubsetab = [rawdat[cnthead[1]][i] for i in gotlist] # country abbreviation
cysubsetnm = [rawdat[cnthead[2]][i] for i in gotlist] # country name
# Sort into alphabetical order by abbreviation
isort = sorted(range(len(cysubsetab)), key=lambda k: cysubsetab[k])
print([cysubsetab[i] for i in isort])
# Generate HTML entry for this continent, followed by 1-line per country
f.write(f'<h4>{cntnm[icnt]}</h4>\n\n')
f.write('<ol>\n')
for i in range(len(gotlist)):
j = isort[i]
#urlname = f'{cysubsetab[j]}/yearly.html'
#urlname = f'countries/{cysubsetab[j]}/yearly.html'
urlname = f'countries/{cysubsetab[j]}/{timescalestr}.html'
f.write(f' <li><a href="{urlname}">{cysubsetab[j]} {cysubsetnm[j]}</a></li>\n')
f.write('</ol>\n\n')
# end of script