ecad-processor 2.0.1

High-performance multi-metric weather data processor for European Climate Assessment & Dataset (ECA&D) archives with Parquet output
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
use crate::error::Result;
use crate::models::{ConsolidatedRecord, WeatherRecord};
use crate::utils::constants::DEFAULT_ROW_GROUP_SIZE;
use arrow::array::*;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use chrono::Datelike;
use parquet::arrow::ArrowWriter;
use parquet::basic::{Compression, GzipLevel};
use parquet::file::properties::WriterProperties;
use std::fs::File;
use std::path::Path;
use std::sync::Arc;

pub struct ParquetWriter {
    compression: Compression,
    row_group_size: usize,
}

impl ParquetWriter {
    pub fn new() -> Self {
        Self {
            compression: Compression::SNAPPY,
            row_group_size: DEFAULT_ROW_GROUP_SIZE,
        }
    }

    pub fn with_compression(mut self, compression: &str) -> Result<Self> {
        self.compression = match compression.to_lowercase().as_str() {
            "snappy" => Compression::SNAPPY,
            "gzip" => Compression::GZIP(GzipLevel::default()),
            "lz4" => Compression::LZ4,
            "zstd" => Compression::ZSTD(parquet::basic::ZstdLevel::default()),
            "none" => Compression::UNCOMPRESSED,
            _ => {
                return Err(crate::error::ProcessingError::Config(format!(
                    "Unsupported compression: {}",
                    compression
                )))
            }
        };
        Ok(self)
    }

    pub fn with_row_group_size(mut self, size: usize) -> Self {
        self.row_group_size = size;
        self
    }

    /// Write consolidated records to Parquet file
    pub fn write_records(&self, records: &[ConsolidatedRecord], path: &Path) -> Result<()> {
        if records.is_empty() {
            return Ok(());
        }

        let schema = self.create_schema();
        let batch = self.records_to_batch(records, schema.clone())?;

        let file = File::create(path)?;
        let props = WriterProperties::builder()
            .set_compression(self.compression)
            .set_max_row_group_size(self.row_group_size)
            .build();

        let mut writer = ArrowWriter::try_new(file, schema, Some(props))?;
        writer.write(&batch)?;
        writer.close()?;

        Ok(())
    }

    /// Write records in batches for memory efficiency
    pub fn write_records_batched(
        &self,
        records: &[ConsolidatedRecord],
        path: &Path,
        batch_size: usize,
    ) -> Result<()> {
        if records.is_empty() {
            return Ok(());
        }

        let schema = self.create_schema();
        let file = File::create(path)?;
        let props = WriterProperties::builder()
            .set_compression(self.compression)
            .set_max_row_group_size(self.row_group_size)
            .build();

        let mut writer = ArrowWriter::try_new(file, schema.clone(), Some(props))?;

        // Write in batches
        for chunk in records.chunks(batch_size) {
            let batch = self.records_to_batch(chunk, schema.clone())?;
            writer.write(&batch)?;
        }

        writer.close()?;
        Ok(())
    }

    /// Create Arrow schema for temperature data
    fn create_schema(&self) -> Arc<Schema> {
        let fields = vec![
            Field::new("station_id", DataType::UInt32, false),
            Field::new("station_name", DataType::Utf8, false),
            Field::new("date", DataType::Date32, false),
            Field::new("latitude", DataType::Float64, false),
            Field::new("longitude", DataType::Float64, false),
            Field::new("min_temp", DataType::Float32, false),
            Field::new("max_temp", DataType::Float32, false),
            Field::new("avg_temp", DataType::Float32, false),
            Field::new("quality_flags", DataType::Utf8, false),
        ];

        Arc::new(Schema::new(fields))
    }

    /// Convert records to Arrow RecordBatch
    fn records_to_batch(
        &self,
        records: &[ConsolidatedRecord],
        schema: Arc<Schema>,
    ) -> Result<RecordBatch> {
        // Extract data into separate vectors
        let station_ids: Vec<u32> = records.iter().map(|r| r.station_id).collect();
        let station_names: Vec<String> = records.iter().map(|r| r.station_name.clone()).collect();
        let dates: Vec<i32> = records
            .iter()
            .map(|r| {
                r.date
                    .signed_duration_since(chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())
                    .num_days() as i32
            })
            .collect();
        let latitudes: Vec<f64> = records.iter().map(|r| r.latitude).collect();
        let longitudes: Vec<f64> = records.iter().map(|r| r.longitude).collect();
        let min_temps: Vec<f32> = records.iter().map(|r| r.min_temp).collect();
        let max_temps: Vec<f32> = records.iter().map(|r| r.max_temp).collect();
        let avg_temps: Vec<f32> = records.iter().map(|r| r.avg_temp).collect();
        let quality_flags: Vec<String> = records.iter().map(|r| r.quality_flags.clone()).collect();

        // Create Arrow arrays
        let station_id_array = Arc::new(UInt32Array::from(station_ids));
        let station_name_array = Arc::new(StringArray::from(station_names));
        let date_array = Arc::new(Date32Array::from(dates));
        let latitude_array = Arc::new(Float64Array::from(latitudes));
        let longitude_array = Arc::new(Float64Array::from(longitudes));
        let min_temp_array = Arc::new(Float32Array::from(min_temps));
        let max_temp_array = Arc::new(Float32Array::from(max_temps));
        let avg_temp_array = Arc::new(Float32Array::from(avg_temps));
        let quality_flags_array = Arc::new(StringArray::from(quality_flags));

        // Create record batch
        let batch = RecordBatch::try_new(
            schema,
            vec![
                station_id_array,
                station_name_array,
                date_array,
                latitude_array,
                longitude_array,
                min_temp_array,
                max_temp_array,
                avg_temp_array,
                quality_flags_array,
            ],
        )?;

        Ok(batch)
    }

