-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-map-htmls.py
More file actions
66 lines (45 loc) · 2.05 KB
/
update-map-htmls.py
File metadata and controls
66 lines (45 loc) · 2.05 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
#! /usr/bin python
#------------------------------------------------------------------------------
# PROGRAM: update-map-htmls.py
#------------------------------------------------------------------------------
# Version 0.1
# 28 November, 2023
# Michael Taylor
# michael DOT a DOT taylor AT uea DOT ac DOT uk
#------------------------------------------------------------------------------
import csv
import re
import os
import glob
#------------------------------------------------------------------------------
# METHODS
#------------------------------------------------------------------------------
def replace_string(csv_file, html_file):
# READ: look up table
with open(csv_file, 'r') as csvfile:
csvreader = csv.DictReader(csvfile)
data = {row['id']: {'abbrev': row['abbrev'], 'name': row['name']} for row in csvreader}
# EXTRACT: timescale variable from HTML filename
timescale = html_file.split('-')[-1].split('.')[0]
# READ: HTML file
with open(html_file, 'r') as f:
html_content = f.read()
# REPLACE: string in HTML for each region to point to associated timescale HTML in region directory
for id, info in data.items():
#id = str(id).zfill(2)
#replacement_string = f"ar6-land/{info['abbrev']}/{timescale}.html"
#html_content = html_content.replace(f"figures/BA_Total-ipcc-ar6-land-region-timeseries-sum-{timescale}-region-{id}.png", replacement_string)
html_content = html_content.replace(f'"Region {id}"', f"{info['abbrev']}")
# WRITE: modified content back to the HTML file
with open(html_file, 'w') as f:
f.write(html_content)
#------------------------------------------------------------------------------
# RUN
#------------------------------------------------------------------------------
csv_file = 'ar6.land.csv'
html_filelist = glob.glob('*.html')
for html_file in html_filelist:
print(html_file)
replace_string(csv_file, html_file)
#------------------------------------------------------------------------------
print('** END')