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
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
"""Data ingestion module for solar radiation datasets.

Supports NASA POWER API, local NetCDF/HDF5/Zarr files, and geocoding.
All data is returned as xarray Datasets for downstream analysis.
"""

from __future__ import annotations

import hashlib
import json
import logging
import time
from datetime import datetime
from pathlib import Path
from typing import Any

import numpy as np
import pandas as pd
import param
import requests
import xarray as xr

from solar_intelligence.config import (
    CACHE_DIR,
    CACHE_TTL_DAYS,
    DEFAULT_END_YEAR,
    DEFAULT_START_YEAR,
    ERA5_CDS_URL,
    ERA5_DATASET_NAME,
    ERA5_SOLAR_VARIABLES,
    ERA5_VAR_MAP,
    NASA_POWER_BASE_URL,
    NASA_POWER_SOLAR_PARAMS,
)

logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Geocoding
# ---------------------------------------------------------------------------

def geocode_location(city_name: str) -> tuple[float, float]:
    """Convert a city name to (latitude, longitude) using geopy.

    Parameters
    ----------
    city_name : str
        City name, e.g. "New Delhi", "San Francisco, CA".

    Returns
    -------
    tuple[float, float]
        (latitude, longitude) rounded to 4 decimal places.

    Raises
    ------
    ValueError
        If the city cannot be geocoded.
    """
    if not isinstance(city_name, str) or not city_name.strip():
        raise ValueError("city_name must be a non-empty string")

    from geopy.exc import GeocoderServiceError
    from geopy.geocoders import Nominatim

    geolocator = Nominatim(user_agent="solar-intelligence-platform")
    max_retries = 3
    for attempt in range(max_retries):
        try:
            location = geolocator.geocode(city_name, timeout=10)
            break
        except (GeocoderServiceError, Exception) as e:
            if attempt < max_retries - 1:
                wait = 2 ** attempt
                logger.warning(
                    "Nominatim retry %d/%d after %ds: %s",
                    attempt + 1, max_retries, wait, e,
                )
                time.sleep(wait)
            else:
                raise ConnectionError(
                    f"Geocoding service unreachable after {max_retries} attempts: {e}"
                ) from e
    if location is None:
        raise ValueError(f"Could not geocode location: '{city_name}'")
    return round(location.latitude, 4), round(location.longitude, 4)


# ---------------------------------------------------------------------------
# Cache Helpers
# ---------------------------------------------------------------------------

def _cache_key(lat: float, lon: float, start: str, end: str, temporal: str) -> str:
    """Generate a deterministic cache filename."""
    raw = f"{lat:.4f}_{lon:.4f}_{start}_{end}_{temporal}"
    digest = hashlib.sha256(raw.encode()).hexdigest()[:12]
    return f"nasa_power_{temporal}_{digest}.nc"


def _cache_is_valid(cache_path: Path, ttl_days: int = CACHE_TTL_DAYS) -> bool:
    """Check if a cached file exists and is within TTL."""
    if not cache_path.exists():
        return False
    age_days = (time.time() - cache_path.stat().st_mtime) / 86400
    return age_days < ttl_days


# ---------------------------------------------------------------------------
# NASA POWER API Client
# ---------------------------------------------------------------------------

