oxigdal-netcdf 0.1.6

NetCDF driver for OxiGDAL - Pure Rust NetCDF-3 with optional NetCDF-4 support
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
//! CF Conventions 1.8 Compliance Module
//!
//! This module provides comprehensive support for CF (Climate and Forecast) Conventions
//! version 1.8 as defined at <http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html>.
//!
//! # Features
//!
//! - Attribute validation
//! - Standard name tables
//! - Units validation
//! - Coordinate variable detection
//! - Grid mapping support
//! - Cell methods parsing
//! - Bounds variables
//! - Cell measures
//!
//! # Example
//!
//! ```ignore
//! use oxigdal_netcdf::cf_conventions::{CfValidator, CfComplianceLevel};
//!
//! let validator = CfValidator::new();
//! let report = validator.validate(&metadata)?;
//!
//! if report.is_compliant(CfComplianceLevel::Required) {
//!     println!("File is CF compliant!");
//! }
//! ```

use serde::{Deserialize, Serialize};

use crate::attribute::Attributes;
use crate::dimension::Dimensions;
use crate::variable::{DataType, Variable, Variables};

// Module declarations
mod coordinates;
mod metadata;
#[cfg(test)]
mod tests;
mod time;
pub mod v1_11;

// Re-exports
pub use coordinates::{AxisType, CoordinateDetector, GridMapping, GridMappingType};
pub use metadata::{
    CellMeasure, CellMeasureType, CellMethod, CellMethodOperation, StandardNameEntry,
    StandardNameTable, UnitsValidator,
};
pub use time::BoundsVariable;

// ============================================================================
// CF Compliance Level and Validation Issue Types
// ============================================================================

/// Level of CF compliance requirement.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum CfComplianceLevel {
    /// Required for CF compliance
    Required,
    /// Recommended for better interoperability
    Recommended,
    /// Optional/informational
    Optional,
}

impl CfComplianceLevel {
    /// Get the level name.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Required => "Required",
            Self::Recommended => "Recommended",
            Self::Optional => "Optional",
        }
    }
}

/// Type of validation issue.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CfIssueType {
    /// Missing required attribute
    MissingAttribute,
    /// Invalid attribute value
    InvalidAttributeValue,
    /// Invalid standard name
    InvalidStandardName,
    /// Invalid units
    InvalidUnits,
    /// Units mismatch with standard name
    UnitsMismatch,
    /// Invalid cell methods
    InvalidCellMethods,
    /// Missing bounds variable
    MissingBounds,
    /// Invalid bounds variable
    InvalidBounds,
    /// Invalid grid mapping
    InvalidGridMapping,
    /// Missing coordinate variable
    MissingCoordinateVariable,
    /// Invalid coordinate variable
    InvalidCoordinateVariable,
    /// Invalid cell measures
    InvalidCellMeasures,
    /// General CF convention issue
    ConventionIssue,
}

impl CfIssueType {
    /// Get the issue type name.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::MissingAttribute => "Missing Attribute",
            Self::InvalidAttributeValue => "Invalid Attribute Value",
            Self::InvalidStandardName => "Invalid Standard Name",
            Self::InvalidUnits => "Invalid Units",
            Self::UnitsMismatch => "Units Mismatch",
            Self::InvalidCellMethods => "Invalid Cell Methods",
            Self::MissingBounds => "Missing Bounds",
            Self::InvalidBounds => "Invalid Bounds",
            Self::InvalidGridMapping => "Invalid Grid Mapping",
            Self::MissingCoordinateVariable => "Missing Coordinate Variable",
            Self::InvalidCoordinateVariable => "Invalid Coordinate Variable",
            Self::InvalidCellMeasures => "Invalid Cell Measures",
            Self::ConventionIssue => "Convention Issue",
        }
    }
}

/// A single validation issue.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CfValidationIssue {
    /// Level of the issue
    pub level: CfComplianceLevel,
    /// Type of issue
    pub issue_type: CfIssueType,
    /// Variable name (if applicable)
    pub variable: Option<String>,
    /// Attribute name (if applicable)
    pub attribute: Option<String>,
    /// Description of the issue
    pub message: String,
    /// CF convention section reference
    pub section: Option<String>,
}

impl CfValidationIssue {
    /// Create a new validation issue.
    #[must_use]
    pub fn new(
        level: CfComplianceLevel,
        issue_type: CfIssueType,
        message: impl Into<String>,
    ) -> Self {
        Self {
            level,
            issue_type,
            variable: None,
            attribute: None,
            message: message.into(),
            section: None,
        }
    }

    /// Set the variable name.
    #[must_use]
    pub fn with_variable(mut self, variable: impl Into<String>) -> Self {
        self.variable = Some(variable.into());
        self
    }