    /// Read sample records from Parquet file
    pub fn read_sample_records(
        &self,
        path: &Path,
        limit: usize,
    ) -> Result<Vec<ConsolidatedRecord>> {
        use arrow::array::*;
        use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;

        let file = File::open(path)?;
        let parquet_reader = ParquetRecordBatchReaderBuilder::try_new(file)?
            .with_batch_size(limit.min(8192))
            .build()?;

        let mut records = Vec::new();
        let mut total_read = 0;

        for batch_result in parquet_reader {
            let batch = batch_result?;

            // Extract arrays from the batch
            let station_ids = batch
                .column(0)
                .as_any()
                .downcast_ref::<UInt32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid station_id column type".to_string(),
                    )
                })?;
            let station_names = batch
                .column(1)
                .as_any()
                .downcast_ref::<StringArray>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid station_name column type".to_string(),
                    )
                })?;
            let dates = batch
                .column(2)
                .as_any()
                .downcast_ref::<Date32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config("Invalid date column type".to_string())
                })?;
            let latitudes = batch
                .column(3)
                .as_any()
                .downcast_ref::<Float64Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid latitude column type".to_string(),
                    )
                })?;
            let longitudes = batch
                .column(4)
                .as_any()
                .downcast_ref::<Float64Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid longitude column type".to_string(),
                    )
                })?;
            let min_temps = batch
                .column(5)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid min_temp column type".to_string(),
                    )
                })?;
            let max_temps = batch
                .column(6)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid max_temp column type".to_string(),
                    )
                })?;
            let avg_temps = batch
                .column(7)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid avg_temp column type".to_string(),
                    )
                })?;
            let quality_flags = batch
                .column(8)
                .as_any()
                .downcast_ref::<StringArray>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid quality_flags column type".to_string(),
                    )
                })?;

            // Convert to ConsolidatedRecord objects
            let batch_records_to_read = (batch.num_rows()).min(limit - total_read);

            for i in 0..batch_records_to_read {
                let date = chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
                    + chrono::Duration::days(dates.value(i) as i64);

                let record = ConsolidatedRecord::new(
                    station_ids.value(i),
                    station_names.value(i).to_string(),
                    date,
                    latitudes.value(i),
                    longitudes.value(i),
                    min_temps.value(i),
                    max_temps.value(i),
                    avg_temps.value(i),
                    quality_flags.value(i).to_string(),
                );

                records.push(record);
                total_read += 1;

                if total_read >= limit {
                    break;
                }
            }

            if total_read >= limit {
                break;
            }
        }

        Ok(records)
    }

    /// Get file statistics
    pub fn get_file_info(&self, path: &Path) -> Result<ParquetFileInfo> {
        use parquet::file::reader::{FileReader, SerializedFileReader};
        use std::fs::File;

        let file = File::open(path)?;
        let reader = SerializedFileReader::new(file)?;
        let metadata = reader.metadata();

        let file_metadata = metadata.file_metadata();
        let row_groups = metadata.num_row_groups();
        let total_rows = file_metadata.num_rows();
        let file_size = std::fs::metadata(path)?.len();

        let mut row_group_sizes = Vec::new();
        for i in 0..row_groups {
            let rg_metadata = metadata.row_group(i);
            row_group_sizes.push(rg_metadata.num_rows());
        }

        Ok(ParquetFileInfo {
            total_rows,
            row_groups: row_groups as i32,
            row_group_sizes,
            file_size,
            compression: self.compression,
        })
    }

    /// Write weather records to Parquet file with optional fields
    pub fn write_weather_records(&self, records: &[WeatherRecord], path: &Path) -> Result<()> {
        if records.is_empty() {
            return Ok(());
        }

        let schema = self.create_weather_schema();
        let batch = self.weather_records_to_batch(records, schema.clone())?;

        let file = File::create(path)?;
        let props = WriterProperties::builder()
            .set_compression(self.compression)
            .set_max_row_group_size(self.row_group_size)
            .build();

        let mut writer = ArrowWriter::try_new(file, schema, Some(props))?;
        writer.write(&batch)?;
        writer.close()?;

        Ok(())
    }

    /// Write weather records in batches for memory efficiency
    pub fn write_weather_records_batched(
        &self,
        records: &[WeatherRecord],
        path: &Path,
        batch_size: usize,
    ) -> Result<()> {
        if records.is_empty() {
            return Ok(());
        }

        let schema = self.create_weather_schema();
        let file = File::create(path)?;
        let props = WriterProperties::builder()
            .set_compression(self.compression)
            .set_max_row_group_size(self.row_group_size)
            .build();

        let mut writer = ArrowWriter::try_new(file, schema.clone(), Some(props))?;

        // Write in batches
        for chunk in records.chunks(batch_size) {
            let batch = self.weather_records_to_batch(chunk, schema.clone())?;
            writer.write(&batch)?;
        }

        writer.close()?;
        Ok(())
    }

    /// Create Arrow schema for multi-metric weather data
    fn create_weather_schema(&self) -> Arc<Schema> {
        let fields = vec![
            Field::new("station_id", DataType::UInt32, false),
            Field::new("station_name", DataType::Utf8, false),
            Field::new("date", DataType::Date32, false),
            Field::new("latitude", DataType::Float64, false),
            Field::new("longitude", DataType::Float64, false),
            // Optional temperature fields
            Field::new("temp_min", DataType::Float32, true),
            Field::new("temp_max", DataType::Float32, true),
            Field::new("temp_avg", DataType::Float32, true),
            // Optional precipitation field
            Field::new("precipitation", DataType::Float32, true),
            // Optional wind speed field
            Field::new("wind_speed", DataType::Float32, true),
            // Quality flag fields (original ECAD)
            Field::new("temp_quality", DataType::Utf8, true),
            Field::new("precip_quality", DataType::Utf8, true),
            Field::new("wind_quality", DataType::Utf8, true),
            // Physical validation fields
            Field::new("temp_validation", DataType::Utf8, true),
            Field::new("precip_validation", DataType::Utf8, true),
            Field::new("wind_validation", DataType::Utf8, true),
        ];

        Arc::new(Schema::new(fields))
    }

    /// Convert weather records to Arrow RecordBatch
    fn weather_records_to_batch(
        &self,
        records: &[WeatherRecord],
        schema: Arc<Schema>,
    ) -> Result<RecordBatch> {
        // Extract data into separate vectors
        let station_ids: Vec<u32> = records.iter().map(|r| r.station_id).collect();
        let station_names: Vec<String> = records.iter().map(|r| r.station_name.clone()).collect();
        let dates: Vec<i32> = records
            .iter()
            .map(|r| {
                r.date
                    .signed_duration_since(chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())
                    .num_days() as i32
            })
            .collect();
        let latitudes: Vec<f64> = records.iter().map(|r| r.latitude).collect();
        let longitudes: Vec<f64> = records.iter().map(|r| r.longitude).collect();

        // Temperature data (optional)
        let temp_mins: Vec<Option<f32>> = records.iter().map(|r| r.temp_min).collect();
        let temp_maxs: Vec<Option<f32>> = records.iter().map(|r| r.temp_max).collect();
        let temp_avgs: Vec<Option<f32>> = records.iter().map(|r| r.temp_avg).collect();

        // Other weather metrics (optional)
        let precipitations: Vec<Option<f32>> = records.iter().map(|r| r.precipitation).collect();
        let wind_speeds: Vec<Option<f32>> = records.iter().map(|r| r.wind_speed).collect();

        // Quality flags (optional)
        let temp_qualities: Vec<Option<String>> =
            records.iter().map(|r| r.temp_quality.clone()).collect();
        let precip_qualities: Vec<Option<String>> =
            records.iter().map(|r| r.precip_quality.clone()).collect();
        let wind_qualities: Vec<Option<String>> =
            records.iter().map(|r| r.wind_quality.clone()).collect();

        // Physical validation flags (optional)
        let temp_validations: Vec<Option<String>> = records
            .iter()
            .map(|r| r.temp_validation.map(|v| format!("{:?}", v)))
            .collect();
        let precip_validations: Vec<Option<String>> = records
            .iter()
            .map(|r| r.precip_validation.map(|v| format!("{:?}", v)))
            .collect();
        let wind_validations: Vec<Option<String>> = records
            .iter()
            .map(|r| r.wind_validation.map(|v| format!("{:?}", v)))
            .collect();

        // Create Arrow arrays
        let station_id_array = Arc::new(UInt32Array::from(station_ids));
        let station_name_array = Arc::new(StringArray::from(station_names));
        let date_array = Arc::new(Date32Array::from(dates));
        let latitude_array = Arc::new(Float64Array::from(latitudes));
        let longitude_array = Arc::new(Float64Array::from(longitudes));

        // Create optional arrays
        let temp_min_array = Arc::new(Float32Array::from(temp_mins));
        let temp_max_array = Arc::new(Float32Array::from(temp_maxs));
        let temp_avg_array = Arc::new(Float32Array::from(temp_avgs));
        let precipitation_array = Arc::new(Float32Array::from(precipitations));
        let wind_speed_array = Arc::new(Float32Array::from(wind_speeds));

        let temp_quality_array = Arc::new(StringArray::from(temp_qualities));
        let precip_quality_array = Arc::new(StringArray::from(precip_qualities));
        let wind_quality_array = Arc::new(StringArray::from(wind_qualities));

        let temp_validation_array = Arc::new(StringArray::from(temp_validations));
        let precip_validation_array = Arc::new(StringArray::from(precip_validations));
        let wind_validation_array = Arc::new(StringArray::from(wind_validations));

        // Create record batch
        let batch = RecordBatch::try_new(
            schema,
            vec![
                station_id_array,
                station_name_array,
                date_array,
                latitude_array,
                longitude_array,
                temp_min_array,
                temp_max_array,
                temp_avg_array,
                precipitation_array,
                wind_speed_array,
                temp_quality_array,
                precip_quality_array,
                wind_quality_array,
                temp_validation_array,
                precip_validation_array,
                wind_validation_array,
            ],
        )?;

        Ok(batch)
    }

    /// Read sample weather records from Parquet file
    pub fn read_sample_weather_records(
        &self,
        path: &Path,
        limit: usize,
    ) -> Result<Vec<WeatherRecord>> {
        use arrow::array::*;
        use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;

        let file = File::open(path)?;
        let parquet_reader = ParquetRecordBatchReaderBuilder::try_new(file)?
            .with_batch_size(limit.min(8192))
            .build()?;

        let mut records = Vec::new();
        let mut total_read = 0;

        for batch_result in parquet_reader {
            let batch = batch_result?;

            // Extract arrays from the batch - handle both old and new schema
            let num_columns = batch.num_columns();

            if num_columns < 13 {
                // Old schema format - return empty for now
                return Ok(Vec::new());
            }

            let has_validation_fields = num_columns >= 16;

            // New WeatherRecord schema
            let station_ids = batch
                .column(0)
                .as_any()
                .downcast_ref::<UInt32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid station_id column type".to_string(),
                    )
                })?;
            let station_names = batch
                .column(1)
                .as_any()
                .downcast_ref::<StringArray>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid station_name column type".to_string(),
                    )
                })?;
            let dates = batch
                .column(2)
                .as_any()
                .downcast_ref::<Date32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config("Invalid date column type".to_string())
                })?;
            let latitudes = batch
                .column(3)
                .as_any()
                .downcast_ref::<Float64Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid latitude column type".to_string(),
                    )
                })?;
            let longitudes = batch
                .column(4)
                .as_any()
                .downcast_ref::<Float64Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid longitude column type".to_string(),
                    )
                })?;

            // Optional temperature fields
            let temp_mins = batch
                .column(5)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid temp_min column type".to_string(),
                    )
                })?;
            let temp_maxs = batch
                .column(6)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid temp_max column type".to_string(),
                    )
                })?;
            let temp_avgs = batch
                .column(7)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid temp_avg column type".to_string(),
                    )
                })?;

            // Optional other weather metrics
            let precipitations = batch
                .column(8)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid precipitation column type".to_string(),
                    )
                })?;
            let wind_speeds = batch
                .column(9)
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid wind_speed column type".to_string(),
                    )
                })?;

            // Optional quality flags
            let temp_qualities = batch
                .column(10)
                .as_any()
                .downcast_ref::<StringArray>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid temp_quality column type".to_string(),
                    )
                })?;
            let precip_qualities = batch
                .column(11)
                .as_any()
                .downcast_ref::<StringArray>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid precip_quality column type".to_string(),
                    )
                })?;
            let wind_qualities = batch
                .column(12)
                .as_any()
                .downcast_ref::<StringArray>()
                .ok_or_else(|| {
                    crate::error::ProcessingError::Config(
                        "Invalid wind_quality column type".to_string(),
                    )
                })?;

            // Optional validation fields (new schema)
            let temp_validations = if has_validation_fields {
                Some(
                    batch
                        .column(13)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .ok_or_else(|| {
                            crate::error::ProcessingError::Config(
                                "Invalid temp_validation column type".to_string(),
                            )
                        })?,
                )
            } else {
                None
            };
            let precip_validations = if has_validation_fields {
                Some(
                    batch
                        .column(14)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .ok_or_else(|| {
                            crate::error::ProcessingError::Config(
                                "Invalid precip_validation column type".to_string(),
                            )
                        })?,
                )
            } else {
                None
            };
            let wind_validations = if has_validation_fields {
                Some(
                    batch
                        .column(15)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .ok_or_else(|| {
                            crate::error::ProcessingError::Config(
                                "Invalid wind_validation column type".to_string(),
                            )
                        })?,
                )
            } else {
                None
            };

            // Convert to WeatherRecord objects
            let batch_records_to_read = (batch.num_rows()).min(limit - total_read);

            for i in 0..batch_records_to_read {
                let date = chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
                    + chrono::Duration::days(dates.value(i) as i64);

                use crate::models::weather::PhysicalValidity;

                let record = WeatherRecord::new_raw(
                    station_ids.value(i),
                    station_names.value(i).to_string(),
                    date,
                    latitudes.value(i),
                    longitudes.value(i),
                    if temp_mins.is_null(i) {
                        None
                    } else {
                        Some(temp_mins.value(i))
                    },
                    if temp_maxs.is_null(i) {
                        None
                    } else {
                        Some(temp_maxs.value(i))
                    },
                    if temp_avgs.is_null(i) {
                        None
                    } else {
                        Some(temp_avgs.value(i))
                    },
                    if precipitations.is_null(i) {
                        None
                    } else {
                        Some(precipitations.value(i))
                    },
                    if wind_speeds.is_null(i) {
                        None
                    } else {
                        Some(wind_speeds.value(i))
                    },
                    if temp_qualities.is_null(i) {
                        None
                    } else {
                        Some(temp_qualities.value(i).to_string())
                    },
                    if precip_qualities.is_null(i) {
                        None
                    } else {
                        Some(precip_qualities.value(i).to_string())
                    },
                    if wind_qualities.is_null(i) {
                        None
                    } else {
                        Some(wind_qualities.value(i).to_string())
                    },
                    // Parse validation fields if available
                    temp_validations.and_then(|arr| {
                        if arr.is_null(i) {
                            None
                        } else {
                            PhysicalValidity::parse(arr.value(i))
                        }
                    }),
                    precip_validations.and_then(|arr| {
                        if arr.is_null(i) {
                            None
                        } else {
                            PhysicalValidity::parse(arr.value(i))
                        }
                    }),
                    wind_validations.and_then(|arr| {
                        if arr.is_null(i) {
                            None
                        } else {
                            PhysicalValidity::parse(arr.value(i))
                        }
                    }),
                );

                records.push(record);
                total_read += 1;

                if total_read >= limit {
                    break;
                }
            }

            if total_read >= limit {
                break;
            }
        }

        Ok(records)
    }

    /// Detect the schema type of a Parquet file
    pub fn detect_schema_type(&self, path: &Path) -> Result<SchemaType> {
        use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;

        let file = File::open(path)?;
        let builder = ParquetRecordBatchReaderBuilder::try_new(file)?;
        let schema = builder.schema();

        // Check number of columns to determine schema type
        let num_columns = schema.fields().len();

        if num_columns == 9 {
            // Old ConsolidatedRecord schema: station_id, station_name, date, lat, lon, min_temp, max_temp, avg_temp, quality_flags
            Ok(SchemaType::ConsolidatedRecord)
        } else if num_columns == 13 || num_columns == 16 {
            // WeatherRecord schema: 13 cols = original, 16 cols = with validation fields
            Ok(SchemaType::WeatherRecord)
        } else {
            Ok(SchemaType::Unknown)
        }
    }

    /// Analyze a WeatherRecord Parquet file comprehensively
    pub fn analyze_weather_dataset(
        &self,
        path: &Path,
        sample_size: usize,
    ) -> Result<WeatherDatasetSummary> {
        use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
        use std::collections::HashSet;

        let file = File::open(path)?;
        let parquet_reader = ParquetRecordBatchReaderBuilder::try_new(file)?.build()?;

        let mut total_records = 0;
        let mut stations: HashSet<u32> = HashSet::new();
        let mut all_records = Vec::new();

        // Bounds tracking
        let mut min_lat = f64::MAX;
        let mut max_lat = f64::MIN;
        let mut min_lon = f64::MAX;
        let mut max_lon = f64::MIN;
        let mut min_date = None;
        let mut max_date = None;

        // Metric statistics
        let mut temp_records = 0;
        let mut temp_stations: HashSet<u32> = HashSet::new();
        let mut temp_dates = Vec::new();
        let mut precip_records = 0;
        let mut precip_stations: HashSet<u32> = HashSet::new();
        let mut precip_dates = Vec::new();
        let mut wind_records = 0;
        let mut wind_stations: HashSet<u32> = HashSet::new();
        let mut wind_dates = Vec::new();

        // Extreme tracking
        let mut coldest_record: Option<WeatherRecord> = None;
        let mut hottest_record: Option<WeatherRecord> = None;
        let mut wettest_record: Option<WeatherRecord> = None;
        let mut windiest_record: Option<WeatherRecord> = None;
        let mut min_temp_val = f32::MAX;
        let mut max_temp_val = f32::MIN;
        let mut max_precip_val = f32::MIN;
        let mut max_wind_val = f32::MIN;

        // Enhanced data quality tracking
        let mut ecad_valid = 0;
        let mut ecad_suspect = 0;
        let mut ecad_missing = 0;
        let mut physically_valid = 0;
        let mut physically_suspect = 0;
        let mut physically_invalid = 0;
        let mut combined_valid = 0;
        let mut combined_suspect_original = 0;
        let mut combined_suspect_range = 0;
        let mut combined_suspect_both = 0;
        let mut combined_invalid = 0;
        let mut combined_missing = 0;

        for batch_result in parquet_reader {
            let batch = batch_result?;
            let num_rows = batch.num_rows();

            if batch.num_columns() < 13 {
                continue; // Skip if not WeatherRecord format
            }

            let has_validation_fields = batch.num_columns() >= 16;

            // Extract arrays
            let station_ids = batch
                .column(0)
                .as_any()
                .downcast_ref::<UInt32Array>()
                .unwrap();
            let station_names = batch
                .column(1)
                .as_any()
                .downcast_ref::<StringArray>()
                .unwrap();
            let dates = batch
                .column(2)
                .as_any()
                .downcast_ref::<Date32Array>()
                .unwrap();
            let latitudes = batch
                .column(3)
                .as_any()
                .downcast_ref::<Float64Array>()
                .unwrap();
            let longitudes = batch
                .column(4)
                .as_any()
                .downcast_ref::<Float64Array>()
                .unwrap();
            let temp_mins = batch
                .column(5)
                .as_any()
                .downcast_ref::<Float32Array>()
                .unwrap();
            let temp_maxs = batch
                .column(6)
                .as_any()
                .downcast_ref::<Float32Array>()
                .unwrap();
            let temp_avgs = batch
                .column(7)
                .as_any()
                .downcast_ref::<Float32Array>()
                .unwrap();
            let precipitations = batch
                .column(8)
                .as_any()
                .downcast_ref::<Float32Array>()
                .unwrap();
            let wind_speeds = batch
                .column(9)
                .as_any()
                .downcast_ref::<Float32Array>()
                .unwrap();
            let temp_qualities = batch
                .column(10)
                .as_any()
                .downcast_ref::<StringArray>()
                .unwrap();
            let precip_qualities = batch
                .column(11)
                .as_any()
                .downcast_ref::<StringArray>()
                .unwrap();
            let wind_qualities = batch
                .column(12)
                .as_any()
                .downcast_ref::<StringArray>()
                .unwrap();

            // Optional validation fields
            let temp_validations = if has_validation_fields {
                Some(
                    batch
                        .column(13)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .unwrap(),
                )
            } else {
                None
            };
            let precip_validations = if has_validation_fields {
                Some(
                    batch
                        .column(14)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .unwrap(),
                )
            } else {
                None
            };
            let wind_validations = if has_validation_fields {
                Some(
                    batch
                        .column(15)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .unwrap(),
                )
            } else {
                None
            };

            for i in 0..num_rows {
                total_records += 1;
                let station_id = station_ids.value(i);
                stations.insert(station_id);

                let date = chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
                    + chrono::Duration::days(dates.value(i) as i64);

                // Update date bounds
                min_date = Some(min_date.map_or(date, |d: chrono::NaiveDate| d.min(date)));
                max_date = Some(max_date.map_or(date, |d: chrono::NaiveDate| d.max(date)));

                // Update geographic bounds
                let lat = latitudes.value(i);
                let lon = longitudes.value(i);
                min_lat = min_lat.min(lat);
                max_lat = max_lat.max(lat);
                min_lon = min_lon.min(lon);
                max_lon = max_lon.max(lon);

                // Create record for sampling and analysis
                use crate::models::weather::{DataQuality, PhysicalValidity};

                let record = WeatherRecord::new_raw(
                    station_id,
                    station_names.value(i).to_string(),
                    date,
                    lat,
                    lon,
                    if temp_mins.is_null(i) {
                        None
                    } else {
                        Some(temp_mins.value(i))
                    },
                    if temp_maxs.is_null(i) {
                        None
                    } else {
                        Some(temp_maxs.value(i))
                    },
                    if temp_avgs.is_null(i) {
                        None
                    } else {
                        Some(temp_avgs.value(i))
                    },
                    if precipitations.is_null(i) {
                        None
                    } else {
                        Some(precipitations.value(i))
                    },
                    if wind_speeds.is_null(i) {
                        None
                    } else {
                        Some(wind_speeds.value(i))
                    },
                    if temp_qualities.is_null(i) {
                        None
                    } else {
                        Some(temp_qualities.value(i).to_string())
                    },
                    if precip_qualities.is_null(i) {
                        None
                    } else {
                        Some(precip_qualities.value(i).to_string())
                    },
                    if wind_qualities.is_null(i) {
                        None
                    } else {
                        Some(wind_qualities.value(i).to_string())
                    },
                    // Parse validation fields if available
                    temp_validations.and_then(|arr| {
                        if arr.is_null(i) {
                            None
                        } else {
                            PhysicalValidity::parse(arr.value(i))
                        }
                    }),
                    precip_validations.and_then(|arr| {
                        if arr.is_null(i) {
                            None
                        } else {
                            PhysicalValidity::parse(arr.value(i))
                        }
                    }),
                    wind_validations.and_then(|arr| {
                        if arr.is_null(i) {
                            None
                        } else {
                            PhysicalValidity::parse(arr.value(i))
                        }
                    }),
                );

                // Track metrics
                if record.has_temperature_data() {
                    temp_records += 1;
                    temp_stations.insert(station_id);
                    temp_dates.push(date);

                    // Track extreme temperatures (exclude invalid values)
                    let temp_quality = record.assess_temperature_quality();
                    if !matches!(temp_quality, DataQuality::Invalid) {
                        if let Some(min_temp) = record.temp_min {
                            if min_temp < min_temp_val {
                                min_temp_val = min_temp;
                                coldest_record = Some(record.clone());
                            }
                        }

                        if let Some(max_temp) = record.temp_max {
                            if max_temp > max_temp_val {
                                max_temp_val = max_temp;
                                hottest_record = Some(record.clone());
                            }
                        }
                    }
                }

                if record.has_precipitation() {
                    precip_records += 1;
                    precip_stations.insert(station_id);
                    precip_dates.push(date);

                    // Track extreme precipitation (exclude invalid values)
                    let precip_quality = record.assess_precipitation_quality();
                    if !matches!(precip_quality, DataQuality::Invalid) {
                        if let Some(precip) = record.precipitation {
                            if precip > max_precip_val {
                                max_precip_val = precip;
                                wettest_record = Some(record.clone());
                            }
                        }
                    }
                }

                if record.has_wind_speed() {
                    wind_records += 1;
                    wind_stations.insert(station_id);
                    wind_dates.push(date);

                    // Track extreme wind speed (exclude invalid values)
                    let wind_quality = record.assess_wind_quality();
                    if !matches!(wind_quality, DataQuality::Invalid) {
                        if let Some(wind) = record.wind_speed {
                            if wind > max_wind_val {
                                max_wind_val = wind;
                                windiest_record = Some(record.clone());
                            }
                        }
                    }
                }

                // Enhanced data quality analysis

                // Track ECAD quality flags for each metric present

                // Temperature ECAD flags
                if record.has_temperature_data() {
                    if let Some(temp_quality) = &record.temp_quality {
                        if temp_quality.contains('0') {
                            ecad_valid += 1;
                        } else if temp_quality.contains('1') {
                            ecad_suspect += 1;
                        } else if temp_quality.contains('9') {
                            ecad_missing += 1;
                        }
                    }
                }

                // Precipitation ECAD flags
                if record.has_precipitation() {
                    if let Some(precip_quality) = &record.precip_quality {
                        if precip_quality == "0" {
                            ecad_valid += 1;
                        } else if precip_quality == "1" {
                            ecad_suspect += 1;
                        } else if precip_quality == "9" {
                            ecad_missing += 1;
                        }
                    }
                }

                // Wind speed ECAD flags
                if record.has_wind_speed() {
                    if let Some(wind_quality) = &record.wind_quality {
                        if wind_quality == "0" {
                            ecad_valid += 1;
                        } else if wind_quality == "1" {
                            ecad_suspect += 1;
                        } else if wind_quality == "9" {
                            ecad_missing += 1;
                        }
                    }
                }

                // Track physical validation for each metric present
                if let Some(validation) = record.temp_validation {
                    match validation {
                        PhysicalValidity::Valid => physically_valid += 1,
                        PhysicalValidity::Suspect => physically_suspect += 1,
                        PhysicalValidity::Invalid => physically_invalid += 1,
                    }
                }
                if let Some(validation) = record.precip_validation {
                    match validation {
                        PhysicalValidity::Valid => physically_valid += 1,
                        PhysicalValidity::Suspect => physically_suspect += 1,
                        PhysicalValidity::Invalid => physically_invalid += 1,
                    }
                }
                if let Some(validation) = record.wind_validation {
                    match validation {
                        PhysicalValidity::Valid => physically_valid += 1,
                        PhysicalValidity::Suspect => physically_suspect += 1,
                        PhysicalValidity::Invalid => physically_invalid += 1,
                    }
                }

                // Track combined quality assessment
                let temp_quality = record.assess_temperature_quality();
                let precip_quality = record.assess_precipitation_quality();
                let wind_quality = record.assess_wind_quality();

                for quality in [temp_quality, precip_quality, wind_quality] {
                    match quality {
                        DataQuality::Valid => combined_valid += 1,
                        DataQuality::SuspectOriginal => combined_suspect_original += 1,
                        DataQuality::SuspectRange => combined_suspect_range += 1,
                        DataQuality::SuspectBoth => combined_suspect_both += 1,
                        DataQuality::Invalid => combined_invalid += 1,
                        DataQuality::Missing => combined_missing += 1,
                    }
                }

                // Store for sampling
                all_records.push(record);
            }
        }

        // Create diverse sampling
        let sample_records = self.create_diverse_sample(&all_records, sample_size);

        // Calculate temporal ranges per metric
        temp_dates.sort();
        precip_dates.sort();
        wind_dates.sort();

        let temperature_range = if !temp_dates.is_empty() {
            Some((temp_dates[0], temp_dates[temp_dates.len() - 1]))
        } else {
            None
        };

        let precipitation_range = if !precip_dates.is_empty() {
            Some((precip_dates[0], precip_dates[precip_dates.len() - 1]))
        } else {
            None
        };

        let wind_range = if !wind_dates.is_empty() {
            Some((wind_dates[0], wind_dates[wind_dates.len() - 1]))
        } else {
            None
        };

        // Countries (simplified - could be enhanced with actual geographic lookup)
        let countries = if min_lon < -5.0 && max_lat > 53.0 {
            vec!["GB".to_string(), "IE".to_string()]
        } else if max_lat > 55.0 {
            vec!["GB".to_string()]
        } else {
            vec!["IE".to_string()]
        };

        Ok(WeatherDatasetSummary {
            total_records,
            total_stations: stations.len(),
            geographic_bounds: GeographicBounds {
                min_lat,
                max_lat,
                min_lon,
                max_lon,
                countries,
            },
            temporal_coverage: TemporalCoverage {
                overall_start: min_date
                    .unwrap_or_else(|| chrono::NaiveDate::from_ymd_opt(1900, 1, 1).unwrap()),
                overall_end: max_date
                    .unwrap_or_else(|| chrono::NaiveDate::from_ymd_opt(2000, 1, 1).unwrap()),
                temperature_range,
                precipitation_range,
                wind_range,
            },
            metric_statistics: MetricStatistics {
                temperature_records: temp_records,
                temperature_stations: temp_stations.len(),
                precipitation_records: precip_records,
                precipitation_stations: precip_stations.len(),
                wind_records,
                wind_stations: wind_stations.len(),
                temperature_range: if min_temp_val != f32::MAX && max_temp_val != f32::MIN {
                    Some((min_temp_val, max_temp_val))
                } else {
                    None
                },
                precipitation_range: if max_precip_val != f32::MIN {
                    Some((0.0, max_precip_val))
                } else {
                    None
                },
                wind_range: if max_wind_val != f32::MIN {
                    Some((0.0, max_wind_val))
                } else {
                    None
                },
            },
            data_quality: EnhancedDataQuality {
                ecad_valid,
                ecad_suspect,
                ecad_missing,
                physically_valid,
                physically_suspect,
                physically_invalid,
                combined_valid,
                combined_suspect_original,
                combined_suspect_range,
                combined_suspect_both,
                combined_invalid,
                combined_missing,
                validation_errors: 0, // Will be populated from integrity report if available
            },
            sample_records,
            extreme_records: ExtremeRecords {
                coldest: coldest_record,
                hottest: hottest_record,
                wettest: wettest_record,
                windiest: windiest_record,
            },
        })
    }

    /// Create diverse sample of records for display
    fn create_diverse_sample(
        &self,
        all_records: &[WeatherRecord],
        sample_size: usize,
    ) -> Vec<WeatherRecord> {
        use std::collections::HashMap;

        if all_records.is_empty() || sample_size == 0 {
            return Vec::new();
        }

        let mut samples = Vec::new();
        let mut station_counts: HashMap<String, usize> = HashMap::new();

        // Strategy: Sample diverse stations and metric combinations
        let total_records = all_records.len();
        let step = if total_records > sample_size * 100 {
            total_records / (sample_size * 50) // Sample more spread out for large datasets
        } else {
            std::cmp::max(1, total_records / sample_size)
        };

        for (i, record) in all_records.iter().enumerate() {
            if i % step == 0 && samples.len() < sample_size {
                // Limit samples per station for diversity
                let station_count = station_counts
                    .entry(record.station_name.clone())
                    .or_insert(0);
                if *station_count < 2 {
                    // Max 2 samples per station
                    samples.push(record.clone());
                    *station_count += 1;
                }
            }
        }

        // If we still need more samples, fill with any remaining records
        if samples.len() < sample_size {
            for record in all_records.iter().step_by(step * 2) {
                if samples.len() >= sample_size {
                    break;
                }
                if !samples
                    .iter()
                    .any(|s| s.station_name == record.station_name && s.date == record.date)
                {
                    samples.push(record.clone());
                }
            }
        }

        samples.truncate(sample_size);
        samples
    }
}