class NASAPowerClient(param.Parameterized):
    """Client for fetching solar radiation data from NASA POWER API.

    NASA POWER provides global solar and meteorological data derived from
    satellite observations and reanalysis, at 1°×1° spatial resolution,
    from 1981 to near real-time.

    Parameters
    ----------
    cache_dir : Path
        Directory for caching API responses as NetCDF files.
    parameters : list[str]
        NASA POWER parameter codes to fetch.
    community : str
        POWER community identifier (RE, SB, AG).
    """

    cache_dir = param.Path(default=CACHE_DIR, doc="Cache directory for API responses")
    parameters = param.List(
        default=NASA_POWER_SOLAR_PARAMS,
        item_type=str,
        doc="NASA POWER parameter codes",
    )
    community = param.String(default="RE", doc="POWER community (RE/SB/AG)")

    def fetch_daily(
        self,
        lat: float,
        lon: float,
        start: str | None = None,
        end: str | None = None,
    ) -> xr.Dataset:
        """Fetch daily solar data from NASA POWER.

        Parameters
        ----------
        lat, lon : float
            Location coordinates.
        start, end : str
            Date range in YYYYMMDD format.

        Returns
        -------
        xr.Dataset
            Dataset with time dimension and solar radiation variables.
        """
        if start is None:
            start = f"{DEFAULT_START_YEAR}0101"
        if end is None:
            end = f"{DEFAULT_END_YEAR}1231"
        return self._fetch("daily", lat, lon, start, end)

    def fetch_monthly(
        self,
        lat: float,
        lon: float,
        start: str | None = None,
        end: str | None = None,
    ) -> xr.Dataset:
        """Fetch monthly-averaged solar data from NASA POWER."""
        if start is None:
            start = f"{DEFAULT_START_YEAR}0101"
        if end is None:
            end = f"{DEFAULT_END_YEAR}1231"
        return self._fetch("monthly", lat, lon, start, end)

    def fetch_hourly(
        self,
        lat: float,
        lon: float,
        start: str | None = None,
        end: str | None = None,
    ) -> xr.Dataset:
        """Fetch hourly solar data from NASA POWER.

        Note: Hourly data is limited to shorter date ranges (~1 year max).
        """
        if start is None:
            start = f"{DEFAULT_END_YEAR}0101"
        if end is None:
            end = f"{DEFAULT_END_YEAR}0107"
        return self._fetch("hourly", lat, lon, start, end)

    def _fetch(
        self,
        temporal: str,
        lat: float,
        lon: float,
        start: str,
        end: str,
    ) -> xr.Dataset:
        """Internal method to fetch and cache NASA POWER data."""
        cache_file = Path(self.cache_dir) / _cache_key(lat, lon, start, end, temporal)

        if _cache_is_valid(cache_file):
            logger.info("Loading cached data: %s", cache_file.name)
            return xr.open_dataset(cache_file)

        logger.info(
            "Fetching NASA POWER %s data: lat=%.4f, lon=%.4f, %s to %s",
            temporal, lat, lon, start, end,
        )

        url = f"{NASA_POWER_BASE_URL}/{temporal}/point"
        params_str = ",".join(self.parameters)

        max_retries = 3
        for attempt in range(max_retries):
            try:
                response = requests.get(
                    url,
                    params={
                        "parameters": params_str,
                        "community": self.community,
                        "longitude": lon,
                        "latitude": lat,
                        "start": start,
                        "end": end,
                        "format": "JSON",
                    },
                    timeout=60,
                )
                response.raise_for_status()
                break
            except (requests.ConnectionError, requests.Timeout) as e:
                if attempt < max_retries - 1:
                    wait = 2 ** attempt
                    logger.warning(
                        "NASA POWER retry %d/%d after %ds: %s",
                        attempt + 1, max_retries, wait, e,
                    )
                    time.sleep(wait)
                else:
                    raise ConnectionError(
                        f"NASA POWER API unreachable after {max_retries} attempts: {e}"
                    ) from e

        data = response.json()
        ds = self._parse_response(data, temporal, lat, lon)

        # Cache as NetCDF
        Path(self.cache_dir).mkdir(parents=True, exist_ok=True)
        ds.to_netcdf(cache_file)
        logger.info("Cached to: %s", cache_file.name)

        return ds

    def _parse_response(
        self,
        data: dict[str, Any],
        temporal: str,
        lat: float,
        lon: float,
    ) -> xr.Dataset:
        """Parse NASA POWER JSON response into xr.Dataset."""
        properties = data.get("properties", {})
        parameter_data = properties.get("parameter", {})

        if not parameter_data:
            raise ValueError(
                f"No data returned from NASA POWER API. "
                f"Response keys: {list(data.keys())}"
            )

        # Build DataFrame from parameter records
        records: dict[str, dict[str, float]] = {}
        for param_name, values in parameter_data.items():
            for date_key, value in values.items():
                if date_key not in records:
                    records[date_key] = {}
                # NASA POWER uses -999.0 for missing data
                records[date_key][param_name] = value if value != -999.0 else np.nan

        df = pd.DataFrame.from_dict(records, orient="index")
        df.index.name = "date"

        # Parse dates based on temporal resolution
        if temporal == "daily":
            df.index = pd.to_datetime(df.index, format="%Y%m%d")
        elif temporal == "monthly":
            # Monthly keys are YYYYMM or YEAR13 (annual average)
            valid_idx = ~df.index.str.endswith("13")
            df = df[valid_idx]
            df.index = pd.to_datetime(df.index, format="%Y%m")
        elif temporal == "hourly":
            df.index = pd.to_datetime(df.index, format="%Y%m%d%H")

        df = df.sort_index()
        df = df.rename_axis("time")

        # Convert to xarray Dataset
        ds = df.to_xarray()
        ds = ds.assign_coords(latitude=lat, longitude=lon)

        # Add metadata attributes
        ds.attrs["source"] = "NASA POWER API"
        ds.attrs["temporal_resolution"] = temporal
        ds.attrs["latitude"] = lat
        ds.attrs["longitude"] = lon
        ds.attrs["fetched_at"] = datetime.now().isoformat()

        # Variable-level attributes
        var_attrs = {
            "ALLSKY_SFC_SW_DWN": {"long_name": "GHI (All Sky)", "units": "kWh/m²/day"},
            "CLRSKY_SFC_SW_DWN": {"long_name": "GHI (Clear Sky)", "units": "kWh/m²/day"},
            "ALLSKY_SFC_SW_DNI": {"long_name": "DNI (All Sky)", "units": "kWh/m²/day"},
            "ALLSKY_SFC_SW_DIFF": {"long_name": "DHI (All Sky)", "units": "kWh/m²/day"},
            "ALLSKY_KT": {"long_name": "Clearness Index", "units": "dimensionless"},
            "T2M": {"long_name": "Temperature at 2m", "units": "°C"},
            "T2M_MAX": {"long_name": "Max Temperature at 2m", "units": "°C"},
            "T2M_MIN": {"long_name": "Min Temperature at 2m", "units": "°C"},
            "WS2M": {"long_name": "Wind Speed at 2m", "units": "m/s"},
            "RH2M": {"long_name": "Relative Humidity at 2m", "units": "%"},
        }
        for var_name, attrs in var_attrs.items():
            if var_name in ds:
                ds[var_name].attrs.update(attrs)

        return ds


# ---------------------------------------------------------------------------
# Unified Data Loader
# ---------------------------------------------------------------------------