    /// Set the attribute name.
    #[must_use]
    pub fn with_attribute(mut self, attribute: impl Into<String>) -> Self {
        self.attribute = Some(attribute.into());
        self
    }

    /// Set the CF section reference.
    #[must_use]
    pub fn with_section(mut self, section: impl Into<String>) -> Self {
        self.section = Some(section.into());
        self
    }
}

/// Validation report containing all issues found.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CfValidationReport {
    /// All validation issues
    issues: Vec<CfValidationIssue>,
    /// CF version validated against
    cf_version: String,
}

impl CfValidationReport {
    /// Create a new validation report.
    #[must_use]
    pub fn new(cf_version: impl Into<String>) -> Self {
        Self {
            issues: Vec::new(),
            cf_version: cf_version.into(),
        }
    }

    /// Add an issue to the report.
    pub fn add_issue(&mut self, issue: CfValidationIssue) {
        self.issues.push(issue);
    }

    /// Get all issues.
    #[must_use]
    pub fn issues(&self) -> &[CfValidationIssue] {
        &self.issues
    }

    /// Get issues at a specific level.
    pub fn issues_at_level(
        &self,
        level: CfComplianceLevel,
    ) -> impl Iterator<Item = &CfValidationIssue> {
        self.issues.iter().filter(move |i| i.level == level)
    }

    /// Check if compliant at a given level (no issues at that level or higher).
    #[must_use]
    pub fn is_compliant(&self, level: CfComplianceLevel) -> bool {
        !self.issues.iter().any(|i| i.level <= level)
    }

    /// Count issues at a specific level.
    #[must_use]
    pub fn count_at_level(&self, level: CfComplianceLevel) -> usize {
        self.issues.iter().filter(|i| i.level == level).count()
    }

    /// Get the CF version.
    #[must_use]
    pub fn cf_version(&self) -> &str {
        &self.cf_version
    }

    /// Get a summary of the report.
    #[must_use]
    pub fn summary(&self) -> String {
        let required = self.count_at_level(CfComplianceLevel::Required);
        let recommended = self.count_at_level(CfComplianceLevel::Recommended);
        let optional = self.count_at_level(CfComplianceLevel::Optional);

        format!(
            "CF-{} Validation: {} required, {} recommended, {} optional issues",
            self.cf_version, required, recommended, optional
        )
    }
}

// ============================================================================
// CF Validator
// ============================================================================

/// CF Conventions validator.
#[derive(Debug)]
pub struct CfValidator {
    /// Standard name table
    standard_names: StandardNameTable,
    /// Units validator
    units_validator: UnitsValidator,
    /// Coordinate detector
    coordinate_detector: CoordinateDetector,
    /// CF version to validate against
    cf_version: String,
}

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

impl CfValidator {
    /// Create a new CF validator for version 1.8.
    #[must_use]
    pub fn new() -> Self {
        Self {
            standard_names: StandardNameTable::cf_1_8(),
            units_validator: UnitsValidator::new(),
            coordinate_detector: CoordinateDetector::new(),
            cf_version: "1.8".to_string(),
        }
    }

    /// Set the CF version to validate against.
    pub fn set_version(&mut self, version: impl Into<String>) {
        self.cf_version = version.into();
    }

    /// Validate global attributes.
    pub fn validate_global_attributes(&self, attrs: &Attributes, report: &mut CfValidationReport) {
        // Check Conventions attribute (required)
        if !attrs.contains("Conventions") {
            report.add_issue(
                CfValidationIssue::new(
                    CfComplianceLevel::Required,
                    CfIssueType::MissingAttribute,
                    "Missing 'Conventions' global attribute",
                )
                .with_attribute("Conventions")
                .with_section("2.6.1"),
            );
        } else if let Some(attr) = attrs.get("Conventions") {
            if let Ok(conv) = attr.value().as_text() {
                if !conv.contains("CF-") {
                    report.add_issue(
                        CfValidationIssue::new(
                            CfComplianceLevel::Required,
                            CfIssueType::InvalidAttributeValue,
                            format!("Conventions attribute '{}' does not contain 'CF-'", conv),
                        )
                        .with_attribute("Conventions")
                        .with_section("2.6.1"),
                    );
                }
            }
        }

        // Check recommended attributes
        let recommended = ["title", "institution", "source", "history", "references"];
        for attr_name in recommended {
            if !attrs.contains(attr_name) {
                report.add_issue(
                    CfValidationIssue::new(
                        CfComplianceLevel::Recommended,
                        CfIssueType::MissingAttribute,
                        format!("Missing recommended global attribute '{}'", attr_name),
                    )
                    .with_attribute(attr_name)
                    .with_section("2.6.2"),
                );
            }
        }
    }

