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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
"""
Data tab.
"""

from __future__ import annotations

import streamlit as st
import pandas as pd
import numpy as np

from config import settings
from ui.bootstrap import _BRAND_GREEN, _HAS_PLOTLY, load_labels, load_metrics

if _HAS_PLOTLY:
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots



def render_tab_data() -> None:
    _data_section = st.radio(

        "Section",

        ["Farquhar Model", "Model Validation", "Data Explorer"],

        horizontal=True,

    )


    if _data_section == "Farquhar Model":

        st.header("How we measure vine photosynthesis")

        st.markdown(

            "Before we can predict photosynthesis, we need to **measure** it. "

            "On-site sensors record light, temperature, humidity, and CO2 every "

            "15 minutes. A well-established plant biology model (Farquhar et al., 1980) "

            "converts these readings into the photosynthesis rate **A** β€” "

            "how fast the vine is converting sunlight into sugar. "

            "This tab shows the results of that calculation."

        )

        with st.expander("How does the Farquhar model work?"):

            st.markdown(

                "Uses the **Farquhar et al. (1980)** mechanistic model with "

                "**Greer & Weedon (2012)** grapevine parameters to compute the net leaf "

                "photosynthesis rate **A** (\u00b5mol CO\u2082 m\u207b\u00b2 s\u207b\u00b9) from on-site sensor readings "

                "(PAR, leaf temperature, air temperature, CO\u2082, VPD). Only daytime rows "

                "(PAR > 50) during the growing season (May\u2013Sep) are used. "

                "Temperature dependencies use **Bernacchi et al. (2001)** kinetic constants.\n\n"

                "The core computation:\n\n"

                "1. **Rubisco-limited rate:**  Ac = Vcmax \u00b7 (ci \u2212 \u0393*) / (ci + Kc \u00b7 (1 + O\u1d62 / Ko))\n"

                "2. **RuBP-limited rate:**  Aj = J \u00b7 (ci \u2212 \u0393*) / (4\u00b7ci + 8\u00b7\u0393*)\n"

                "3. **Net assimilation:**  A = min(Ac, Aj) \u2212 Rd\n\n"

                "Where Vcmax and Jmax follow temperature-dependent Arrhenius curves "

                "(peak at 39\u00b0C and 36\u00b0C respectively for Semillon), J is solved from the "

                "light-response quadratic, and ci (intercellular CO\u2082) is derived from "

                "ambient CO\u2082 scaled by stomatal conductance (reduced by VPD and CWSI).\n\n"

                "#### How VPD and CWSI reduce photosynthesis\n\n"

                "Both VPD and CWSI act on photosynthesis through the same bottleneck: "

                "**stomatal conductance (gs)**. Stomata are the pores on the leaf surface "

                "that let CO\u2082 in for photosynthesis \u2014 but also let water vapor out.\n\n"

                "**VPD (Vapor Pressure Deficit)** measures how dry the air is. "

                "When VPD is high (hot, dry air), the leaf would lose water too fast, "

                "so the vine partially closes its stomata to conserve water. "

                "Less open stomata = less CO\u2082 enters the leaf = lower ci = lower A. "

                "The model applies an exponential decay: "

                "`gs_scale = exp(\u22120.3 \u00b7 max(0, VPD \u2212 1.0))`, "

                "so the effect kicks in above 1 kPa and intensifies with drier air.\n\n"

                "**CWSI (Crop Water Stress Index)** is computed from the leaf\u2013air "

                "temperature difference: `CWSI = (Tleaf \u2212 Tair \u2212 \u0394Tmin) / (\u0394Tmax \u2212 \u0394Tmin)`, "

                "clipped to [0, 1]. A well-watered vine transpires freely, keeping its "

                "leaves cooler than the air (CWSI \u2248 0). A stressed vine closes stomata, "

                "transpiration drops, and leaves heat up (CWSI \u2192 1). "

                "The model reduces stomatal conductance by `(1 \u2212 0.5 \u00b7 CWSI)`, "

                "so at full stress (CWSI = 1) stomatal opening is halved.\n\n"

                "Combined effect on ci:  "

                "`ci = CO\u2082 \u00b7 (1 \u2212 1 / (1.6 \u00b7 gs_factor))`  where  "

                "`gs_factor = 1.2 \u00b7 VPD_scale \u00b7 (1 \u2212 0.5 \u00b7 CWSI)`\n\n"

                "This means on a hot, dry Negev afternoon (VPD > 3 kPa, CWSI > 0.5), "

                "ci drops sharply and photosynthesis can fall even when light is abundant "

                "\u2014 the vine has plenty of energy but cannot get enough CO\u2082 through "

                "its closed stomata.\n\n"

                "**Key references:**\n"

                "- [Farquhar, von Caemmerer & Berry (1980)](https://doi.org/10.1007/BF00386231) \u2014 "

                "Biochemical model of photosynthetic CO\u2082 assimilation\n"

                "- [Greer & Weedon (2012)](https://doi.org/10.1111/j.1365-3040.2011.02471.x) \u2014 "

                "Modelling photosynthetic responses to temperature of grapevine\n"

                "- [Bernacchi et al. (2001)](https://doi.org/10.1046/j.1365-3040.2001.00668.x) \u2014 "

                "Temperature dependence of Kc, Ko, and \u0393*"

            )


        st.subheader("Sensor inputs")

        st.markdown("The Farquhar model uses **6 columns** from the Air1 reference station, "

                    "sampled every 15 minutes:")

        used_sensors_tab = pd.DataFrame([

            {"Column": "Air1_PAR_ref", "Measurement": "Photosynthetically Active Radiation", "Units": "\u00b5mol m\u207b\u00b2 s\u207b\u00b9", "Used in": "Farquhar: electron transport (J)", "Notes": "Primary light input driving photosynthesis rate"},

            {"Column": "Air1_leafTemperature_ref", "Measurement": "Leaf temperature", "Units": "\u00b0C", "Used in": "Farquhar: Vcmax, Jmax, Kc, Ko, \u0393*; CWSI", "Notes": "Controls enzyme kinetics; also used to compute CWSI"},

            {"Column": "Air1_airTemperature_ref", "Measurement": "Air temperature", "Units": "\u00b0C", "Used in": "CWSI computation", "Notes": "Tleaf \u2212 Tair drives the water stress index"},

            {"Column": "Air1_CO2_ref", "Measurement": "Ambient CO\u2082 concentration", "Units": "ppm", "Used in": "Farquhar: intercellular CO\u2082 (ci)", "Notes": "Substrate for carbon fixation by Rubisco (\u00d70.7 correction applied)"},

            {"Column": "Air1_VPD_ref", "Measurement": "Vapor Pressure Deficit", "Units": "kPa", "Used in": "Farquhar: stomatal conductance \u2192 ci", "Notes": "High VPD closes stomata, reducing ci and thus A"},

            {"Column": "Air1_airHumidity_ref", "Measurement": "Relative humidity", "Units": "%", "Used in": "Loaded but not consumed", "Notes": "VPD already encodes humidity; column is redundant"},

        ])

        st.dataframe(used_sensors_tab, hide_index=True)


        labels_path = settings.PROCESSED_DIR / "stage1_labels.csv"

        validation_img = settings.OUTPUTS_DIR / "stage1_validation.png"


        if labels_path.exists():

            df_labels = load_labels(str(labels_path))

            df_labels.index = pd.to_datetime(df_labels.index, utc=True)


            # Metrics row

            c1, c2, c3, c4 = st.columns(4)

            c1.metric("Observations", f"{len(df_labels):,}")

            c2.metric("Avg photosynthesis rate", f"{df_labels.iloc[:, 0].mean():.1f}",

                      help="Higher values mean the vine is growing faster. Typical range: 5-20.")

            c3.metric("Date from", df_labels.index.min().strftime("%Y-%m-%d"))

            c4.metric("Date to", df_labels.index.max().strftime("%Y-%m-%d"))


            with st.expander("What does this table show?"):

                st.markdown(

                    "Descriptive statistics (count, mean, std, min, quartiles, max) of the "

                    "computed photosynthesis rate **A**. Typical grapevine values are 0\u201325 "

                    "\u00b5mol CO\u2082 m\u207b\u00b2 s\u207b\u00b9. Values outside this range may indicate sensor issues."

                )

            st.dataframe(df_labels.describe())

            st.caption(

                "This table summarizes the photosynthesis measurements. The 'mean' row shows "

                "the average rate across all observations. Values between 5-20 are typical "

                "for healthy grapevines during the growing season."

            )


            st.download_button(

                "Download labels CSV",

                df_labels.to_csv(),

                file_name="stage1_labels.csv",

                mime="text/csv",

            )


            if validation_img.exists():

                with st.expander("How to read the validation plots"):

                    st.markdown(

                        "**Top panel \u2014 Diurnal pattern:** A vs. hour of day (UTC). Expect a bell curve "

                        "peaking mid-morning to early afternoon when light and temperature are optimal.\n\n"

                        "**Bottom panel \u2014 A vs PAR:** Photosynthesis rate plotted against Photosynthetically "

                        "Active Radiation. A should increase with PAR and saturate at high light levels, "

                        "forming a characteristic light-response curve."

                    )

                st.image(str(validation_img), width='stretch')

        else:

            st.info("No pre-computed photosynthesis labels found. Run `python scripts/run_pipeline.py` to generate them.")


    # ---------------------------------------------------------------------------


    if _data_section == "Model Validation":

        st.header("FvCB Model Validation")

        st.markdown(

            "Comparison of our Farquhar-von Caemmerer-Berry (FvCB) photosynthesis model "

            "against measured data from "

            "[Greer & Weedon (2012)](https://doi.org/10.1111/j.1365-3040.2011.02471.x) "

            "for field-grown *Vitis vinifera* cv. **Semillon** in a hot climate "

            "(Riverina, NSW, Australia)."

        )


        # --- Reference data from Greer & Weedon (2012) ---

        _val_temps = [20, 25, 30, 35, 40]


        # Fig 5a: Light-saturated Amax at ambient CO2 (389 ppm)

        _paper_amax = {20: 12.0, 25: 16.9, 30: 19.9, 35: 15.3, 40: 12.0}

        _paper_amax_se = {20: 1.5, 25: 1.3, 30: 1.8, 35: 1.2, 40: 1.5}


        # Fig 11: Vcmax and Jmax (from Arrhenius fit to A/Ci curves)

        _paper_vcmax = {20: 20, 25: 38.5, 30: 58, 35: 85, 40: 110}

        _paper_jmax = {20: 60, 25: 98.3, 30: 135, 35: 165, 40: 170}


        # Table 1: Stomatal limitation (%)

        _paper_stom_lim = {20: 13, 25: 20, 30: 25, 35: 31, 40: 34}


        from src.farquhar_model import FarquharModel as _FMVal

        _val_model = _FMVal()


        # ---- Section 1: Light Response Curves ----

        st.subheader("1. Photosynthetic light response at different temperatures")

        st.markdown(

            "Light response curves (A vs PFD) at five leaf temperatures. "

            "Model run at ambient CO$_2$ = 389 ppm, VPD = 1.5 kPa."

        )


        _val_vpd = st.slider(

            "VPD for model curves (kPa)", 0.5, 3.0, 1.5, 0.1,

            key="val_vpd"

        )

        _val_pfds = np.arange(0, 2100, 25)


        if _HAS_PLOTLY:

            _lr_fig = go.Figure()

            _temp_colors = {20: "#1f77b4", 25: "#2ca02c", 30: "#d62728",

                            35: "#9467bd", 40: "#ff7f0e"}

            for t in _val_temps:

                # Model curve

                _a_vals = [_val_model.calc_photosynthesis(

                    PAR=float(p), Tleaf=t, CO2=389, VPD=_val_vpd, Tair=t

                ) for p in _val_pfds]

                _lr_fig.add_trace(go.Scatter(

                    x=_val_pfds, y=_a_vals, mode="lines",

                    name=f"{t} Β°C (model)",

                    line=dict(color=_temp_colors[t]),

                ))

                # Paper reference point (Amax)

                _lr_fig.add_trace(go.Scatter(

                    x=[1800], y=[_paper_amax[t]],

                    mode="markers",

                    name=f"{t} Β°C (Greer & Weedon)",

                    marker=dict(color=_temp_colors[t], size=12, symbol="star",

                                line=dict(width=1, color="black")),

                    error_y=dict(type="data", array=[_paper_amax_se[t]], visible=True),

                    showlegend=True,

                ))

            _lr_fig.update_layout(

                xaxis_title="PFD [¡mol photons m⁻² s⁻¹]",

                yaxis_title="A [Β΅mol COβ‚‚ m⁻² s⁻¹]",

                height=500,

                legend=dict(font=dict(size=10)),

            )

            st.plotly_chart(_lr_fig)

        else:

            st.info("Install plotly for interactive charts.")


        # ---- Section 2: A comparison table ----

        st.subheader("2. Light-saturated A: Model vs Paper")

        _rows = []

        for t in _val_temps:

            a_model = _val_model.calc_photosynthesis(

                PAR=2000, Tleaf=t, CO2=389, VPD=_val_vpd, Tair=t

            )

            _rows.append({

                "T_leaf (Β°C)": t,

                "A_model": round(a_model, 1),

                f"A_paper (Greer & Weedon)": _paper_amax[t],

                "Difference (%)": round((a_model - _paper_amax[t]) / _paper_amax[t] * 100, 0),

                "Limitation": "RuBP regen." if t <= 30 else "Rubisco",

            })

        st.dataframe(pd.DataFrame(_rows), hide_index=True)


        # ---- Section 3: Vcmax / Jmax temperature response ----

        st.subheader("3. Vcmax and Jmax temperature response")

        st.markdown(

            "Temperature dependence of maximum carboxylation rate (Vcmax) and "

            "electron transport capacity (Jmax). Model uses modified Arrhenius "

            "(Medlyn et al. 2002) with Greer & Weedon (2012) activation/deactivation "

            "energies. Topt(Vcmax) = 39 Β°C, Topt(Jmax) = 36 Β°C."

        )


        _t_range = np.arange(15, 50, 0.5)

        _vcmax_curve = [_val_model.calc_Vcmax(t + 273.15) for t in _t_range]

        _jmax_curve = [_val_model.calc_Jmax(t + 273.15) for t in _t_range]


        # Scale paper's Cc-based Vcmax/Jmax to our Ci-based values for comparison

        _scale_v = _val_model.params["k25_vcmax"] / 38.5  # ratio Ci-based/Cc-based

        _scale_j = _val_model.params["k25_jmax"] / 98.3

        _paper_vcmax_scaled = {t: v * _scale_v for t, v in _paper_vcmax.items()}

        _paper_jmax_scaled = {t: j * _scale_j for t, j in _paper_jmax.items()}


        if _HAS_PLOTLY:

            _vj_fig = make_subplots(rows=1, cols=2,

                                    subplot_titles=("Vcmax", "Jmax"))

            _vj_fig.add_trace(go.Scatter(

                x=list(_t_range), y=_vcmax_curve, mode="lines",

                name="Vcmax (model)", line=dict(color="#d62728"),

            ), row=1, col=1)

            _vj_fig.add_trace(go.Scatter(

                x=list(_paper_vcmax_scaled.keys()),

                y=list(_paper_vcmax_scaled.values()),

                mode="markers", name="Vcmax (paper, scaled)",

                marker=dict(color="#d62728", size=10, symbol="star",

                            line=dict(width=1, color="black")),

            ), row=1, col=1)

            _vj_fig.add_trace(go.Scatter(

                x=list(_t_range), y=_jmax_curve, mode="lines",

                name="Jmax (model)", line=dict(color="#1f77b4"),

            ), row=1, col=2)

            _vj_fig.add_trace(go.Scatter(

                x=list(_paper_jmax_scaled.keys()),

                y=list(_paper_jmax_scaled.values()),

                mode="markers", name="Jmax (paper, scaled)",

                marker=dict(color="#1f77b4", size=10, symbol="star",

                            line=dict(width=1, color="black")),

            ), row=1, col=2)

            _vj_fig.update_xaxes(title_text="Leaf temperature (Β°C)")

            _vj_fig.update_yaxes(title_text="¡mol m⁻² s⁻¹")

            _vj_fig.update_layout(height=400)

            st.plotly_chart(_vj_fig)

        else:

            st.info("Install plotly for interactive charts.")


        # ---- Section 4: Limitation regime ----

        st.subheader("4. RuBP regeneration vs Rubisco carboxylation limitation")

        st.markdown(

            "The paper's key finding: **below 30 Β°C**, photosynthesis is limited by "

            "RuBP regeneration (electron transport / light reactions). "

            "**Above 30 Β°C**, Rubisco carboxylation becomes limiting due to "

            "declining CO$_2$ affinity and increased photorespiration.\n\n"

            "This 30 Β°C transition is critical for shading decisions:\n"

            "- **Below 30 Β°C**: shading reduces light and hurts photosynthesis (RuBP-limited)\n"

            "- **Above 30 Β°C**: shading may help by reducing heat stress on Rubisco"

        )


        if _HAS_PLOTLY:

            _ac_vals = []

            _aj_vals = []

            _t_lim = np.arange(15, 46, 0.5)

            for t in _t_lim:

                Tk = t + 273.15

                Vcmax = _val_model.calc_Vcmax(Tk)

                Jmax = _val_model.calc_Jmax(Tk)

                J = _val_model.calc_electron_transport(2000, Jmax)

                gamma = _val_model.calc_gamma_star(Tk)

                Kc = _val_model.calc_Kc(Tk)

                Ko = _val_model.calc_Ko(Tk)

                ci = _val_model._ci_from_ca(389, 1.5, 0.0)

                Ac = Vcmax * (ci - gamma) / (ci + Kc * (1 + 210.0 / Ko))

                Aj = J * (ci - gamma) / (4 * ci + 8 * gamma)

                Rd = 0.015 * Vcmax

                _ac_vals.append(Ac - Rd)

                _aj_vals.append(Aj - Rd)


            _lim_fig = go.Figure()

            _lim_fig.add_trace(go.Scatter(

                x=list(_t_lim), y=_ac_vals, mode="lines",

                name="Ac (Rubisco-limited)",

                line=dict(color="#d62728", dash="dash"),

            ))

            _lim_fig.add_trace(go.Scatter(

                x=list(_t_lim), y=_aj_vals, mode="lines",

                name="Aj (RuBP-limited)",

                line=dict(color="#1f77b4", dash="dash"),

            ))

            # Actual A = min(Ac, Aj)

            _a_net = [max(0, min(ac, aj)) for ac, aj in zip(_ac_vals, _aj_vals)]

            _lim_fig.add_trace(go.Scatter(

                x=list(_t_lim), y=_a_net, mode="lines",

                name="A_net = min(Ac, Aj)",

                line=dict(color="black", width=3),

            ))

            _lim_fig.add_vline(x=30, line_dash="dot", line_color="gray",

                               annotation_text="30 Β°C transition")

            _lim_fig.update_layout(

                xaxis_title="Leaf temperature (Β°C)",

                yaxis_title="A [Β΅mol COβ‚‚ m⁻² s⁻¹]",

                height=450,

            )

            st.plotly_chart(_lim_fig)


        # ---- Section 5: Key findings ----

        st.subheader("5. Key findings from validation")

        st.markdown("""

    **Agreement with Greer & Weedon (2012):**

    - Temperature ranking of Amax matches: 30 Β°C > 25 Β°C > 35 Β°C > 20 Β°C > 40 Β°C

    - RuBP/Rubisco limitation transition occurs at ~30-32 Β°C (paper: 30 Β°C)

    - Vcmax peaks at 39 Β°C, Jmax peaks at 36 Β°C (exact match with paper)

    - Quantitative match within 2-15% at 20-35 Β°C

    - Jmax/Vcmax ratio declines from ~2.4 at 20 Β°C to ~1.1 at 45 Β°C (paper: 3.0 to 1.5)


    **Known limitations:**

    - At 40 Β°C, model underestimates A by ~12% due to Bernacchi (2001) Rubisco kinetics

      being parameterised for tobacco, not heat-adapted grapevine

    - Stomatal response to temperature is modelled via VPD only; the paper shows

      direct temperature effects on gs (Table 1: 0.199 at 20 Β°C to 0.140 at 40 Β°C)

    - The model uses Ci-based (intercellular CO2) calculations; the paper uses

      Cc-based (chloroplast CO2) with mesophyll conductance gm = 5-10 ¡mol m⁻² s⁻¹ Pa⁻¹


    **Reference:**

    Greer, D.H. & Weedon, M.M. (2012) Modelling photosynthetic responses to temperature

    of grapevine (*Vitis vinifera* cv. Semillon) leaves on vines grown in a hot climate.

    *Plant, Cell & Environment*, 35, 1050-1064.

    [DOI: 10.1111/j.1365-3040.2011.02471.x](https://doi.org/10.1111/j.1365-3040.2011.02471.x)

    """)


    # ---------------------------------------------------------------------------


    if _data_section == "Data Explorer":

        st.header("Data Explorer")

        st.markdown(

            "This tab lets you explore the **raw data** behind the predictions. "

            "Choose a data source below:\n\n"

            "- **Vineyard sensors** \u2014 Photosynthesis rate **A**, PAR, leaf/air temperature, and \u0394T from on-site crop sensors.\n"

            "- **Weather station data** \u2014 IMS station 43 (Sde Boker) and the merged dataset used for ML training.\n"

            "- **AI Data Engineering** \u2014 **Gemini-powered** sensor anomaly detection (Z-score/IQR + physical bounds) and engineered features "

            "(cyclical time encodings, Stress Risk Score). Run the pipeline and inspect thresholds, cleaning summary, and the daytime stress profile."

        )

        eda_stage = st.radio(

            "Data source",

            ["Vineyard sensors", "Weather station data", "AI Data Engineering"],

            horizontal=True,

            label_visibility="visible",

        )


        if eda_stage == "Vineyard sensors":

            st.subheader("Vineyard sensor data")

            with st.expander("About this data"):

                st.markdown(

                    "Shows the distribution and temporal patterns of the computed photosynthesis "

                    "rate **A**, plus the raw sensor inputs used to calculate it. "

                    "This helps verify that the model produces physiologically plausible values."

                )

            try:

                from scripts.eda import get_stage1_eda

                s1 = get_stage1_eda()

            except Exception as e:

                st.error(str(e))

                s1 = {"error": str(e)}


            if s1.get("error"):

                st.warning(s1["error"])

            else:

                stats = s1["labels_stats"]

                c1, c2, c3, c4 = st.columns(4)

                c1.metric("Observations", stats["count"])

                c2.metric("Mean A", f"{stats['A_mean']:.2f}")

                c3.metric("Std A", f"{stats['A_std']:.2f}")

                c4.metric("Range", f"{stats['A_min']:.1f} \u2013 {stats['A_max']:.1f}")

                st.caption(f"Date range: {stats['date_min']} to {stats['date_max']}")


                if _HAS_PLOTLY and s1.get("labels") is not None:

                    A = s1["labels"].iloc[:, 0]

                    with st.expander("About: Distribution of A"):

                        st.markdown(

                            "Histogram of all computed A values. A right-skewed distribution is typical: "

                            "many low-A values (early/late day, cloudy) with a tail of high-A values "

                            "(midday, full sun). The peak should be between 5\u201315 \u00b5mol m\u207b\u00b2 s\u207b\u00b9 for grapevines."

                        )

                    fig = px.histogram(x=A[A >= 1].dropna(), nbins=50, title="Distribution of A (Stage 1 labels, A \u2265 1)")

                    fig.update_layout(xaxis_title="A (\u00b5mol m\u207b\u00b2 s\u207b\u00b9)", xaxis_range=[1, None])

                    st.plotly_chart(fig)


                    with st.expander("About: A over time"):

                        st.markdown(

                            "Time series of A across the dataset. Only the **growing season** "

                            "(May\u2013Sep) is included \u2014 the gaps between clusters represent the "

                            "dormant months (Oct\u2013Apr) when the vine does not photosynthesize "

                            "and no data is collected. Within each season, look for diurnal "

                            "oscillations and any anomalous spikes that may indicate sensor issues."

                        )

                    # Resample to daily mean to compress gaps and smooth diurnal noise

                    A_daily = A.resample("D").mean().dropna()

                    fig2 = go.Figure()

                    fig2.add_trace(go.Scatter(

                        x=A_daily.index, y=A_daily.values, mode="lines",

                        name="A (daily mean)", line=dict(width=1.5, color=_BRAND_GREEN),

                        connectgaps=False,

                    ))

                    fig2.update_layout(

                        title="A over time (daily mean)",

                        xaxis_title="Time", yaxis_title="A (\u00b5mol m\u207b\u00b2 s\u207b\u00b9)",

                    )

                    st.plotly_chart(fig2)


                if s1.get("sensor_sample") is not None and not s1["sensor_sample"].empty and _HAS_PLOTLY:

                    df = s1["sensor_sample"]

                    st.subheader("Sensor distributions (daytime PAR > 50)")

                    with st.expander("About sensor distributions"):

                        st.markdown(

                            "Histograms of the main sensor inputs used in the Farquhar model, filtered "

                            "to daytime only (PAR > 50 \u00b5mol m\u207b\u00b2 s\u207b\u00b9).\n\n"

                            "- **PAR:** Light energy for photosynthesis (400\u2013700 nm). "

                            "Values above 2500 are sensor artifacts and are excluded.\n"

                            "- **Leaf Temp:** Leaf surface temperature (\u00b0C).\n"

                            "- **Air Temp:** Ambient temperature near the canopy (\u00b0C)."

                        )

                    sensor_cols = [c for c in ["Air1_PAR_ref", "Air1_leafTemperature_ref", "Air1_airTemperature_ref"] if c in df.columns]

                    if sensor_cols:

                        cols = st.columns(len(sensor_cols))

                        for col_st, col_name in zip(cols, sensor_cols):

                            with col_st:

                                series = df[col_name].dropna()

                                # Remove PAR outliers (sensor artifacts above 2500)

                                if col_name == "Air1_PAR_ref":

                                    series = series[series <= 2500]

                                fig = px.histogram(series, nbins=40, title=col_name.replace("Air1_", "").replace("_ref", ""))

                                fig.update_layout(height=300)

                                st.plotly_chart(fig)


                    # --- Air-Leaf Temperature Delta ---

                    if "Air1_leafTemperature_ref" in df.columns and "Air1_airTemperature_ref" in df.columns:

                        st.subheader("Leaf\u2013Air temperature difference (\u0394T)")

                        with st.expander("Why is \u0394T important?"):

                            st.markdown(

                                "The difference between leaf and air temperature "

                                "(**\u0394T = T_leaf \u2212 T_air**) is a direct indicator of "

                                "**plant water stress**.\n\n"

                                "- **\u0394T < 0** (leaf cooler than air): the vine is transpiring "

                                "normally \u2014 evaporative cooling keeps the leaf below air "

                                "temperature. The stomata are open and photosynthesis is active.\n"

                                "- **\u0394T \u2248 0**: transpiration is slowing down.\n"

                                "- **\u0394T > 0** (leaf warmer than air): the vine has partially "

                                "or fully closed its stomata due to water stress or extreme VPD. "

                                "Transpiration has stopped cooling the leaf, so it heats up "

                                "above ambient. Photosynthesis is severely limited.\n\n"

                                "This is the basis of the **Crop Water Stress Index (CWSI)** "

                                "used in the Farquhar model. In agrivoltaics, a rising \u0394T is "

                                "the signal that the vine would benefit from tracker shading: "

                                "the extra light cannot be used anyway because the stomata "

                                "are shut."

                            )

                        delta_t = df["Air1_leafTemperature_ref"] - df["Air1_airTemperature_ref"]

                        delta_t = delta_t.dropna()

                        col_hist, col_time = st.columns(2)

                        with col_hist:

                            fig_dt = px.histogram(

                                delta_t, nbins=50,

                                title="\u0394T distribution (daytime)",

                                color_discrete_sequence=[_BRAND_GREEN],

                            )

                            fig_dt.update_layout(

                                xaxis_title="\u0394T = T_leaf \u2212 T_air (\u00b0C)",

                                yaxis_title="Count",

                                height=350,

                            )

                            fig_dt.add_vline(x=0, line_dash="dash", line_color="red",

                                             annotation_text="T_leaf = T_air")

                            st.plotly_chart(fig_dt)

                        with col_time:

                            if "time" in df.columns:

                                # Filter to growing season only (May-Sep)

                                _ts = pd.to_datetime(df["time"], utc=True)

                                _grow_mask = _ts.dt.month.isin([5, 6, 7, 8, 9])

                                _dt_grow = delta_t[_grow_mask]

                                _ts_grow = _ts[_grow_mask]

                                fig_dt2 = go.Figure()

                                fig_dt2.add_trace(go.Scatter(

                                    x=_ts_grow, y=_dt_grow.values,

                                    mode="markers", marker=dict(size=2, color=_BRAND_GREEN, opacity=0.4),

                                    name="\u0394T",

                                ))

                                fig_dt2.add_hline(y=0, line_dash="dash", line_color="red")

                                fig_dt2.update_layout(

                                    title="\u0394T over time",

                                    xaxis_title="Time",

                                    yaxis_title="\u0394T (\u00b0C)",

                                    height=350,

                                )

                                st.plotly_chart(fig_dt2)


        elif eda_stage == "Weather station data":

            st.subheader("Weather station data")

            with st.expander("About this data"):

                st.markdown(

                    "Shows the IMS weather station data and the merged dataset used for "

                    "prediction model training. This helps verify data overlap, check for "

                    "missing values, and understand the weather patterns."

                )

            try:

                from scripts.eda import get_stage2_eda

                s2 = get_stage2_eda()

            except Exception as e:

                st.error(str(e))

                s2 = {"error": str(e)}


            if s2.get("error"):

                st.warning(s2["error"])

            else:

                stats = s2["stats"]

                c1, c2, c3 = st.columns(3)

                c1.metric("IMS rows", f"{stats['ims_rows']:,}")

                c2.metric("Merged rows", f"{stats['merged_rows']:,}")

                c3.metric("Features", len(stats["feature_cols"]))

                st.caption(f"IMS range: {stats['ims_date_min']} to {stats['ims_date_max']}")


                with st.expander("What are the feature columns?"):

                    st.markdown(

                        "IMS weather variables and engineered time features used as "

                        "ML inputs. No on-site sensor data is included (strict separation to avoid leakage).\n\n"

                        "- **air_temperature_c, tdmax_c, tdmin_c:** Temperature from IMS station.\n"

                        "- **ghi_w_m2:** Global Horizontal Irradiance (solar radiation) \u2014 proxy for PAR.\n"

                        "- **rh_percent:** Relative humidity.\n"

                        "- **rain_mm:** Precipitation.\n"

                        "- **wind_speed_ms:** Wind speed.\n"

                        "- **hour_sin, hour_cos:** Cyclical encoding of hour-of-day.\n"

                        "- **doy_sin, doy_cos:** Cyclical encoding of day-of-year (seasonality)."

                    )

                merged = s2["merged"]

                st.dataframe(merged.describe())


                if _HAS_PLOTLY and "A" in merged.columns:

                    with st.expander("About: Distribution of A (merged)"):

                        st.markdown(

                            "Distribution of A in the merged IMS+labels dataset. This is the subset "

                            "of Stage 1 labels that have matching IMS timestamps. Compare with Stage 1 "

                            "distribution to check for sampling bias."

                        )

                    fig = px.histogram(merged["A"][merged["A"] >= 1].dropna(), nbins=50, title="Distribution of A (merged set, A \u2265 1)")

                    fig.update_layout(xaxis_title="A (\u00b5mol m\u207b\u00b2 s\u207b\u00b9)", xaxis_range=[1, None])

                    st.plotly_chart(fig)


                if _HAS_PLOTLY and merged is not None:

                    num_cols = [c for c in stats["feature_cols"] if c in merged.columns][:4]

                    if num_cols:

                        with st.expander("About: Feature distributions"):

                            st.markdown(

                                "Histograms of the first four numeric IMS features in the merged dataset. "

                                "Check for: reasonable value ranges, skewness, outliers, and missing-value "

                                "patterns that might affect model training."

                            )

                        fig = make_subplots(rows=2, cols=2, subplot_titles=num_cols)

                        for i, col in enumerate(num_cols):

                            r, c = i // 2 + 1, i % 2 + 1

                            fig.add_trace(go.Histogram(x=merged[col].dropna(), nbinsx=30), row=r, col=c)

                        fig.update_layout(title="Feature distributions (merged)")

                        st.plotly_chart(fig)


        else:

            # ── AI Data Engineering ──────────────────────────────────────────────

            st.subheader("AI Data Engineering")

            st.markdown(

                "Gemini analyzes each sensor column's statistics against known physical constraints "

                "for grapevines in the Negev desert, then returns per-column anomaly thresholds "

                "(hard bounds + Z-score + IQR multiplier). The pipeline also generates five "

                "engineered features fed directly into the ML prediction models."

            )


            _llm_err = None

            try:

                from src.llm_data_engineer import LLMDataEngineer, SENSOR_CONTEXT


                _loader_path = settings.SENSORS_WIDE_SAMPLE_PATH

                if not _loader_path.exists():

                    _loader_path = settings.SENSORS_WIDE_PATH

                if not _loader_path.exists():

                    raise FileNotFoundError("Sensor data file not found.")


                _df_raw = pd.read_csv(_loader_path)

                _engineer = LLMDataEngineer(verbose=False)

                _key_cols = list(SENSOR_CONTEXT.keys())

                _present = [c for c in _key_cols if c in _df_raw.columns]


                with st.spinner("Querying Gemini for anomaly thresholds…"):

                    _thresholds = _engineer.analyze_anomalies(_df_raw, columns=_present)


                _df_clean = _engineer.apply_cleaning(_df_raw, _thresholds, strategy="clip")


                with st.spinner("Querying Gemini for feature engineering spec…"):

                    _feat_spec = _engineer.get_feature_spec(list(_df_clean.columns))


                _df_eng = _engineer.engineer_features(_df_clean, feature_spec=_feat_spec)


                # Violation counts

                _viol_before, _viol_after = {}, {}

                for _col, _t in _thresholds.items():

                    if _col not in _df_raw.columns:

                        continue

                    _lo, _hi = _t.get("lower_bound"), _t.get("upper_bound")

                    _m = pd.Series(False, index=_df_raw.index)

                    if _lo is not None:

                        _m |= _df_raw[_col] < _lo

                    if _hi is not None:

                        _m |= _df_raw[_col] > _hi

                    _viol_before[_col] = int(_m.sum())

                    _m2 = pd.Series(False, index=_df_clean.index)

                    if _lo is not None:

                        _m2 |= _df_clean[_col] < _lo

                    if _hi is not None:

                        _m2 |= _df_clean[_col] > _hi

                    _viol_after[_col] = int(_m2.sum())


                # Stress profile

                _df_eng["_hr_local"] = (pd.to_datetime(_df_eng["time"], utc=True).dt.hour + 3) % 24

                _daytime = _df_eng[_df_eng["Air1_PAR_ref"] > 50] if "Air1_PAR_ref" in _df_eng.columns else _df_eng

                _stress_profile = (

                    _daytime.groupby("_hr_local")["stress_risk_score"].mean()

                    .reindex(range(24), fill_value=float("nan"))

                )


                _used_gemini = "Statistical fallback" not in list(_thresholds.values())[0].get("rationale", "")


            except Exception as _exc:

                _llm_err = str(_exc)


            if _llm_err:

                st.error(f"Pipeline error: {_llm_err}")

            else:

                _source_badge = (

                    "πŸ€– Thresholds sourced from **Gemini**"

                    if _used_gemini

                    else "βš™οΈ Thresholds from **statistical fallback** (set `GOOGLE_API_KEY` to enable Gemini)"

                )

                st.caption(_source_badge)


                # ── Section 1: Anomaly thresholds ──────────────────────────────

                st.markdown("#### Gemini anomaly thresholds")

                with st.expander("How are thresholds generated?"):

                    st.markdown(

                        "For each sensor column, `LLMDataEngineer` sends the full descriptive "

                        "statistics (min, max, percentiles) plus domain context β€” physical units, "

                        "expected range for the Negev site, known failure modes β€” to Gemini. "

                        "Gemini returns a JSON with:\n\n"

                        "- **Hard bounds** (`lower_bound` / `upper_bound`): values outside these are "

                        "physically impossible or known sensor faults.\n"

                        "- **Z-score threshold**: flags readings that deviate more than N standard "

                        "deviations from the column mean.\n"

                        "- **IQR multiplier**: flags readings outside Q1 βˆ’ kΒ·IQR … Q3 + kΒ·IQR.\n\n"

                        "A reading is flagged only when the hard-bound violation OR both the Z-score "

                        "AND IQR conditions are met simultaneously. Default strategy is **clip** "

                        "(clamp to bounds), preserving row count."

                    )


                _thresh_rows = []

                _col_labels = {

                    "Air1_PAR_ref": "PAR (ΞΌmol/mΒ²/s)",

                    "Air1_leafTemperature_ref": "T_leaf (Β°C)",

                    "Air1_airTemperature_ref": "T_air (Β°C)",

                    "Air1_VPD_ref": "VPD (kPa)",

                    "Air1_airHumidity_ref": "Humidity (%)",

                    "Air1_CO2_ref": "COβ‚‚ raw (ppm)",

                }

                for _col in _present:

                    _t = _thresholds.get(_col, {})

                    _vb = _viol_before.get(_col, 0)

                    _va = _viol_after.get(_col, 0)

                    _thresh_rows.append({

                        "Sensor": _col_labels.get(_col, _col),

                        "Lower bound": _t.get("lower_bound", "β€”"),

                        "Upper bound": _t.get("upper_bound", "β€”"),

                        "Z-score Οƒ": _t.get("zscore_threshold", "β€”"),

                        "IQR Γ—": _t.get("iqr_multiplier", "β€”"),

                        "Violations (raw)": _vb,

                        "After clip": _va,

                        "Rationale": _t.get("rationale", ""),

                    })

                _thresh_df = pd.DataFrame(_thresh_rows)

                st.dataframe(_thresh_df, hide_index=True)


                # ── Section 2: Cleaning summary metrics ────────────────────────

                st.markdown("#### Cleaning summary")

                _total_viol = sum(_viol_before.values())

                _total_cleared = sum(v for v in _viol_after.values() if v == 0)

                _pct_retained = len(_df_clean) / len(_df_raw) * 100

                _cm1, _cm2, _cm3, _cm4 = st.columns(4)

                _cm1.metric("Rows in dataset", f"{len(_df_raw):,}")

                _cm2.metric("Physical violations found", str(_total_viol))

                _cm3.metric("Columns fully cleared", f"{_total_cleared} / {len(_viol_before)}")

                _cm4.metric("Rows retained (clip)", f"{_pct_retained:.1f}%")


                if _HAS_PLOTLY and _total_viol > 0:

                    _viol_cols = [_col_labels.get(c, c) for c in _viol_before if _viol_before[c] > 0]

                    _viol_vals = [_viol_before[c] for c in _viol_before if _viol_before[c] > 0]

                    _fig_viol = px.bar(

                        x=_viol_cols, y=_viol_vals,

                        labels={"x": "Sensor", "y": "Violation count"},

                        title="Physical violations by sensor (before cleaning)",

                        color_discrete_sequence=[_BRAND_GREEN],

                    )

                    _fig_viol.update_layout(height=300)

                    st.plotly_chart(_fig_viol)


                # ── Section 3: Before / after distributions ────────────────────

                if _HAS_PLOTLY:

                    st.markdown("#### Before vs after cleaning β€” PAR & VPD")

                    with st.expander("What to look for"):

                        st.markdown(

                            "The **raw** histogram (red) includes all sensor readings. "

                            "The **cleaned** histogram (green) shows the same column after "

                            "the Gemini-generated thresholds are applied. Outlier spikes at the "

                            "far right of PAR and VPD should disappear or be clipped to the bound."

                        )

                    _ba_cols = st.columns(2)

                    for _idx, _col in enumerate(["Air1_PAR_ref", "Air1_VPD_ref"]):

                        if _col not in _df_raw.columns:

                            continue

                        _label = _col_labels.get(_col, _col)

                        _hi_bound = _thresholds.get(_col, {}).get("upper_bound")

                        _raw_s = _df_raw[_col].dropna()

                        _clean_s = _df_clean[_col].dropna()

                        _fig_ba = go.Figure()

                        _fig_ba.add_trace(go.Histogram(

                            x=_raw_s, nbinsx=60, name="Raw",

                            marker_color="crimson", opacity=0.55,

                        ))

                        _fig_ba.add_trace(go.Histogram(

                            x=_clean_s, nbinsx=60, name="Cleaned",

                            marker_color=_BRAND_GREEN, opacity=0.7,

                        ))

                        if _hi_bound is not None:

                            _fig_ba.add_vline(

                                x=_hi_bound, line_dash="dash", line_color="orange",

                                annotation_text=f"bound={_hi_bound}",

                            )

                        _fig_ba.update_layout(

                            barmode="overlay", title=f"{_label} β€” raw vs cleaned",

                            xaxis_title=_label, height=320,

                        )

                        with _ba_cols[_idx]:

                            st.plotly_chart(_fig_ba)


                # ── Section 4: Engineered features ─────────────────────────────

                st.markdown("#### Engineered features")

                with st.expander("How are features engineered?"):

                    st.markdown(

                        "After cleaning, the pipeline asks Gemini to confirm the optimal weights "

                        "and normalisation bounds for the **Stress Risk Score**, given the available "

                        "sensor columns and the Semillon grapevine stress physiology. "

                        "It then computes five new columns:\n\n"

                        "| Feature | Formula | Purpose |\n"

                        "|---|---|---|\n"

                        "| `hour_sin` | sin(2π·h/24) | Cyclical hour-of-day |\n"

                        "| `hour_cos` | cos(2π·h/24) | Cyclical hour-of-day |\n"

                        "| `doy_sin` | sin(2π·d/365) | Seasonal position |\n"

                        "| `doy_cos` | cos(2π·d/365) | Seasonal position |\n"

                        "| `stress_risk_score` | w_VPDΒ·norm(VPD) + w_CWSIΒ·norm(CWSI) | Acute stress in [0, 1] |\n\n"

                        "Cyclical encodings ensure that midnight→01:00 and 23:00→midnight are "

                        "treated as equally close by the model β€” something a raw hour integer cannot do."

                    )


                # Feature spec card

                _fs_vpd_w = _feat_spec.get("vpd_weight", "β€”")

                _fs_cwsi_w = _feat_spec.get("cwsi_weight", "β€”")

                _fs_vpd_clip = _feat_spec.get("vpd_clip_max", "β€”")

                _fs_rat = _feat_spec.get("rationale", "")

                _fc1, _fc2, _fc3 = st.columns(3)

                _fc1.metric("VPD weight", _fs_vpd_w)

                _fc2.metric("CWSI weight", _fs_cwsi_w)

                _fc3.metric("VPD clip max (kPa)", _fs_vpd_clip)

                st.caption(f"Gemini rationale: {_fs_rat}")


                # Feature stats table

                _eng_feat_cols = ["hour_sin", "hour_cos", "doy_sin", "doy_cos", "stress_risk_score"]

                _feat_stats = (

                    _df_eng[[c for c in _eng_feat_cols if c in _df_eng.columns]]

                    .describe(percentiles=[0.25, 0.5, 0.75])

                    .loc[["min", "mean", "max"]]

                    .round(4)

                )

                st.dataframe(_feat_stats)


                # ── Section 5: Daytime stress profile ──────────────────────────

                if _HAS_PLOTLY and "stress_risk_score" in _df_eng.columns:

                    st.markdown("#### Daytime stress profile")

                    with st.expander("How to read this chart"):

                        st.markdown(

                            "Mean **Stress Risk Score** per local hour (Israel = UTC+3), "

                            "computed over all daytime readings (PAR > 50 μmol m⁻² s⁻¹). "

                            "A score of 1.0 means the vine is under maximum atmospheric demand; "

                            "0.0 means no stress. The midday–afternoon peak is the primary "

                            "window where SolarWine shading interventions are concentrated."

                        )

                    _profile_df = _stress_profile.dropna().reset_index()

                    _profile_df.columns = ["Hour (local)", "Stress Risk Score"]

                    _peak_hr = int(_profile_df.loc[_profile_df["Stress Risk Score"].idxmax(), "Hour (local)"])

                    _fig_stress = px.bar(

                        _profile_df,

                        x="Hour (local)", y="Stress Risk Score",

                        color="Stress Risk Score",

                        color_continuous_scale=["#00BD3E", "#f5c518", "#e63946"],

                        range_y=[0, 1],

                        title=f"Hourly stress profile β€” peak at {_peak_hr:02d}:00 local",

                    )

                    _fig_stress.add_hline(

                        y=0.5, line_dash="dot", line_color="orange",

                        annotation_text="Intervention threshold (0.5)",

                    )

                    _fig_stress.update_layout(

                        xaxis=dict(tickmode="linear", dtick=1),

                        coloraxis_showscale=False,

                        height=380,

                    )

                    st.plotly_chart(_fig_stress)

                    st.caption(

                        f"Peak stress: {_peak_hr:02d}:00 local "

                        f"(score = {_stress_profile.max():.3f}). "

                        f"Low-stress morning window (before 10:00): "

                        f"mean score = {_stress_profile.loc[6:9].mean():.3f} β€” shading withheld."

                    )


    # ---------------------------------------------------------------------------

    # ---------------------------------------------------------------------------
    # Tab β€” Shading Simulator (was Panel vs Open Sky)
    # ---------------------------------------------------------------------------