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
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
from __future__ import annotations

import hashlib
import os
from collections import OrderedDict
from pathlib import Path
from typing import Any, Sequence

import numpy as np
import pandas as pd
import rasterio
from rasterio.windows import Window
import torch
from torch.utils.data import Dataset
from tqdm import tqdm
import xarray as xr
import yaml
import zarr

from .grid_utils import (
    MISSING_TEXT_VALUES,
    _GridParams,
    _build_land_mask_patch_table,
    _center_lon_deg,
    _deep_update_config,
    _force_include_cache_hash,
    _normalize_lon,
    _parse_date_int,
    _parse_force_include_regions,
    _path_cache_hash,
    _sanitize_cache_text,
    _validate_grid_params,
)
from .normalizations import (
    CELSIUS_TO_KELVIN_OFFSET,
    salinity_normalize,
    temperature_normalize,
)

VALID_CODE_MAX = 254.0
NODATA_CODE = 255
DEFAULT_ACCEPTED_ARGO_QC_FLAGS = (1, 2)
ARGO_LEVEL_QC_VARS = {
    "depth": "argo_depth_qc_on_glorys_depth",
    "temp": "argo_temp_qc_on_glorys_depth",
    "psal": "argo_psal_qc_on_glorys_depth",
}
ARGO_PROFILE_QC_VARS = {
    "juld": "argo_juld_qc",
    "position": "argo_position_qc",
    "profile_depth": "argo_profile_depth_qc",
    "profile_potm": "argo_profile_potm_qc",
    "profile_psal": "argo_profile_psal_qc",
}
COMPACT_PROFILE_QC_VARS = {
    "temp": "argo_temp_profile_qc",
    "psal": "argo_psal_profile_qc",
}


def _decode_stretched_uint8(values: np.ndarray, stretch: dict[str, Any]) -> np.ndarray:
    """Decode uint8 GeoTIFF values into physical units from manifest metadata."""
    arr = np.asarray(values, dtype=np.uint8)
    nodata = int(stretch.get("nodata", NODATA_CODE))
    valid_code_max = float(stretch.get("valid_code_max", VALID_CODE_MAX))
    minimum = np.float32(stretch["minimum"])
    maximum = np.float32(stretch["maximum"])
    out = np.full(arr.shape, np.nan, dtype=np.float32)
    valid = arr != nodata
    out[valid] = minimum + (
        arr[valid].astype(np.float32)
        / np.float32(valid_code_max)
        * np.float32(maximum - minimum)
    )
    return out


def _kelvin_to_celsius(values: np.ndarray) -> np.ndarray:
    """Convert decoded Kelvin temperature values to Celsius for model normalization."""
    return np.asarray(values, dtype=np.float32) - np.float32(CELSIUS_TO_KELVIN_OFFSET)


def _resolve_manifest_path(root_dir: Path, raw_path: str | Path) -> Path:
    """Resolve a manifest path that may be absolute or export-root relative."""
    path = Path(raw_path)
    if path.is_absolute():
        return path
    return root_dir / path


def _resolve_land_mask_path(root_dir: Path, raw_path: str | Path) -> Path:
    """Resolve a land-mask path inside the packaged GeoTIFF dataset root."""
    export_path = _resolve_manifest_path(root_dir, raw_path)
    if not export_path.exists():
        raise FileNotFoundError(
            "Land-mask GeoTIFF must be present in the packaged dataset layout: "
            f"{export_path}"
        )
    return export_path


def _records_by_date(
    entries: Sequence[dict[str, Any]], root_dir: Path
) -> dict[int, Path]:
    """Map manifest raster entries by date."""
    records: dict[int, Path] = {}
    for entry in entries:
        records[int(entry["date"])] = _resolve_manifest_path(root_dir, entry["path"])
    return records


def _date_signature(dates: Sequence[int]) -> str:
    """Return a compact hashable date coverage signature."""
    if not dates:
        return "empty"
    raw = (int(min(dates)), int(max(dates)), int(len(dates)))
    return "-".join(str(value) for value in raw)


class RasterDatasetCache:
    """Small LRU cache for rasterio datasets opened by one worker process."""

    def __init__(self, max_open: int = 8) -> None:
        """Initialize a bounded raster path cache."""
        self.max_open = int(max_open)
        self._pid = os.getpid()
        self._items: OrderedDict[Path, rasterio.io.DatasetReader] = OrderedDict()

    def _ensure_current_process(self) -> None:
        """Drop inherited file handles after DataLoader worker forks."""
        pid = os.getpid()
        if pid == self._pid:
            return
        self.close()
        self._pid = pid

    def get(self, path: Path) -> rasterio.io.DatasetReader:
        """Return an opened raster dataset for ``path``."""
        self._ensure_current_process()
        path = Path(path)
        if path in self._items:
            src = self._items.pop(path)
            self._items[path] = src
            return src
        src = rasterio.open(path)
        self._items[path] = src
        while len(self._items) > self.max_open:
            _, old = self._items.popitem(last=False)
            old.close()
        return src

    def close(self) -> None:
        """Close all cached raster datasets."""
        for src in self._items.values():
            src.close()
        self._items.clear()


class GeoTIFFRasterStore:
    """Date-indexed GeoTIFF raster source for one exported variable."""

    def __init__(
        self,
        *,
        paths_by_date: dict[int, Path],
        stretch: dict[str, Any],
        cache: RasterDatasetCache,
        kelvin_temperature: bool,
    ) -> None:
        """Initialize a date-to-raster lookup."""
        self.paths_by_date = dict(paths_by_date)
        self.stretch = dict(stretch)
        self.cache = cache
        self.kelvin_temperature = bool(kelvin_temperature)

    @property
    def dates(self) -> set[int]:
        """Return available YYYYMMDD dates."""
        return set(int(value) for value in self.paths_by_date)

    def read_patch(
        self,
        *,
        target_date: int,
        grid_y0: int,
        grid_x0: int,
        tile_size: int,
    ) -> np.ndarray:
        """Read and decode one patch for ``target_date``."""
        path = self.paths_by_date[int(target_date)]
        src = self.cache.get(path)
        window = Window(
            col_off=int(grid_x0),
            row_off=int(grid_y0),
            width=int(tile_size),
            height=int(tile_size),
        )
        encoded = src.read(window=window)
        decoded = _decode_stretched_uint8(encoded, self.stretch)
        if self.kelvin_temperature:
            decoded = _kelvin_to_celsius(decoded)
        return decoded.astype(np.float32, copy=False)


def _normalize_accepted_qc_flags(values: Sequence[int] | None) -> tuple[int, ...]:
    """Return accepted ARGO QC flags as small integer codes."""
    if values is None:
        return DEFAULT_ACCEPTED_ARGO_QC_FLAGS
    flags = tuple(sorted({int(value) for value in values}))
    if not flags:
        raise ValueError("accepted_argo_qc_flags must contain at least one code.")
    return flags