class DataLoader(param.Parameterized):
    """Unified loader for solar radiation datasets.

    Supports NASA POWER API, local NetCDF/HDF5/Zarr files,
    and provides spatial/temporal slicing utilities.
    """

    api_client = param.ClassSelector(
        class_=NASAPowerClient,
        default=None,
        allow_None=True,
        doc="NASA POWER API client instance",
    )

    def __init__(self, **params):
        super().__init__(**params)
        if self.api_client is None:
            self.api_client = NASAPowerClient()

    def load_from_api(
        self,
        lat: float,
        lon: float,
        start_year: int = DEFAULT_START_YEAR,
        end_year: int = DEFAULT_END_YEAR,
        temporal: str = "daily",
    ) -> xr.Dataset:
        """Load solar data from NASA POWER API.

        Parameters
        ----------
        lat, lon : float
            Location coordinates.
        start_year, end_year : int
            Year range for data retrieval.
        temporal : str
            Resolution: "daily", "monthly", or "hourly".

        Returns
        -------
        xr.Dataset
        """
        start = f"{start_year}0101"
        end = f"{end_year}1231"

        if temporal == "daily":
            return self.api_client.fetch_daily(lat, lon, start, end)
        elif temporal == "monthly":
            return self.api_client.fetch_monthly(lat, lon, start, end)
        elif temporal == "hourly":
            return self.api_client.fetch_hourly(lat, lon, start, end)
        else:
            raise ValueError(f"Unknown temporal resolution: {temporal}")

    def load_for_location(
        self,
        city: str | None = None,
        lat: float | None = None,
        lon: float | None = None,
        start_year: int = DEFAULT_START_YEAR,
        end_year: int = DEFAULT_END_YEAR,
    ) -> xr.Dataset:
        """Load solar data for a city name or coordinates.

        Provide either `city` or both `lat`/`lon`.
        """
        if city is not None:
            lat, lon = geocode_location(city)
            logger.info("Geocoded '%s' → (%.4f, %.4f)", city, lat, lon)
        elif lat is None or lon is None:
            raise ValueError("Provide either 'city' or both 'lat' and 'lon'.")

        ds = self.load_from_api(lat, lon, start_year, end_year)
        return ds

    @staticmethod
    def load_netcdf(path: str | Path) -> xr.Dataset:
        """Load a local NetCDF file."""
        path = Path(path)
        if not path.exists():
            raise FileNotFoundError(f"NetCDF file not found: {path}")
        return xr.open_dataset(path)

    @staticmethod
    def load_zarr(path: str | Path) -> xr.Dataset:
        """Load a local Zarr store."""
        path = Path(path)
        if not path.exists():
            raise FileNotFoundError(f"Zarr store not found: {path}")
        return xr.open_zarr(path)

    @staticmethod
    def load_hdf5(path: str | Path) -> xr.Dataset:
        """Load a local HDF5 file."""
        path = Path(path)
        if not path.exists():
            raise FileNotFoundError(f"HDF5 file not found: {path}")
        return xr.open_dataset(path, engine="h5netcdf")

    @staticmethod
    def slice_time(
        ds: xr.Dataset,
        start: str | None = None,
        end: str | None = None,
    ) -> xr.Dataset:
        """Slice dataset along time dimension.

        Parameters
        ----------
        ds : xr.Dataset
            Input dataset with 'time' dimension.
        start, end : str, optional
            ISO date strings for slicing.
        """
        if "time" not in ds.dims:
            raise ValueError("Dataset has no 'time' dimension.")
        return ds.sel(time=slice(start, end))

    @staticmethod
    def slice_location(
        ds: xr.Dataset,
        lat: float,
        lon: float,
        method: str = "nearest",
    ) -> xr.Dataset:
        """Select nearest grid point to given coordinates.

        Works with gridded datasets that have lat/lon dimensions.
        """
        sel_kwargs: dict[str, Any] = {}
        for lat_name in ("lat", "latitude"):
            if lat_name in ds.dims or lat_name in ds.coords:
                sel_kwargs[lat_name] = lat
                break
        for lon_name in ("lon", "longitude"):
            if lon_name in ds.dims or lon_name in ds.coords:
                sel_kwargs[lon_name] = lon
                break

        if not sel_kwargs:
            logger.warning("No lat/lon dimensions found — returning dataset as-is.")
            return ds

        return ds.sel(**sel_kwargs, method=method)


# ---------------------------------------------------------------------------
# Synthetic Data Generator (for testing & demos)
# ---------------------------------------------------------------------------

def generate_synthetic_solar_data(
    lat: float = 0.0,
    lon: float = 0.0,
    start_year: int = DEFAULT_START_YEAR,
    end_year: int = DEFAULT_END_YEAR,
) -> xr.Dataset:
    """Generate realistic synthetic solar data for testing.

    Uses latitude-dependent irradiance model with seasonal variation,
    cloud effects, and temperature correlation.

    Parameters
    ----------
    lat, lon : float
        Location coordinates.
    start_year, end_year : int
        Date range.

    Returns
    -------
    xr.Dataset
        Synthetic dataset matching NASA POWER variable structure.
    """
    rng = np.random.default_rng(seed=abs(int(lat * 100 + lon * 10)))

    dates = pd.date_range(f"{start_year}-01-01", f"{end_year}-12-31", freq="D")
    n_days = len(dates)

    # Day of year for seasonal cycle
    doy = dates.dayofyear.values
    lat_rad = np.radians(lat)

    # Solar declination angle (approximate)
    declination = 23.45 * np.sin(np.radians((360 / 365) * (doy - 81)))
    decl_rad = np.radians(declination)

    # Day length factor
    cos_hour_angle = -np.tan(lat_rad) * np.tan(decl_rad)
    cos_hour_angle = np.clip(cos_hour_angle, -1, 1)
    day_length_hours = (2 / 15) * np.degrees(np.arccos(cos_hour_angle))

    # Extra-terrestrial irradiance on horizontal surface
    solar_constant = 1361  # W/m²
    cos_zenith_noon = np.sin(lat_rad) * np.sin(decl_rad) + \
        np.cos(lat_rad) * np.cos(decl_rad)
    cos_zenith_noon = np.clip(cos_zenith_noon, 0, 1)

    # Clear sky GHI (kWh/m²/day) — simplified model
    clearsky_ghi = (solar_constant * cos_zenith_noon * day_length_hours *
                    0.75 / 1000)  # atmospheric transmittance ~0.75
    clearsky_ghi = np.clip(clearsky_ghi, 0, 12)

    # Cloud / clearness factor with seasonal and random variation
    cloud_base = 0.55 + 0.15 * np.cos(np.radians((360 / 365) * (doy - 172)))
    cloud_noise = rng.normal(0, 0.08, n_days)
    clearness = np.clip(cloud_base + cloud_noise, 0.15, 0.95)

    # Actual GHI
    ghi = clearsky_ghi * clearness

    # DNI / DHI decomposition (simplified Erbs model)
    kt = clearness
    kd = np.where(
        kt <= 0.22, 1.0 - 0.09 * kt,
        np.where(
            kt <= 0.80,
            0.9511 - 0.1604 * kt + 4.388 * kt**2 - 16.638 * kt**3 + 12.336 * kt**4,
            0.165,
        ),
    )
    dhi = ghi * kd
    dni = (ghi - dhi) / np.clip(cos_zenith_noon, 0.05, 1.0)
    dni = np.clip(dni, 0, 15)

    # Temperature model (seasonal + diurnal mean + noise)
    temp_annual_mean = 15 + 20 * np.cos(np.radians(lat))  # latitude-dependent
    temp_seasonal = 10 * np.sin(np.radians((360 / 365) * (doy - 81)))
    if lat < 0:
        temp_seasonal = -temp_seasonal
    temp_noise = rng.normal(0, 2, n_days)
    temperature = temp_annual_mean + temp_seasonal + temp_noise

    # Wind speed (random with slight seasonal variation)
    wind = 3.0 + 1.5 * np.sin(np.radians((360 / 365) * (doy - 45))) + \
        rng.normal(0, 0.8, n_days)
    wind = np.clip(wind, 0.5, 15)

    # Relative humidity
    humidity = 55 + 15 * np.cos(np.radians((360 / 365) * (doy - 200))) + \
        rng.normal(0, 5, n_days)
    humidity = np.clip(humidity, 15, 95)

    ds = xr.Dataset(
        {
            "ALLSKY_SFC_SW_DWN": ("time", ghi.astype(np.float32)),
            "CLRSKY_SFC_SW_DWN": ("time", clearsky_ghi.astype(np.float32)),
            "ALLSKY_SFC_SW_DNI": ("time", dni.astype(np.float32)),
            "ALLSKY_SFC_SW_DIFF": ("time", dhi.astype(np.float32)),
            "ALLSKY_KT": ("time", kt.astype(np.float32)),
            "T2M": ("time", temperature.astype(np.float32)),
            "T2M_MAX": ("time", (temperature + rng.uniform(3, 7, n_days)).astype(np.float32)),
            "T2M_MIN": ("time", (temperature - rng.uniform(3, 7, n_days)).astype(np.float32)),
            "WS2M": ("time", wind.astype(np.float32)),
            "RH2M": ("time", humidity.astype(np.float32)),
        },
        coords={
            "time": dates,
            "latitude": lat,
            "longitude": lon,
        },
        attrs={
            "source": "Synthetic (solar-intelligence)",
            "temporal_resolution": "daily",
            "latitude": lat,
            "longitude": lon,
            "description": "Realistic synthetic solar data for testing and demos",
        },
    )

    # Add variable attributes
    var_attrs = {
        "ALLSKY_SFC_SW_DWN": {"long_name": "GHI (All Sky)", "units": "kWh/m²/day"},
        "CLRSKY_SFC_SW_DWN": {"long_name": "GHI (Clear Sky)", "units": "kWh/m²/day"},
        "ALLSKY_SFC_SW_DNI": {"long_name": "DNI (All Sky)", "units": "kWh/m²/day"},
        "ALLSKY_SFC_SW_DIFF": {"long_name": "DHI (All Sky)", "units": "kWh/m²/day"},
        "ALLSKY_KT": {"long_name": "Clearness Index", "units": "dimensionless"},
        "T2M": {"long_name": "Temperature at 2m", "units": "°C"},
        "T2M_MAX": {"long_name": "Max Temperature at 2m", "units": "°C"},
        "T2M_MIN": {"long_name": "Min Temperature at 2m", "units": "°C"},
        "WS2M": {"long_name": "Wind Speed at 2m", "units": "m/s"},
        "RH2M": {"long_name": "Relative Humidity at 2m", "units": "%"},
    }
    for var_name, attrs in var_attrs.items():
        if var_name in ds:
            ds[var_name].attrs.update(attrs)

    return ds


