Passed
Push — master ( e2ee43...bdae2e )
by Stefan
06:05
created

nccmpx.ncequal()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 xarray as xr
18
19
__all__ = ["nccmpattrs", "ncallclose", "ncequal", "ncidentical"]
20
21
22
def nccmpattrs(file1, file2, ignore=[]):
23
	"""Compare variable attributes and global attributes"""
24
	msg = ""
25
	same = True
26
	with xr.open_dataset(file1) as ds1:
27
		ds2 = xr.open_dataset(file2)
28
		for v in ds1.variables:
29
			if ds1[v].attrs != ds2[v].attrs:
30
				msg = "{0}\nL\t{1}\t{2}\nR\t{1}\t{3}".format(
31
					msg, v, ds1[v].attrs, ds2[v].attrs,
32
				)
33
				same = False
34
		for attr in ds1.attrs:
35
			lattr = getattr(ds1, attr)
36
			rattr = getattr(ds2, attr)
37
			if lattr != rattr and attr not in ignore:
38
				msg = "{0}\nL\t{1}\t{2}\nR\t{1}\t{3}".format(
39
					msg, attr, lattr, rattr,
40
				)
41
				same = False
42
	assert same, msg
43
44
45
def ncallclose(file1, file2):
46
	with xr.open_dataset(file1) as ds1:
47
		ds2 = xr.open_dataset(file2)
48
		xr.testing.assert_allclose(ds1, ds2)
49
50
51
def ncequal(file1, file2):
52
	with xr.open_dataset(file1) as ds1:
53
		ds2 = xr.open_dataset(file2)
54
		xr.testing.assert_equal(ds1, ds2)
55
56
57
def ncidentical(file1, file2, ignore=[]):
58
	with xr.open_dataset(file1) as ds1:
59
		ds2 = xr.open_dataset(file2)
60
		for ign in ignore:
61
			del ds1.attrs[ign]
62
			del ds2.attrs[ign]
63
		xr.testing.assert_identical(ds1, ds2)
64
65
66
if __name__ == "__main__":
67
	nccmpattrs(*sys.argv[1:])
68
	ncallclose(*sys.argv[1:])
69
	ncequal(*sys.argv[1:])
70
	ncidentical(*sys.argv[1:])
71