1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# vim:fileencoding=utf-8 |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2020 Stefan Bender |
5
|
|
|
# |
6
|
|
|
# This module is part of sciapy. |
7
|
|
|
# sciapy is free software: you can redistribute it or modify |
8
|
|
|
# it under the terms of the GNU General Public License as published |
9
|
|
|
# by the Free Software Foundation, version 2. |
10
|
|
|
# See accompanying LICENSE file or http://www.gnu.org/licenses/gpl-2.0.html. |
11
|
|
|
"""Compare netcdf files |
12
|
|
|
|
13
|
|
|
Compare netcdf files, testing the variable attributes and values |
14
|
|
|
to ensure format compatibility. |
15
|
|
|
""" |
16
|
|
|
import sys |
17
|
|
|
import numpy as np |
18
|
|
|
import xarray as xr |
19
|
|
|
|
20
|
|
|
__all__ = ["nccmpattrs", "ncallclose", "ncequal", "ncidentical"] |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def cmpvarattrs(v1, v2): |
24
|
|
|
"""Compare variable attribute values""" |
25
|
|
|
msg = "" |
26
|
|
|
same = True |
27
|
|
|
for a in v1.attrs: |
28
|
|
|
a1 = getattr(v1, a, None) |
29
|
|
|
a2 = getattr(v2, a, None) |
30
|
|
|
try: |
31
|
|
|
np.testing.assert_equal(a1, a2) |
32
|
|
|
except AssertionError: |
33
|
|
|
msg = "{0}\nL\t{1}\t{2}\t{3}\nR\t{1}\t{2}\t{4}".format( |
34
|
|
|
msg, v1.name, a, a1, a2, |
35
|
|
|
) |
36
|
|
|
same = False |
37
|
|
|
return same, msg |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def nccmpattrs(file1, file2, ignore=[]): |
41
|
|
|
"""Compare variable attributes and global attributes""" |
42
|
|
|
msg = "" |
43
|
|
|
same = True |
44
|
|
|
with xr.open_dataset(file1, decode_cf=False) as ds1: |
45
|
|
|
ds2 = xr.open_dataset(file2, decode_cf=False) |
46
|
|
|
for v in ds1.variables: |
47
|
|
|
vsame, vmsg = cmpvarattrs(ds1[v], ds2[v]) |
48
|
|
|
if not vsame: |
49
|
|
|
msg = "{0}{1}".format(msg, vmsg) |
50
|
|
|
same = False |
51
|
|
|
for attr in set(ds1.attrs).difference(set(ignore)): |
52
|
|
|
lattr = getattr(ds1, attr, None) |
53
|
|
|
rattr = getattr(ds2, attr, None) |
54
|
|
|
try: |
55
|
|
|
np.testing.assert_equal(lattr, rattr) |
56
|
|
|
except AssertionError: |
57
|
|
|
msg = "{0}\nL\t{1}\t{2}\nR\t{1}\t{3}".format( |
58
|
|
|
msg, attr, lattr, rattr, |
59
|
|
|
) |
60
|
|
|
same = False |
61
|
|
|
assert same, msg |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def ncallclose(file1, file2): |
65
|
|
|
with xr.open_dataset(file1, decode_cf=False) as ds1: |
66
|
|
|
ds2 = xr.open_dataset(file2, decode_cf=False) |
67
|
|
|
xr.testing.assert_allclose(ds1, ds2) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def ncequal(file1, file2): |
71
|
|
|
with xr.open_dataset(file1, decode_cf=False) as ds1: |
72
|
|
|
ds2 = xr.open_dataset(file2, decode_cf=False) |
73
|
|
|
xr.testing.assert_equal(ds1, ds2) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def ncidentical(file1, file2, ignore=[]): |
77
|
|
|
with xr.open_dataset(file1, decode_cf=False) as ds1: |
78
|
|
|
ds2 = xr.open_dataset(file2, decode_cf=False) |
79
|
|
|
for ign in ignore: |
80
|
|
|
if ign in ds1.attrs.keys(): |
81
|
|
|
del ds1.attrs[ign] |
82
|
|
|
if ign in ds2.attrs.keys(): |
83
|
|
|
del ds2.attrs[ign] |
84
|
|
|
xr.testing.assert_identical(ds1, ds2) |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
if __name__ == "__main__": |
88
|
|
|
nccmpattrs(*sys.argv[1:]) |
89
|
|
|
ncallclose(*sys.argv[1:]) |
90
|
|
|
ncequal(*sys.argv[1:]) |
91
|
|
|
ncidentical(*sys.argv[1:]) |
92
|
|
|
|