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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
import pandas as pd
import matplotlib.pyplot as plt
from flask import Blueprint, render_template_string, request, jsonify, Response
import os
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import numpy as np
import json
from scipy.optimize import curve_fit
from scipy.stats import linregress

# Create Blueprint
her_plot_bp = Blueprint('her_plot', __name__, url_prefix='/her')

# Atomic weights for conversion between atomic and weight fractions
ATOMIC_WEIGHTS = {
    'Ag': 107.8682, 'Au': 196.966569, 'Cd': 112.411, 'Cu': 63.546, 'Ga': 69.723,
    'Hg': 200.59, 'In': 114.818, 'Mn': 54.938044, 'Mo': 95.96, 'Nb': 92.90637,
    'Ni': 58.6934, 'Pd': 106.42, 'Pt': 195.084, 'Rh': 102.90550, 'Sn': 118.710,
    'Tl': 204.38, 'W': 183.84, 'Zn': 65.38
}

# Voltage conversion functions
def lin_fxn(x, a, b):
    return a*x+b

def fit_lin(X, Y):
    params, covariance = curve_fit(lin_fxn, X, Y)
    a_fit, b_fit = params
    return (a_fit, b_fit)

def est_x(x, X, Y):
    fit = fit_lin(X, Y)
    x = fit[0]*x+fit[1]
    return x

def load_calibration_data_for_voltage_conversion(custom_params=None):
    """Load calibration data for voltage conversion from full cell to half cell."""
    # Default experiment conditions: Neutral CO2RR in 4cm2 cell, Sputtered Copper Catalyst, 0.1M Bicarbonate - ref electrode (3M kcl) 230mV vs SHE
    default_params = {
        'ref_pot': 0.23,  # V Ag/AgCl electrode
        'cathode_pH': 10,
        'anode_pH': 3,
        'geo_area': 4,  # cm2
        'membrane_loss': 0.1,  # V
        # Note: anode_measured_potential_vs_ref is now interpolated from calibration data
    }
    
    # Use custom parameters if provided, otherwise use defaults
    if custom_params:
        params = {**default_params, **custom_params}
    else:
        params = default_params
    
    ref_pot = params['ref_pot']
    cathode_pH = params['cathode_pH']
    anode_pH = params['anode_pH']
    Nern_pH_loss = (cathode_pH-anode_pH)*0.059 
    geo_area = params['geo_area']
    membrane_loss = params['membrane_loss']
    
    # Measurements from calibration work
    j = np.array([50,100,200])
    cathode_pot = np.array([-1.62,-2.0,-2.3])
    cathode_R = np.array([0.48,0.34,0.3])
    anode_pot = np.array([1.3,1.35,1.4])
    anode_R = np.array([0,0,0]) #almost negligible
    fullcell_pot = np.array([3,3.4,3.7])
    fullcell_R = np.array([0.47,0.35,0.3])
    
    n = len(cathode_pot)
    cathode_pot_corr = np.zeros(n)
    anode_pot_corr = np.zeros(n)
    cathode_overpot = np.zeros(n)
    anode_overpot = np.zeros(n)
    fullcell_pot_corr = np.zeros(n)

    for i in range(0, n):
        cathode_pot_corr[i] = correct_potential(cathode_pot[i], cathode_R[i], cathode_pH, j[i], geo_area, ref_pot)
        anode_pot_corr[i] = correct_potential(anode_pot[i], anode_R[i], anode_pH, j[i], geo_area, ref_pot)
        cathode_overpot[i] = get_overpotential(cathode_pot_corr[i],0.08)
        anode_overpot[i] = get_overpotential(anode_pot_corr[i], 1.23)

        fullcell_pot_corr[i] = fullcell_pot[i]-fullcell_R[i]*j[i]/1000*geo_area

    conditions_dict = {
        'ref pot': ref_pot,
        'cathode pH': cathode_pH,
        'anode pH': anode_pH,
        'Nern pH loss': Nern_pH_loss,
        'geo area': geo_area,
        'membrane loss': membrane_loss,
    }
    measurements_dict = {
        'j': j, 'cathode pot': cathode_pot, 'cathode R': cathode_R, 'anode pot': anode_pot, 'anode R': anode_R, 
        'fullcell pot': fullcell_pot, 'fullcell R': fullcell_R,
    }

    data_dict = {
        'cathode pot corr': cathode_pot_corr, 'anode pot corr': anode_pot_corr,
        'cathode overpot': cathode_overpot, 'anode overpot': anode_overpot, 'fullcell pot corr': fullcell_pot_corr    
    }
    return {'measurements': measurements_dict, 'conditions': conditions_dict, 'extracted params': data_dict}

def she2rhe(ushe, pH, ref_pot):
    ushe = ushe+ref_pot+(0.059*pH)
    return ushe

def rhe2she(urhe, pH, ref_pot):
    urhe = urhe - (0.059 * pH)
    return urhe

def correct_potential(pot, R, pH, j, area, ref_pot):
    if pot<0:
        corrected_pot = she2rhe(pot+j/1000*area*R,pH, ref_pot)
    else:
        corrected_pot = she2rhe(pot-j/1000*area*R,pH, ref_pot)
    return corrected_pot

def interpolate_cathode_R(current_density):
    """
    Interpolate cathode resistance R from log(j) vs R calibration data.
    
    Calibration data:
    j = [50, 100, 200] mA/cm²
    R = [0.48, 0.34, 0.3] ohm
    
    Fits log(j) vs R and interpolates R for given current density.
    """
    # Calibration data
    j_array = np.array([50, 100, 200])  # mA/cm²
    R_array = np.array([0.48, 0.34, 0.3])  # ohm
    
    # Convert to log scale for j
    log_j = np.log10(j_array)
    
    # Fit linear relationship: R = a * log10(j) + b
    fit_params = np.polyfit(log_j, R_array, 1)
    a, b = fit_params
    
    # Interpolate R for given current density (convert mA/cm² to mA/cm², already in correct units)
    if current_density <= 0:
        # Use minimum R if current density is too small
        return R_array[-1]  # Use the smallest R (at highest j)
    
    log_j_input = np.log10(current_density)
    R_interpolated = a * log_j_input + b
    
    # Clamp to reasonable bounds (between min and max R values)
    R_interpolated = np.clip(R_interpolated, R_array.min(), R_array.max())
    
    return R_interpolated

def interpolate_anode_potential_vs_ref(current_density):
    """
    Interpolate anode measured potential vs reference from log(j) vs anode_pot calibration data.
    
    Calibration data:
    j = [50, 100, 200] mA/cm²
    anode_pot = [1.3, 1.35, 1.4] V
    
    Fits log(j) vs anode_pot and interpolates anode_pot for given current density.
    """
    # Calibration data
    j_array = np.array([50, 100, 200])  # mA/cm²
    anode_pot_array = np.array([1.3, 1.35, 1.4])  # V
    
    # Convert to log scale for j
    log_j = np.log10(j_array)
    
    # Fit linear relationship: anode_pot = a * log10(j) + b
    fit_params = np.polyfit(log_j, anode_pot_array, 1)
    a, b = fit_params
    
    # Interpolate anode_pot for given current density
    if current_density <= 0:
        # Use minimum anode_pot if current density is too small
        return anode_pot_array[0]  # Use the smallest anode_pot (at lowest j)
    
    log_j_input = np.log10(current_density)
    anode_pot_interpolated = a * log_j_input + b
    
    # Clamp to reasonable bounds (between min and max anode_pot values)
    anode_pot_interpolated = np.clip(anode_pot_interpolated, anode_pot_array.min(), anode_pot_array.max())
    
    return anode_pot_interpolated

def cell2rhe(vcell, ref_pot, anode_pH, 
              membrane_loss, Nern_pH_loss, current_density, geo_area,
              custom_anode_potential_vs_ref=None, custom_R_cathode=None):
    """
    Convert full cell voltage to cathode potential vs RHE.
    
    Steps (matching notebook example):
    1. Interpolate anode measured potential vs reference from calibration data (or use custom value)
    2. Convert anode measured potential (vs reference) to RHE:
       V_anode_RHE = anode_measured_potential_vs_ref + ref_pot + 0.059 * anode_pH
    3. Calculate cathode RHE (before IR correction):
       V_cathode_RHE = (V_anode_RHE + membrane_loss + Nern_pH_loss) - full_cell_V
    4. Interpolate cathode resistance from calibration data (or use custom value)
    5. Apply IR correction:
       V_cathode_RHE = V_cathode_RHE - (i/1000 * R * A)
       where i is current density in A/cm², R is interpolated resistance, A is geometric area
    
    Parameters:
    -----------
    custom_anode_potential_vs_ref : float, optional
        Custom anode measured potential vs reference (V). If provided, overrides interpolation.
    custom_R_cathode : float, optional
        Custom cathode resistance (Ω). If provided, overrides interpolation.
    """
    # Step 1: Interpolate anode measured potential vs reference (or use custom value)
    if custom_anode_potential_vs_ref is not None:
        anode_measured_potential_vs_ref = custom_anode_potential_vs_ref
    else:
        anode_measured_potential_vs_ref = interpolate_anode_potential_vs_ref(current_density)
    
    # Step 2: Convert anode measured potential to RHE
    v_anode_rhe = anode_measured_potential_vs_ref + ref_pot + 0.059 * anode_pH
    
    # Step 3: Calculate cathode RHE with membrane and Nernst pH losses (before IR correction)
    v_cathode_rhe = (v_anode_rhe + membrane_loss + Nern_pH_loss) - vcell
    
    # Step 4: Interpolate cathode resistance from calibration data (or use custom value)
    if custom_R_cathode is not None:
        R = custom_R_cathode
    else:
        R = interpolate_cathode_R(current_density)  # current_density in mA/cm², R in ohm
    
    # Step 5: Apply IR correction
    # Convert current density from mA/cm² to A/cm² and apply IR correction
    # i/1000 converts mA/cm² to A/cm²
    IR_drop = (current_density / 1000.0) * R * geo_area
    v_cathode_rhe = v_cathode_rhe - IR_drop
    
    return v_cathode_rhe

def get_overpotential(pot, pot_theory):
    overpot = abs(pot-pot_theory)
    return overpot

def fullcell2halfcell(vcell, current_density, custom_params=None):
    '''
    Main function to convert a voltage value from full cell to half cell vs she or rhe
    
    Parameters:
    -----------
    vcell : float
        Full cell voltage (V)
    current_density : float
        Current density (mA/cm²)
    custom_params : dict, optional
        Custom parameters for voltage conversion
    '''
    cali_dict = load_calibration_data_for_voltage_conversion(custom_params)
    
    # Extract custom values if provided
    custom_anode_pot = custom_params.get('anode_measured_potential_vs_ref') if custom_params else None
    custom_R = custom_params.get('R_cathode') if custom_params else None
    
    urhe = cell2rhe(vcell, 
                    cali_dict['conditions']['ref pot'],
                    cali_dict['conditions']['anode pH'],
                    cali_dict['conditions']['membrane loss'],
                    cali_dict['conditions']['Nern pH loss'],
                    current_density,
                    cali_dict['conditions']['geo area'],
                    custom_anode_potential_vs_ref=custom_anode_pot,
                    custom_R_cathode=custom_R)
    # Use cathode_pH from calibration dict (which includes custom params if provided)
    ushe = rhe2she(urhe, cali_dict['conditions']['cathode pH'], cali_dict['conditions']['ref pot'])
    
    return ushe, urhe