#[derive(Debug, PartialEq)]
pub enum SchemaType {
    ConsolidatedRecord,
    WeatherRecord,
    Unknown,
}

#[derive(Debug, Clone)]
pub struct WeatherDatasetSummary {
    pub total_records: usize,
    pub total_stations: usize,
    pub geographic_bounds: GeographicBounds,
    pub temporal_coverage: TemporalCoverage,
    pub metric_statistics: MetricStatistics,
    pub data_quality: EnhancedDataQuality,
    pub sample_records: Vec<WeatherRecord>,
    pub extreme_records: ExtremeRecords,
}

#[derive(Debug, Clone)]
pub struct GeographicBounds {
    pub min_lat: f64,
    pub max_lat: f64,
    pub min_lon: f64,
    pub max_lon: f64,
    pub countries: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct TemporalCoverage {
    pub overall_start: chrono::NaiveDate,
    pub overall_end: chrono::NaiveDate,
    pub temperature_range: Option<(chrono::NaiveDate, chrono::NaiveDate)>,
    pub precipitation_range: Option<(chrono::NaiveDate, chrono::NaiveDate)>,
    pub wind_range: Option<(chrono::NaiveDate, chrono::NaiveDate)>,
}

#[derive(Debug, Clone)]
pub struct MetricStatistics {
    pub temperature_records: usize,
    pub temperature_stations: usize,
    pub precipitation_records: usize,
    pub precipitation_stations: usize,
    pub wind_records: usize,
    pub wind_stations: usize,
    pub temperature_range: Option<(f32, f32)>,
    pub precipitation_range: Option<(f32, f32)>,
    pub wind_range: Option<(f32, f32)>,
}

#[derive(Debug, Clone)]
pub struct EnhancedDataQuality {
    // ECAD quality flag assessment
    pub ecad_valid: usize,
    pub ecad_suspect: usize,
    pub ecad_missing: usize,

