-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pyproj.py
70 lines (56 loc) · 2.24 KB
/
test_pyproj.py
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
"""
Test xarray's capabilities to understand CF projection information.
"""
import glob
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
import pyproj
import pytest
from dataset_creator import create_dataset, load_metadata
from dataset_handler import extract_grid_mapping_names
datasets = {
f: create_dataset(load_metadata(f)) for f in glob.glob("dataset_definitions/*.json")
}
@pytest.mark.parametrize("dataset_name, dataset", datasets.items())
def test_from_cf(dataset_name, dataset):
"""
Test the pasting of each projection that is referenced by at least one variable.
"""
projections = set()
for var in dataset.variables:
if "grid_mapping" in dataset[var].attrs:
gm = extract_grid_mapping_names(dataset[var].attrs["grid_mapping"])
projections.update(set(gm))
for proj in projections:
pyproj.CRS.from_cf(dataset[proj].attrs)
@pytest.mark.parametrize("dataset_name, dataset", datasets.items())
def test_from_wkt(dataset_name, dataset):
"""
Test the pasting of each projection that is referenced by at least one variable.
"""
projections = set()
for var in dataset.variables:
if "grid_mapping" in dataset[var].attrs:
gm = extract_grid_mapping_names(dataset[var].attrs["grid_mapping"])
projections.update(set(gm))
for proj in projections:
if "crs_wkt" in dataset[proj].attrs:
pyproj.CRS.from_wkt(dataset[proj].attrs["crs_wkt"])
@pytest.mark.parametrize("dataset_name, dataset", datasets.items())
def test_roundtrip_cf(dataset_name, dataset):
"""
Test if projection can be created from CF attributes and then converted back to CF attributes.
"""
projections = set()
for var in dataset.variables:
if "grid_mapping" in dataset[var].attrs:
gm = extract_grid_mapping_names(dataset[var].attrs["grid_mapping"])
projections.update(set(gm))
for proj in projections:
crs = pyproj.CRS.from_cf(dataset[proj].attrs)
attrs_orig = dataset[proj].attrs
attrs_gen = crs.to_cf()
assert (
attrs_gen == attrs_orig
), f"Attributes missmatch! \n\nNew:\n {attrs_gen} \n\nold:\n{attrs_orig}"