# ---------------------------------------------------------------------------
# Global Solar Grid Generator (for Datashader maps)
# ---------------------------------------------------------------------------

def generate_global_solar_grid(
    resolution: float = 1.0,
    lat_range: tuple[float, float] = (-60, 60),
    lon_range: tuple[float, float] = (-180, 180),
) -> xr.Dataset:
    """Generate a global solar irradiance grid for large-scale Datashader maps.

    Uses a physics-based latitude model to produce a realistic global GHI
    distribution with millions of grid points for Datashader rendering.

    Parameters
    ----------
    resolution : float
        Grid resolution in degrees (0.25 -> ~1M points, 0.1 -> ~6.5M points).
    lat_range, lon_range : tuple
        Bounding box for the grid.

    Returns
    -------
    xr.Dataset
        Gridded dataset with lat/lon dimensions and GHI values.
    """
    rng = np.random.default_rng(seed=42)

    lats = np.arange(lat_range[0], lat_range[1] + resolution, resolution)
    lons = np.arange(lon_range[0], lon_range[1] + resolution, resolution)

    lat_grid, lon_grid = np.meshgrid(lats, lons, indexing="ij")

    # Physics-based GHI model: peaks at equator, drops at poles
    lat_rad = np.radians(lat_grid)
    base_ghi = 7.0 * np.cos(lat_rad) ** 0.8

    # Longitude-dependent "desert boost" (Sahara, Arabian, Australian deserts)
    desert_boost = np.zeros_like(lon_grid)
    # Sahara/Arabian (lat 15-30N, lon -15 to 60)
    mask_sahara = (lat_grid > 10) & (lat_grid < 35) & (lon_grid > -15) & (lon_grid < 60)
    desert_boost[mask_sahara] = 0.8
    # Australian (lat -30 to -15, lon 115 to 150)
    mask_australia = (lat_grid > -35) & (lat_grid < -15) & (lon_grid > 115) & (lon_grid < 150)
    desert_boost[mask_australia] = 0.6
    # Southwest US (lat 25-40N, lon -120 to -100)
    mask_sw_us = (lat_grid > 25) & (lat_grid < 40) & (lon_grid > -120) & (lon_grid < -100)
    desert_boost[mask_sw_us] = 0.5

    ghi = base_ghi + desert_boost

    # Add spatial noise
    ghi += rng.normal(0, 0.15, ghi.shape)
    ghi = np.clip(ghi, 0.5, 9.0).astype(np.float32)

    ds = xr.Dataset(
        {"GHI": (["lat", "lon"], ghi)},
        coords={"lat": lats, "lon": lons},
        attrs={
            "source": "Synthetic global grid (solar-intelligence)",
            "units": "kWh/m^2/day",
            "description": "Global solar irradiance grid for Datashader visualization",
        },
    )
    return ds


# ---------------------------------------------------------------------------
# ERA5 CDS API Client
# ---------------------------------------------------------------------------