    // Physical validation assessment
    pub physically_valid: usize,
    pub physically_suspect: usize,
    pub physically_invalid: usize,

    // Combined quality assessment
    pub combined_valid: usize,
    pub combined_suspect_original: usize,
    pub combined_suspect_range: usize,
    pub combined_suspect_both: usize,
    pub combined_invalid: usize,
    pub combined_missing: usize,

    pub validation_errors: usize,
}

#[derive(Debug, Clone)]
pub struct ExtremeRecords {
    pub coldest: Option<WeatherRecord>,
    pub hottest: Option<WeatherRecord>,
    pub wettest: Option<WeatherRecord>,
    pub windiest: Option<WeatherRecord>,
}

impl WeatherDatasetSummary {
    pub fn display_comprehensive_summary(&self) -> String {
        let mut summary = String::new();

        // Header
        summary.push_str("UNIFIED WEATHER DATASET ANALYSIS\n");
        summary.push_str("================================\n\n");

        // Dataset Overview
        summary.push_str(&format!(
            "Dataset Overview:\n\
            - Records: {} unified weather records\n\
            - Stations: {} across {} ({:.1}°N-{:.1}°N, {:.1}°W-{:.1}°E)\n\
            - Timespan: {} to {} ({} years)\n\n",
            self.total_records,
            self.total_stations,
            self.geographic_bounds.countries.join("/"),
            self.geographic_bounds.min_lat,
            self.geographic_bounds.max_lat,
            if self.geographic_bounds.min_lon < 0.0 {
                -self.geographic_bounds.min_lon
            } else {
                self.geographic_bounds.min_lon
            },
            self.geographic_bounds.max_lon,
            self.temporal_coverage.overall_start,
            self.temporal_coverage.overall_end,
            self.temporal_coverage.overall_end.year() - self.temporal_coverage.overall_start.year()
        ));

        // Metric Coverage Table
        summary.push_str("Metric Coverage:\n");
        summary.push_str(
            "┌─────────────────┬──────────┬─────────────┬──────────────┬─────────────┐\n",
        );
        summary.push_str(
            "│ Metric          │ Stations │ Records     │ Coverage     │ Date Range  │\n",
        );
        summary.push_str(
            "├─────────────────┼──────────┼─────────────┼──────────────┼─────────────┤\n",
        );

        // Temperature row
        let temp_coverage = if self.total_records > 0 {
            (self.metric_statistics.temperature_records as f32 / self.total_records as f32) * 100.0
        } else {
            0.0
        };

        let temp_range = if let Some((start, end)) = self.temporal_coverage.temperature_range {
            format!("{}-{}", start.year(), end.year())
        } else {
            "N/A".to_string()
        };

        summary.push_str(&format!(
            "│ Temperature     │ {:8} │ {:11} │ {:10.1}%  │ {:11} │\n",
            self.metric_statistics.temperature_stations,
            self.metric_statistics.temperature_records,
            temp_coverage,
            temp_range
        ));

        // Precipitation row
        let precip_coverage = if self.total_records > 0 {
            (self.metric_statistics.precipitation_records as f32 / self.total_records as f32)
                * 100.0
        } else {
            0.0
        };

        let precip_range = if let Some((start, end)) = self.temporal_coverage.precipitation_range {
            format!("{}-{}", start.year(), end.year())
        } else {
            "N/A".to_string()
        };

        summary.push_str(&format!(
            "│ Precipitation   │ {:8} │ {:11} │ {:10.1}%  │ {:11} │\n",
            self.metric_statistics.precipitation_stations,
            self.metric_statistics.precipitation_records,
            precip_coverage,
            precip_range
        ));

        // Wind row
        let wind_coverage = if self.total_records > 0 {
            (self.metric_statistics.wind_records as f32 / self.total_records as f32) * 100.0
        } else {
            0.0
        };

        let wind_range = if let Some((start, end)) = self.temporal_coverage.wind_range {
            format!("{}-{}", start.year(), end.year())
        } else {
            "N/A".to_string()
        };

        summary.push_str(&format!(
            "│ Wind Speed      │ {:8} │ {:11} │ {:10.1}%  │ {:11} │\n",
            self.metric_statistics.wind_stations,
            self.metric_statistics.wind_records,
            wind_coverage,
            wind_range
        ));

        summary.push_str(
            "└─────────────────┴──────────┴─────────────┴──────────────┴─────────────┘\n\n",
        );

        // Sample Records (diverse)
        if !self.sample_records.is_empty() {
            summary.push_str("Sample Records (diverse stations & metrics):\n");
            for (i, record) in self.sample_records.iter().enumerate() {
                let mut metrics_display = Vec::new();

                // Temperature display
                let temp_parts: Vec<String> = [
                    record.temp_min.map(|t| format!("min={:.1}°C", t)),
                    record.temp_avg.map(|t| format!("avg={:.1}°C", t)),
                    record.temp_max.map(|t| format!("max={:.1}°C", t)),
                ]
                .into_iter()
                .flatten()
                .collect();

                if !temp_parts.is_empty() {
                    metrics_display.push(format!("temp({})", temp_parts.join(", ")));
                }

                if let Some(precip) = record.precipitation {
                    metrics_display.push(format!("precip={:.1}mm", precip));
                }

                if let Some(wind) = record.wind_speed {
                    metrics_display.push(format!("wind={:.1}m/s", wind));
                }

                let metrics_str = if metrics_display.is_empty() {
                    "no data".to_string()
                } else {
                    metrics_display.join(", ")
                };

                summary.push_str(&format!(
                    "{}. {} on {}: {}\n",
                    i + 1,
                    record.station_name,
                    record.date,
                    metrics_str
                ));
            }
            summary.push('\n');
        }

        // Extreme Records
        summary.push_str("Extreme Records:\n");
        if let Some(ref coldest) = self.extreme_records.coldest {
            if let Some(min_temp) = coldest.temp_min {
                summary.push_str(&format!(
                    "- Coldest: {:.1}°C at {} ({})\n",
                    min_temp, coldest.station_name, coldest.date
                ));
            }
        }

        if let Some(ref hottest) = self.extreme_records.hottest {
            if let Some(max_temp) = hottest.temp_max {
                summary.push_str(&format!(
                    "- Hottest: {:.1}°C at {} ({})\n",
                    max_temp, hottest.station_name, hottest.date
                ));
            }
        }

        if let Some(ref wettest) = self.extreme_records.wettest {
            if let Some(precip) = wettest.precipitation {
                summary.push_str(&format!(
                    "- Wettest: {:.1}mm at {} ({})\n",
                    precip, wettest.station_name, wettest.date
                ));
            }
        }

        if let Some(ref windiest) = self.extreme_records.windiest {
            if let Some(wind) = windiest.wind_speed {
                summary.push_str(&format!(
                    "- Windiest: {:.1}m/s at {} ({})\n",
                    wind, windiest.station_name, windiest.date
                ));
            }
        }
        summary.push('\n');

        // Enhanced Data Quality Analysis
        summary.push_str("Data Quality Analysis:\n");

        let total_ecad = self.data_quality.ecad_valid
            + self.data_quality.ecad_suspect
            + self.data_quality.ecad_missing;
        if total_ecad > 0 {
            summary.push_str("├─ ECAD Assessment:\n");
            summary.push_str(&format!(
                "│  ├─ Valid (flag=0): {} ({:.1}%)\n",
                self.data_quality.ecad_valid,
                (self.data_quality.ecad_valid as f32 / total_ecad as f32) * 100.0
            ));
            summary.push_str(&format!(
                "│  ├─ Suspect (flag=1): {} ({:.1}%)\n",
                self.data_quality.ecad_suspect,
                (self.data_quality.ecad_suspect as f32 / total_ecad as f32) * 100.0
            ));
            summary.push_str(&format!(
                "│  └─ Missing (flag=9): {} ({:.1}%)\n",
                self.data_quality.ecad_missing,
                (self.data_quality.ecad_missing as f32 / total_ecad as f32) * 100.0
            ));
            summary.push_str("\n");
        }

        let total_physical = self.data_quality.physically_valid
            + self.data_quality.physically_suspect
            + self.data_quality.physically_invalid;
        if total_physical > 0 {
            summary.push_str("├─ Physical Validation:\n");
            summary.push_str(&format!(
                "│  ├─ Valid: {} ({:.1}%)\n",
                self.data_quality.physically_valid,
                (self.data_quality.physically_valid as f32 / total_physical as f32) * 100.0
            ));
            summary.push_str(&format!(
                "│  ├─ Suspect: {} ({:.1}%)\n",
                self.data_quality.physically_suspect,
                (self.data_quality.physically_suspect as f32 / total_physical as f32) * 100.0
            ));
            summary.push_str(&format!(
                "│  └─ Invalid: {} ({:.3}%)\n",
                self.data_quality.physically_invalid,
                (self.data_quality.physically_invalid as f32 / total_physical as f32) * 100.0
            ));
            summary.push_str("\n");
        }

        let total_combined = self.data_quality.combined_valid
            + self.data_quality.combined_suspect_original
            + self.data_quality.combined_suspect_range
            + self.data_quality.combined_suspect_both
            + self.data_quality.combined_invalid
            + self.data_quality.combined_missing;
        if total_combined > 0 {
            summary.push_str("└─ Combined Quality:\n");
            summary.push_str(&format!(
                "   ├─ Valid: {} ({:.1}%)\n",
                self.data_quality.combined_valid,
                (self.data_quality.combined_valid as f32 / total_combined as f32) * 100.0
            ));
            summary.push_str(&format!(
                "   ├─ Suspect (original): {} ({:.1}%)\n",
                self.data_quality.combined_suspect_original,
                (self.data_quality.combined_suspect_original as f32 / total_combined as f32)
                    * 100.0
            ));
            summary.push_str(&format!(
                "   ├─ Suspect (range): {} ({:.2}%)\n",
                self.data_quality.combined_suspect_range,
                (self.data_quality.combined_suspect_range as f32 / total_combined as f32) * 100.0
            ));
            if self.data_quality.combined_invalid > 0 {
                summary.push_str(&format!(
                    "   ├─ Invalid: {} ({:.3}%)\n",
                    self.data_quality.combined_invalid,
                    (self.data_quality.combined_invalid as f32 / total_combined as f32) * 100.0
                ));
            }
            summary.push_str(&format!(
                "   └─ Missing: {} ({:.1}%)\n",
                self.data_quality.combined_missing,
                (self.data_quality.combined_missing as f32 / total_combined as f32) * 100.0
            ));
        }

        if self.data_quality.physically_invalid > 0 {
            summary.push_str(&format!(
                "\n⚠️  Found {} physically impossible values that were excluded from extreme records analysis\n",
                self.data_quality.physically_invalid
            ));
        }

        summary
    }
}

impl Default for ParquetWriter {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
pub struct ParquetFileInfo {
    pub total_rows: i64,
    pub row_groups: i32,
    pub row_group_sizes: Vec<i64>,
    pub file_size: u64,
    pub compression: Compression,
}

impl ParquetFileInfo {
    pub fn summary(&self) -> String {
        format!(
            "Parquet File Summary:\n\
            - Total rows: {}\n\
            - Row groups: {}\n\
            - File size: {:.2} MB\n\
            - Compression: {:?}\n\
            - Avg rows per group: {:.0}",
            self.total_rows,
            self.row_groups,
            self.file_size as f64 / 1_048_576.0, // Convert to MB
            self.compression,
            self.total_rows as f64 / self.row_groups as f64
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::ConsolidatedRecord;
    use chrono::NaiveDate;
    use tempfile::NamedTempFile;

    #[test]
    fn test_write_empty_records() {
        let writer = ParquetWriter::new();
        let temp_file = NamedTempFile::new().unwrap();

        let result = writer.write_records(&[], temp_file.path());
        assert!(result.is_ok());
    }

    #[test]
    fn test_write_single_record() -> Result<()> {
        let writer = ParquetWriter::new();
        let temp_file = NamedTempFile::new().unwrap();

        let date = NaiveDate::from_ymd_opt(2023, 7, 15).unwrap();
        let record = ConsolidatedRecord::new(
            12345,
            "Test Station".to_string(),
            date,
            51.5074,
            -0.1278,
            15.0,
            25.0,
            20.0,
            "000".to_string(),
        );

        writer.write_records(&[record], temp_file.path())?;

        // Verify file was created and has content
        let metadata = std::fs::metadata(temp_file.path())?;
        assert!(metadata.len() > 0);

        Ok(())
    }

    #[test]
    fn test_different_compressions() -> Result<()> {
        let compressions = ["snappy", "gzip", "lz4", "zstd", "none"];

        for compression in &compressions {
            let writer = ParquetWriter::new().with_compression(compression)?;
            let temp_file = NamedTempFile::new().unwrap();

            let date = NaiveDate::from_ymd_opt(2023, 7, 15).unwrap();
            let record = ConsolidatedRecord::new(
                12345,
                "Test Station".to_string(),
                date,
                51.5074,
                -0.1278,
                15.0,
                25.0,
                20.0,
                "000".to_string(),
            );

            let result = writer.write_records(&[record], temp_file.path());
            assert!(result.is_ok(), "Failed with compression: {}", compression);
        }

        Ok(())
    }
}