def convert_atomic_to_weight_fraction(df, element_columns):
    """
    Convert atomic fraction to weight fraction for elemental compositions.
    """
    df_converted = df.copy()
    
    for col in element_columns:
        if col in df_converted.columns and col in ATOMIC_WEIGHTS:
            df_converted[col] = df_converted[col] * ATOMIC_WEIGHTS[col]
    
    # Normalize to get weight fractions (0-1 scale)
    for idx, row in df_converted.iterrows():
        total_weight = sum(row[col] for col in element_columns if col in df_converted.columns and col in ATOMIC_WEIGHTS)
        if total_weight > 0:
            for col in element_columns:
                if col in df_converted.columns and col in ATOMIC_WEIGHTS:
                    df_converted.at[idx, col] = row[col] / total_weight
    
    return df_converted

def load_original_data():
    """Load the original data from CSV file or current data from dashboard"""
    try:
        # First try to load current data from dashboard
        current_data_file = "Data/current_data_her.json"
        if os.path.exists(current_data_file):
            with open(current_data_file, 'r') as f:
                saved_data = json.load(f)
            
            if isinstance(saved_data, dict) and 'data' in saved_data and 'columns' in saved_data:
                current_data = saved_data['data']
                column_order = saved_data['columns']
                df = pd.DataFrame(current_data, columns=column_order)
            elif isinstance(saved_data, list):
                df = pd.DataFrame(saved_data)
            else:
                df = pd.DataFrame(saved_data)
            
            # Filter for HER reaction if reaction column exists
            if 'reaction' in df.columns:
                df = df[df['reaction'] == 'HER'].copy()
                df = df.drop('reaction', axis=1)
            
            print(f"DEBUG: Available columns after loading HER data: {list(df.columns)}")
            print(f"DEBUG: Data shape: {df.shape}")
            print(f"DEBUG: Voltage columns present: {[col for col in df.columns if 'voltage' in col.lower()]}")
            
            return df
    except Exception as e:
        print(f"Could not load current data: {e}")
    
    # Fallback to original CSV data
    try:
        df = pd.read_csv("Data/DashboardData.csv")
        if 'reaction' in df.columns:
            df = df[df['reaction'] == 'HER'].copy()
            df = df.drop('reaction', axis=1)
        return df
    except Exception as e:
        print(f"Could not load CSV data: {e}")
        return pd.DataFrame()

def calculate_pca_components(df):
    """Calculate PCA components from elemental composition data."""
    if df.empty or len(df) < 2:
        df['PCA1'] = np.nan
        df['PCA2'] = np.nan
        return df
    
    # Get only elemental composition columns
    voltage_cols_to_exclude = ['voltage_mean', 'voltage_std', 'voltage']
    composition_col = 'xrf composition' if 'xrf composition' in df.columns else 'target composition'
    element_cols = [col for col in df.columns if col not in ['sample id', 'source', 'batch number', 'batch date', 'current density', composition_col, 'target composition', 'xrf composition', 'rep'] + voltage_cols_to_exclude and not col.startswith('fe_') and not col.startswith('partial_current_') and not col.startswith('max_partial_current_') and not col.endswith('std')]
    
    # Filter out non-numeric columns
    numeric_element_cols = []
    for col in element_cols:
        try:
            if pd.to_numeric(df[col], errors='coerce').notna().sum() >= 2:
                numeric_element_cols.append(col)
        except:
            continue
    
    if len(numeric_element_cols) < 2:
        df['PCA1'] = np.nan
        df['PCA2'] = np.nan
        return df
    
    # Prepare data for PCA
    pca_data = df[numeric_element_cols].copy()
    for col in pca_data.columns:
        pca_data[col] = pd.to_numeric(pca_data[col], errors='coerce')
    pca_data = pca_data.fillna(0)
    
    if pca_data.sum().sum() == 0:
        df['PCA1'] = 0
        df['PCA2'] = 0
        return df
    
    try:
        # Standardize and apply PCA
        scaler = StandardScaler()
        pca_data_scaled = scaler.fit_transform(pca_data)
        pca = PCA(n_components=2)
        pca_components = pca.fit_transform(pca_data_scaled)
        
        df['PCA1'] = pca_components[:, 0]
        df['PCA2'] = pca_components[:, 1]
    except Exception as e:
        print(f"PCA calculation failed: {e}")
        df['PCA1'] = np.nan
        df['PCA2'] = np.nan
    
    return df

def format_column_name(column_name):
    """Format column names to be more readable."""
    if column_name in ['voltage_mean', 'voltage']:
        return 'Full Cell Voltage (V)'
    elif column_name == 'voltage_she':
        return 'Est. Half-cell potential vs SHE (V)'
    elif column_name == 'voltage_rhe':
        return 'Est. Half-cell potential vs RHE (V)'
    elif column_name.startswith('fe_'):
        base_name = column_name.replace('fe_', '').replace('_mean', '')
        if base_name == 'h2':
            return 'Faradaic Efficiency H₂'
        elif base_name == 'co':
            return 'Faradaic Efficiency CO'
        elif base_name == 'ch4':
            return 'Faradaic Efficiency CH₄'
        elif base_name == 'c2h4':
            return 'Faradaic Efficiency C₂H₄'
        elif base_name == 'gas_total':
            return 'Faradaic Efficiency Gas Total'
        elif base_name == 'liquid':
            return 'Faradaic Efficiency Liquid'
        else:
            return 'Faradaic Efficiency ' + base_name.upper()
    elif column_name == 'cost_per_gram':
        return 'Cost per kg'
    elif column_name in ['PCA1', 'PCA2']:
        return column_name
    elif column_name in ['Ag', 'Au', 'Cd', 'Cu', 'Ga', 'Hg', 'In', 'Ni', 'Pd', 'Pt', 'Rh', 'Sn', 'Tl', 'Zn']:
        return column_name
    else:
        return column_name

def find_pd_mean_value(df, target_column, source):
    """Find the mean value of a target column for a specific source where Pd composition is 1.0"""
    # Filter data for the specific source and Pd = 1.0
    filtered_data = df[(df['source'] == source) & (df['Pd'] == 1.0)]
    
    if filtered_data.empty:
        return None
    
    # Get the mean value of the target column
    mean_value = filtered_data[target_column].mean()
    return mean_value

def load_xrd_data(sample_id, data_type="raw"):
    """Load XRD data for a specific sample ID from Data/XRD or Data/CustomXRD directory."""
    try:
        # First check for custom XRD data, then fall back to original
        custom_xrd_base = "Data/CustomXRD"
        original_xrd_base = "Data/XRD"
        
        # Construct potential file paths
        if data_type == "raw":
            custom_path = f"{custom_xrd_base}/raw/{sample_id}.xy"
            original_path = f"{original_xrd_base}/raw/{sample_id}.xy"
        elif data_type == "normalized":
            custom_path = f"{custom_xrd_base}/normalized/{sample_id}.csv"
            original_path = f"{original_xrd_base}/normalized/{sample_id}.csv"
        else:
            print(f"Invalid data type: {data_type}")
            return None
        
        # Check custom XRD first, then original
        xrd_file_path = None
        if os.path.exists(custom_path):
            xrd_file_path = custom_path
            print(f"DEBUG: Using custom XRD file: {custom_path}")
        elif os.path.exists(original_path):
            xrd_file_path = original_path
            print(f"DEBUG: Using original XRD file: {original_path}")
        else:
            print(f"XRD file not found in custom or original locations for sample {sample_id} ({data_type})")
            return None
        
        # Read the file
        data = []
        with open(xrd_file_path, 'r') as f:
            lines = f.readlines()
            # Skip the first line (header)
            for line_num, line in enumerate(lines[1:], 2):  # Start from line 2
                line = line.strip()
                if line and not line.startswith('#'):  # Skip empty lines and comments
                    try:
                        # Handle different separators (space, tab, comma)
                        parts = line.replace(',', ' ').split()
                        if len(parts) >= 2:
                            x_val = float(parts[0])
                            y_val = float(parts[1])
                            data.append([x_val, y_val])
                    except ValueError:
                        # Skip lines that can't be parsed as numbers
                        if line_num <= 10:  # Only log first few errors to avoid spam
                            print(f"Warning: Could not parse line {line_num} in {xrd_file_path}: {line}")
                        continue
        
        if not data:
            print(f"No valid data found in XRD file: {xrd_file_path}")
            return None
        
        print(f"Loaded XRD data for sample {sample_id} ({data_type}): {len(data)} data points")
        return data
        
    except Exception as e:
        print(f"Error loading XRD data: {e}")
        return None