class ArgoGeoTIFFProfileStore:
    """Profile-indexed ARGO zarr source exported with the GeoTIFF dataset."""

    def __init__(
        self,
        path: str | Path,
        *,
        include_salinity: bool = False,
        filter_bad_quality: bool = True,
        accepted_qc_flags: Sequence[int] | None = None,
    ) -> None:
        """Open a compact ARGO profile zarr store."""
        self.path = Path(path)
        if not self.path.exists():
            raise FileNotFoundError(f"ARGO profile zarr does not exist: {self.path}")
        self.include_salinity = bool(include_salinity)
        self.filter_bad_quality = bool(filter_bad_quality)
        self.accepted_qc_flags = _normalize_accepted_qc_flags(accepted_qc_flags)
        self._pid = os.getpid()
        self.ds = self._open_dataset()
        self._zarr_pid = os.getpid()
        self._zarr_group = self._open_zarr_group()
        required = {
            "target_date",
            "grid_row",
            "grid_col",
            "argo_temp_kelvin_uint8",
            "argo_temp_valid",
        }
        if self.include_salinity:
            required.update({"argo_psal_uint8", "argo_psal_valid"})
        missing = sorted(name for name in required if name not in self.ds)
        if missing:
            raise RuntimeError(
                f"ARGO profile zarr is missing required variables {missing}: {self.path}"
            )
        self.target_date = np.asarray(self.ds["target_date"].values, dtype=np.int32)
        self.grid_row = np.asarray(self.ds["grid_row"].values, dtype=np.int32)
        self.grid_col = np.asarray(self.ds["grid_col"].values, dtype=np.int32)
        self.depth_axis_m = np.asarray(
            self.ds["glorys_depth"].values, dtype=np.float32
        ).reshape(-1)
        temp_valid = np.asarray(self.ds["argo_temp_valid"].values, dtype=bool)
        if self.filter_bad_quality:
            temp_valid &= self._quality_mask_for_variable("temp")
        self._has_valid_temp = temp_valid.any(axis=1)
        (
            self._valid_profile_indices_by_date,
            self._profile_index_bounds_by_date,
        ) = self._build_valid_profile_index()
        self.temperature_stretch = self._temperature_stretch()
        self.salinity_stretch = (
            self._salinity_stretch() if self.include_salinity else None
        )

    def _open_dataset(self) -> xr.Dataset:
        """Open the zarr dataset in the current process."""
        return xr.open_zarr(self.path, consolidated=None)

    def _open_zarr_group(self) -> zarr.Group:
        """Open the zarr group used for direct array reads."""
        return zarr.open_group(self.path, mode="r")

    def _ensure_current_process(self) -> xr.Dataset:
        """Reopen zarr handles after DataLoader worker forks."""
        pid = os.getpid()
        if pid == self._pid:
            return self.ds
        # Do not close inherited xarray/zarr handles in a forked worker; closing
        # those locks after fork can block before the worker reads its first batch.
        self.ds = self._open_dataset()
        self._pid = pid
        return self.ds

    def _ensure_zarr_group(self) -> zarr.Group:
        """Return a direct zarr group opened in the current process."""
        pid = os.getpid()
        if pid != self._zarr_pid:
            self._zarr_group = self._open_zarr_group()
            self._zarr_pid = pid
        return self._zarr_group

    def _accepted_qc_mask(self, values: np.ndarray) -> np.ndarray:
        """Return True where QC is missing or one of the accepted flags."""
        qc = np.asarray(values, dtype=np.int16)
        missing = qc < 0
        accepted = np.isin(qc, np.asarray(self.accepted_qc_flags, dtype=np.int16))
        return missing | accepted

    def _profile_qc_names_for_variable(self, variable: str) -> tuple[str, ...]:
        """Return profile-level QC variables relevant to one ARGO variable."""
        names = ["juld", "position", "profile_depth"]
        if variable == "psal":
            names.append("profile_psal")
        return tuple(ARGO_PROFILE_QC_VARS[name] for name in names)

    def _level_qc_names_for_variable(self, variable: str) -> tuple[str, ...]:
        """Return level-level QC variables relevant to one ARGO variable."""
        if variable == "psal":
            return (ARGO_LEVEL_QC_VARS["depth"], ARGO_LEVEL_QC_VARS["psal"])
        return (ARGO_LEVEL_QC_VARS["depth"], ARGO_LEVEL_QC_VARS["temp"])

    def _quality_mask_for_variable(
        self,
        variable: str,
        *,
        indices: np.ndarray | None = None,
    ) -> np.ndarray:
        """Return a profile-depth quality mask for one compact ARGO variable."""
        if indices is None:
            profile_count = int(self.target_date.size)
            mask = np.ones((profile_count, int(self.depth_axis_m.size)), dtype=bool)
            indexer: Any = slice(None)
        else:
            selected = np.asarray(indices, dtype=np.int64).reshape(-1)
            mask = np.ones(
                (int(selected.size), int(self.depth_axis_m.size)), dtype=bool
            )
            indexer = selected
        ds = self._ensure_current_process()
        compact_name = COMPACT_PROFILE_QC_VARS.get(variable)
        if compact_name in ds:
            qc = np.asarray(ds[compact_name].isel(profile=indexer).values).reshape(-1)
            return mask & self._accepted_qc_mask(qc)[:, None]
        for name in self._level_qc_names_for_variable(variable):
            if name not in ds:
                continue
            qc = np.asarray(ds[name].isel(profile=indexer).values)
            mask &= self._accepted_qc_mask(qc)
        for name in self._profile_qc_names_for_variable(variable):
            if name not in ds:
                continue
            qc = np.asarray(ds[name].isel(profile=indexer).values).reshape(-1)
            # Bad profile-level QC rejects every depth for that profile.
            mask &= self._accepted_qc_mask(qc)[:, None]
        return mask

    def _build_valid_profile_index(
        self,
    ) -> tuple[np.ndarray, dict[int, tuple[int, int]]]:
        """Build date slices over valid-temperature profile indices."""
        valid_indices = np.flatnonzero(self._has_valid_temp).astype(np.int64)
        if valid_indices.size == 0:
            return valid_indices, {}

        # Querying per sample must not scan the full multi-million-profile store.
        order = np.argsort(self.target_date[valid_indices], kind="stable")
        sorted_indices = valid_indices[order]
        sorted_dates = self.target_date[sorted_indices]
        unique_dates, starts, counts = np.unique(
            sorted_dates, return_index=True, return_counts=True
        )
        bounds = {
            int(date): (int(start), int(start + count))
            for date, start, count in zip(
                unique_dates.tolist(),
                starts.tolist(),
                counts.tolist(),
                strict=False,
            )
        }
        return sorted_indices, bounds

    def _temperature_stretch(self) -> dict[str, Any]:
        """Read temperature stretch metadata from variable or dataset attributes."""
        ds = self._ensure_current_process()
        attrs = dict(ds["argo_temp_kelvin_uint8"].attrs)
        if "minimum" in attrs and "maximum" in attrs:
            return attrs
        ds_attrs = dict(ds.attrs)
        stretch = ds_attrs.get("temperature_stretch")
        if isinstance(stretch, dict):
            return stretch
        raise RuntimeError(
            f"ARGO profile zarr lacks temperature stretch metadata: {self.path}"
        )

    def _salinity_stretch(self) -> dict[str, Any]:
        """Read salinity stretch metadata from variable or dataset attributes."""
        ds = self._ensure_current_process()
        attrs = dict(ds["argo_psal_uint8"].attrs)
        if "minimum" in attrs and "maximum" in attrs:
            return attrs
        ds_attrs = dict(ds.attrs)
        stretch = ds_attrs.get("salinity_stretch")
        if isinstance(stretch, dict):
            return stretch
        raise RuntimeError(
            f"ARGO profile zarr lacks salinity stretch metadata: {self.path}"
        )

    def query_indices(
        self,
        *,
        target_date: int,
        grid_y0: int,
        grid_x0: int,
        tile_size: int,
    ) -> np.ndarray:
        """Return profile indices assigned to one date and grid patch."""
        y0 = int(grid_y0)
        x0 = int(grid_x0)
        tile = int(tile_size)
        bounds = self._profile_index_bounds_by_date.get(int(target_date))
        if bounds is None:
            return np.zeros((0,), dtype=np.int64)
        start, stop = bounds
        candidates = self._valid_profile_indices_by_date[start:stop]
        mask = (
            (self.grid_row[candidates] >= y0)
            & (self.grid_row[candidates] < y0 + tile)
            & (self.grid_col[candidates] >= x0)
            & (self.grid_col[candidates] < x0 + tile)
        )
        return candidates[mask].astype(np.int64, copy=False)

    def load_temperature_profiles(self, indices: np.ndarray) -> np.ndarray:
        """Load selected ARGO temperature profiles as Celsius arrays."""
        indices = np.asarray(indices, dtype=np.int64).reshape(-1)
        depth_size = int(self.depth_axis_m.size)
        if indices.size == 0:
            return np.zeros((0, depth_size), dtype=np.float32)
        group = self._ensure_zarr_group()
        encoded = np.asarray(
            group["argo_temp_kelvin_uint8"].get_orthogonal_selection(
                (indices, slice(None))
            ),
            dtype=np.uint8,
        )
        valid = np.asarray(
            group["argo_temp_valid"].get_orthogonal_selection((indices, slice(None))),
            dtype=bool,
        )
        if self.filter_bad_quality:
            valid &= self._quality_mask_for_variable("temp", indices=indices)
        kelvin = _decode_stretched_uint8(encoded, self.temperature_stretch)
        kelvin[~valid] = np.nan
        return _kelvin_to_celsius(kelvin).astype(np.float32, copy=False)

    def load_salinity_profiles(self, indices: np.ndarray) -> np.ndarray:
        """Load selected ARGO salinity profiles as raw PSU arrays."""
        if self.salinity_stretch is None:
            raise RuntimeError(
                "ARGO salinity profiles were not enabled for this store."
            )
        indices = np.asarray(indices, dtype=np.int64).reshape(-1)
        depth_size = int(self.depth_axis_m.size)
        if indices.size == 0:
            return np.zeros((0, depth_size), dtype=np.float32)
        group = self._ensure_zarr_group()
        encoded = np.asarray(
            group["argo_psal_uint8"].get_orthogonal_selection((indices, slice(None))),
            dtype=np.uint8,
        )
        valid = np.asarray(
            group["argo_psal_valid"].get_orthogonal_selection((indices, slice(None))),
            dtype=bool,
        )
        if self.filter_bad_quality:
            valid &= self._quality_mask_for_variable("psal", indices=indices)
        salinity = _decode_stretched_uint8(encoded, self.salinity_stretch)
        salinity[~valid] = np.nan
        return salinity.astype(np.float32, copy=False)

    def quality_cache_signature(self) -> str:
        """Return the ARGO quality-filter settings that affect support counts."""
        marker_text = "markers-" + "-".join(
            name for name in COMPACT_PROFILE_QC_VARS.values() if name in self.ds
        )
        flags_text = "-".join(str(value) for value in self.accepted_qc_flags)
        return _sanitize_cache_text(
            f"filter{int(self.filter_bad_quality)}_flags{flags_text}_{marker_text}"
        )

    def close(self) -> None:
        """Close the opened zarr dataset."""
        self.ds.close()