class ERA5Client(param.Parameterized):
    """Client for fetching solar radiation data from Copernicus ERA5 via CDS API.

    ERA5 provides global hourly reanalysis data at 0.25 degree resolution
    from 1940 to near real-time. Requires a free CDS account and API key.

    Setup:
        1. Register at https://cds.climate.copernicus.eu
        2. Create ~/.cdsapirc with your UID:API-KEY
        3. pip install cdsapi

    Parameters
    ----------
    cache_dir : Path
        Directory for caching downloaded ERA5 NetCDF files.
    variables : list[str]
        ERA5 variable names to fetch.
    dataset : str
        CDS dataset identifier.
    """

    cache_dir = param.Path(default=CACHE_DIR, doc="Cache directory for ERA5 data")
    variables = param.List(
        default=ERA5_SOLAR_VARIABLES,
        item_type=str,
        doc="ERA5 variable names to fetch",
    )
    dataset = param.String(default=ERA5_DATASET_NAME)

    def _era5_cache_key(
        self, lat: float, lon: float, start: str, end: str,
    ) -> str:
        """Generate a cache filename for ERA5 data."""
        raw = f"era5_{lat:.4f}_{lon:.4f}_{start}_{end}"
        digest = hashlib.sha256(raw.encode()).hexdigest()[:12]
        return f"era5_{digest}.nc"

    def _check_cdsapi(self):
        """Check that cdsapi is installed and configured."""
        try:
            import cdsapi  # noqa: F401
        except ImportError:
            raise ImportError(
                "cdsapi package not installed. Run: pip install cdsapi\n"
                "Then create ~/.cdsapirc with your CDS API key.\n"
                "Register free at https://cds.climate.copernicus.eu"
            )

    def fetch_daily(
        self,
        lat: float,
        lon: float,
        start_year: int = DEFAULT_START_YEAR,
        end_year: int = DEFAULT_END_YEAR,
        area_margin: float = 0.5,
    ) -> xr.Dataset:
        """Fetch daily-aggregated ERA5 solar data from CDS API.

        Downloads hourly data and aggregates to daily resolution
        to match NASA POWER format.

        Parameters
        ----------
        lat, lon : float
            Location coordinates.
        start_year, end_year : int
            Year range.
        area_margin : float
            Margin in degrees around the point for area selection.

        Returns
        -------
        xr.Dataset
            Daily dataset with standardized variable names matching
            NASA POWER format (GHI in kWh/m2/day, T in Celsius).
        """
        self._check_cdsapi()
        import cdsapi

        start = f"{start_year}0101"
        end = f"{end_year}1231"
        cache_file = Path(self.cache_dir) / self._era5_cache_key(lat, lon, start, end)

        if _cache_is_valid(cache_file):
            logger.info("Loading cached ERA5 data: %s", cache_file.name)
            return xr.open_dataset(cache_file)

        logger.info(
            "Fetching ERA5 data: lat=%.4f, lon=%.4f, %d-%d",
            lat, lon, start_year, end_year,
        )

        # Area bounding box: [North, West, South, East]
        area = [
            lat + area_margin,
            lon - area_margin,
            lat - area_margin,
            lon + area_margin,
        ]

        Path(self.cache_dir).mkdir(parents=True, exist_ok=True)
        client = cdsapi.Client()

        # Fetch in monthly chunks to avoid CDS cost limits
        monthly_files = []
        days = [str(d).zfill(2) for d in range(1, 32)]
        # Sample 4 times per day (enough for daily aggregation)
        hours = ["00:00", "06:00", "12:00", "18:00"]

        for year in range(start_year, end_year + 1):
            for month in range(1, 13):
                month_str = str(month).zfill(2)
                raw_file = Path(self.cache_dir) / f"era5_raw_{year}{month_str}_{hashlib.sha256(f'{lat}{lon}'.encode()).hexdigest()[:8]}.nc"

                if raw_file.exists():
                    monthly_files.append(raw_file)
                    continue

                logger.info("Fetching ERA5: %d-%s...", year, month_str)
                try:
                    client.retrieve(
                        self.dataset,
                        {
                            "product_type": ["reanalysis"],
                            "variable": self.variables,
                            "year": [str(year)],
                            "month": [month_str],
                            "day": days,
                            "time": hours,
                            "area": area,
                            "data_format": "netcdf",
                        },
                        str(raw_file),
                    )
                    monthly_files.append(raw_file)
                except Exception as e:
                    logger.warning("Failed to fetch %d-%s: %s", year, month_str, e)

        if not monthly_files:
            raise RuntimeError("No ERA5 data could be fetched")

        # CDS may return zip files -- extract NetCDF from them
        import zipfile

        nc_files = []
        for f in monthly_files:
            if zipfile.is_zipfile(f):
                extract_dir = f.parent / f"{f.stem}_extracted"
                extract_dir.mkdir(exist_ok=True)
                with zipfile.ZipFile(f, "r") as zf:
                    zf.extractall(extract_dir)
                # Find the .nc file inside
                nc_found = list(extract_dir.glob("*.nc"))
                if nc_found:
                    nc_files.append(nc_found[0])
                else:
                    logger.warning("No .nc file found in zip: %s", f)
            else:
                nc_files.append(f)

        if not nc_files:
            raise RuntimeError("No valid NetCDF files extracted from ERA5 downloads")

        # Merge all monthly files
        if len(nc_files) == 1:
            merged_ds = xr.open_dataset(nc_files[0])
        else:
            merged_ds = xr.open_mfdataset(nc_files, combine="by_coords")

        # Save merged raw to temp
        raw_merged = Path(self.cache_dir) / f"era5_merged_{hashlib.sha256(start.encode()).hexdigest()[:8]}.nc"
        merged_ds.to_netcdf(raw_merged)
        merged_ds.close()

        # Parse and convert to standard format
        ds = self._parse_era5(raw_merged, lat, lon)

        # Cache the processed result
        ds.to_netcdf(cache_file)
        logger.info("ERA5 data cached to: %s", cache_file.name)

        # Clean up raw files and extracted directories
        import shutil

        for f in monthly_files:
            if f.exists():
                f.unlink()
            extract_dir = f.parent / f"{f.stem}_extracted"
            if extract_dir.exists():
                shutil.rmtree(extract_dir)
        if raw_merged.exists():
            raw_merged.unlink()

        return ds

    def _parse_era5(
        self, path: Path, lat: float, lon: float,
    ) -> xr.Dataset:
        """Parse raw ERA5 NetCDF and convert to standard daily format.

        Handles:
        - Selecting nearest grid point to requested lat/lon
        - Aggregating hourly -> daily
        - Unit conversions: J/m2 -> kWh/m2/day, K -> C
        - Wind speed from u/v components
        - Variable renaming to NASA POWER standard names
        """
        ds = xr.open_dataset(path)

        # Select nearest grid point
        for lat_name in ("latitude", "lat"):
            if lat_name in ds.dims:
                ds = ds.sel(**{lat_name: lat}, method="nearest")
                break
        for lon_name in ("longitude", "lon"):
            if lon_name in ds.dims:
                ds = ds.sel(**{lon_name: lon}, method="nearest")
                break

        # Rename time if needed
        for time_name in ("valid_time", "time"):
            if time_name in ds.dims:
                if time_name != "time":
                    ds = ds.rename({time_name: "time"})
                break

        result_vars = {}

        # --- Solar radiation: J/m2 cumulative per hour -> kWh/m2/day ---
        # Determine sampling rate to scale sub-sampled data
        if "time" in ds.dims and len(ds.time) > 48:
            # Count unique hours in a day to determine sampling rate
            hours_per_day = len(set(ds.time.dt.hour.values))
            hours_per_day = min(hours_per_day, 24)
            scale_factor = 24 / max(hours_per_day, 1)
        else:
            scale_factor = 1.0

        if "ssrd" in ds:
            # ssrd is accumulated J/m2 per hour; sum over day then convert
            daily_ssrd = ds["ssrd"].resample(time="1D").sum()
            # Scale up if sub-sampled (e.g., 4 samples/day -> scale by 6)
            ghi = daily_ssrd * scale_factor / 3_600_000  # J/m2 -> kWh/m2
            ghi = ghi.clip(min=0)
            result_vars["ALLSKY_SFC_SW_DWN"] = ghi

        if "fdir" in ds:
            daily_fdir = ds["fdir"].resample(time="1D").sum()
            dni = daily_fdir * scale_factor / 3_600_000
            dni = dni.clip(min=0)
            result_vars["ALLSKY_SFC_SW_DNI"] = dni

        # Compute DHI = GHI - DNI * cos(zenith) ≈ GHI - DNI (simplified)
        if "ALLSKY_SFC_SW_DWN" in result_vars and "ALLSKY_SFC_SW_DNI" in result_vars:
            dhi = result_vars["ALLSKY_SFC_SW_DWN"] - result_vars["ALLSKY_SFC_SW_DNI"] * 0.6
            result_vars["ALLSKY_SFC_SW_DIFF"] = dhi.clip(min=0)

        # --- Temperature: K -> C ---
        if "t2m" in ds:
            t2m_daily = ds["t2m"].resample(time="1D").mean()
            if float(t2m_daily.mean()) > 200:
                t2m_daily = t2m_daily - 273.15
            result_vars["T2M"] = t2m_daily

            t2m_max = ds["t2m"].resample(time="1D").max()
            if float(t2m_max.mean()) > 200:
                t2m_max = t2m_max - 273.15
            result_vars["T2M_MAX"] = t2m_max

            t2m_min = ds["t2m"].resample(time="1D").min()
            if float(t2m_min.mean()) > 200:
                t2m_min = t2m_min - 273.15
            result_vars["T2M_MIN"] = t2m_min

        # --- Wind speed: combine u10 + v10 ---
        if "u10" in ds and "v10" in ds:
            u10_daily = ds["u10"].resample(time="1D").mean()
            v10_daily = ds["v10"].resample(time="1D").mean()
            ws = np.sqrt(u10_daily**2 + v10_daily**2)
            result_vars["WS2M"] = ws

        # --- Dewpoint -> Relative Humidity (approximation) ---
        if "d2m" in ds and "t2m" in ds:
            d2m_daily = ds["d2m"].resample(time="1D").mean()
            t2m_daily_rh = ds["t2m"].resample(time="1D").mean()
            if float(d2m_daily.mean()) > 200:
                d2m_daily = d2m_daily - 273.15
                t2m_daily_rh = t2m_daily_rh - 273.15
            # Magnus formula approximation for RH
            rh = 100 * np.exp((17.625 * d2m_daily) / (243.04 + d2m_daily)) / \
                np.exp((17.625 * t2m_daily_rh) / (243.04 + t2m_daily_rh))
            result_vars["RH2M"] = rh.clip(min=0, max=100)

        # --- Cloud cover ---
        if "tcc" in ds:
            tcc_daily = ds["tcc"].resample(time="1D").mean()
            result_vars["CLOUD_COVER"] = tcc_daily

        # --- Clearness index (GHI / theoretical clearsky) ---
        if "ALLSKY_SFC_SW_DWN" in result_vars:
            ghi_vals = result_vars["ALLSKY_SFC_SW_DWN"]
            # Simple clearsky estimate for KT
            doy = ghi_vals.time.dt.dayofyear
            lat_rad = np.radians(lat)
            decl = 23.45 * np.sin(np.radians((360 / 365) * (doy - 81)))
            cos_z = np.sin(lat_rad) * np.sin(np.radians(decl)) + \
                np.cos(lat_rad) * np.cos(np.radians(decl))
            cos_z = cos_z.clip(min=0.05)
            cos_hour = (-np.tan(lat_rad) * np.tan(np.radians(decl))).clip(-1, 1)
            day_length = (2 / 15) * np.degrees(np.arccos(cos_hour))
            clearsky = (1361 * cos_z * day_length * 0.75 / 1000).clip(min=0.1)
            kt = (ghi_vals / clearsky).clip(min=0, max=1)
            result_vars["ALLSKY_KT"] = kt
            result_vars["CLRSKY_SFC_SW_DWN"] = clearsky

        ds_out = xr.Dataset(result_vars)
        ds_out = ds_out.assign_coords(latitude=lat, longitude=lon)
        ds_out.attrs["source"] = "ERA5 (Copernicus Climate Data Store)"
        ds_out.attrs["temporal_resolution"] = "daily"
        ds_out.attrs["latitude"] = lat
        ds_out.attrs["longitude"] = lon
        ds_out.attrs["fetched_at"] = datetime.now().isoformat()

        # Variable attributes
        var_attrs = {
            "ALLSKY_SFC_SW_DWN": {"long_name": "GHI (ERA5)", "units": "kWh/m²/day"},
            "CLRSKY_SFC_SW_DWN": {"long_name": "Clear Sky GHI (estimated)", "units": "kWh/m²/day"},
            "ALLSKY_SFC_SW_DNI": {"long_name": "DNI (ERA5)", "units": "kWh/m²/day"},
            "ALLSKY_SFC_SW_DIFF": {"long_name": "DHI (ERA5, derived)", "units": "kWh/m²/day"},
            "ALLSKY_KT": {"long_name": "Clearness Index", "units": "dimensionless"},
            "T2M": {"long_name": "Temperature at 2m", "units": "°C"},
            "T2M_MAX": {"long_name": "Max Temperature at 2m", "units": "°C"},
            "T2M_MIN": {"long_name": "Min Temperature at 2m", "units": "°C"},
            "WS2M": {"long_name": "Wind Speed at 10m", "units": "m/s"},
            "RH2M": {"long_name": "Relative Humidity at 2m", "units": "%"},
            "CLOUD_COVER": {"long_name": "Total Cloud Cover", "units": "fraction"},
        }
        for var_name, attrs in var_attrs.items():
            if var_name in ds_out:
                ds_out[var_name].attrs.update(attrs)

        return ds_out

    def fetch_monthly(
        self,
        lat: float,
        lon: float,
        start_year: int = DEFAULT_START_YEAR,
        end_year: int = DEFAULT_END_YEAR,
    ) -> xr.Dataset:
        """Fetch ERA5 data and aggregate to monthly resolution.

        Parameters
        ----------
        lat, lon : float
            Location coordinates.
        start_year, end_year : int
            Year range.

        Returns
        -------
        xr.Dataset
            Monthly-averaged dataset.
        """
        daily = self.fetch_daily(lat, lon, start_year, end_year)
        monthly_vars = {}
        for var in daily.data_vars:
            monthly_vars[var] = daily[var].resample(time="1ME").mean()

        ds_monthly = xr.Dataset(monthly_vars)
        ds_monthly.attrs = daily.attrs.copy()
        ds_monthly.attrs["temporal_resolution"] = "monthly"
        return ds_monthly