@her_plot_bp.route('/')
def her_plot_main():
    """Main HER plot page"""
    # Load and process data
    current_df = load_original_data()
    
    if current_df.empty:
        return "<h2>Error: No HER data available</h2><p>Please ensure HER data is available in the main dashboard.</p>"
    
    # Calculate PCA components
    df_with_pca = calculate_pca_components(current_df)
    
    # Identify element columns
    voltage_cols_to_exclude = ['voltage_mean', 'voltage_std', 'voltage']
    composition_col = 'xrf composition' if 'xrf composition' in df_with_pca.columns else 'target composition'
    element_cols = [col for col in df_with_pca.columns if col not in ['sample id', 'source', 'batch number', 'batch date', 'current density', composition_col, 'target composition', 'xrf composition', 'PCA1', 'PCA2', 'rep'] + voltage_cols_to_exclude and not col.startswith('fe_') and not col.startswith('partial_current_') and not col.startswith('max_partial_current_') and not col.endswith('std')]
    
    # Add PCA1 as first option if available
    if 'PCA1' in df_with_pca.columns:
        element_cols.insert(0, 'PCA1')
    if 'Cu' in df_with_pca.columns and 'Cu' not in element_cols:
        element_cols.insert(0, 'Cu')
    
    # Determine voltage column
    if 'voltage_mean' in df_with_pca.columns:
        y_axis_column = 'voltage_mean'
    elif 'voltage' in df_with_pca.columns:
        y_axis_column = 'voltage'
    else:
        return "<h2>Error: No voltage data available</h2><p>Required voltage column not found.</p>"
    
    # Generate element options for dropdown
    element_options = ''.join([f'<option value="{col}">{format_column_name(col)}</option>' for col in element_cols])
    
    # Create the HTML template exactly matching the original
    html_template = f'''
    <!DOCTYPE html>
    <html>
    <head>
        <title>OCx25 Dataset: HER</title>
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
        <style>
            /* CACHE BUSTER: 2025-01-16 16:52 */
            body {{
                font-family: 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
                margin: 0;
                padding: 0;
                background: #fafafa;
                min-height: 100vh;
                color: #202124;
                overflow-x: hidden;
                line-height: 1.6;
            }}
            
            .back-link {{
                position: fixed;
                top: 20px;
                left: 20px;
                z-index: 1000;
                background: #4285f4;
                color: white;
                padding: 12px 20px;
                border-radius: 8px;
                text-decoration: none;
                font-weight: 500;
                font-size: 14px;
                transition: all 0.2s ease;
                box-shadow: 0 2px 8px rgba(66, 133, 244, 0.3);
            }}
            
            .back-link:hover {{
                background: #3367d6;
                transform: translateY(-1px);
                box-shadow: 0 4px 12px rgba(66, 133, 244, 0.4);
            }}
            
            .container {{
                max-width: 100%;
                margin: 0 auto;
                background: #ffffff;
                border-radius: 12px;
                box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
                overflow: hidden;
                margin: 12px;
                border: 1px solid #e8eaed;
            }}
            
            h1 {{
                color: #202124;
                text-align: center;
                font-size: 2.4em;
                font-weight: 400;
                letter-spacing: -0.5px;
                margin: 0;
                padding: 32px 24px 16px 24px;
                background: #ffffff;
                border-bottom: 1px solid #e8eaed;
            }}
            
            h3 {{
                color: #5f6368;
                text-align: center;
                margin: 0;
                padding: 0 24px 20px 24px;
                background: #ffffff;
                font-size: 1.1em;
                font-weight: 400;
                letter-spacing: 0.2px;
            }}
            
            .controls {{
                background: #f8f9fa;
                padding: 24px;
                border-bottom: 1px solid #e8eaed;
                display: flex;
                gap: 24px;
                align-items: center;
                flex-wrap: wrap;
                justify-content: center;
            }}
            
            .control-group {{
                display: flex;
                flex-direction: column;
                gap: 8px;
                align-items: center;
            }}
            
            label {{
                font-weight: 500;
                color: #5f6368;
                font-size: 0.875em;
                text-transform: none;
                letter-spacing: 0.2px;
            }}
            
            select {{
                padding: 12px 16px;
                border: 1px solid #dadce0;
                border-radius: 8px;
                background: #ffffff;
                font-size: 14px;
                font-weight: 400;
                color: #202124;
                cursor: pointer;
                transition: all 0.2s ease;
                min-width: 160px;
                box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
            }}
            
            select:hover {{
                border-color: #4285f4;
                box-shadow: 0 2px 8px rgba(66, 133, 244, 0.15);
            }}
            
            select:focus {{
                outline: none;
                border-color: #4285f4;
                box-shadow: 0 0 0 2px rgba(66, 133, 244, 0.2);
            }}
            
            .checkbox-container {{
                display: flex;
                align-items: center;
                gap: 12px;
                background: #ffffff;
                padding: 16px 20px;
                border-radius: 8px;
                border: 1px solid #e8eaed;
                box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
            }}
            
            input[type="checkbox"] {{
                width: 18px;
                height: 18px;
                accent-color: #4285f4;
                cursor: pointer;
            }}
            
            .checkbox-container label {{
                margin: 0;
                color: #5f6368;
                font-weight: 400;
            }}
            
            .plots-container {{
                display: flex;
                flex-direction: column;
                gap: 20px;
                padding: 32px;
                background: #fafafa;
                min-height: 800px;
            }}
            
            .plots-row {{
                display: flex;
                gap: 20px;
                min-height: 600px;
            }}
            
            .plot-section {{
                flex: 1;
                background: #ffffff;
                border-radius: 8px;
                box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
                overflow: hidden;
                transition: all 0.2s ease;
                border: 1px solid #e8eaed;
            }}
            
            .plot-section:hover {{
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
            }}
            
            .plot-section .header-row {{
                background: #f8f9fa;
                padding: 20px 20px 16px 20px;
                border-bottom: 1px solid #e8eaed;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }}
            
            .plot-section .header-row h3 {{
                background: none;
                padding: 0;
                margin: 0;
                flex: 1;
                border-bottom: none;
                color: #202124;
            }}
            
            .header-controls {{
                display: flex;
                align-items: center;
                gap: 20px;
            }}
            
            .toggle-group {{
                display: flex;
                align-items: center;
                gap: 10px;
            }}
            
            .toggle-label {{
                font-size: 14px;
                color: #5f6368;
                font-weight: 500;
            }}
            
            .toggle-switch {{
                display: flex;
                background: #e8eaed;
                border-radius: 20px;
                padding: 2px;
                position: relative;
            }}
            
            .toggle-switch input[type="radio"] {{
                display: none;
            }}
            
            .toggle-switch label {{
                padding: 8px 16px;
                font-size: 13px;
                font-weight: 500;
                color: #5f6368;
                cursor: pointer;
                border-radius: 18px;
                transition: all 0.2s ease;
                position: relative;
                z-index: 1;
            }}
            
            .toggle-switch input[type="radio"]:checked + label {{
                background: #4285f4;
                color: white;
                box-shadow: 0 2px 4px rgba(66, 133, 244, 0.3);
            }}
            
            .reset-btn {{
                background: #ffffff;
                color: #ea4335;
                border: 1px solid #ea4335;
                padding: 12px 16px;
                border-radius: 8px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s ease;
                box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
                text-transform: none;
                letter-spacing: 0.2px;
                min-width: 120px;
            }}
            
            .reset-btn:hover {{
                background: #ea4335;
                color: #ffffff;
                box-shadow: 0 2px 8px rgba(234, 67, 53, 0.15);
                transform: translateY(-1px);
            }}
            
            .reset-btn:active {{
                transform: translateY(0);
            }}
            
            .export-btn {{
                background: #ffffff;
                color: #34a853;
                border: 1px solid #34a853;
                padding: 12px 16px;
                border-radius: 8px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s ease;
                box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
                text-transform: none;
                letter-spacing: 0.2px;
                min-width: 160px;
            }}
            
            .export-btn:hover {{
                background: #34a853;
                color: #ffffff;
                box-shadow: 0 2px 8px rgba(52, 168, 83, 0.15);
                transform: translateY(-1px);
            }}
            
            .export-btn:active {{
                transform: translateY(0);
            }}
            
            .download-notebook-btn {{
                display: inline-block;
                background: #ffffff;
                color: #1a73e8;
                border: 1px solid #1a73e8;
                padding: 10px 16px;
                border-radius: 6px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s ease;
                text-transform: none;
                letter-spacing: 0.2px;
                margin-top: 8px;
                text-decoration: none;
            }}
            
            .download-notebook-btn:hover {{
                background: #1a73e8;
                color: #ffffff;
                box-shadow: 0 2px 8px rgba(26, 115, 232, 0.15);
                transform: translateY(-1px);
            }}
            
            .download-notebook-btn:active {{
                transform: translateY(0);
            }}
            
            .voltage-config-btn {{
                background: #ffffff;
                color: #9c27b0;
                border: 1px solid #9c27b0;
                padding: 12px 16px;
                border-radius: 8px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s ease;
                box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
                text-transform: none;
                letter-spacing: 0.2px;
                min-width: 200px;
            }}
            
            .voltage-config-btn:hover {{
                background: #9c27b0;
                color: #ffffff;
                box-shadow: 0 2px 8px rgba(156, 39, 176, 0.15);
                transform: translateY(-1px);
            }}
            
            /* Context Menu Styles */
            .context-menu {{
                position: absolute;
                background: white;
                border: 1px solid #ddd;
                border-radius: 8px;
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
                z-index: 1000;
                min-width: 150px;
                padding: 4px 0;
            }}
            
            .context-menu-item {{
                padding: 8px 16px;
                cursor: pointer;
                font-size: 14px;
                color: #333;
                transition: background-color 0.2s ease;
            }}
            
            .context-menu-item:hover {{
                background-color: #f5f5f5;
            }}
            
            /* XRD Analysis Button Overlay */
            .xrd-analysis-btn {{
                position: absolute;
                top: 20px;
                right: 20px;
                z-index: 1000;
            }}
            
            .analysis-btn {{
                background: linear-gradient(135deg, #9c27b0 0%, #673ab7 100%);
                color: white;
                border: none;
                padding: 10px 16px;
                border-radius: 8px;
                font-size: 14px;
                font-weight: 600;
                cursor: pointer;
                transition: all 0.3s ease;
                box-shadow: 0 4px 15px rgba(156, 39, 176, 0.3);
                text-transform: none;
                letter-spacing: 0.3px;
            }}
            
            .analysis-btn:hover {{
                transform: translateY(-2px);
                box-shadow: 0 8px 25px rgba(156, 39, 176, 0.4);
            }}
            
            .analysis-btn:active {{
                transform: translateY(0);
            }}
            
            /* XRD Analysis Tooltip */
            .xrd-tooltip {{
                position: absolute;
                background: white;
                border: 2px solid #9c27b0;
                border-radius: 8px;
                box-shadow: 0 4px 20px rgba(156, 39, 176, 0.3);
                z-index: 1000;
                padding: 0;
                max-width: 150px;
                pointer-events: auto;
            }}
            
            .tooltip-content {{
                padding: 8px;
            }}
            
            .tooltip-link {{
                background: linear-gradient(135deg, #9c27b0 0%, #673ab7 100%);
                color: white;
                padding: 6px 10px;
                border-radius: 6px;
                font-size: 11px;
                font-weight: 600;
                cursor: pointer;
                text-align: center;
                transition: all 0.2s ease;
                white-space: nowrap;
            }}
            
            .tooltip-link:hover {{
                transform: translateY(-1px);
                box-shadow: 0 4px 12px rgba(156, 39, 176, 0.4);
            }}
            
            .voltage-config-btn:active {{
                transform: translateY(0);
            }}
            
            .plot-content {{
                padding: 24px;
                min-height: 200px;
                display: flex;
                align-items: center;
                justify-content: center;
                background: #ffffff;
            }}
            
            #plot {{
                width: 100% !important;
                height: 600px !important;
                min-width: 800px !important;
                min-height: 400px !important;
                max-width: 100% !important;
                max-height: 800px !important;
            }}
            
            #xrdPlotContent {{
                width: 100% !important;
                height: 500px !important;
                min-width: 800px !important;
                min-height: 300px !important;
                max-width: 100% !important;
                max-height: 700px !important;
            }}
            
            .info-panel {{
                margin: 16px;
                padding: 20px;
                background: #f8f9fa;
                border-radius: 8px;
                font-size: 14px;
                color: #5f6368;
                border: 1px solid #e8eaed;
                box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
            }}
            
            .info-panel strong {{
                color: #202124;
                font-weight: 500;
            }}
            
            .info-panel em {{
                color: #5f6368;
                font-style: italic;
            }}
            
            .loading {{
                text-align: center;
                color: #5f6368;
                font-style: normal;
                margin: 24px 0;
                font-size: 14px;
            }}
            
            /* Custom scrollbar */
            ::-webkit-scrollbar {{
                width: 6px;
            }}
            
            ::-webkit-scrollbar-track {{
                background: #f1f3f4;
                border-radius: 3px;
            }}
            
            ::-webkit-scrollbar-thumb {{
                background: #dadce0;
                border-radius: 3px;
            }}
            
            ::-webkit-scrollbar-thumb:hover {{
                background: #bdc1c6;
            }}
            
            /* Modal styles */
            .modal {{
                display: none;
                position: fixed;
                z-index: 1000;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                background-color: rgba(0,0,0,0.5);
            }}
            
            .modal-content {{
                background-color: #ffffff;
                margin: 5% auto;
                padding: 0;
                border-radius: 12px;
                width: 80%;
                max-width: 600px;
                box-shadow: 0 4px 20px rgba(0,0,0,0.3);
                animation: modalSlideIn 0.3s ease;
            }}
            
            @keyframes modalSlideIn {{
                from {{ transform: translateY(-50px); opacity: 0; }}
                to {{ transform: translateY(0); opacity: 1; }}
            }}
            
            .modal-header {{
                background: #f8f9fa;
                padding: 20px 24px;
                border-bottom: 1px solid #e8eaed;
                border-radius: 12px 12px 0 0;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }}
            
            .modal-header h2 {{
                margin: 0;
                color: #202124;
                font-size: 1.5em;
                font-weight: 500;
            }}
            
            .close {{
                color: #5f6368;
                font-size: 28px;
                font-weight: bold;
                cursor: pointer;
                transition: color 0.2s ease;
            }}
            
            .close:hover {{
                color: #202124;
            }}
            
            .modal-body {{
                padding: 24px;
            }}
            
            .form-group {{
                margin-bottom: 20px;
            }}
            
            .form-group label {{
                display: block;
                margin-bottom: 8px;
                font-weight: 500;
                color: #202124;
                font-size: 14px;
            }}
            
            .form-group input {{
                width: 100%;
                padding: 12px 16px;
                border: 1px solid #dadce0;
                border-radius: 8px;
                font-size: 14px;
                transition: border-color 0.2s ease;
                box-sizing: border-box;
            }}
            
            .form-group input:focus {{
                outline: none;
                border-color: #4285f4;
                box-shadow: 0 0 0 2px rgba(66, 133, 244, 0.2);
            }}
            
            .form-row {{
                display: flex;
                gap: 16px;
            }}
            
            .form-row .form-group {{
                flex: 1;
            }}
            
            .modal-footer {{
                padding: 20px 24px;
                border-top: 1px solid #e8eaed;
                display: flex;
                justify-content: flex-end;
                gap: 12px;
            }}
            
            .btn {{
                padding: 12px 24px;
                border-radius: 8px;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                transition: all 0.2s ease;
                border: none;
            }}
            
            .btn-primary {{
                background: #4285f4;
                color: white;
            }}
            
            .btn-primary:hover {{
                background: #3367d6;
                transform: translateY(-1px);
            }}
            
            .btn-secondary {{
                background: #f8f9fa;
                color: #5f6368;
                border: 1px solid #dadce0;
            }}
            
            .btn-secondary:hover {{
                background: #e8eaed;
            }}
            
            @media (max-width: 1200px) {{
                .controls {{
                    flex-direction: column;
                    gap: 24px;
                }}
                .control-group {{
                    min-width: 200px;
                }}
                h1 {{
                    font-size: 2em;
                }}
            }}
            
            @media (max-width: 768px) {{
                .container {{
                    margin: 16px; 
                    border-radius: 8px;
                }}
                h1 {{
                    padding: 32px 24px 20px 24px;
                    font-size: 1.8em;
                }}
                h3 {{
                    padding: 0 24px 24px 24px;
                }}
                .controls {{
                    padding: 24px;
                }}
                .plots-container {{
                    padding: 24px;
                }}
            }}
        </style>
    </head>
    <body>
        <a href="/" class="back-link">← Back to Dashboard</a>
        
        <div class="container">
            <h1><strong>OCx25 Dataset:</strong> HER Performance Data Visualization</h1>
            
            <div class="controls">
                <div class="control-group">
                    <label for="xAxis">X-Axis</label>
                    <select id="xAxis" onchange="updatePlot()">
                        {element_options}
                    </select>
                </div>
                
                <div class="control-group">
                    <label for="voltageType">Voltage Type</label>
                    <select id="voltageType" onchange="updatePlot()">
                        <option value="fullcell">Full Cell Voltage</option>
                        <option value="she">Est. Half-cell potential vs SHE</option>
                        <option value="rhe">Est. Half-cell potential vs RHE</option>
                    </select>
                </div>
                
                <div class="checkbox-container">
                    <input type="checkbox" id="errorBars" checked onchange="updatePlot()">
                    <label for="errorBars">Show Error Bars</label>
                </div>
                
                <div class="checkbox-container">
                    <input type="checkbox" id="disableXrdErrors">
                    <label for="disableXrdErrors">Disable pop-up error messages</label>
                </div>
                
                <div class="control-group">
                    <button id="voltageConfigBtn" class="voltage-config-btn" onclick="openVoltageConfig()">
                        ⚙️ Configure Voltage Conversion
                    </button>
                </div>
                
                <div class="control-group">
                    <button id="exportBtn" class="export-btn" onclick="exportData()">
                        ⊞ Export Data (CSV)
                    </button>
                </div>
            </div>
            
            <div class="plots-container">
                <div class="plot-section">
                    <div class="header-row">
                        <h3>Main Plot</h3>
                        <div class="header-controls">
                            <div class="toggle-group">
                                <label class="toggle-label">Units:</label>
                                <div class="toggle-switch">
                                    <input type="radio" id="unitAtomic" name="unitType" value="atomic" checked>
                                    <label for="unitAtomic">At. fraction</label>
                                    <input type="radio" id="unitWeight" name="unitType" value="weight">
                                    <label for="unitWeight">Wt. fraction</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="plot-content">
                        <div id="plot"></div>
                    </div>
                </div>
                
                <div class="plot-section">
                    <div class="header-row">
                        <h3>XRD Analysis</h3>
                        <div class="header-controls">
                            <div class="toggle-group">
                                <label class="toggle-label">Data Type:</label>
                                <div class="toggle-switch">
                                    <input type="radio" id="xrdRaw" name="xrdDataType" value="raw">
                                    <label for="xrdRaw">Raw</label>
                                    <input type="radio" id="xrdNormalized" name="xrdDataType" value="normalized" checked>
                                    <label for="xrdNormalized">Normalized</label>
                                </div>
                            </div>
                            <button id="resetXrdBtn" class="reset-btn" onclick="resetXrdPlot()">
                                ⟳ Reset
                            </button>
                        </div>
                    </div>
                    <div class="plot-content">
                        <div id="xrdPlotContent"></div>
                        
                        <!-- XRD Analysis Tooltip -->
                        <div id="xrdTooltip" class="xrd-tooltip" style="display: none;" 
                             onmouseenter="cancelTooltipHide()" 
                             onmouseleave="hideXrdTooltip()">
                            <div class="tooltip-content">
                                <div class="tooltip-link" onclick="openXrdAnalysisFromTooltip()">
                                    View XRD Analysis
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div id="loading" class="loading" style="display: none;">Updating plot...</div>
            
            <div class="info-panel">
                 <strong>Symbol Coding:</strong><br>
                <span style="font-weight: bold;">Circles</span>: Samples synthesized by Chemical Reduction (UofT)<br>
                <span style="font-weight: bold;">Diamonds</span>: Samples synthesized by Spark Ablation (VSP)<br>
                
                <strong>Default Color Coding:</strong><br>
                • <span style="color: #ef4444;">Red points</span>: Performance below Pd (UofT) threshold<br>
                • <span style="color: #3b82f6;">Blue points</span>: Performance below Pd (VSP) threshold<br>
                • <span style="color: #6b7280;">Black points</span>: Performance above both thresholds<br>
                <em>Note: The specific threshold values depend on the selected y-axis metric and are calculated as the mean performance for each source.</em>
                
                <br><br><strong>Note on Error Bars:</strong><br>
                Error bars are shown only when averaging across identical XRF compositions in this analysis.<br>
                • <strong>UofT (Chemical Reduction):</strong> Samples were first made as powders, XRF-measured once, then used to prepare 3 GDEs (Gas Diffusion Electrodes) for electrochemical testing. Since all GDEs came from the same powder vial (same composition), they were grouped together to calculate mean and standard deviation.<br>
                • <strong>VSP (Spark Ablation):</strong> Samples were deposited directly as 3 separate GDEs. Each had slightly different XRF compositions, so they could not be grouped. Their results are shown individually, without averaged error bars.
                
                <br><br><strong>Voltage Conversion Methodology:</strong><br>
                The conversion from full cell voltage to half-cell potentials (vs SHE and vs RHE) is performed using calibration data from electrochemical measurements in a three-electrode configuration. The conversion accounts for:<br>
                • Membrane overpotential and ionic resistance<br>
                • Nernstian pH gradient effects<br>
                • Reference electrode potential corrections<br>
                • Current density-dependent ohmic losses<br>
                The methodology follows established protocols for accurate half-cell potential determination in CO₂ reduction electrolyzers.<br>
                <a href="https://www.nature.com/articles/s41893-025-01643-4" target="_blank">Arabyarmohammadi, F. et al. Voltage distribution within carbon dioxide reduction electrolysers. <em>Nature Sustainability</em> (2025)</a>
                <br><br>
                <a href="https://huggingface.co/spaces/facebook/OCx25/blob/main/voltage_conversion_example.ipynb"
                   target="_blank" rel="noopener" class="download-notebook-btn">
                    📓 View Voltage-Conversion Notebook ↗
                </a>
                
                <br><br><strong>XRD Analysis:</strong><br>
                Click on any point in the main plot to view the corresponding XRD pattern in the XRD Analysis window.<br>
                • XRD data is loaded from <code>/Data/XRD/raw/</code> or <code>/Data/CustomXRD/raw/</code> directories<br>
                • Custom XRD data can be uploaded via the dashboard's "Load Your Own XRD Data" section<br>
                • Files should be named using the sample ID (e.g., <code>sample_001.xy</code> for raw or <code>sample_001.csv</code> for normalized)<br>
                • The plot shows 2θ (degrees) vs Intensity (counts)<br>
                • If no XRD data is found for a sample, an error message will be displayed
            </div>
        </div>
        
        <!-- Voltage Configuration Modal -->
        <div id="voltageConfigModal" class="modal">
            <div class="modal-content">
                <div class="modal-header">
                    <h2>Configure Voltage Conversion Parameters</h2>
                    <span class="close" onclick="closeVoltageConfig()">&times;</span>
                </div>
                <div class="modal-body">
                    <p style="margin-bottom: 20px; color: #5f6368; font-size: 14px;">
                        Adjust the parameters used for converting full cell voltages to half-cell potentials. 
                        These values are based on experimental calibration data.
                    </p>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="ref_pot">Reference Electrode Potential (V vs SHE)</label>
                            <input type="number" id="ref_pot" step="0.001" value="0.23">
                        </div>
                        <div class="form-group">
                            <label for="geo_area">Geometric Area (cm²)</label>
                            <input type="number" id="geo_area" step="0.1" value="4">
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="cathode_pH">Cathode pH</label>
                            <input type="number" id="cathode_pH" step="0.1" value="10">
                        </div>
                        <div class="form-group">
                            <label for="anode_pH">Anode pH</label>
                            <input type="number" id="anode_pH" step="0.1" value="3">
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="membrane_loss">Membrane Loss (V)</label>
                            <input type="number" id="membrane_loss" step="0.01" value="0.1">
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="anode_measured_potential_vs_ref">Anode Measured Half-cell Potential vs Reference (V)</label>
                        <input type="number" id="anode_measured_potential_vs_ref" step="0.001" value="1.3">
                    </div>
                    <div class="form-group">
                        <label for="R_cathode">R Cathode (Ω)</label>
                        <input type="number" id="R_cathode" step="0.001" value="0.4633">
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" onclick="resetVoltageConfig()">Reset to Defaults</button>
                    <button class="btn btn-primary" onclick="applyVoltageConfig()">Apply Changes</button>
                </div>
            </div>
        </div>
        
        <script>
            // Store data globally
            let currentData = null;
            let originalData = null;
            let currentUnitType = 'atomic';
            let currentYColumn = '{y_axis_column}'; // Dynamic voltage column
            let voltageConversionParams = null; // Store custom voltage conversion parameters
            
            // Initialize with data
            const initialData = {json.dumps(df_with_pca.to_dict('records'))};
            currentData = initialData;
            originalData = initialData;
            
            console.log('Loaded HER data:', currentData.length, 'rows');
            console.log('Available columns:', Object.keys(currentData[0] || {{}}));
            console.log('Y-axis column detected:', currentYColumn);
            
            // Detect actual voltage column from data
            if (currentData && currentData.length > 0) {{
                const firstRow = currentData[0];
                if (firstRow.hasOwnProperty('voltage_mean')) {{
                    currentYColumn = 'voltage_mean';
                }} else if (firstRow.hasOwnProperty('voltage')) {{
                    currentYColumn = 'voltage';
                }} else {{
                    console.warn('No voltage column found, using fallback:', currentYColumn);
                }}
                console.log('Final Y-axis column:', currentYColumn);
            }}
            
            // Set default x-axis
            const xAxisSelect = document.getElementById('xAxis');
            if (xAxisSelect && xAxisSelect.options.length > 0) {{
                xAxisSelect.value = xAxisSelect.options[0].value;
            }}
            
            function getColumnUnits(columnName, unitType = 'atomic') {{
                if (columnName === 'PCA1' || columnName === 'PCA2') {{
                    return '';
                }} else if (['Ag', 'Au', 'Cd', 'Cu', 'Ga', 'Hg', 'In', 'Ni', 'Pd', 'Pt', 'Rh', 'Sn', 'Tl', 'Zn'].includes(columnName)) {{
                    return unitType === 'weight' ? ' (wt. fraction)' : ' (at. fraction)';
                }} else if (columnName === 'cost_per_gram') {{
                    return ' ($/kg)';
                }} else {{
                    return '';
                }}
            }}
            
            function formatColumnName(columnName) {{
                const formatMap = {{
                    'voltage_mean': 'Full Cell Voltage (V)',
                    'voltage': 'Full Cell Voltage (V)',
                    'voltage_she': 'Est. Half-cell potential vs SHE (V)',
                    'voltage_rhe': 'Est. Half-cell potential vs RHE (V)',
                    'PCA1': 'PCA1',
                    'PCA2': 'PCA2',
                    'cost_per_gram': 'Cost per kg'
                }};
                return formatMap[columnName] || columnName;
            }}
            
            async function updatePlot() {{
                const xCol = document.getElementById('xAxis').value;
                const voltageType = document.getElementById('voltageType').value;
                const selectedUnit = document.querySelector('input[name="unitType"]:checked').value;
                currentUnitType = selectedUnit;
                
                try {{
                    // Update data with unit conversion if needed
                    const requestBody = {{
                        xAxis: xCol, 
                        unitType: selectedUnit, 
                        voltageType: voltageType
                    }};
                    
                    // Include custom voltage conversion parameters if set
                    if (voltageConversionParams) {{
                        requestBody.voltageConversionParams = voltageConversionParams;
                    }}
                    
                    const response = await fetch('/her/update_data', {{
                        method: 'POST',
                        headers: {{'Content-Type': 'application/json'}},
                        body: JSON.stringify(requestBody)
                    }});
                    
                    if (response.ok) {{
                        const result = await response.json();
                        currentData = result.data;
                        originalData = result.originalData;
                    }}
                }} catch (error) {{
                    console.log('Using local data due to fetch error:', error);
                }}
                
                createPlot(xCol, currentYColumn, currentData, originalData);
            }}
            
            function createPlot(xCol, yCol, data, originalDataForCalc = null) {{
                console.log('Creating plot with:', xCol, 'vs', yCol);
                console.log('Data points:', data.length);
                
                if (!data || data.length === 0) {{
                    document.getElementById('plot').innerHTML = '<div style="text-align: center; padding: 50px;"><h3>No data available for plotting</h3></div>';
                    return;
                }}
                
                // Check if required columns exist
                if (!data[0].hasOwnProperty(xCol)) {{
                    console.error('X-axis column not found:', xCol);
                    document.getElementById('plot').innerHTML = '<div style="text-align: center; padding: 50px;"><h3>X-axis column "' + xCol + '" not found in data</h3></div>';
                    return;
                }}
                if (!data[0].hasOwnProperty(yCol)) {{
                    console.error('Y-axis column not found:', yCol);
                    document.getElementById('plot').innerHTML = '<div style="text-align: center; padding: 50px;"><h3>Y-axis column "' + yCol + '" not found in data</h3></div>';
                    return;
                }}
                
                const calcData = originalDataForCalc || data;
                
                // Calculate Pd means for threshold lines
                let pdMeanUoft = null;
                let pdMeanVsp = null;
                
                for (let row of calcData) {{
                    if (row['Pd'] && Math.abs(parseFloat(row['Pd']) - 1.0) < 0.001) {{
                        if (row['source'] === 'uoft') pdMeanUoft = parseFloat(row[yCol]);
                        if (row['source'] === 'vsp') pdMeanVsp = parseFloat(row[yCol]);
                    }}
                }}
                
                console.log('Pd thresholds - UofT:', pdMeanUoft, 'VSP:', pdMeanVsp);
                
                // Create traces for each source and color combination
                const traces = [];
                const uoftData = data.filter(row => row['source'] === 'uoft');
                const vspData = data.filter(row => row['source'] === 'vsp');
                
                // Color code points based on performance thresholds
                function getPointColor(yValue, pdMeanUoft, pdMeanVsp) {{
                    if (pdMeanUoft !== null && pdMeanVsp !== null) {{
                        if (pdMeanVsp > pdMeanUoft) {{
                            if (yValue > pdMeanVsp) return '#6b7280';
                            else if (yValue > pdMeanUoft) return '#3b82f6';
                            else return '#ef4444';
                        }} else if (pdMeanUoft > pdMeanVsp) {{
                            if (yValue > pdMeanUoft) return '#6b7280';
                            else if (yValue > pdMeanVsp) return '#ef4444';
                            else return '#3b82f6';
                        }} else {{
                            return yValue > pdMeanUoft ? '#6b7280' : '#ef4444';
                        }}
                    }}
                    return '#6b7280';
                }}
                
                // Group and create traces
                const uoftByColor = {{}};
                uoftData.forEach(row => {{
                    const color = getPointColor(row[yCol], pdMeanUoft, pdMeanVsp);
                    if (!uoftByColor[color]) uoftByColor[color] = [];
                    uoftByColor[color].push(row);
                }});
                
                const vspByColor = {{}};
                vspData.forEach(row => {{
                    const color = getPointColor(row[yCol], pdMeanUoft, pdMeanVsp);
                    if (!vspByColor[color]) vspByColor[color] = [];
                    vspByColor[color].push(row);
                }});
                
                // Create traces for UofT data (circles)
                Object.keys(uoftByColor).forEach(color => {{
                    const colorData = uoftByColor[color];
                    const trace = {{
                        x: colorData.map(row => row[xCol]),
                        y: colorData.map(row => row[yCol]),
                        mode: 'markers',
                        type: 'scatter',
                        marker: {{
                            size: 12,
                            color: color,
                            line: {{
                                width: colorData.map(row => clickedPoints.has(row['sample id']) ? 4 : 1.5),
                                color: colorData.map(row => clickedPoints.has(row['sample id']) ? '#00FF00' : 'rgba(0,0,0,0.3)')
                            }},
                            symbol: 'circle',
                            opacity: 0.9
                        }},
                        text: colorData.map(row => 
                            `Sample: ${{row['sample id'] || 'N/A'}}<br>` +
                            `Source: ${{row['source']}}<br>` +
                            `Batch: ${{row['batch number'] || 'N/A'}}<br>` +
                            `Formula: ${{row['xrf composition'] || row['target composition'] || 'N/A'}}<br>` +
                            `Current Density: ${{row['current density']}} mA/cm²<br>` +
                            (row['sample_count'] !== undefined ? `Samples Aggregated: ${{row['sample_count']}}<br>` : '') +
                            `X: ${{row[xCol].toFixed(3)}}${{getColumnUnits(xCol, currentUnitType)}}<br>` +
                            `Y: ${{row[yCol].toFixed(3)}} V`
                        ),
                        hoverinfo: 'text',
                        name: 'UofT (chemical reduction)' + (color === '#6b7280' ? ' - Above threshold' : color === '#3b82f6' ? ' - Medium performance' : ' - Below threshold'),
                        showlegend: true,
                        customdata: colorData.map(row => row['sample id']),
                        source: colorData.map(row => row['source']),
                        'xrf composition': colorData.map(row => row['xrf composition'] || row['target composition'])
                    }};
                    
                    // Add error bars if enabled and available
                    if (document.getElementById('errorBars').checked && yCol === 'voltage_mean' && colorData[0] && colorData[0]['voltage_std'] !== undefined) {{
                        trace.error_y = {{
                            type: 'data',
                            array: colorData.map(row => row['voltage_std'] || 0),
                            color: color,
                            thickness: 1.5,
                            width: 2
                        }};
                    }}
                    
                    traces.push(trace);
                }});
                
                // Create traces for VSP data (diamonds)
                Object.keys(vspByColor).forEach(color => {{
                    const colorData = vspByColor[color];
                    const trace = {{
                        x: colorData.map(row => row[xCol]),
                        y: colorData.map(row => row[yCol]),
                        mode: 'markers',
                        type: 'scatter',
                        marker: {{
                            size: 12,
                            color: color,
                            line: {{
                                width: colorData.map(row => clickedPoints.has(row['sample id']) ? 4 : 1.5),
                                color: colorData.map(row => clickedPoints.has(row['sample id']) ? '#00FF00' : 'rgba(0,0,0,0.3)')
                            }},
                            symbol: 'diamond',
                            opacity: 0.9
                        }},
                        text: colorData.map(row => 
                            `Sample: ${{row['sample id'] || 'N/A'}}<br>` +
                            `Source: ${{row['source']}}<br>` +
                            `Batch: ${{row['batch number'] || 'N/A'}}<br>` +
                            `Formula: ${{row['xrf composition'] || row['target composition'] || 'N/A'}}<br>` +
                            `Current Density: ${{row['current density']}} mA/cm²<br>` +
                            (row['sample_count'] !== undefined ? `Samples Aggregated: ${{row['sample_count']}}<br>` : '') +
                            `X: ${{row[xCol].toFixed(3)}}${{getColumnUnits(xCol, currentUnitType)}}<br>` +
                            `Y: ${{row[yCol].toFixed(3)}} V`
                        ),
                        hoverinfo: 'text',
                        name: 'VSP (spark ablation)' + (color === '#6b7280' ? ' - Above threshold' : color === '#3b82f6' ? ' - Medium performance' : ' - Below threshold'),
                        showlegend: true,
                        customdata: colorData.map(row => row['sample id']),
                        source: colorData.map(row => row['source']),
                        'xrf composition': colorData.map(row => row['xrf composition'] || row['target composition'])
                    }};
                    
                    if (document.getElementById('errorBars').checked && yCol === 'voltage_mean' && colorData.length > 0 && colorData[0]['voltage_std'] !== undefined) {{
                        trace.error_y = {{
                            type: 'data',
                            array: colorData.map(row => row['voltage_std'] || 0),
                            color: color,
                            thickness: 1.5,
                            width: 2
                        }};
                    }}
                    
                    traces.push(trace);
                }});
                
                // Get voltage type for dynamic labeling
                const voltageType = document.getElementById('voltageType').value;
                let voltageLabel = 'Full Cell Voltage (V)';
                if (voltageType === 'she') {{
                    voltageLabel = 'Est. Half-cell potential vs SHE (V)';
                }} else if (voltageType === 'rhe') {{
                    voltageLabel = 'Est. Half-cell potential vs RHE (V)';
                }}
                
                const layout = {{
                    title: {{
                        text: `${{formatColumnName(xCol)}} vs ${{voltageLabel.replace(' (V)', '')}}`,
                        font: {{size: 18}},
                        x: 0.5
                    }},
                    xaxis: {{
                        title: formatColumnName(xCol) + getColumnUnits(xCol, currentUnitType),
                        showgrid: true,
                        gridcolor: '#e8e8e8'
                    }},
                    yaxis: {{
                        title: voltageLabel,
                        showgrid: true,
                        gridcolor: '#e8e8e8'
                    }},
                    hovermode: 'closest',
                    template: 'plotly_white',
                    height: 600,
                    width: null,
                    autosize: true,
                    margin: {{l: 80, r: 200, t: 80, b: 60}},
                    showlegend: true,
                    legend: {{
                        x: 1.02,
                        y: 1,
                        bgcolor: 'rgba(255,255,255,0.9)'
                    }}
                }};
                
                // Add reference lines
                const shapes = [];
                if (pdMeanUoft !== null) {{
                    const xValues = data.map(row => row[xCol]);
                    const xMin = Math.min(...xValues);
                    const xMax = Math.max(...xValues);
                    
                    const xRange = xMax - xMin;
                    shapes.push({{
                        type: 'line',
                        x0: xMin - xRange * 0.1,
                        x1: xMax + xRange * 0.1,
                        y0: pdMeanUoft,
                        y1: pdMeanUoft,
                        line: {{color: '#ef4444', dash: 'dash', width: 3}}
                    }});
                }}
                
                if (pdMeanVsp !== null) {{
                    const xValues = data.map(row => row[xCol]);
                    const xMin = Math.min(...xValues);
                    const xMax = Math.max(...xValues);
                    
                    const xRange = xMax - xMin;
                    shapes.push({{
                        type: 'line',
                        x0: xMin - xRange * 0.1,
                        x1: xMax + xRange * 0.1,
                        y0: pdMeanVsp,
                        y1: pdMeanVsp,
                        line: {{color: '#3b82f6', dash: 'dot', width: 3}}
                    }});
                }}
                
                // Add annotations for reference lines
                const annotations = [];
                
                if (pdMeanUoft !== null) {{
                    const xValues = data.map(row => row[xCol]);
                    const xMax = Math.max(...xValues);
                    const xRange = Math.max(...xValues) - Math.min(...xValues);
                    
                    annotations.push({{
                        x: xMax + xRange * 0.1,
                        y: pdMeanUoft,
                        text: `Pd (UofT)`,
                        showarrow: false,
                        xanchor: 'left',
                        yanchor: 'middle',
                        bgcolor: 'rgba(255,255,255,0.9)',
                        bordercolor: '#ef4444',
                        borderwidth: 2,
                        font: {{color: '#ef4444', size: 14}}
                    }});
                }}
                
                if (pdMeanVsp !== null) {{
                    const xValues = data.map(row => row[xCol]);
                    const xMax = Math.max(...xValues);
                    const xRange = Math.max(...xValues) - Math.min(...xValues);
                    
                    annotations.push({{
                        x: xMax + xRange * 0.1,
                        y: pdMeanVsp,
                        text: `Pd (VSP)`,
                        showarrow: false,
                        xanchor: 'left',
                        yanchor: 'middle',
                        bgcolor: 'rgba(255,255,255,0.9)',
                        bordercolor: '#3b82f6',
                        borderwidth: 2,
                        font: {{color: '#3b82f6', size: 14}}
                    }});
                }}
                
                if (shapes.length > 0) {{
                    layout.shapes = shapes;
                }}
                
                if (annotations.length > 0) {{
                    layout.annotations = annotations;
                }}
                
                const exportCsvButton = {{
                    name: 'exportCsv',
                    title: 'Export CSV',
                    icon: {{width: 500, height: 500, path: 'M50 400 L450 400 L450 450 L50 450 Z M100 50 L400 50 L400 350 L100 350 Z'}},
                    click: function(gd) {{ try {{
                        const gdDiv = 'plot';
                        const gdEl = document.getElementById(gdDiv);
                        if (!gdEl || !gdEl.data) return;
                        const rows = [];
                        rows.push(['x','y'].join(','));
                        (gdEl.data || []).forEach(tr => {{
                            const xs = tr.x || [];
                            const ys = tr.y || [];
                            const n = Math.min(xs.length, ys.length);
                            for (let i = 0; i < n; i++) rows.push([xs[i], ys[i]].join(','));
                        }});
                        const blob = new Blob([rows.join('\\n')], {{ type: 'text/csv' }});
                        const url = URL.createObjectURL(blob);
                        const a = document.createElement('a'); a.href = url; a.download = 'her_plot.csv';
                        document.body.appendChild(a); a.click(); URL.revokeObjectURL(url); document.body.removeChild(a);
                    }} catch(e) {{ console.error('Export CSV failed:', e); }} }}
                }};
                Plotly.newPlot('plot', traces, layout, {{
                    responsive: true,
                    modeBarButtonsToAdd: [exportCsvButton],
                    toImageButtonOptions: {{
                        format: 'png',
                        filename: 'her_plot',
                        height: 800,
                        width: 1200,
                        scale: 3
                    }}
                }});
                
                // Add click event handler for XRD loading and deselection
                document.getElementById('plot').on('plotly_click', function(data) {{
                    console.log('Plot clicked:', data);
                    
                    if (data.points && data.points.length > 0) {{
                        const point = data.points[0];
                        const pointData = point.data;
                        const pointIndex = point.pointIndex;
                        
                        // Get the sample ID from the clicked point
                        const sampleId = pointData.customdata ? pointData.customdata[pointIndex] : null;
                        
                        if (sampleId) {{
                            console.log('Sample ID:', sampleId);
                            
                            // Check if this point is already selected
                            if (clickedPoints.has(sampleId)) {{
                                console.log('Point already selected, deselecting:', sampleId);
                                
                                // Remove from clicked points set
                                clickedPoints.delete(sampleId);
                                
                                // Update plot to remove green border
                                updateClickedPointVisual(sampleId);
                                
                                // Remove XRD data for the clicked sample
                                removeXrdPlot(sampleId);
                            }} else {{
                                console.log('Point not selected, selecting:', sampleId);
                                
                                // Add to clicked points set
                                clickedPoints.add(sampleId);
                                
                                // Store clicked point data for XRD legend
                                clickedPointData = {{
                                    source: pointData.source ? pointData.source[pointIndex] : 'Unknown',
                                    'xrf composition': pointData['xrf composition'] ? pointData['xrf composition'][pointIndex] : 'Unknown'
                                }};
                                
                                // Update plot to show clicked point with green border
                                updateClickedPointVisual(sampleId);
                                
                                // Load XRD data for the clicked sample
                                loadXrdPlot(sampleId);
                            }}
                        }} else {{
                            console.log('No sample ID found for clicked point');
                        }}
                    }}
                }});
                
                console.log('Plot created successfully');
            }}
            
            // Function to update visual appearance of clicked points
            function updateClickedPointVisual(sampleId) {{
                // Get current plot data
                const plotDiv = document.getElementById('plot');
                const plotData = plotDiv.data;
                
                // Update marker borders for clicked points
                plotData.forEach(trace => {{
                    if (trace.customdata) {{
                        trace.marker.line.width = trace.customdata.map(id => 
                            clickedPoints.has(id) ? 4 : 1.5
                        );
                        trace.marker.line.color = trace.customdata.map(id => 
                            clickedPoints.has(id) ? '#00FF00' : 'rgba(0,0,0,0.3)'
                        );
                    }}
                }});
                
                // Redraw the plot
                Plotly.redraw('plot');
            }}
            
            // Function to export data as CSV
            async function exportData() {{
                try {{
                    const exportBtn = document.getElementById('exportBtn');
                    exportBtn.textContent = '⊞ Exporting...';
                    exportBtn.disabled = true;
                    
                    // Send request to export CSV
                    const response = await fetch('/her/export_csv', {{
                        method: 'POST',
                        headers: {{
                            'Content-Type': 'application/json',
                        }},
                        body: JSON.stringify({{}})
                    }});
                    
                    if (!response.ok) {{
                        throw new Error('Export failed');
                    }}
                    
                    // Get the CSV data
                    const csvData = await response.text();
                    
                    // Create and download the file
                    const blob = new Blob([csvData], {{ type: 'text/csv' }});
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = 'HER_data.csv';
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                    document.body.removeChild(a);
                    
                    // Reset button
                    exportBtn.textContent = '⊞ Export Data (CSV)';
                    exportBtn.disabled = false;
                    
                }} catch (error) {{
                    console.error('Export error:', error);
                    alert('Export failed. Please try again.');
                    
                    // Reset button
                    const exportBtn = document.getElementById('exportBtn');
                    exportBtn.textContent = '⊞ Export Data (CSV)';
                    exportBtn.disabled = false;
                }}
            }}
            
            // Voltage configuration functions
            function openVoltageConfig() {{
                // Initialize with interpolated values if not already set
                const defaultCurrentDensity = 50; // mA/cm² - default for interpolation (matching notebook example)
                if (!document.getElementById('anode_measured_potential_vs_ref').value || 
                    document.getElementById('anode_measured_potential_vs_ref').value === '1.35') {{
                    const interpolatedAnodePot = interpolateAnodePotentialVsRef(defaultCurrentDensity);
                    document.getElementById('anode_measured_potential_vs_ref').value = interpolatedAnodePot.toFixed(4);
                }}
                if (!document.getElementById('R_cathode').value || 
                    document.getElementById('R_cathode').value === '0.34') {{
                    const interpolatedR = interpolateCathodeR(defaultCurrentDensity);
                    document.getElementById('R_cathode').value = interpolatedR.toFixed(4);
                }}
                document.getElementById('voltageConfigModal').style.display = 'block';
            }}
            
            function closeVoltageConfig() {{
                document.getElementById('voltageConfigModal').style.display = 'none';
            }}
            
            // Interpolation functions (matching Python implementation)
            function interpolateAnodePotentialVsRef(currentDensity) {{
                // Calibration data: j = [50, 100, 200] mA/cm², anode_pot = [1.3, 1.35, 1.4] V
                const jArray = [50, 100, 200];
                const anodePotArray = [1.3, 1.35, 1.4];
                
                if (currentDensity <= 0) {{
                    return anodePotArray[0];
                }}
                
                // Fit linear relationship: anode_pot = a * log10(j) + b
                const logJ = jArray.map(j => Math.log10(j));
                const n = logJ.length;
                const sumX = logJ.reduce((a, b) => a + b, 0);
                const sumY = anodePotArray.reduce((a, b) => a + b, 0);
                const sumXY = logJ.reduce((sum, x, i) => sum + x * anodePotArray[i], 0);
                const sumX2 = logJ.reduce((sum, x) => sum + x * x, 0);
                
                const a = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
                const b = (sumY - a * sumX) / n;
                
                const logJInput = Math.log10(currentDensity);
                let anodePotInterpolated = a * logJInput + b;
                
                // Clamp to reasonable bounds
                anodePotInterpolated = Math.max(anodePotArray[0], Math.min(anodePotArray[anodePotArray.length - 1], anodePotInterpolated));
                
                return anodePotInterpolated;
            }}
            
            function interpolateCathodeR(currentDensity) {{
                // Calibration data: j = [50, 100, 200] mA/cm², R = [0.48, 0.34, 0.3] Ω
                const jArray = [50, 100, 200];
                const RArray = [0.48, 0.34, 0.3];
                
                if (currentDensity <= 0) {{
                    return RArray[RArray.length - 1];
                }}
                
                // Fit linear relationship: R = a * log10(j) + b
                const logJ = jArray.map(j => Math.log10(j));
                const n = logJ.length;
                const sumX = logJ.reduce((a, b) => a + b, 0);
                const sumY = RArray.reduce((a, b) => a + b, 0);
                const sumXY = logJ.reduce((sum, x, i) => sum + x * RArray[i], 0);
                const sumX2 = logJ.reduce((sum, x) => sum + x * x, 0);
                
                const a = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
                const b = (sumY - a * sumX) / n;
                
                const logJInput = Math.log10(currentDensity);
                let RInterpolated = a * logJInput + b;
                
                // Clamp to reasonable bounds
                RInterpolated = Math.max(RArray[RArray.length - 1], Math.min(RArray[0], RInterpolated));
                
                return RInterpolated;
            }}
            
            function resetVoltageConfig() {{
                const defaultCurrentDensity = 50; // mA/cm² - default for interpolation (matching notebook example)
                
                document.getElementById('ref_pot').value = '0.23';
                document.getElementById('geo_area').value = '4';
                document.getElementById('cathode_pH').value = '10';
                document.getElementById('anode_pH').value = '3';
                document.getElementById('membrane_loss').value = '0.1';
                
                // Calculate and set interpolated values
                const interpolatedAnodePot = interpolateAnodePotentialVsRef(defaultCurrentDensity);
                const interpolatedR = interpolateCathodeR(defaultCurrentDensity);
                document.getElementById('anode_measured_potential_vs_ref').value = interpolatedAnodePot.toFixed(4);
                document.getElementById('R_cathode').value = interpolatedR.toFixed(4);
            }}
            
            function applyVoltageConfig() {{
                // Get parameter values from form
                const params = {{
                    ref_pot: parseFloat(document.getElementById('ref_pot').value),
                    geo_area: parseFloat(document.getElementById('geo_area').value),
                    cathode_pH: parseFloat(document.getElementById('cathode_pH').value),
                    anode_pH: parseFloat(document.getElementById('anode_pH').value),
                    membrane_loss: parseFloat(document.getElementById('membrane_loss').value),
                    anode_measured_potential_vs_ref: parseFloat(document.getElementById('anode_measured_potential_vs_ref').value),
                    R_cathode: parseFloat(document.getElementById('R_cathode').value)
                }};
                
                // Store parameters globally
                voltageConversionParams = params;
                
                // Close modal
                closeVoltageConfig();
                
                // Update plot with new parameters
                updatePlot();
            }}
            
            // Close modal when clicking outside of it
            window.onclick = function(event) {{
                const modal = document.getElementById('voltageConfigModal');
                if (event.target == modal) {{
                    closeVoltageConfig();
                }}
            }}
            
            // Initialize plot
            updatePlot();
            
            // XRD functionality
            let accumulatedPoints = [];
            let accumulatedXrdData = [];
            let clickedPointData = null;
            let clickedPoints = new Set(); // Track clicked points by sample ID
            
            // Initialize XRD plot (empty)
            document.getElementById('xrdPlotContent').innerHTML = '';
            
            // Add event listeners to XRD data type toggle
            document.querySelectorAll('input[name="xrdDataType"]').forEach(radio => {{
                radio.addEventListener('change', function() {{
                    console.log('XRD data type changed to:', this.value);
                    // Reload all accumulated XRD plots with new data type
                    reloadAccumulatedXrdPlots();
                }});
            }});
            
            // Add event listeners to unit type toggle
            document.querySelectorAll('input[name="unitType"]').forEach(radio => {{
                radio.addEventListener('change', function() {{
                    console.log('Unit type changed to:', this.value);
                    currentUnitType = this.value;
                    updatePlot();
                }});
            }});
            
            // Function to load XRD plot for a specific sample ID and add to accumulation
            async function loadXrdPlot(sampleId) {{
                try {{
                    // Get selected data type from toggle
                    const dataType = document.querySelector('input[name="xrdDataType"]:checked').value;
                    
                    // Fetch XRD data for the specific sample
                    const response = await fetch('/her/get_xrd_data', {{
                        method: 'POST',
                        headers: {{
                            'Content-Type': 'application/json',
                        }},
                        body: JSON.stringify({{ 
                            sample_id: sampleId,
                            data_type: dataType
                        }})
                    }});
                    
                    if (!response.ok) {{
                        const errorData = await response.json();
                        throw new Error(errorData.error || 'Failed to fetch XRD data');
                    }}
                    
                    const result = await response.json();
                    
                    if (!result.success) {{
                        throw new Error(result.error || 'XRD data not found');
                    }}
                    
                    // Add to accumulation
                    addXrdToAccumulation(result.data, result.sample_id, result.data_points);
                    
                }} catch (error) {{
                    console.error('Error loading XRD data:', error);
                    // Only show alert if error messages are not disabled
                    const disableErrors = document.getElementById('disableXrdErrors').checked;
                    if (!disableErrors) {{
                        alert('Error loading XRD data:\\n\\n' + error.message);
                    }}
                }}
            }}
            
            // Function to remove XRD plot for a specific sample ID
            function removeXrdPlot(sampleId) {{
                console.log('Removing XRD plot for sample:', sampleId);
                
                // Remove from accumulation
                const index = accumulatedXrdData.findIndex(item => item.sampleId === sampleId);
                if (index !== -1) {{
                    accumulatedXrdData.splice(index, 1);
                    console.log('Removed XRD data from accumulation. Remaining samples:', accumulatedXrdData.length);
                    
                    // Update XRD plot display
                    showAccumulatedXrdPlots();
                }} else {{
                    console.log('Sample not found in XRD accumulation:', sampleId);
                }}
            }}
            
            // Function to add XRD data to accumulation
            function addXrdToAccumulation(xrdData, sampleId, dataPoints) {{
                // Check if this sample is already in accumulation
                const existingIndex = accumulatedXrdData.findIndex(item => item.sampleId === sampleId);
                
                if (existingIndex !== -1) {{
                    console.log('Sample already in XRD accumulation:', sampleId);
                    return; // Don't add duplicates
                }}
                
                // Get source and XRF composition from the clicked point data
                const source = clickedPointData ? clickedPointData.source : 'Unknown';
                const xrfComposition = clickedPointData ? clickedPointData['xrf composition'] : 'Unknown';
                
                // Add to accumulation
                accumulatedXrdData.push({{
                    data: xrdData,
                    sampleId: sampleId,
                    dataPoints: dataPoints,
                    source: source,
                    xrfComposition: xrfComposition
                }});
                
                console.log('Added XRD data to accumulation. Total samples:', accumulatedXrdData.length);
                
                // Show accumulated XRD plots
                showAccumulatedXrdPlots();
            }}
            
            // Function to show accumulated XRD plots
            function showAccumulatedXrdPlots() {{
                if (accumulatedXrdData.length === 0) {{
                    document.getElementById('xrdPlotContent').innerHTML = ''; // Clear content if no data
                    return;
                }}
                
                const traces = [];
                const colors = ['#4285f4', '#ea4335', '#34a853', '#fbbc04', '#ff6d01', '#9c27b0', '#00bcd4', '#795548'];
                
                accumulatedXrdData.forEach((xrdItem, index) => {{
                    const color = colors[index % colors.length];
                    
                    // Use stored source and XRF composition to match point analysis format
                    const source = xrdItem.source || 'Unknown';
                    const xrfComposition = xrdItem.xrfComposition || 'Unknown';
                    
                    const trace = {{
                        x: xrdItem.data.x,
                        y: xrdItem.data.y,
                        mode: 'lines',
                        type: 'scatter',
                        line: {{
                            color: color,
                            width: 2
                        }},
                        name: source + ' - ' + xrfComposition,
                        hovertemplate: '<br>2θ: %{{x:.2f}}°<br>Intensity: %{{y:.2f}}<extra></extra>'
                    }};
                    traces.push(trace);
                }});
                
                const layout = {{
                    title: '',
                    xaxis: {{
                        title: '2θ (degrees)',
                        showgrid: true,
                        gridcolor: '#e0e0e0'
                    }},
                    yaxis: {{
                        title: 'Intensity',
                        showgrid: true,
                        gridcolor: '#e0e0e0',
                        zeroline: false
                    }},
                    showlegend: true,
                    legend: {{
                        x: 1.02,
                        y: 1,
                        xanchor: 'left',
                        yanchor: 'top'
                    }},
                    margin: {{ l: 60, r: 150, t: 20, b: 60 }},
                    width: null,
                    height: 500,
                    autosize: true,
                    hovermode: 'closest'
                }};
                
                const exportXrdCsvButton = {{
                    name: 'exportCsv',
                    title: 'Export CSV',
                    icon: {{width: 500, height: 500, path: 'M50 400 L450 400 L450 450 L50 450 Z M100 50 L400 50 L400 350 L100 350 Z'}},
                    click: function(gd) {{ try {{
                        const gdDiv = 'xrdPlotContent';
                        const gdEl = document.getElementById(gdDiv);
                        if (!gdEl || !gdEl.data) return;
                        const rows = [];
                        rows.push(['x','y'].join(','));
                        (gdEl.data || []).forEach(tr => {{
                            const xs = tr.x || [];
                            const ys = tr.y || [];
                            const n = Math.min(xs.length, ys.length);
                            for (let i = 0; i < n; i++) rows.push([xs[i], ys[i]].join(','));
                        }});
                        const blob = new Blob([rows.join('\\n')], {{ type: 'text/csv' }});
                        const url = URL.createObjectURL(blob);
                        const a = document.createElement('a'); a.href = url; a.download = 'her_xrd_plot.csv';
                        document.body.appendChild(a); a.click(); URL.revokeObjectURL(url); document.body.removeChild(a);
                    }} catch(e) {{ console.error('Export CSV failed:', e); }} }}
                }};
                Plotly.newPlot('xrdPlotContent', traces, layout, {{
                    responsive: true,
                    modeBarButtonsToAdd: [exportXrdCsvButton],
                    toImageButtonOptions: {{
                        format: 'png',
                        filename: 'her_xrd_plot',
                        height: 600,
                        width: 1500,
                        scale: 3
                    }}
                }});
                
                // Add hover event listener for XRD plots
                document.getElementById('xrdPlotContent').on('plotly_hover', function(data) {{
                    if (data && data.points && data.points.length > 0) {{
                        const point = data.points[0];
                        const traceIndex = point.curveNumber;
                        
                        // Get sample ID from the trace
                        if (traceIndex < accumulatedXrdData.length) {{
                            const sampleId = accumulatedXrdData[traceIndex].sampleId;
                            showXrdTooltip(point.x, point.y, sampleId);
                        }}
                    }}
                }});
                
                // Hide tooltip when mouse leaves
                document.getElementById('xrdPlotContent').on('plotly_unhover', function(data) {{
                    hideXrdTooltip();
                }});
                
                console.log('Accumulated XRD plots created successfully. Total traces:', traces.length);
            }}
            
            // Function to reset XRD accumulation
            function resetXrdPlot() {{
                accumulatedXrdData = [];
                clickedPoints.clear(); // Clear clicked points
                document.getElementById('xrdPlotContent').innerHTML = '';
                
                // Update plot to remove red borders
                const plotDiv = document.getElementById('plot');
                if (plotDiv && plotDiv.data) {{
                    plotDiv.data.forEach(trace => {{
                        if (trace.marker && trace.marker.line) {{
                            trace.marker.line.width = 1.5;
                            trace.marker.line.color = 'rgba(0,0,0,0.3)';
                        }}
                    }});
                    Plotly.redraw('plot');
                }}
                
                console.log('XRD accumulation reset');
            }}
            
            // Function to reload all accumulated XRD plots with current data type
            async function reloadAccumulatedXrdPlots() {{
                if (accumulatedXrdData.length === 0) {{
                    return; // Nothing to reload
                }}
                
                console.log('Reloading accumulated XRD plots with new data type');
                
                // Store current accumulated data with metadata
                const currentSamples = [...accumulatedXrdData];
                
                // Clear current accumulation
                accumulatedXrdData = [];
                
                // Reload each sample with new data type while preserving metadata
                for (const xrdItem of currentSamples) {{
                    await reloadSingleXrdPlot(xrdItem.sampleId, xrdItem.source, xrdItem.xrfComposition);
                }}
            }}
            
            // Function to reload a single XRD plot while preserving metadata
            async function reloadSingleXrdPlot(sampleId, source, xrfComposition) {{
                try {{
                    // Get selected data type from toggle
                    const dataType = document.querySelector('input[name="xrdDataType"]:checked').value;
                    
                    // Fetch XRD data for the specific sample
                    const response = await fetch('/her/get_xrd_data', {{
                        method: 'POST',
                        headers: {{
                            'Content-Type': 'application/json',
                        }},
                        body: JSON.stringify({{ 
                            sample_id: sampleId,
                            data_type: dataType
                        }})
                    }});
                    
                    if (!response.ok) {{
                        const errorData = await response.json();
                        throw new Error(errorData.error || 'Failed to fetch XRD data');
                    }}
                    
                    const result = await response.json();
                    
                    if (!result.success) {{
                        throw new Error(result.error || 'XRD data not found');
                    }}
                    
                    // Add to accumulation with preserved metadata
                    addXrdToAccumulationWithMetadata(result.data, sampleId, result.data_points, source, xrfComposition);
                    
                }} catch (error) {{
                    console.error('Error reloading XRD data:', error);
                    alert('Error reloading XRD data:\\n\\n' + error.message);
                }}
            }}
            
            // Function to add XRD data to accumulation with explicit metadata
            function addXrdToAccumulationWithMetadata(xrdData, sampleId, dataPoints, source, xrfComposition) {{
                // Check if this sample is already in accumulation
                const existingIndex = accumulatedXrdData.findIndex(item => item.sampleId === sampleId);
                
                if (existingIndex !== -1) {{
                    console.log('Sample already in XRD accumulation:', sampleId);
                    return; // Don't add duplicates
                }}
                
                // Add to accumulation with explicit metadata
                accumulatedXrdData.push({{
                    data: xrdData,
                    sampleId: sampleId,
                    dataPoints: dataPoints,
                    source: source,
                    xrfComposition: xrfComposition
                }});
                
                console.log('Added XRD data to accumulation with metadata. Total samples:', accumulatedXrdData.length);
                
                // Show accumulated XRD plots
                showAccumulatedXrdPlots();
            }}
            
            // Global variable to store the selected sample ID
            let selectedXrdSampleId = null;
            let tooltipHideTimeout = null;
            
            // Function to show XRD tooltip
            function showXrdTooltip(x, y, sampleId) {{
                selectedXrdSampleId = sampleId;
                
                // Clear any existing hide timeout
                if (tooltipHideTimeout) {{
                    clearTimeout(tooltipHideTimeout);
                    tooltipHideTimeout = null;
                }}
                
                const tooltip = document.getElementById('xrdTooltip');
                
                if (tooltip) {{
                    // Position tooltip below Plotly's original hover tooltip
                    tooltip.style.display = 'block';
                    tooltip.style.left = (event.pageX + 20) + 'px';
                    tooltip.style.top = (event.pageY + 40) + 'px'; // Position below Plotly's tooltip
                }}
            }}
            
            // Function to hide XRD tooltip with delay
            function hideXrdTooltip() {{
                // Add a small delay before hiding to allow mouse to move to tooltip
                tooltipHideTimeout = setTimeout(() => {{
                    const tooltip = document.getElementById('xrdTooltip');
                    if (tooltip) {{
                        tooltip.style.display = 'none';
                    }}
                }}, 200); // 200ms delay
            }}
            
            // Function to cancel hide when hovering over tooltip
            function cancelTooltipHide() {{
                if (tooltipHideTimeout) {{
                    clearTimeout(tooltipHideTimeout);
                    tooltipHideTimeout = null;
                }}
            }}
            
            // Function to open XRD analysis from tooltip
            function openXrdAnalysisFromTooltip() {{
                if (selectedXrdSampleId) {{
                    // Parse sample ID to extract dataset and sample
                    // For sample like "uoft8_241025_Cd-0.875-Ni-0.125_pp0_rep1"
                    // Dataset should be "uoft8_241025" (first two parts)
                    const parts = selectedXrdSampleId.split('_');
                    const dataset = parts.slice(0, 2).join('_'); // First two parts
                    const sample = selectedXrdSampleId;
                    
                    // Navigate to XRD dashboard with parameters
                    const url = `/xrd/?dataset=${{encodeURIComponent(dataset)}}&sample=${{encodeURIComponent(sample)}}`;
                    window.open(url, '_blank');
                    
                    // Hide tooltip after clicking
                    hideXrdTooltip();
                }}
            }}
            
        </script>
    </body>
    </html>
    '''
    
    return html_template

