Skip to main content

_standard_knowledge_py/
standard.rs

1use std::collections::BTreeMap;
2use std::convert::From;
3
4use dyn_clone;
5use pyo3::prelude::*;
6use standard_knowledge::Standard;
7
8use crate::test_suite::PyTestSuite;
9
10#[pyclass(name = "Standard")]
11#[derive(Clone)]
12pub struct PyStandard(pub Standard);
13
14/// A CF compatible standard
15#[pymethods]
16impl PyStandard {
17    fn __repr__(&self) -> PyResult<String> {
18        Ok(format!("<Standard: {}>", self.0.name))
19    }
20
21    #[getter]
22    fn name(&self) -> PyResult<String> {
23        Ok(self.0.name.clone())
24    }
25
26    #[getter]
27    fn long_name(&self) -> PyResult<Option<String>> {
28        Ok(self.0.long_name.clone())
29    }
30
31    #[getter]
32    fn unit(&self) -> PyResult<String> {
33        Ok(self.0.unit.clone())
34    }
35
36    #[getter]
37    fn description(&self) -> PyResult<String> {
38        Ok(self.0.description.clone())
39    }
40
41    #[getter]
42    fn aliases(&self) -> PyResult<Vec<String>> {
43        Ok(self.0.aliases.clone())
44    }
45
46    #[getter]
47    fn ioos_category(&self) -> PyResult<Option<String>> {
48        Ok(self.0.ioos_category.clone())
49    }
50
51    #[getter]
52    fn common_variable_names(&self) -> PyResult<Vec<String>> {
53        Ok(self.0.common_variable_names.clone())
54    }
55
56    #[getter]
57    fn related_standards(&self) -> PyResult<Vec<String>> {
58        Ok(self.0.related_standards.clone())
59    }
60
61    #[getter]
62    fn other_units(&self) -> PyResult<Vec<String>> {
63        Ok(self.0.other_units.clone())
64    }
65
66    #[getter]
67    fn comments(&self) -> PyResult<Option<String>> {
68        Ok(self.0.comments.clone())
69    }
70
71    #[getter]
72    fn qc(&self, py: Python) -> PyResult<Option<Vec<Py<PyTestSuite>>>> {
73        if self.0.qartod.is_empty() {
74            Ok(None)
75        } else {
76            let test_suites: Result<Vec<_>, _> = self
77                .0
78                .qartod
79                .iter()
80                .map(|test_suite| {
81                    let py_test_suite = PyTestSuite::new(dyn_clone::clone_box(&**test_suite));
82                    Py::new(py, py_test_suite)
83                })
84                .collect();
85            Ok(Some(test_suites?))
86        }
87    }
88
89    /// Return a dictionary of Xarray attributes
90    fn attrs(&self) -> PyResult<BTreeMap<&str, &str>> {
91        let map = self.0.xarray_attrs();
92
93        Ok(map)
94    }
95}
96
97impl From<Standard> for PyStandard {
98    fn from(standard: Standard) -> Self {
99        PyStandard(standard)
100    }
101}