    /// Validate a variable's attributes.
    pub fn validate_variable(
        &self,
        var: &Variable,
        dimensions: &Dimensions,
        report: &mut CfValidationReport,
    ) {
        let var_name = var.name();
        let attrs = var.attributes();

        // Check standard_name
        if let Some(attr) = attrs.get("standard_name") {
            if let Ok(std_name) = attr.value().as_text() {
                // Validate standard name exists
                if !self.standard_names.contains(std_name) {
                    report.add_issue(
                        CfValidationIssue::new(
                            CfComplianceLevel::Recommended,
                            CfIssueType::InvalidStandardName,
                            format!("Unknown standard_name '{}'", std_name),
                        )
                        .with_variable(var_name)
                        .with_attribute("standard_name")
                        .with_section("3.3"),
                    );
                }

                // Check units match canonical units
                if let Some(canonical) = self.standard_names.canonical_units(std_name) {
                    if let Some(units_attr) = attrs.get("units") {
                        if let Ok(units) = units_attr.value().as_text() {
                            if !self.units_validator.are_compatible(units, canonical) {
                                report.add_issue(
                                    CfValidationIssue::new(
                                        CfComplianceLevel::Required,
                                        CfIssueType::UnitsMismatch,
                                        format!(
                                            "Units '{}' not compatible with canonical units '{}' for standard_name '{}'",
                                            units, canonical, std_name
                                        ),
                                    )
                                    .with_variable(var_name)
                                    .with_section("3.1"),
                                );
                            }
                        }
                    }
                }
            }
        }

        // Check units attribute for data variables
        if !self
            .coordinate_detector
            .is_coordinate_variable(var, dimensions)
            && !attrs.contains("units")
            && var.data_type() != DataType::Char
            && var.data_type() != DataType::String
        {
            report.add_issue(
                CfValidationIssue::new(
                    CfComplianceLevel::Recommended,
                    CfIssueType::MissingAttribute,
                    format!("Variable '{}' missing 'units' attribute", var_name),
                )
                .with_variable(var_name)
                .with_section("3.1"),
            );
        }

        // Validate units if present
        if let Some(units_attr) = attrs.get("units") {
            if let Ok(units) = units_attr.value().as_text() {
                if !self.units_validator.is_valid(units) {
                    report.add_issue(
                        CfValidationIssue::new(
                            CfComplianceLevel::Recommended,
                            CfIssueType::InvalidUnits,
                            format!("Invalid units '{}' for variable '{}'", units, var_name),
                        )
                        .with_variable(var_name)
                        .with_attribute("units")
                        .with_section("3.1"),
                    );
                }
            }
        }

        // Check cell_methods
        if let Some(cm_attr) = attrs.get("cell_methods") {
            if let Ok(cell_methods) = cm_attr.value().as_text() {
                if let Err(e) = CellMethod::parse_cell_methods(cell_methods) {
                    report.add_issue(
                        CfValidationIssue::new(
                            CfComplianceLevel::Required,
                            CfIssueType::InvalidCellMethods,
                            format!("Invalid cell_methods '{}': {}", cell_methods, e),
                        )
                        .with_variable(var_name)
                        .with_attribute("cell_methods")
                        .with_section("7.3"),
                    );
                }
            }
        }

        // Check bounds attribute
        if let Some(bounds_attr) = attrs.get("bounds") {
            if let Ok(_bounds_var_name) = bounds_attr.value().as_text() {
                // Bounds validation would require access to all variables
                // This is handled in validate_all
            }
        }

        // Check cell_measures
        if let Some(cm_attr) = attrs.get("cell_measures") {
            if let Ok(cell_measures) = cm_attr.value().as_text() {
                if let Err(e) = CellMeasure::parse_cell_measures(cell_measures) {
                    report.add_issue(
                        CfValidationIssue::new(
                            CfComplianceLevel::Required,
                            CfIssueType::InvalidCellMeasures,
                            format!("Invalid cell_measures '{}': {}", cell_measures, e),
                        )
                        .with_variable(var_name)
                        .with_attribute("cell_measures")
                        .with_section("7.2"),
                    );
                }
            }
        }
    }