@her_plot_bp.route('/update_data', methods=['POST'])
def update_data():
    """Handle AJAX requests to update plot data with unit conversions"""
    try:
        data = request.get_json()
        x_axis = data.get('xAxis', 'Cu')
        unit_type = data.get('unitType', 'atomic')
        voltage_type = data.get('voltageType', 'fullcell')
        
        # Load fresh data
        df = load_original_data()
        
        if df.empty:
            return jsonify({'error': 'No data available'}), 400
        
        # Calculate PCA if needed
        df = calculate_pca_components(df)
        
        # Apply voltage conversion if needed
        if voltage_type in ['she', 'rhe'] and ('voltage' in df.columns or 'voltage_mean' in df.columns):
            # Get custom parameters if provided
            custom_params = data.get('voltageConversionParams')
            
            # Determine which voltage column to use
            voltage_col = 'voltage_mean' if 'voltage_mean' in df.columns else 'voltage'
            # Get current density column
            current_density_col = 'current density' if 'current density' in df.columns else None
            
            # Convert voltage values
            voltage_values = df[voltage_col].values
            converted_voltages = []
            
            for idx, v in enumerate(voltage_values):
                if pd.notna(v):
                    # Get current density for this row, default to 100 mA/cm² if not available
                    current_density = df[current_density_col].iloc[idx] if current_density_col and pd.notna(df[current_density_col].iloc[idx]) else 100.0
                    ushe, urhe = fullcell2halfcell(v, current_density, custom_params)
                    if voltage_type == 'she':
                        converted_voltages.append(ushe)
                    else:  # rhe
                        converted_voltages.append(urhe)
                else:
                    converted_voltages.append(np.nan)
            
            # Create new column with converted voltage
            if voltage_type == 'she':
                df['voltage_she'] = converted_voltages
                df[voltage_col] = df['voltage_she']  # Replace original voltage
            else:  # rhe
                df['voltage_rhe'] = converted_voltages
                df[voltage_col] = df['voltage_rhe']  # Replace original voltage
        
        # Store original data for calculations (color mapping, reference lines)
        original_df = df.copy()
        
        # Apply unit conversion if weight fraction is selected (only for display)
        if unit_type == 'weight':
            element_cols = [col for col in df.columns if col in ATOMIC_WEIGHTS]
            if element_cols:
                df = convert_atomic_to_weight_fraction(df, element_cols)
        
        return jsonify({
            'success': True,
            'data': df.to_dict('records'),
            'originalData': original_df.to_dict('records')
        })
    
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@her_plot_bp.route('/export_csv', methods=['POST'])
def export_csv():
    """Export HER data as CSV"""
    try:
        # Get current data and calculate PCA
        current_df = load_original_data()
        df_with_pca = calculate_pca_components(current_df)
        
        # Add voltage conversion columns if voltage data exists
        if 'voltage' in df_with_pca.columns or 'voltage_mean' in df_with_pca.columns:
            # Determine which voltage column to use
            voltage_col = 'voltage_mean' if 'voltage_mean' in df_with_pca.columns else 'voltage'
            # Get current density column
            current_density_col = 'current density' if 'current density' in df_with_pca.columns else None
            
            # Convert voltage values to SHE and RHE
            voltage_values = df_with_pca[voltage_col].values
            she_values = []
            rhe_values = []
            
            for idx, v in enumerate(voltage_values):
                if pd.notna(v):
                    # Get current density for this row, default to 100 mA/cm² if not available
                    current_density = df_with_pca[current_density_col].iloc[idx] if current_density_col and pd.notna(df_with_pca[current_density_col].iloc[idx]) else 100.0
                    ushe, urhe = fullcell2halfcell(v, current_density)
                    she_values.append(ushe)
                    rhe_values.append(urhe)
                else:
                    she_values.append(np.nan)
                    rhe_values.append(np.nan)
            
            # Add the new columns
            df_with_pca['V vs SHE'] = she_values
            df_with_pca['V vs RHE'] = rhe_values
        
        # Use the dataframe with PCA components and voltage conversions
        csv_data = df_with_pca.to_csv(index=False)
        
        # Create response with CSV data
        response = Response(
            csv_data,
            mimetype='text/csv',
            headers={'Content-Disposition': 'attachment; filename=HER_data.csv'}
        )
        
        return response
        
    except Exception as e:
        return jsonify({'success': False, 'error': str(e)}), 500