# ---------------------------------------------------------------------------
# Dual-Source Data Loader (NASA POWER + ERA5)
# ---------------------------------------------------------------------------

class DualSourceLoader(param.Parameterized):
    """Fetch solar data from both NASA POWER and ERA5 for cross-validation.

    Provides a unified interface to load data from both sources,
    align them on a common time axis, and compute comparison metrics.

    Parameters
    ----------
    use_era5 : bool
        Whether to fetch ERA5 data (requires CDS API setup).
    use_nasa : bool
        Whether to fetch NASA POWER data.
    """

    use_era5 = param.Boolean(default=True, doc="Fetch ERA5 data")
    use_nasa = param.Boolean(default=True, doc="Fetch NASA POWER data")

    _nasa_client = param.Parameter(default=None, precedence=-1)
    _era5_client = param.Parameter(default=None, precedence=-1)

    def __init__(self, **params):
        super().__init__(**params)
        if self.use_nasa:
            self._nasa_client = NASAPowerClient()
        if self.use_era5:
            self._era5_client = ERA5Client()

    def fetch(
        self,
        lat: float,
        lon: float,
        start_year: int = DEFAULT_START_YEAR,
        end_year: int = DEFAULT_END_YEAR,
    ) -> dict[str, xr.Dataset]:
        """Fetch data from all enabled sources.

        Parameters
        ----------
        lat, lon : float
            Location coordinates.
        start_year, end_year : int
            Year range.

        Returns
        -------
        dict[str, xr.Dataset]
            Keys are source names ("nasa_power", "era5"), values are datasets.
        """
        results = {}

        if self.use_nasa and self._nasa_client:
            try:
                start = f"{start_year}0101"
                end = f"{end_year}1231"
                results["nasa_power"] = self._nasa_client.fetch_daily(
                    lat, lon, start, end,
                )
                logger.info("NASA POWER data loaded: %d days", len(results["nasa_power"].time))
            except Exception as e:
                logger.error("NASA POWER fetch failed: %s", e)

        if self.use_era5 and self._era5_client:
            try:
                results["era5"] = self._era5_client.fetch_daily(
                    lat, lon, start_year, end_year,
                )
                logger.info("ERA5 data loaded: %d days", len(results["era5"].time))
            except Exception as e:
                logger.error("ERA5 fetch failed: %s", e)

        return results

    @staticmethod
    def align_datasets(
        datasets: dict[str, xr.Dataset],
        variable: str = "ALLSKY_SFC_SW_DWN",
    ) -> pd.DataFrame:
        """Align multiple source datasets on common time axis.

        Parameters
        ----------
        datasets : dict[str, xr.Dataset]
            Source name -> dataset mapping.
        variable : str
            Variable to extract and align.

        Returns
        -------
        pd.DataFrame
            DataFrame with time index and one column per source.
        """
        series = {}
        for name, ds in datasets.items():
            if variable in ds.data_vars:
                s = ds[variable].to_series()
                s.index = pd.to_datetime(s.index)
                series[name] = s

        if not series:
            return pd.DataFrame()

        df = pd.DataFrame(series)
        df.index.name = "time"
        return df

    @staticmethod
    def comparison_stats(
        datasets: dict[str, xr.Dataset],
        variable: str = "ALLSKY_SFC_SW_DWN",
    ) -> dict[str, Any]:
        """Compute comparison statistics between sources.

        Parameters
        ----------
        datasets : dict[str, xr.Dataset]
            Source name -> dataset mapping.
        variable : str
            Variable to compare.

        Returns
        -------
        dict
            Statistics including per-source means, correlation, RMSE, bias.
        """
        aligned = DualSourceLoader.align_datasets(datasets, variable)
        if aligned.empty or len(aligned.columns) < 2:
            return {"error": "Need at least 2 sources for comparison"}

        stats = {"variable": variable, "sources": {}}

        for col in aligned.columns:
            stats["sources"][col] = {
                "mean": float(aligned[col].mean()),
                "std": float(aligned[col].std()),
                "min": float(aligned[col].min()),
                "max": float(aligned[col].max()),
                "count": int(aligned[col].count()),
            }

        # Pairwise comparison between first two sources
        cols = list(aligned.columns)
        common = aligned[cols].dropna()
        if len(common) > 10:
            a, b = common[cols[0]], common[cols[1]]
            stats["comparison"] = {
                "source_a": cols[0],
                "source_b": cols[1],
                "correlation": float(a.corr(b)),
                "rmse": float(np.sqrt(((a - b) ** 2).mean())),
                "mae": float((a - b).abs().mean()),
                "bias": float((a - b).mean()),
                "bias_pct": float((a - b).mean() / a.mean() * 100),
                "common_days": len(common),
            }

        return stats