    /// Validate grid mappings.
    pub fn validate_grid_mapping(
        &self,
        var: &Variable,
        variables: &Variables,
        report: &mut CfValidationReport,
    ) {
        let var_name = var.name();

        if let Some(gm_attr) = var.attributes().get("grid_mapping") {
            if let Ok(gm_var_name) = gm_attr.value().as_text() {
                // Check grid mapping variable exists
                if let Some(gm_var) = variables.get(gm_var_name) {
                    // Check grid_mapping_name attribute
                    if let Some(gmn_attr) = gm_var.attributes().get("grid_mapping_name") {
                        if let Ok(gm_name) = gmn_attr.value().as_text() {
                            let gm_type = GridMappingType::from_cf_name(gm_name);
                            if gm_type == GridMappingType::Unknown {
                                report.add_issue(
                                    CfValidationIssue::new(
                                        CfComplianceLevel::Recommended,
                                        CfIssueType::InvalidGridMapping,
                                        format!("Unknown grid_mapping_name '{}'", gm_name),
                                    )
                                    .with_variable(gm_var_name)
                                    .with_section("5.6"),
                                );
                            }
                        }
                    } else {
                        report.add_issue(
                            CfValidationIssue::new(
                                CfComplianceLevel::Required,
                                CfIssueType::InvalidGridMapping,
                                format!("Grid mapping variable '{}' missing 'grid_mapping_name' attribute", gm_var_name),
                            )
                            .with_variable(gm_var_name)
                            .with_section("5.6"),
                        );
                    }
                } else {
                    report.add_issue(
                        CfValidationIssue::new(
                            CfComplianceLevel::Required,
                            CfIssueType::InvalidGridMapping,
                            format!(
                                "Grid mapping variable '{}' referenced by '{}' not found",
                                gm_var_name, var_name
                            ),
                        )
                        .with_variable(var_name)
                        .with_section("5.6"),
                    );
                }
            }
        }
    }

    /// Validate coordinate variables.
    pub fn validate_coordinates(
        &self,
        dimensions: &Dimensions,
        variables: &Variables,
        report: &mut CfValidationReport,
    ) {
        // Check that each dimension has a coordinate variable (recommended)
        for dim in dimensions.iter() {
            let dim_name = dim.name();
            if variables.get(dim_name).is_none() {
                report.add_issue(
                    CfValidationIssue::new(
                        CfComplianceLevel::Recommended,
                        CfIssueType::MissingCoordinateVariable,
                        format!(
                            "Dimension '{}' has no corresponding coordinate variable",
                            dim_name
                        ),
                    )
                    .with_section("5.1"),
                );
            }
        }

        // Validate coordinate variables
        for var in variables.iter() {
            if self
                .coordinate_detector
                .is_coordinate_variable(var, dimensions)
            {
                // Coordinate variables should be monotonic
                // (This would require data access, so we just check attributes)

                // Check for axis attribute (recommended for coordinate variables)
                if var.attributes().get("axis").is_none() {
                    if let Some(_axis) = self.coordinate_detector.detect_axis(var) {
                        report.add_issue(
                            CfValidationIssue::new(
                                CfComplianceLevel::Recommended,
                                CfIssueType::MissingAttribute,
                                format!(
                                    "Coordinate variable '{}' should have 'axis' attribute",
                                    var.name()
                                ),
                            )
                            .with_variable(var.name())
                            .with_section("4"),
                        );
                    }
                }
            }
        }
    }

    /// Validate bounds variables.
    pub fn validate_bounds(
        &self,
        variables: &Variables,
        dimensions: &Dimensions,
        report: &mut CfValidationReport,
    ) {
        for var in variables.iter() {
            if let Some(bounds_attr) = var.attributes().get("bounds") {
                if let Ok(bounds_var_name) = bounds_attr.value().as_text() {
                    if let Some(bounds_var) = variables.get(bounds_var_name) {
                        // Validate bounds structure
                        let bounds = BoundsVariable::new(bounds_var_name, var.name(), 2);
                        if let Err(e) = bounds.validate(var, bounds_var, dimensions) {
                            report.add_issue(
                                CfValidationIssue::new(
                                    CfComplianceLevel::Required,
                                    CfIssueType::InvalidBounds,
                                    format!("Invalid bounds variable '{}': {}", bounds_var_name, e),
                                )
                                .with_variable(var.name())
                                .with_section("7.1"),
                            );
                        }
                    } else {
                        report.add_issue(
                            CfValidationIssue::new(
                                CfComplianceLevel::Required,
                                CfIssueType::MissingBounds,
                                format!(
                                    "Bounds variable '{}' referenced by '{}' not found",
                                    bounds_var_name,
                                    var.name()
                                ),
                            )
                            .with_variable(var.name())
                            .with_section("7.1"),
                        );
                    }
                }
            }
        }
    }

    /// Perform full validation.
    pub fn validate(
        &self,
        global_attrs: &Attributes,
        dimensions: &Dimensions,
        variables: &Variables,
    ) -> CfValidationReport {
        let mut report = CfValidationReport::new(&self.cf_version);

        // Validate global attributes
        self.validate_global_attributes(global_attrs, &mut report);

        // Validate each variable
        for var in variables.iter() {
            self.validate_variable(var, dimensions, &mut report);
            self.validate_grid_mapping(var, variables, &mut report);
        }

        // Validate coordinates
        self.validate_coordinates(dimensions, variables, &mut report);

        // Validate bounds
        self.validate_bounds(variables, dimensions, &mut report);

        report
    }
}