class GeoTIFFPatchIndex:
    """Build compact patch/date metadata rows for GeoTIFF training stores."""

    CACHE_VERSION = 3

    def __init__(
        self,
        *,
        root_dir: Path,
        dates: Sequence[int],
        argo_store: ArgoGeoTIFFProfileStore | None,
        cache_dir: str | Path | None,
        grid_params: _GridParams,
    ) -> None:
        """Initialize index inputs."""
        self.root_dir = Path(root_dir)
        self.dates = sorted(int(value) for value in dates)
        self.argo_store = argo_store
        self.cache_dir = None if cache_dir is None else Path(cache_dir)
        self.grid_params = grid_params
        _validate_grid_params(self.grid_params)
        if str(self.grid_params.patch_grid_source).strip().lower() != "land_mask":
            raise ValueError(
                "GeoTIFF datasets require grid.patch_grid_source='land_mask'."
            )

    def load_rows(self) -> list[dict[str, Any]]:
        """Load cached rows or build a fresh patch/date registry."""
        cache_path = self._cache_path()
        if cache_path is not None and cache_path.exists():
            return pd.read_csv(cache_path).to_dict(orient="records")

        patch_df = _build_land_mask_patch_table(self.grid_params)
        if self.grid_params.val_year is None:
            patch_records = patch_df.to_dict(orient="records")
            phases = self._split_phases(len(patch_records))
            for rec, phase in zip(patch_records, phases, strict=False):
                rec["split"] = phase
                rec["phase"] = phase
            patch_df = pd.DataFrame.from_records(patch_records)
        support_counts = self._build_support_counts(patch_df)
        rows: list[dict[str, Any]] = []
        export_index = 0
        for date_value in self.dates:
            for patch in patch_df.to_dict(orient="records"):
                patch_id = int(patch["patch_id"])
                row = dict(patch)
                row["date"] = int(date_value)
                row["export_index"] = int(export_index)
                if self.grid_params.val_year is not None:
                    phase = self._phase_for_date(int(date_value))
                    row["split"] = phase
                    row["phase"] = phase
                else:
                    phase = str(patch.get("split", patch.get("phase", "train")))
                    row["split"] = phase
                    row["phase"] = phase
                row["argo_profile_count"] = int(
                    support_counts.get((patch_id, int(date_value)), 0)
                )
                rows.append(row)
                export_index += 1

        if cache_path is not None:
            cache_path.parent.mkdir(parents=True, exist_ok=True)
            pd.DataFrame.from_records(rows).to_csv(cache_path, index=False)
        return rows

    def _cache_path(self) -> Path | None:
        """Return the metadata cache path for these index settings."""
        if self.cache_dir is None:
            return None
        res_text = str(float(self.grid_params.resolution_deg)).replace(".", "p")
        land_text = str(float(self.grid_params.max_land_fraction)).replace(".", "p")
        grid_source = _sanitize_cache_text(self.grid_params.patch_grid_source)
        mask_hash = _path_cache_hash(self.grid_params.land_mask_path)
        force_hash = _force_include_cache_hash(self.grid_params.force_include_regions)
        root_hash = hashlib.sha1(str(self.root_dir).encode("utf-8")).hexdigest()[:8]
        split_text = (
            f"valyear{int(self.grid_params.val_year)}"
            if self.grid_params.val_year is not None
            else "patchsplit"
        )
        argo_quality_text = (
            "noargo"
            if self.argo_store is None
            else self.argo_store.quality_cache_signature()
        )
        name = (
            f"argo_geotiff_gridded_v{self.CACHE_VERSION}_root{root_hash}_"
            f"dates{_date_signature(self.dates)}_"
            f"tile{int(self.grid_params.tile_size)}_res{res_text}_"
            f"stride{int(self.grid_params.effective_patch_stride)}_"
            f"grid{grid_source}_land{land_text}_mask{mask_hash}_"
            f"force{force_hash}_{split_text}_argo{argo_quality_text}.csv"
        )
        return self.cache_dir / name

    def _phase_for_date(self, date_value: int) -> str:
        """Return the train/validation phase for one date."""
        year = int(date_value) // 10000
        return "val" if year == int(self.grid_params.val_year) else "train"

    def _split_phases(self, n_patches: int) -> list[str]:
        """Return deterministic spatial train/validation phases."""
        phases = np.full((int(n_patches),), "train", dtype=object)
        val_len = int(round(int(n_patches) * float(self.grid_params.val_fraction)))
        if n_patches > 1:
            val_len = min(
                max(val_len, 1 if self.grid_params.val_fraction > 0.0 else 0),
                int(n_patches) - 1,
            )
        else:
            val_len = 0
        if val_len > 0:
            rng = np.random.default_rng(int(self.grid_params.split_seed))
            val_indices = rng.permutation(np.arange(int(n_patches)))[:val_len]
            phases[val_indices] = "val"
        return [str(value) for value in phases.tolist()]

    def _build_support_counts(
        self,
        patch_df: pd.DataFrame,
    ) -> dict[tuple[int, int], int]:
        """Count ARGO profiles per overlapping patch/date row."""
        support_counts: dict[tuple[int, int], int] = {}
        if self.argo_store is None or patch_df.empty or not self.dates:
            return support_counts

        date_set = set(int(value) for value in self.dates)
        tile = int(self.grid_params.tile_size)
        patch_by_start = {
            (int(row["grid_y0"]), int(row["grid_x0"])): int(row["patch_id"])
            for row in patch_df.to_dict(orient="records")
        }
        y_starts = np.asarray(
            sorted({key[0] for key in patch_by_start}), dtype=np.int64
        )
        x_starts = np.asarray(
            sorted({key[1] for key in patch_by_start}), dtype=np.int64
        )
        selected_profile_indices: list[np.ndarray] = []
        for date_value in sorted(date_set):
            bounds = self.argo_store._profile_index_bounds_by_date.get(int(date_value))
            if bounds is None:
                continue
            start, stop = bounds
            selected_profile_indices.append(
                self.argo_store._valid_profile_indices_by_date[start:stop]
            )
        if not selected_profile_indices:
            return support_counts
        profile_indices = np.concatenate(selected_profile_indices).astype(
            np.int64,
            copy=False,
        )

        for profile_idx in tqdm(
            profile_indices.tolist(),
            total=int(profile_indices.size),
            desc="Counting ARGO overlap support",
            unit="profile",
            dynamic_ncols=True,
        ):
            date_value = int(self.argo_store.target_date[profile_idx])
            row_idx = int(self.argo_store.grid_row[profile_idx])
            col_idx = int(self.argo_store.grid_col[profile_idx])
            y_candidates = y_starts[(y_starts <= row_idx) & (row_idx < y_starts + tile)]
            x_candidates = x_starts[(x_starts <= col_idx) & (col_idx < x_starts + tile)]
            for y0 in y_candidates.tolist():
                for x0 in x_candidates.tolist():
                    patch_id = patch_by_start.get((int(y0), int(x0)))
                    if patch_id is None:
                        continue
                    key = (int(patch_id), int(date_value))
                    support_counts[key] = support_counts.get(key, 0) + 1
        return support_counts


DEFAULT_DATASET_ROOT_DIR = Path(__file__).resolve().parents[1]
DEFAULT_GEOTIFF_ROOT_DIR = DEFAULT_DATASET_ROOT_DIR.as_posix()
DEFAULT_METADATA_CACHE_DIR = (DEFAULT_DATASET_ROOT_DIR / "depthdif_cache").as_posix()
DEFAULT_CONFIG_PATH = (
    Path(__file__).resolve().parent / "configs/default_dataset.yaml"
).as_posix()
DEFAULT_LAND_MASK_RELATIVE_PATH = "masks/world_land_mask_glorys_0p1.tif"
EO_SOURCE_DEFAULTS = {"ostia": "analysed_sst", "sss": "sos"}
EO_STRETCH_BY_SOURCE_VAR = {
    ("ostia", "analysed_sst"): ("temperature_kelvin", "temperature"),
    ("sss", "sos"): ("salinity", "salinity"),
}