# ---------------------------------------------------------------------------
# ERA5 / SARAH-3 Dataset Loader (local files)
# ---------------------------------------------------------------------------

class ClimateDatasetLoader(param.Parameterized):
    """Load solar radiation data from ERA5 or SARAH-3 climate datasets.

    ERA5 (Copernicus Climate Data Store):
        - Global reanalysis dataset, 0.25 deg resolution, hourly
        - Variable: 'ssrd' (surface solar radiation downwards, J/m2)
        - Requires CDS API key and cdsapi package

    SARAH-3 (CM SAF):
        - Satellite-derived surface radiation, Europe/Africa, 0.05 deg resolution
        - Variable: 'SIS' (Surface Incoming Shortwave radiation, W/m2)
        - NetCDF format from CM SAF web interface

    Parameters
    ----------
    era5_var_map : dict
        Mapping of ERA5 variable names to standard names.
    sarah_var_map : dict
        Mapping of SARAH-3 variable names to standard names.
    """

    era5_var_map = param.Dict(
        default={
            "ssrd": "ALLSKY_SFC_SW_DWN",
            "fdir": "ALLSKY_SFC_SW_DNI",
            "t2m": "T2M",
            "u10": "WS2M",
        },
        doc="ERA5 variable name mapping",
    )

    sarah_var_map = param.Dict(
        default={
            "SIS": "ALLSKY_SFC_SW_DWN",
            "SID": "ALLSKY_SFC_SW_DNI",
            "SDU": "ALLSKY_SFC_SW_DIFF",
        },
        doc="SARAH-3 variable name mapping",
    )

    def load_era5(
        self,
        path: str | Path,
        lat: float | None = None,
        lon: float | None = None,
    ) -> xr.Dataset:
        """Load an ERA5 NetCDF file and convert to standard format.

        Handles unit conversions:
        - ssrd: J/m2 cumulative -> kWh/m2/day
        - t2m: Kelvin -> Celsius

        Parameters
        ----------
        path : str or Path
            Path to ERA5 NetCDF file.
        lat, lon : float, optional
            If provided, select nearest grid point.

        Returns
        -------
        xr.Dataset
            Standardized dataset matching NASA POWER variable structure.
        """
        path = Path(path)
        if not path.exists():
            raise FileNotFoundError(f"ERA5 file not found: {path}")

        ds = xr.open_dataset(path)

        # Select location if specified
        if lat is not None and lon is not None:
            for lat_name in ("latitude", "lat"):
                if lat_name in ds.dims:
                    ds = ds.sel(**{lat_name: lat}, method="nearest")
                    break
            for lon_name in ("longitude", "lon"):
                if lon_name in ds.dims:
                    ds = ds.sel(**{lon_name: lon}, method="nearest")
                    break

        # Rename time dimension if needed
        for time_name in ("valid_time", "time"):
            if time_name in ds.dims:
                if time_name != "time":
                    ds = ds.rename({time_name: "time"})
                break

        # Convert variables
        result_vars = {}
        for era5_name, std_name in self.era5_var_map.items():
            if era5_name not in ds:
                continue
            data = ds[era5_name]

            if era5_name in ("ssrd", "fdir"):
                # J/m2 -> kWh/m2
                data = data / 3_600_000
                data = data.clip(min=0)

            elif era5_name == "t2m":
                # Kelvin -> Celsius
                if float(data.mean()) > 200:
                    data = data - 273.15

            result_vars[std_name] = data

        ds_out = xr.Dataset(result_vars)
        ds_out.attrs["source"] = "ERA5 (Copernicus Climate Data Store)"
        ds_out.attrs["original_file"] = str(path)

        return ds_out

    def load_sarah3(
        self,
        path: str | Path,
        lat: float | None = None,
        lon: float | None = None,
    ) -> xr.Dataset:
        """Load a SARAH-3 NetCDF file and convert to standard format.

        Handles unit conversions:
        - SIS/SID/SDU: W/m2 (daily mean) -> kWh/m2/day

        Parameters
        ----------
        path : str or Path
            Path to SARAH-3 NetCDF file.
        lat, lon : float, optional
            If provided, select nearest grid point.

        Returns
        -------
        xr.Dataset
            Standardized dataset matching NASA POWER variable structure.
        """
        path = Path(path)
        if not path.exists():
            raise FileNotFoundError(f"SARAH-3 file not found: {path}")

        ds = xr.open_dataset(path)

        if lat is not None and lon is not None:
            for lat_name in ("lat", "latitude"):
                if lat_name in ds.dims:
                    ds = ds.sel(**{lat_name: lat}, method="nearest")
                    break
            for lon_name in ("lon", "longitude"):
                if lon_name in ds.dims:
                    ds = ds.sel(**{lon_name: lon}, method="nearest")
                    break

        result_vars = {}
        for sarah_name, std_name in self.sarah_var_map.items():
            if sarah_name not in ds:
                continue
            data = ds[sarah_name]

            # W/m2 daily mean -> kWh/m2/day = W/m2 * 24 / 1000
            data = data * 24 / 1000
            data = data.clip(min=0)

            result_vars[std_name] = data

        ds_out = xr.Dataset(result_vars)
        ds_out.attrs["source"] = "SARAH-3 (CM SAF)"
        ds_out.attrs["original_file"] = str(path)

        return ds_out


# ---------------------------------------------------------------------------
# Multi-Location Comparison Utility
# ---------------------------------------------------------------------------

def generate_multi_location_data(
    locations: dict[str, tuple[float, float]],
    start_year: int = DEFAULT_START_YEAR,
    end_year: int = DEFAULT_END_YEAR,
) -> dict[str, xr.Dataset]:
    """Generate synthetic solar data for multiple locations.

    Parameters
    ----------
    locations : dict
        Mapping of location name -> (lat, lon).
    start_year, end_year : int
        Date range.

    Returns
    -------
    dict[str, xr.Dataset]
        Mapping of location name -> dataset.
    """
    datasets = {}
    for name, (lat, lon) in locations.items():
        datasets[name] = generate_synthetic_solar_data(lat, lon, start_year, end_year)
    return datasets