forked from WCRP-CORDEX/simulation-status
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuns.py
More file actions
443 lines (396 loc) · 15 KB
/
Copy pathfuns.py
File metadata and controls
443 lines (396 loc) · 15 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
import datetime
import pandas as pd
import requests
from functools import reduce
from operator import and_, or_
span_style = '''
span.planned {color: #F54d4d; font-weight: bold}
span.running {color: #009900; font-weight: bold}
span.completed {color: #17202a; font-weight: bold}
span.published {color: #3399FF; font-weight: bold}
span.unmigrated {color: #ca6f1e; font-weight: bold}
span.reginst {color: black; font-weight: bold}
span.unreginst {color: grey; font-style: italic; font-weight: bold}
span.unregistered {color: grey; font-style: italic; font-weight: bold}
span.ARCM {color: black; font-weight: bold}
span.AORCM {color: #2980b9; font-weight: bold}
span.AGCM {color: black; font-weight: bold}
span.AOGCM {color: #2980b9; font-weight: bold}
span.ESD {color: #ca6f1e; font-weight: bold}
'''
html_style = '''
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Montserrat', sans-serif;
padding-top: 15px;
padding-left: 15px;
padding-right: 15px;
padding-bottom: 600px;
}
tr:hover {background-color:#f5f5f5;}
th, td {text-align: center; padding: 3px;}
table {border-collapse: collapse;}''' + span_style + '''
a {color: DodgerBlue}
a:link { text-decoration: none; }
a:visited { text-decoration: none; }
a:hover { text-decoration: underline; }
a:active { text-decoration: underline;}
ul.twocol { columns: 2; -webkit-columns: 2; -moz-columns: 2; }
.logo {
text-align: center;
margin-bottom: 20px;
}
</style>
'''
html_legend = '''
<p style="font-size: smaller;"> Colour legend for status:
(
<span class="planned">planned</span>
<span class="running">running</span>
<span class="completed">completed</span>
<span class="published">published</span>
)
//
source_type:
(
<span class="unregistered">unregistered</span>
<span class="ARCM">ARCM</span>
<span class="AORCM">AORCM</span>
<span class="ESD">ESD</span>
)
</p>
'''
table_props = [('width', '100px')]
def html_header(title = 'CORDEX-CMIP6 downscaling plans', mip_era='CMIP6'):
return(f'''<!DOCTYPE html>
<html><head>
{html_style}
</head><body>
<div class="logo">
<img src="https://cordex.org/wp-content/uploads/2025/02/CORDEX_RGB_logo_baseline_positive-300x133.png"
alt="CORDEX Logo" >
<h1 id="top">{title}</h1>
</div>
<div style="display:table;width:100%;">
<div style="display:table-row;">
<div style="display:table-cell;width:50%;">
<a href="https://wcrp-cordex.github.io/simulation-status">Back to main</a>, see
<a href="./{mip_era}_downscaling_plans.html">full list</a>, or see tables by
<a href="./CORDEX_{mip_era}_status.html">domain</a>,
<a href="./CORDEX_{mip_era}_status_by_scenario.html">scenario</a> or
<a href="./CORDEX_{mip_era}_status_by_experiment.html">experiment</a>
</div>
<div style="display:table-cell;text-align:right;width:50%;">
(Version: {datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M")} UTC)
</div>
</div>
</div>
<p style="text-align: justify;">
Simulation status according to CORDEX-{mip_era} downscaling plans reported by the groups and collected in <a href="https://github.com/WCRP-CORDEX/simulation-status/blob/main/{mip_era}_downscaling_plans.csv">{mip_era}_downscaling_plans.csv</a>.
To contribute/update simulations open an issue or pull request at <a href="https://github.com/WCRP-CORDEX/simulation-status">https://github.com/WCRP-CORDEX/simulation-status</a>.
'''
)
def html_footer():
return('</body></html>')
def addtag(word, field):
rval = word
if (field == 'comments') and word.startswith('#'):
rval = f'<span class="tag">{word[1:]}</span>'
elif (field == 'comments') and word.startswith('http'):
rval = f'<a href="{word}">{word}</a>'
elif (field == 'status') and word in ['selected', 'planned', 'running', 'completed', 'unmigrated', 'published']:
rval = f'<span class="{word}">{word}</span>'
return(rval)
def taggify(text, field):
rval = text
if field in ['status', 'comments']:
rval = ' '.join([addtag(x, field) for x in text.split(' ')])
elif isinstance(rval, bool):
rval = '✅' if rval else '❌'
return(rval)
def csv2datatable(csvfile, htmlout, title='', intro='', rename_fields = {}, column_as_link="", column_as_link_source=""):
plans = pd.read_csv(csvfile, na_filter=False)
if column_as_link:
if not column_as_link_source:
column_as_link_source = column_as_link
plans[column_as_link] = (
'<a href="' + plans[column_as_link_source] + '">' + plans[column_as_link] + "</a>"
)
if column_as_link != column_as_link_source:
plans.drop(columns=column_as_link_source, inplace=True)
field_names = dict(zip(plans.columns,plans.columns))
field_names.update(rename_fields)
fp = open(htmlout,'w')
fp.write('''<!DOCTYPE html>
<html lang="en">
<head>
<meta name="author" content="J. Fernandez" />
<meta name="keywords" content="HTML, CSS, JavaScript" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.js"></script>
''')
if title:
fp.write(f'<title>{title}</title>')
fp.write(f'''
<style>
body {{
font-family: "Montserrat", sans-serif;
padding-top: 15px;
padding-left: 15px;
padding-right: 15px;
padding-bottom: 600px;
}}
span.tag {{
background-color: #c5def5;
padding: 0 10px;
font-size: 12px;
font-weight: 500;
line-height: 22px !important;
border: 1px solid transparent;
border-radius: 2em;
}}
a {{color: DodgerBlue}}
a:link {{ text-decoration: none; }}
a:visited {{ text-decoration: none; }}
a:hover {{ text-decoration: underline; }}
a:active {{ text-decoration: underline;}}
{span_style}
.logo {{
text-align: center;
margin-bottom: 20px;
}}
</style>
</head>
<body>
''')
if title:
fp.write(f'''
<div class="logo">
<img src="https://cordex.org/wp-content/uploads/2025/02/CORDEX_RGB_logo_baseline_positive-300x133.png"
alt="CORDEX Logo" >
<h1 id="top">{title}</h1>
</div>
''')
if intro:
fp.write(f'{intro}')
fp.write('''
<table id="table_id" class="display">
<thead>
<tr>
''')
[fp.write(f' <th>{field_names[x]}</th>\n') for x in plans]
fp.write(f'''
</tr>
</thead>
<tbody>
''')
for idx, plan in plans.iterrows():
fp.write(f' <tr>\n')
for field,item in zip(plans.columns, plan):
fp.write(f' <td>{taggify(item, field)}</td>\n')
fp.write(f' </tr>\n')
fp.write('''
</tbody>
</table>
<script>
// URL parameter helpers - using older browser-compatible methods
function getParam(name) {
var urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name) || '';
}
function setParam(name, value) {
var url = new URL(window.location);
if (value && value !== '') {
url.searchParams.set(name, value);
} else {
url.searchParams.delete(name);
}
window.history.replaceState({}, '', url);
}
$(document).ready(function() {
// Get initial values from URL
var initialSearch = getParam('search');
var initialLength = parseInt(getParam('length')) || 20;
var initialOrder = getParam('order');
var initialOrderArray = [[0, 'asc']]; // default
if (initialOrder) {
try {
initialOrderArray = JSON.parse(initialOrder);
} catch (e) {
console.log('Could not parse order parameter');
}
}
// Initialize DataTable with URL parameters
var table = $('#table_id').DataTable({
pageLength: initialLength,
lengthMenu: [20, 50, 100, 200, 500],
order: initialOrderArray,
searching: true
});
// Set initial search if provided
if (initialSearch) {
table.search(initialSearch).draw();
}
// Update URL when search changes
table.on('search.dt', function() {
var searchValue = table.search();
setParam('search', searchValue);
});
// Update URL when page length changes
table.on('length.dt', function(e, settings, len) {
setParam('length', len == 20 ? '' : len);
});
// Update URL when column order changes
table.on('order.dt', function() {
var currentOrder = table.order();
setParam('order', JSON.stringify(currentOrder));
});
console.log('DataTable initialized with URL management');
});
</script>
</body>
</html>''')
fp.close()
def add_registration_info(plans_df, cmip_era='CMIP6'):
"""
Add registration information from CORDEX CV repositories.
Parameters:
-----------
plans_df : pandas.DataFrame
DataFrame with simulation plans containing 'source_id' and 'institution_id' columns
cmip_era : str
Either 'CMIP6' or 'CMIP7' to determine which CV repo to use
Returns:
--------
pandas.DataFrame
Modified DataFrame with added columns: 'registered', 'source_type', 'inst_registered'
"""
base_url = f"https://raw.githubusercontent.com/WCRP-CORDEX/cordex-{cmip_era.lower()}-cv/refs/heads/main/CORDEX-{cmip_era}"
# Fetch source_id registration info
url_source_id = f"{base_url}_source_id.json"
try:
response = requests.get(url_source_id)
response.raise_for_status()
source_ids = response.json()["source_id"]
source_type_map = {key: value["source_type"] for key, value in source_ids.items()}
plans_df["registered"] = plans_df["source_id"].apply(lambda x: x in source_type_map)
plans_df["source_type"] = plans_df["source_id"].apply(
lambda x: source_type_map.get(x, "") if x in source_type_map else "unregistered"
)
except (requests.exceptions.HTTPError, requests.exceptions.RequestException) as e:
# CV repo doesn't exist yet or is unavailable - mark all as unregistered
print(f"Warning: Could not fetch source_id CV from {url_source_id}: {e}")
plans_df["registered"] = False
plans_df["source_type"] = "unregistered"
# Fetch institution_id registration info
url_institution_id = f"{base_url}_institution_id.json"
try:
response = requests.get(url_institution_id)
response.raise_for_status()
institution_ids = response.json()["institution_id"]
plans_df["inst_registered"] = plans_df["institution_id"].apply(
lambda x: "reginst" if x in institution_ids else "unreginst"
)
except (requests.exceptions.HTTPError, requests.exceptions.RequestException) as e:
# CV repo doesn't exist yet or is unavailable - mark all as unregistered
print(f"Warning: Could not fetch institution_id CV from {url_institution_id}: {e}")
plans_df["inst_registered"] = "unreginst"
return plans_df
def generate_domain_table(dom_plans, collapse_institutions=True):
"""
Generate a pivot table matrix for a specific domain with HTML styling.
Parameters:
-----------
dom_plans : pandas.DataFrame
DataFrame containing simulation plans for a specific domain
collapse_institutions : bool
Whether to collapse institutions in the table display
Returns:
--------
pandas.DataFrame
Styled pivot table ready for HTML export
"""
# Add HTML status column
dom_plans = dom_plans.assign(
htmlstatus=pd.Series(
'<span sort="' + dom_plans.driving_experiment_id + '" class="' +
dom_plans.status + '">' + dom_plans.driving_experiment_id + '</span>',
index=dom_plans.index
)
)
# Add HTML-styled source_id and institution_id
dom_plans = dom_plans.assign(
source_id=pd.Series(
'<span sort="' + dom_plans.source_id + '" class="' +
dom_plans.source_type + '">' + dom_plans.source_id + '</span>',
index=dom_plans.index
)
)
dom_plans = dom_plans.assign(
institution_id=pd.Series(
'<span sort="' + dom_plans.institution_id + '" class="' +
dom_plans.inst_registered + '">' + dom_plans.institution_id + '</span>',
index=dom_plans.index
)
)
dom_plans = dom_plans.assign(
model_id=pd.Series(
dom_plans.institution_id + '-' + dom_plans.source_id,
index=dom_plans.index
)
)
# Create pivot table
column_id = 'source_id' if collapse_institutions else 'model_id'
dom_plans_matrix = dom_plans.pivot_table(
index=('driving_source_id', 'driving_variant_label'),
columns=column_id,
values='htmlstatus',
aggfunc=lambda x: ' '.join(sorted(x.dropna()))
)
# Bring ERA5 to the top
dom_plans_matrix = pd.concat([
dom_plans_matrix.query("driving_source_id == 'ERA5'"),
dom_plans_matrix.drop(('ERA5', ''), axis=0, errors='ignore')
], axis=0)
# Add institution row if collapsing
if collapse_institutions:
inst = dom_plans.drop_duplicates(subset=['institution_id', 'source_id']).pivot_table(
index=('driving_source_id', 'driving_variant_label'),
columns='source_id',
values='institution_id',
aggfunc=lambda x: ', '.join(x.dropna())
).agg(lambda x: ', '.join(x.dropna()))
inst.name = ('', 'Institutes')
dom_plans_matrix = pd.concat([dom_plans_matrix, inst.to_frame().T])
dom_plans_matrix = dom_plans_matrix.T.set_index([('', 'Institutes'), dom_plans_matrix.columns]).T
dom_plans_matrix.columns.names = ['Institution(s)', 'RCM']
return dom_plans_matrix
def assign_node_to_sim(simulations, nodes):
"""Using the ESGF nodes list and adds the node_name, node_url and node_index columns.
node_index is the index into the list of nodes.
"""
sims = simulations.copy()
sims['node_url'] = None
sims['node_name'] = None
sims['node_index'] = -1
for i, node in enumerate(nodes):
if not node.get('simulations'):
continue
match = []
for j, terms in enumerate(node['simulations']):
this_match = reduce(and_, [sims[k].isin([v] if isinstance(v, str) else v) for k, v in terms.items()])
if not this_match.any():
raise ValueError(f'Search term {j} of node {i} does not match any simulation.')
match.append(this_match)
hosted = sims[reduce(or_, match)]
if (dups := set(hosted.index).intersection(set(sims[sims.node_index != -1].index))):
raise ValueError(f"Node {i} matches {len(dups)} simulations already matched by previous nodes : {sims[list(dups)]}")
sims.loc[hosted.index, 'node_url'] = node['url']
sims.loc[hosted.index, 'node_name'] = node['name']
sims.loc[hosted.index, 'node_index'] = i
return sims