class ArgoGeoTIFFGriddedPatchDataset(Dataset):
    """Dataset that lazily reads training patches from exported GeoTIFF stores."""

    DEFAULT_CONFIG_PATH = DEFAULT_CONFIG_PATH
    DEFAULT_GEOTIFF_ROOT_DIR = DEFAULT_DATASET_ROOT_DIR.as_posix()
    DEFAULT_METADATA_CACHE_DIR = (
        DEFAULT_DATASET_ROOT_DIR / "depthdif_cache"
    ).as_posix()

    def __init__(
        self,
        *,
        geotiff_root_dir: str | Path = DEFAULT_GEOTIFF_ROOT_DIR,
        metadata_cache_dir: str | Path | None = DEFAULT_METADATA_CACHE_DIR,
        split: str = "all",
        tile_size: int = 128,
        resolution_deg: float = 0.1,
        patch_grid_source: str = "land_mask",
        land_mask_path: str | Path | None = None,
        patch_stride: int | None = None,
        max_land_fraction: float = 0.30,
        force_include_regions: Sequence[dict[str, Any]] | None = None,
        finetune_sampling: dict[str, Any] | None = None,
        temporal_window_days: int = 7,
        glorys_var_name: str = "thetao",
        ostia_var_name: str = "analysed_sst",
        eo_source: str = "ostia",
        eo_var_name: str | None = None,
        require_argo_for_train: bool = True,
        require_argo_for_val: bool = True,
        require_argo_for_all: bool = False,
        synthetic_mode: bool = False,
        synthetic_pixel_count: int = 250,
        return_info: bool = True,
        return_coords: bool = True,
        include_salinity: bool = False,
        output_fields: Sequence[str] | str | None = None,
        date_start: int | str | None = None,
        date_end: int | str | None = None,
        max_dates: int | None = None,
        count_argo_support: bool | None = None,
        random_seed: int = 7,
        cache_size: int = 8,
        val_fraction: float = 0.2,
        val_year: int | None = None,
        filter_bad_argo_quality: bool = True,
        accepted_argo_qc_flags: Sequence[int] | None = None,
    ) -> None:
        """Initialize the GeoTIFF-backed patch dataset."""
        self.split = str(split).strip().lower()
        if self.split not in {"all", "train", "val"}:
            raise ValueError("split must be one of: 'all', 'train', 'val'")
        self.root_dir = Path(geotiff_root_dir)
        self.manifest_path = self.root_dir / "manifest.yaml"
        if not self.manifest_path.exists():
            raise FileNotFoundError(
                f"GeoTIFF manifest does not exist: {self.manifest_path}"
            )
        with self.manifest_path.open("r", encoding="utf-8") as f:
            self.manifest = yaml.safe_load(f)

        self.tile_size = int(tile_size)
        self.resolution_deg = float(resolution_deg)
        self.patch_grid_source = str(patch_grid_source)
        manifest_grid = self.manifest.get("grid", {})
        configured_land_mask = (
            land_mask_path
            or manifest_grid.get("source")
            or DEFAULT_LAND_MASK_RELATIVE_PATH
        )
        self.land_mask_path = _resolve_land_mask_path(
            self.root_dir,
            configured_land_mask,
        )
        self.patch_stride = None if patch_stride is None else int(patch_stride)
        self.max_land_fraction = float(max_land_fraction)
        self.force_include_regions = _parse_force_include_regions(force_include_regions)
        self.finetune_sampling = self._normalize_finetune_sampling(finetune_sampling)
        self.finetune_sampling_summary: dict[str, Any] = {
            "enabled": bool(self.finetune_sampling["enabled"]),
            "applied": False,
            "split": self.split,
        }
        self.temporal_window_days = int(temporal_window_days)
        self.glorys_var_name = str(glorys_var_name)
        self.ostia_var_name = str(ostia_var_name)
        self.eo_source, self.eo_var_name = self._normalize_eo_selection(
            eo_source=eo_source,
            eo_var_name=eo_var_name,
            ostia_var_name=self.ostia_var_name,
        )
        self.eo_stretch_name, self.eo_normalization = self._resolve_eo_metadata(
            self.eo_source, self.eo_var_name
        )
        self.return_info = bool(return_info)
        self.return_coords = bool(return_coords)
        self.output_fields = self._normalize_output_fields(
            output_fields, include_salinity=bool(include_salinity)
        )
        self.include_salinity = "salinity" in self.output_fields
        self._loads_temperature = "temperature" in self.output_fields
        self.random_seed = int(random_seed)
        self.require_argo_for_train = bool(require_argo_for_train)
        self.require_argo_for_val = bool(require_argo_for_val)
        self.require_argo_for_all = bool(require_argo_for_all)
        self.synthetic_mode = bool(synthetic_mode)
        self.synthetic_pixel_count = int(synthetic_pixel_count)
        self.filter_bad_argo_quality = bool(filter_bad_argo_quality)
        self.accepted_argo_qc_flags = _normalize_accepted_qc_flags(
            accepted_argo_qc_flags
        )
        self.date_start = self._optional_int(date_start)
        self.date_end = self._optional_int(date_end)
        self.max_dates = None if max_dates is None else int(max_dates)
        if self.temporal_window_days < 1:
            raise ValueError("sampling.temporal_window_days must be >= 1.")
        if self.synthetic_pixel_count < 0:
            raise ValueError("synthetic.pixel_count must be >= 0.")
        if self.max_dates is not None and self.max_dates < 1:
            raise ValueError("max_dates must be >= 1 when provided.")
        if (
            self.date_start is not None
            and self.date_end is not None
            and int(self.date_start) > int(self.date_end)
        ):
            raise ValueError("date_start must be <= date_end.")

        self.raster_cache = RasterDatasetCache(max_open=cache_size)
        self._depth_axis_m = np.asarray(
            self.manifest.get("depth_axis_m", ()), dtype=np.float32
        ).reshape(-1)
        if self._depth_axis_m.size == 0:
            raise RuntimeError("GeoTIFF manifest is missing depth_axis_m.")

        self.argo_store = self._open_argo_store()
        if self.argo_store is not None and int(
            self.argo_store.depth_axis_m.size
        ) != int(self._depth_axis_m.size):
            raise RuntimeError(
                "ARGO profile zarr depth axis does not match GeoTIFF manifest depth_axis_m."
            )

        self.glorys_store, self.salinity_store, self.eo_store = (
            self._build_raster_stores()
        )
        # Backward-compatible alias for callers that still inspect the old name.
        self.ostia_store = self.eo_store
        self.available_dates = self._filter_available_dates(
            sorted(self.glorys_store.dates & self.eo_store.dates)
        )
        if not self.available_dates:
            raise RuntimeError("No overlapping GeoTIFF raster dates were found.")
        self.count_argo_support = self._resolve_count_argo_support(count_argo_support)
        if self._require_argo_for_current_split() and not self.count_argo_support:
            raise ValueError(
                "count_argo_support=False is incompatible with requiring ARGO "
                "profiles for the current split."
            )
        if self.include_salinity:
            if self.salinity_store is None:
                raise RuntimeError("GeoTIFF salinity store was not initialized.")
            missing_salinity_dates = sorted(
                set(self.available_dates) - self.salinity_store.dates
            )
            if missing_salinity_dates:
                raise RuntimeError(
                    "GeoTIFF manifest is missing GLORYS salinity 'so' rasters "
                    f"for dates: {missing_salinity_dates[:5]}"
                )

        grid_params = _GridParams(
            tile_size=self.tile_size,
            resolution_deg=self.resolution_deg,
            invalid_threshold=0.5,
            invalid_mask_flags=("land",),
            val_fraction=float(val_fraction),
            val_year=None if val_year is None else int(val_year),
            split_seed=self.random_seed,
            patch_grid_source=self.patch_grid_source,
            land_mask_path=self.land_mask_path,
            patch_stride=self.patch_stride,
            max_land_fraction=self.max_land_fraction,
            force_include_regions=self._effective_force_include_regions(),
        )
        index = GeoTIFFPatchIndex(
            root_dir=self.root_dir,
            dates=self.available_dates,
            argo_store=self.argo_store if self.count_argo_support else None,
            cache_dir=metadata_cache_dir,
            grid_params=grid_params,
        )
        rows = index.load_rows()
        rows = self._filter_rows(rows)
        rows = self._apply_finetune_sampling(rows)
        if not rows:
            raise RuntimeError("Dataset is empty after split/ARGO filtering.")
        self._rows = rows

    def _filter_available_dates(self, dates: Sequence[int]) -> list[int]:
        """Apply optional date-window controls before row indexing."""
        filtered = [
            int(date)
            for date in dates
            if (self.date_start is None or int(date) >= int(self.date_start))
            and (self.date_end is None or int(date) <= int(self.date_end))
        ]
        if self.max_dates is not None:
            filtered = filtered[: int(self.max_dates)]
        return filtered

    def _resolve_count_argo_support(self, value: bool | None) -> bool:
        """Decide whether to compute profile counts during index creation."""
        if value is not None:
            return bool(value)
        # The profile-count pass scans the compact ARGO store. Keep first use of
        # downloaded checkouts fast unless this split filters by ARGO support.
        return bool(self._require_argo_for_current_split())

    @staticmethod
    def _normalize_eo_selection(
        *,
        eo_source: str,
        eo_var_name: str | None,
        ostia_var_name: str,
    ) -> tuple[str, str]:
        """Resolve the dense surface EO raster group and variable."""
        source = str(eo_source or "ostia").strip().lower()
        if not source:
            source = "ostia"
        var_name = eo_var_name
        if var_name is None:
            var_name = (
                ostia_var_name if source == "ostia" else EO_SOURCE_DEFAULTS.get(source)
            )
        if var_name is None or not str(var_name).strip():
            raise ValueError(f"No EO variable configured for source {source!r}.")
        return source, str(var_name).strip()

    @staticmethod
    def _resolve_eo_metadata(eo_source: str, eo_var_name: str) -> tuple[str, str]:
        """Return manifest stretch and normalization family for one EO raster."""
        key = (str(eo_source).strip().lower(), str(eo_var_name).strip())
        metadata = EO_STRETCH_BY_SOURCE_VAR.get(key)
        if metadata is None:
            supported = ", ".join(
                f"{source}/{var}" for source, var in sorted(EO_STRETCH_BY_SOURCE_VAR)
            )
            raise ValueError(
                "Unsupported EO raster selection "
                f"{key[0]!r}/{key[1]!r}. Supported selections: {supported}."
            )
        return metadata

    def _open_argo_store(self) -> ArgoGeoTIFFProfileStore | None:
        """Open the optional compact ARGO zarr profile store."""
        argo_info = self.manifest.get("argo", {})
        raw_path = argo_info.get("path")
        if raw_path is None or str(raw_path).strip().lower() in MISSING_TEXT_VALUES:
            return None
        return ArgoGeoTIFFProfileStore(
            _resolve_manifest_path(self.root_dir, raw_path),
            include_salinity=self.include_salinity,
            filter_bad_quality=self.filter_bad_argo_quality,
            accepted_qc_flags=self.accepted_argo_qc_flags,
        )

    def _build_raster_stores(
        self,
    ) -> tuple[GeoTIFFRasterStore, GeoTIFFRasterStore | None, GeoTIFFRasterStore]:
        """Build date-indexed dense raster stores from manifest entries."""
        rasters = self.manifest.get("rasters", {})
        stretch = self.manifest.get("stretch", {})
        temp_stretch = stretch.get("temperature_kelvin")
        if not isinstance(temp_stretch, dict):
            raise RuntimeError(
                "GeoTIFF manifest is missing temperature_kelvin stretch."
            )
        eo_stretch = stretch.get(self.eo_stretch_name)
        if not isinstance(eo_stretch, dict):
            raise RuntimeError(
                "GeoTIFF manifest is missing EO stretch "
                f"{self.eo_stretch_name!r} for {self.eo_source}/{self.eo_var_name}."
            )
        glorys_rasters = rasters.get("glorys", {})
        glorys_entries = (
            glorys_rasters.get(self.glorys_var_name, [])
            if isinstance(glorys_rasters, dict)
            else []
        )
        eo_rasters = rasters.get(self.eo_source, {})
        eo_entries = (
            eo_rasters.get(self.eo_var_name, []) if isinstance(eo_rasters, dict) else []
        )
        if not glorys_entries or not eo_entries:
            raise RuntimeError(
                "GeoTIFF manifest is missing GLORYS/EO raster entries for "
                f"{self.glorys_var_name!r}/{self.eo_source}/{self.eo_var_name}."
            )
        salinity_store = None
        if self.include_salinity:
            salinity_stretch = stretch.get("salinity")
            if not isinstance(salinity_stretch, dict):
                raise RuntimeError("GeoTIFF manifest is missing salinity stretch.")
            salinity_entries = (
                glorys_rasters.get("so", []) if isinstance(glorys_rasters, dict) else []
            )
            if not salinity_entries:
                raise RuntimeError(
                    "GeoTIFF manifest is missing GLORYS salinity 'so' raster entries."
                )
            salinity_store = GeoTIFFRasterStore(
                paths_by_date=_records_by_date(salinity_entries, self.root_dir),
                stretch=salinity_stretch,
                cache=self.raster_cache,
                kelvin_temperature=False,
            )
        return (
            GeoTIFFRasterStore(
                paths_by_date=_records_by_date(glorys_entries, self.root_dir),
                stretch=temp_stretch,
                cache=self.raster_cache,
                kelvin_temperature=True,
            ),
            salinity_store,
            GeoTIFFRasterStore(
                paths_by_date=_records_by_date(eo_entries, self.root_dir),
                stretch=eo_stretch,
                cache=self.raster_cache,
                kelvin_temperature=self.eo_normalization == "temperature",
            ),
        )

    @property
    def rows(self) -> list[dict[str, Any]]:
        """Return patch/date metadata rows."""
        return self._rows

    @property
    def depth_axis_m(self) -> np.ndarray:
        """Return the GLORYS depth axis in meters."""
        return self._depth_axis_m.copy()

    @classmethod
    def from_config(
        cls,
        config_path: str | Path | None = None,
        *,
        split: str = "all",
        dataset_overrides: dict[str, Any] | None = None,
    ) -> "ArgoGeoTIFFGriddedPatchDataset":
        """Build a GeoTIFF dataset from a YAML data config."""
        if config_path is None:
            config_path = cls.DEFAULT_CONFIG_PATH
        config_path = Path(config_path).expanduser()
        if not config_path.is_absolute():
            config_path = (DEFAULT_DATASET_ROOT_DIR / config_path).resolve()
        with config_path.open("r", encoding="utf-8") as f:
            cfg = yaml.safe_load(f)

        ds_cfg = cfg.get("data", cfg).get("dataset", {})
        if dataset_overrides:
            ds_cfg = _deep_update_config(ds_cfg, dataset_overrides)
        return cls(
            geotiff_root_dir=cls._cfg_get(
                ds_cfg,
                "core.geotiff_root_dir",
                "geotiff_root_dir",
                default=cls.DEFAULT_GEOTIFF_ROOT_DIR,
            ),
            metadata_cache_dir=cls._cfg_get(
                ds_cfg,
                "core.metadata_cache_dir",
                "metadata_cache_dir",
                default=cls.DEFAULT_METADATA_CACHE_DIR,
            ),
            split=split,
            tile_size=int(
                cls._cfg_get(ds_cfg, "grid.tile_size", "tile_size", default=128)
            ),
            resolution_deg=float(
                cls._cfg_get(
                    ds_cfg, "grid.resolution_deg", "resolution_deg", default=0.1
                )
            ),
            patch_grid_source=str(
                cls._cfg_get(
                    ds_cfg,
                    "grid.patch_grid_source",
                    "patch_grid_source",
                    default="land_mask",
                )
            ),
            land_mask_path=cls._cfg_get(
                ds_cfg,
                "grid.land_mask_path",
                "land_mask_path",
                default=None,
            ),
            patch_stride=cls._optional_int(
                cls._cfg_get(
                    ds_cfg,
                    "grid.patch_stride",
                    "patch_stride",
                    default=None,
                )
            ),
            max_land_fraction=float(
                cls._cfg_get(
                    ds_cfg,
                    "grid.max_land_fraction",
                    "max_land_fraction",
                    default=0.30,
                )
            ),
            force_include_regions=cls._cfg_get(
                ds_cfg,
                "grid.force_include_regions",
                "force_include_regions",
                default=None,
            ),
            finetune_sampling=cls._cfg_get(
                ds_cfg,
                "finetune_sampling",
                "finetune_sampling",
                default=None,
            ),
            temporal_window_days=int(
                cls._cfg_get(
                    ds_cfg,
                    "sampling.temporal_window_days",
                    "temporal_window_days",
                    default=7,
                )
            ),
            glorys_var_name=str(
                cls._cfg_get(
                    ds_cfg,
                    "sampling.glorys_var_name",
                    "glorys_var_name",
                    default="thetao",
                )
            ),
            ostia_var_name=str(
                cls._cfg_get(
                    ds_cfg,
                    "sampling.ostia_var_name",
                    "ostia_var_name",
                    default="analysed_sst",
                )
            ),
            eo_source=str(
                cls._cfg_get(
                    ds_cfg,
                    "sampling.eo_source",
                    "eo_source",
                    default="ostia",
                )
            ),
            eo_var_name=cls._cfg_get(
                ds_cfg,
                "sampling.eo_var_name",
                "eo_var_name",
                default=None,
            ),
            val_fraction=float(cfg.get("split", {}).get("val_fraction", 0.2)),
            val_year=cls._optional_int(cfg.get("split", {}).get("val_year", None)),
            date_start=cls._cfg_get(
                ds_cfg, "sampling.date_start", "date_start", default=None
            ),
            date_end=cls._cfg_get(
                ds_cfg, "sampling.date_end", "date_end", default=None
            ),
            max_dates=cls._optional_int(
                cls._cfg_get(ds_cfg, "sampling.max_dates", "max_dates", default=None)
            ),
            count_argo_support=cls._optional_bool(
                cls._cfg_get(
                    ds_cfg,
                    "selection.count_argo_support",
                    "count_argo_support",
                    default=None,
                )
            ),
            require_argo_for_train=bool(
                cls._cfg_get(
                    ds_cfg,
                    "selection.require_argo_for_train",
                    "require_argo_for_train",
                    default=True,
                )
            ),
            require_argo_for_val=bool(
                cls._cfg_get(
                    ds_cfg,
                    "selection.require_argo_for_val",
                    "require_argo_for_val",
                    default=True,
                )
            ),
            require_argo_for_all=bool(
                cls._cfg_get(
                    ds_cfg,
                    "selection.require_argo_for_all",
                    "require_argo_for_all",
                    default=False,
                )
            ),
            filter_bad_argo_quality=bool(
                cls._cfg_get(
                    ds_cfg,
                    "selection.filter_bad_argo_quality",
                    "filter_bad_argo_quality",
                    default=True,
                )
            ),
            accepted_argo_qc_flags=cls._cfg_get(
                ds_cfg,
                "selection.accepted_argo_qc_flags",
                "accepted_argo_qc_flags",
                default=None,
            ),
            synthetic_mode=bool(
                cls._cfg_get(
                    ds_cfg, "synthetic.enabled", "synthetic_enabled", default=False
                )
            ),
            synthetic_pixel_count=int(
                cls._cfg_get(
                    ds_cfg,
                    "synthetic.pixel_count",
                    "synthetic_pixel_count",
                    default=250,
                )
            ),
            return_info=bool(
                cls._cfg_get(ds_cfg, "output.return_info", "return_info", default=True)
            ),
            return_coords=bool(
                cls._cfg_get(
                    ds_cfg, "output.return_coords", "return_coords", default=True
                )
            ),
            include_salinity=bool(
                cls._cfg_get(
                    ds_cfg,
                    "output.include_salinity",
                    "include_salinity",
                    default=False,
                )
            ),
            output_fields=cls._cfg_get(
                ds_cfg, "output.fields", "output_fields", default=None
            ),
            random_seed=int(
                cls._cfg_get(ds_cfg, "runtime.random_seed", "random_seed", default=7)
            ),
            cache_size=int(
                cls._cfg_get(ds_cfg, "runtime.cache_size", "cache_size", default=8)
            ),
        )

    @staticmethod
    def _cfg_get(
        cfg: dict[str, Any],
        nested_key: str,
        flat_key: str,
        *,
        default: Any,
    ) -> Any:
        """Read nested config values while keeping flat-key compatibility."""
        node: Any = cfg
        for part in nested_key.split("."):
            if not isinstance(node, dict) or part not in node:
                node = None
                break
            node = node[part]
        if node is not None:
            return node
        _ = flat_key
        return default

    @staticmethod
    def _normalize_output_fields(
        output_fields: Sequence[str] | str | None,
        *,
        include_salinity: bool,
    ) -> tuple[str, ...]:
        """Resolve physical fields loaded for each dataset sample."""
        if output_fields is None:
            return ("temperature", "salinity") if include_salinity else ("temperature",)
        if isinstance(output_fields, str):
            fields = (output_fields,)
        else:
            fields = tuple(str(field) for field in output_fields)
        normalized = tuple(field.strip().lower() for field in fields if field.strip())
        if not normalized:
            raise ValueError("dataset.output.fields must contain at least one field.")
        unsupported = sorted(set(normalized) - {"temperature", "salinity"})
        if unsupported:
            raise ValueError(
                "dataset.output.fields contains unsupported fields: "
                f"{unsupported}. Supported fields are: temperature, salinity."
            )
        if len(set(normalized)) != len(normalized):
            raise ValueError("dataset.output.fields cannot contain duplicates.")
        return normalized

    @staticmethod
    def _optional_bool(value: Any) -> bool | None:
        """Parse nullable boolean config values."""
        if value is None:
            return None
        if isinstance(value, str):
            normalized = value.strip().lower()
            if normalized in MISSING_TEXT_VALUES:
                return None
            if normalized in {"true", "yes", "on", "1"}:
                return True
            if normalized in {"false", "no", "off", "0"}:
                return False
        if isinstance(value, bool):
            return value
        raise ValueError(f"Expected a nullable boolean value, got {value!r}.")

    @staticmethod
    def _optional_int(value: Any) -> int | None:
        """Parse nullable integer config values."""
        if value is None:
            return None
        if isinstance(value, str) and value.strip().lower() in MISSING_TEXT_VALUES:
            return None
        return int(value)

    @staticmethod
    def _normalize_finetune_sampling(raw_cfg: dict[str, Any] | None) -> dict[str, Any]:
        """Normalize optional hard-area finetuning row-sampling settings."""
        cfg = dict(raw_cfg or {})
        hard_fraction = float(cfg.get("hard_fraction", 0.75))
        if not (0.0 < hard_fraction <= 1.0):
            raise ValueError("finetune_sampling.hard_fraction must be in (0, 1].")
        default_max_land_fraction = float(cfg.get("default_max_land_fraction", 0.85))
        if not (0.0 <= default_max_land_fraction <= 1.0):
            raise ValueError(
                "finetune_sampling.default_max_land_fraction must be in [0, 1]."
            )

        raw_splits = cfg.get("apply_to_splits", ("train",))
        if isinstance(raw_splits, str):
            apply_to_splits = (raw_splits.strip().lower(),)
        else:
            apply_to_splits = tuple(str(value).strip().lower() for value in raw_splits)
        if not apply_to_splits or any(
            value not in {"all", "train", "val"} for value in apply_to_splits
        ):
            raise ValueError(
                "finetune_sampling.apply_to_splits must contain split names from "
                "{'all', 'train', 'val'}."
            )

        hard_regions: list[dict[str, Any]] = []
        for idx, raw_region in enumerate(cfg.get("hard_regions", ()) or ()):
            if not isinstance(raw_region, dict):
                raise ValueError(
                    "Each finetune_sampling.hard_regions item must be a mapping."
                )
            region = dict(raw_region)
            region["name"] = str(region.get("name", f"hard_region_{idx}"))
            region["lon_min"] = float(region["lon_min"])
            region["lon_max"] = float(region["lon_max"])
            region["lat_min"] = float(region["lat_min"])
            region["lat_max"] = float(region["lat_max"])
            region["max_land_fraction"] = float(
                region.get("max_land_fraction", default_max_land_fraction)
            )
            if not (0.0 <= region["max_land_fraction"] <= 1.0):
                raise ValueError(
                    "finetune_sampling.hard_regions[].max_land_fraction must be "
                    "in [0, 1]."
                )
            hard_regions.append(region)

        return {
            "enabled": bool(cfg.get("enabled", False)),
            "hard_fraction": hard_fraction,
            "apply_to_splits": apply_to_splits,
            "relax_land_filter": bool(cfg.get("relax_land_filter", True)),
            "default_max_land_fraction": default_max_land_fraction,
            "hard_regions": tuple(hard_regions),
        }

    def _finetune_applies_to_current_split(self) -> bool:
        """Return whether hard-area finetuning should filter this split."""
        if not bool(self.finetune_sampling["enabled"]):
            return False
        apply_to_splits = set(self.finetune_sampling["apply_to_splits"])
        return "all" in apply_to_splits or self.split in apply_to_splits

    def _effective_force_include_regions(self) -> tuple[Any, ...]:
        """Return force-include regions, extended by finetune boxes when needed."""
        if not (
            self._finetune_applies_to_current_split()
            and bool(self.finetune_sampling["relax_land_filter"])
        ):
            return self.force_include_regions

        merged = {region.name: region for region in self.force_include_regions}
        for raw_region in self.finetune_sampling["hard_regions"]:
            parsed_region = _parse_force_include_regions([raw_region])[0]
            existing = merged.get(parsed_region.name)
            if existing is not None:
                # Duplicate named boxes keep the most permissive finetune land cap.
                parsed_region = parsed_region.__class__(
                    name=parsed_region.name,
                    lon_min=parsed_region.lon_min,
                    lon_max=parsed_region.lon_max,
                    lat_min=parsed_region.lat_min,
                    lat_max=parsed_region.lat_max,
                    max_land_fraction=max(
                        float(existing.max_land_fraction),
                        float(parsed_region.max_land_fraction),
                    ),
                )
            merged[parsed_region.name] = parsed_region
        return tuple(merged.values())

    @staticmethod
    def _row_in_hard_region(
        row: dict[str, Any], regions: Sequence[dict[str, Any]]
    ) -> bool:
        """Return whether a patch center falls inside any hard finetune box."""
        lat_center = float(row.get("lat_center", np.nan))
        lon_center = _normalize_lon(float(row.get("lon_center", np.nan)))
        if not (np.isfinite(lat_center) and np.isfinite(lon_center)):
            return False
        for region in regions:
            lat_min = min(float(region["lat_min"]), float(region["lat_max"]))
            lat_max = max(float(region["lat_min"]), float(region["lat_max"]))
            lon_min = min(float(region["lon_min"]), float(region["lon_max"]))
            lon_max = max(float(region["lon_min"]), float(region["lon_max"]))
            if lat_min <= lat_center <= lat_max and lon_min <= lon_center <= lon_max:
                return True
        return False

    def _apply_finetune_sampling(
        self, rows: list[dict[str, Any]]
    ) -> list[dict[str, Any]]:
        """Apply deterministic hard/easy row filtering for finetuning runs."""
        if not self._finetune_applies_to_current_split():
            self.finetune_sampling_summary = {
                "enabled": bool(self.finetune_sampling["enabled"]),
                "applied": False,
                "split": self.split,
                "total_rows": len(rows),
            }
            return rows

        regions = self.finetune_sampling["hard_regions"]
        hard_indices = [
            idx
            for idx, row in enumerate(rows)
            if self._row_in_hard_region(row, regions)
        ]
        if not hard_indices:
            raise RuntimeError(
                "Finetune hard-area sampling matched no rows for split "
                f"{self.split!r}. Check data.dataset.finetune_sampling.hard_regions."
            )

        hard_fraction = float(self.finetune_sampling["hard_fraction"])
        hard_index_set = set(hard_indices)
        easy_indices = [idx for idx in range(len(rows)) if idx not in hard_index_set]
        requested_easy = int(
            round(len(hard_indices) * (1.0 - hard_fraction) / hard_fraction)
        )
        selected_easy: list[int] = []
        if requested_easy > 0 and easy_indices:
            sample_count = min(int(requested_easy), len(easy_indices))
            rng = np.random.default_rng(int(self.random_seed))
            selected_easy = sorted(
                int(value)
                for value in rng.choice(easy_indices, size=sample_count, replace=False)
            )

        selected_indices = sorted(hard_indices + selected_easy)
        filtered_rows = [rows[idx] for idx in selected_indices]
        actual_hard_fraction = len(hard_indices) / float(len(filtered_rows))
        self.finetune_sampling_summary = {
            "enabled": True,
            "applied": True,
            "split": self.split,
            "target_hard_fraction": hard_fraction,
            "actual_hard_fraction": actual_hard_fraction,
            "hard_rows": len(hard_indices),
            "easy_rows": len(selected_easy),
            "total_rows": len(filtered_rows),
            "available_easy_rows": len(easy_indices),
            "region_names": [str(region["name"]) for region in regions],
        }
        return filtered_rows

    def _filter_rows(self, rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
        """Apply split and ARGO-support filters."""
        if self.split in {"train", "val"}:
            rows = [
                row
                for row in rows
                if str(row.get("split", row.get("phase", ""))).strip().lower()
                == self.split
            ]
        require_argo = self._require_argo_for_current_split()
        if require_argo:
            rows = [row for row in rows if int(row.get("argo_profile_count", 0)) > 0]
        return rows

    def _require_argo_for_current_split(self) -> bool:
        """Return whether the current split requires sparse ARGO support."""
        if self.synthetic_mode:
            return False
        if self.split == "train":
            return self.require_argo_for_train
        if self.split == "val":
            return self.require_argo_for_val
        return self.require_argo_for_all

    def __len__(self) -> int:
        """Return dataset row count."""
        return len(self._rows)

    def _load_y_patch(self, row: dict[str, Any]) -> np.ndarray:
        """Load the dense GLORYS target patch."""
        y_np = self.glorys_store.read_patch(
            target_date=int(row["date"]),
            grid_y0=int(row["grid_y0"]),
            grid_x0=int(row["grid_x0"]),
            tile_size=self.tile_size,
        )
        if y_np.ndim != 3:
            raise RuntimeError(
                f"Expected GLORYS patch shape (D,H,W), got {tuple(y_np.shape)}"
            )
        if int(y_np.shape[0]) != int(self._depth_axis_m.size):
            raise RuntimeError(
                "GLORYS raster band count does not match manifest depth_axis_m: "
                f"{int(y_np.shape[0])} != {int(self._depth_axis_m.size)}"
            )
        return y_np.astype(np.float32, copy=False)

    def _load_y_salinity_patch(self, row: dict[str, Any]) -> np.ndarray:
        """Load the dense GLORYS salinity target patch as raw PSU."""
        if self.salinity_store is None:
            raise RuntimeError("GeoTIFF salinity output is not enabled.")
        salinity_np = self.salinity_store.read_patch(
            target_date=int(row["date"]),
            grid_y0=int(row["grid_y0"]),
            grid_x0=int(row["grid_x0"]),
            tile_size=self.tile_size,
        )
        if salinity_np.ndim != 3:
            raise RuntimeError(
                "Expected GLORYS salinity patch shape (D,H,W), "
                f"got {tuple(salinity_np.shape)}"
            )
        if int(salinity_np.shape[0]) != int(self._depth_axis_m.size):
            raise RuntimeError(
                "GLORYS salinity raster band count does not match manifest "
                f"depth_axis_m: {int(salinity_np.shape[0])} != "
                f"{int(self._depth_axis_m.size)}"
            )
        return salinity_np.astype(np.float32, copy=False)

    def _load_land_mask_patch(self, row: dict[str, Any]) -> np.ndarray:
        """Load the configured on-disk world-mask patch as an ocean mask."""
        src = self.raster_cache.get(self.land_mask_path)
        window = Window(
            col_off=int(row["grid_x0"]),
            row_off=int(row["grid_y0"]),
            width=int(self.tile_size),
            height=int(self.tile_size),
        )
        land_np = src.read(1, window=window)
        expected_shape = (int(self.tile_size), int(self.tile_size))
        if land_np.shape != expected_shape:
            raise RuntimeError(
                "Land-mask patch shape does not match dataset tile_size: "
                f"{tuple(land_np.shape)} != {expected_shape}"
            )
        # The world raster stores 1 for land, while model masks use 1 for ocean.
        return (np.asarray(land_np, dtype=np.float32) <= 0.5).astype(
            np.float32,
            copy=False,
        )[None, ...]

    def _load_eo_patch(self, row: dict[str, Any]) -> np.ndarray:
        """Load the configured dense surface-context patch."""
        eo_np = self.eo_store.read_patch(
            target_date=int(row["date"]),
            grid_y0=int(row["grid_y0"]),
            grid_x0=int(row["grid_x0"]),
            tile_size=self.tile_size,
        )
        if eo_np.ndim == 3 and int(eo_np.shape[0]) == 1:
            eo_np = eo_np[0]
        if eo_np.ndim != 2:
            raise RuntimeError(
                f"Expected EO patch shape (H,W), got {tuple(eo_np.shape)}"
            )
        return eo_np.astype(np.float32, copy=False)[None, ...]

    def _normalize_eo_tensor(self, tensor: torch.Tensor) -> torch.Tensor:
        """Normalize the EO channel according to its physical variable family."""
        if self.eo_normalization == "temperature":
            return temperature_normalize(mode="norm", tensor=tensor)
        if self.eo_normalization == "salinity":
            return salinity_normalize(mode="norm", tensor=tensor)
        raise RuntimeError(f"Unsupported EO normalization: {self.eo_normalization}")

    def _spatial_support_from_valid_mask(
        self,
        valid_mask_np: np.ndarray,
        *,
        source_name: str,
    ) -> np.ndarray:
        """Collapse a per-band validity mask into one spatial ocean-support mask."""
        valid_np = np.asarray(valid_mask_np, dtype=bool)
        if valid_np.ndim == 3:
            spatial_mask = valid_np.any(axis=0, keepdims=True)
        elif valid_np.ndim == 2:
            spatial_mask = valid_np[None, ...]
        else:
            raise RuntimeError(
                f"{source_name} support must be shaped as (C,H,W) or (H,W), "
                f"got {tuple(valid_np.shape)}."
            )
        expected_shape = (1, int(self.tile_size), int(self.tile_size))
        if tuple(spatial_mask.shape) != expected_shape:
            raise RuntimeError(
                f"{source_name} support shape does not match dataset tile_size: "
                f"{tuple(spatial_mask.shape)} != {expected_shape}."
            )
        return spatial_mask.astype(np.float32, copy=False)

    def _build_land_mask_patch(
        self,
        row: dict[str, Any],
        *,
        y_valid_mask_np: np.ndarray | None,
        eo_np: np.ndarray | None,
    ) -> np.ndarray:
        """Build one spatial ocean mask from GLORYS, EO, or the on-disk mask."""
        if y_valid_mask_np is not None:
            return self._spatial_support_from_valid_mask(
                y_valid_mask_np,
                source_name="GLORYS target",
            )
        if eo_np is not None:
            return self._spatial_support_from_valid_mask(
                np.isfinite(eo_np),
                source_name="EO surface context",
            )
        if self.land_mask_path.exists():
            return self._load_land_mask_patch(row)
        raise RuntimeError(
            "Could not build land_mask: GLORYS target support was unavailable, "
            "EO support was unavailable, and the configured on-disk land mask "
            f"does not exist: {self.land_mask_path}"
        )

    def _empty_sparse_patch(self) -> tuple[np.ndarray, np.ndarray]:
        """Return an empty sparse profile patch and validity mask."""
        depth_size = int(self._depth_axis_m.size)
        shape = (depth_size, self.tile_size, self.tile_size)
        return np.full(shape, np.nan, dtype=np.float32), np.zeros(shape, dtype=bool)

    def _rasterize_profile_values(
        self,
        row: dict[str, Any],
        indices: np.ndarray,
        values: np.ndarray,
    ) -> tuple[np.ndarray, np.ndarray]:
        """Rasterize selected profile values into one sparse patch."""
        depth_size = int(self._depth_axis_m.size)
        if indices.size == 0:
            return self._empty_sparse_patch()
        if values.ndim != 2 or int(values.shape[1]) != depth_size:
            raise RuntimeError(
                "ARGO profile values do not match manifest depth_axis_m: "
                f"{tuple(values.shape)}"
            )

        value_sum = np.zeros(
            (depth_size, self.tile_size, self.tile_size), dtype=np.float64
        )
        value_count = np.zeros(
            (depth_size, self.tile_size, self.tile_size), dtype=np.uint16
        )
        y0 = int(row["grid_y0"])
        x0 = int(row["grid_x0"])
        for local_idx, profile_idx in enumerate(indices.tolist()):
            row_idx = int(self.argo_store.grid_row[int(profile_idx)]) - y0
            col_idx = int(self.argo_store.grid_col[int(profile_idx)]) - x0
            if (
                row_idx < 0
                or row_idx >= self.tile_size
                or col_idx < 0
                or col_idx >= self.tile_size
            ):
                continue
            profile = values[int(local_idx)]
            valid = np.isfinite(profile)
            if not np.any(valid):
                continue
            # Multiple ARGO profiles can land on the same grid cell and depth.
            value_sum[valid, row_idx, col_idx] += profile[valid].astype(np.float64)
            value_count[valid, row_idx, col_idx] += 1

        value_np = np.full(value_sum.shape, np.nan, dtype=np.float32)
        value_valid = value_count > 0
        value_np[value_valid] = (
            value_sum[value_valid] / value_count[value_valid].astype(np.float64)
        ).astype(
            np.float32,
            copy=False,
        )
        return value_np, value_valid

    def _query_temperature_valid_argo_indices(self, row: dict[str, Any]) -> np.ndarray:
        """Return temperature-valid ARGO indices for the current patch."""
        if self.argo_store is None:
            return np.zeros((0,), dtype=np.int64)
        return self.argo_store.query_indices(
            target_date=int(row["date"]),
            grid_y0=int(row["grid_y0"]),
            grid_x0=int(row["grid_x0"]),
            tile_size=self.tile_size,
        )

    def _rasterize_argo_patch(
        self, row: dict[str, Any]
    ) -> tuple[np.ndarray, np.ndarray]:
        """Rasterize compact ARGO temperature observations into one patch."""
        indices = self._query_temperature_valid_argo_indices(row)
        if indices.size == 0 or self.argo_store is None:
            return self._empty_sparse_patch()
        values = self.argo_store.load_temperature_profiles(indices)
        return self._rasterize_profile_values(row, indices, values)

    def _rasterize_argo_salinity_patch(
        self, row: dict[str, Any]
    ) -> tuple[np.ndarray, np.ndarray]:
        """Rasterize compact ARGO salinity observations into one patch."""
        if not self.include_salinity:
            raise RuntimeError("ARGO salinity output is not enabled.")
        indices = self._query_temperature_valid_argo_indices(row)
        if indices.size == 0 or self.argo_store is None:
            return self._empty_sparse_patch()
        # Keep salinity on the same temperature-valid support used for filtering.
        values = self.argo_store.load_salinity_profiles(indices)
        return self._rasterize_profile_values(row, indices, values)

    def _synthetic_rng_for_row(
        self,
        row: dict[str, Any],
        *,
        idx: int,
    ) -> np.random.Generator:
        """Build a deterministic synthetic-sampling RNG for one row."""
        seed = np.random.SeedSequence(
            [
                int(self.random_seed),
                int(row.get("patch_id", 0)),
                int(row.get("date", 0)),
                int(idx),
            ]
        )
        return np.random.default_rng(seed)

    def _build_synthetic_x_from_glorys(
        self,
        y_np: np.ndarray,
        y_valid_mask_np: np.ndarray,
        row: dict[str, Any],
        *,
        idx: int,
    ) -> tuple[np.ndarray, np.ndarray]:
        """Build sparse synthetic observations by sampling the dense target."""
        x_np = np.full(y_np.shape, np.nan, dtype=np.float32)
        x_valid = np.zeros(y_valid_mask_np.shape, dtype=bool)
        if self.synthetic_pixel_count == 0:
            return x_np, x_valid

        valid_columns = np.asarray(y_valid_mask_np, dtype=bool).any(axis=0)
        flat_valid_columns = np.flatnonzero(valid_columns.reshape(-1))
        if flat_valid_columns.size == 0:
            return x_np, x_valid

        sample_count = min(
            int(self.synthetic_pixel_count), int(flat_valid_columns.size)
        )
        rng = self._synthetic_rng_for_row(row, idx=idx)
        selected = rng.choice(flat_valid_columns, size=sample_count, replace=False)
        row_indices, col_indices = np.unravel_index(selected, valid_columns.shape)
        for row_idx, col_idx in zip(row_indices.tolist(), col_indices.tolist()):
            depth_valid = y_valid_mask_np[:, int(row_idx), int(col_idx)]
            if not np.any(depth_valid):
                continue
            # Synthetic mode uses decoded dense target values as sparse input.
            x_np[depth_valid, int(row_idx), int(col_idx)] = y_np[
                depth_valid,
                int(row_idx),
                int(col_idx),
            ]
            x_valid[depth_valid, int(row_idx), int(col_idx)] = True
        return x_np, x_valid

    def __getitem__(self, idx: int) -> dict[str, Any]:
        """Return one model-ready training sample."""
        row = self._rows[int(idx)]
        eo_np = self._load_eo_patch(row)
        temperature_payload: dict[str, torch.Tensor] | None = None
        salinity_payload: dict[str, torch.Tensor] | None = None
        land_support_np: np.ndarray | None = None

        if self._loads_temperature:
            y_np = self._load_y_patch(row)
            y_valid_mask_np = np.isfinite(y_np)
            if self.synthetic_mode:
                x_np, x_valid_mask_np = self._build_synthetic_x_from_glorys(
                    y_np,
                    y_valid_mask_np,
                    row,
                    idx=int(idx),
                )
            else:
                x_np, x_valid_mask_np = self._rasterize_argo_patch(row)

            x = temperature_normalize(mode="norm", tensor=torch.from_numpy(x_np))
            y = temperature_normalize(mode="norm", tensor=torch.from_numpy(y_np))
            x = torch.nan_to_num(x, nan=0.0, posinf=0.0, neginf=0.0)
            y = torch.nan_to_num(y, nan=0.0, posinf=0.0, neginf=0.0)
            x_valid_mask = torch.from_numpy(
                x_valid_mask_np.astype(np.bool_, copy=False)
            )
            y_valid_mask = torch.from_numpy(
                y_valid_mask_np.astype(np.bool_, copy=False)
            )
            temperature_payload = {
                "x": x,
                "y": y,
                "x_valid_mask": x_valid_mask,
                "y_valid_mask": y_valid_mask,
                "x_valid_mask_1d": x_valid_mask.any(dim=0, keepdim=True),
            }
            land_support_np = y_valid_mask_np

        if self.include_salinity:
            y_salinity_np = self._load_y_salinity_patch(row)
            y_salinity_valid_mask_np = np.isfinite(y_salinity_np)
            if self.synthetic_mode:
                x_salinity_np, x_salinity_valid_mask_np = (
                    self._build_synthetic_x_from_glorys(
                        y_salinity_np,
                        y_salinity_valid_mask_np,
                        row,
                        idx=int(idx),
                    )
                )
            else:
                x_salinity_np, x_salinity_valid_mask_np = (
                    self._rasterize_argo_salinity_patch(row)
                )
            x_salinity = salinity_normalize(
                mode="norm", tensor=torch.from_numpy(x_salinity_np)
            )
            y_salinity = salinity_normalize(
                mode="norm", tensor=torch.from_numpy(y_salinity_np)
            )
            x_salinity = torch.nan_to_num(x_salinity, nan=0.0, posinf=0.0, neginf=0.0)
            y_salinity = torch.nan_to_num(y_salinity, nan=0.0, posinf=0.0, neginf=0.0)
            x_salinity_valid_mask = torch.from_numpy(
                x_salinity_valid_mask_np.astype(np.bool_, copy=False)
            )
            y_salinity_valid_mask = torch.from_numpy(
                y_salinity_valid_mask_np.astype(np.bool_, copy=False)
            )
            salinity_payload = {
                "x_salinity": x_salinity,
                "y_salinity": y_salinity,
                "x_salinity_valid_mask": x_salinity_valid_mask,
                "y_salinity_valid_mask": y_salinity_valid_mask,
                "x_salinity_valid_mask_1d": x_salinity_valid_mask.any(
                    dim=0, keepdim=True
                ),
            }
            if land_support_np is None:
                # Salinity-only runs should derive the spatial mask from salinity support.
                land_support_np = y_salinity_valid_mask_np

        land_mask_np = self._build_land_mask_patch(
            row,
            y_valid_mask_np=land_support_np,
            eo_np=eo_np,
        )
        eo = self._normalize_eo_tensor(torch.from_numpy(eo_np))
        eo = torch.nan_to_num(eo, nan=0.0, posinf=0.0, neginf=0.0)
        sample: dict[str, Any] = {
            "eo": eo,
            "land_mask": torch.from_numpy(land_mask_np),
            "date": _parse_date_int(row.get("date", 19700115)),
        }
        if temperature_payload is not None:
            sample.update(temperature_payload)
        if salinity_payload is not None:
            sample.update(salinity_payload)
        if self.return_coords:
            sample["coords"] = torch.tensor(
                [
                    0.5 * (float(row["lat0"]) + float(row["lat1"])),
                    _center_lon_deg(float(row["lon0"]), float(row["lon1"])),
                ],
                dtype=torch.float32,
            )
        if self.return_info:
            info = dict(row)
            info["x_source"] = "glorys_synthetic" if self.synthetic_mode else "argo"
            info["synthetic_pixel_count"] = (
                int(self.synthetic_pixel_count) if self.synthetic_mode else 0
            )
            sample["info"] = info
        return sample