@her_plot_bp.route('/get_xrd_data', methods=['POST'])
def get_xrd_data():
    """Get XRD data for a specific sample ID"""
    try:
        data = request.get_json()
        sample_id = data.get('sample_id')
        data_type = data.get('data_type', 'raw')  # Default to raw
        
        if not sample_id:
            return jsonify({'success': False, 'error': 'Sample ID is required'}), 400
        
        print(f"DEBUG: Requesting XRD data for sample: {sample_id}, type: {data_type}")
        print(f"DEBUG: Flask working directory: {os.getcwd()}")
        
        # Load XRD data with specified type
        xrd_data = load_xrd_data(sample_id, data_type)
        
        if xrd_data is None:
            return jsonify({
                'success': False, 
                'error': f'No XRD data found for sample {sample_id} ({data_type})',
                'sample_id': sample_id,
                'data_type': data_type
            }), 404
        
        # Convert to format suitable for Plotly
        x_values = [point[0] for point in xrd_data]
        y_values = [point[1] for point in xrd_data]
        
        return jsonify({
            'success': True,
            'sample_id': sample_id,
            'data_type': data_type,
            'data': {
                'x': x_values,
                'y': y_values
            },
            'data_points': len(xrd_data)
        })
        
    except Exception as e:
        print(f"Error in get_xrd_data: {e}")
        return jsonify({'success': False, 'error': str(e)}), 500