Commit 957b0b85 authored by Marco Kurzynski's avatar Marco Kurzynski
Browse files

fixed ncdf logic

parent f175e933
......@@ -29,72 +29,99 @@ NCDF
def compare_nc(nc1, nc2, percent_error=0.0, ignore=[]):
ret = 0
for key, dim1 in nc1.dimensions.items():
ret = 0 # innocent until proven guilty
# Comparing dimensions by keys
nc1_dims = list(nc1.dimensions.keys())
nc2_dims = list(nc2.dimensions.keys())
dims = {dim for dim in nc1_dims if dim in nc2_dims}
for dim in dims:
nc1_dim = nc1.dimensions[dim]
nc2_dim = nc2.dimensions[dim]
if nc1_dim.name != nc2_dim.name:
print(f"{tcolor.GREEN} Dimension {dim} names do not match\n< {nc1_dim.name}\n> {nc2_dim.name}")
if nc1_dim.size != nc2_dim.size:
print(f"{tcolor.GREEN} Dimension {dim} sizes do not match\n< {nc1_dim.size}\n> {nc2_dim.size}")
nc1_dims.remove(dim)
nc2_dims.remove(dim)
if nc1_dims:
print(f"{tcolor.GREEN} Dimensions {nc1_dims} only in {nc1.filepath()}{tcolor.ENDC}")
ret = -1
if nc2_dims:
print(f"{tcolor.GREEN} Dimensions {nc2_dims} only in {nc2.filepath()}{tcolor.ENDC}")
ret = -1
# Comparing attrs by name
nc1_attrs = nc1.ncattrs()
nc2_attrs = nc2.ncattrs()
for i in ignore:
try:
nc1_attrs.remove(i)
except ValueError:
pass
try:
dim2 = nc2.dimensions[key]
nc2_attrs.remove(i)
except ValueError:
pass
ncattrs = {attr for attr in nc1_attrs if attr in nc2_attrs}
if dim1.name != dim2.name:
print(
f"{tcolor.GREEN} Dimension name {dim1.name} and {dim2.name} differ{tcolor.ENDC}"
)
ret = -1
elif dim1.size != dim2.size:
print(
f"{tcolor.GREEN} Dimension size {dim1.size} and {dim2.size} differ{tcolor.ENDC}"
)
ret = -1
except KeyError:
print(f"{tcolor.GREEN} Dimension {key} missing{tcolor.ENDC}")
for attr in ncattrs:
attr1 = getattr(nc1, attr)
attr2 = getattr(nc2, attr)
if attr1 != attr2:
print(f"{tcolor.GREEN} Attribute {name} differs\n< {attr1}\n> {attr2}{tcolor.ENDC}")
ret = -1
nc1_attrs.remove(attr)
nc2_attrs.remove(attr)
c = Counter(attr for attr in nc1.ncattrs() + nc2.ncattrs())
if 1 in c.values(): # if 1 in values, that means an attr appeared in one nc file
print(f'{tcolor.GREEN} Attributes differ: {[attr for attr in c.keys() if c[attr] == 1]}{tcolor.ENDC}')
if nc1_attrs:
print(f"{tcolor.GREEN} Attributes {nc1_attrs} only in {nc1.filepath()}{tcolor.ENDC}")
ret = -1
if nc2_attrs:
print(f"{tcolor.GREEN} Attributes {nc2_attrs} only in {nc2.filepath()}{tcolor.ENDC}")
ret = -1
# if test have failed, return now
# Stop if dimensions or attrs do not match
if ret:
return ret
names = nc1.ncattrs()
for i in ignore:
try:
names.remove(i)
except ValueError:
pass
# Comparing variables by keys
nc1_variables = list(nc1.variables.keys())
nc2_variables = list(nc2.variables.keys())
variables = {var for var in nc1_variables if var in nc2_variables}
for name in names:
attr1 = getattr(nc1, name)
attr2 = getattr(nc2, name)
if attr1 != attr2:
print(
f"{tcolor.GREEN} Attribute {name} differs\n< {attr1}\n> {attr2}{tcolor.ENDC}"
)
for var_key in variables:
var1 = nc1[var_key]
var2 = nc2[var_key]
if var2.shape != var1.shape:
print(f"{tcolor.GREEN} Shape {var1.size} and {var2.size} differ{tcolor.ENDC}")
ret = -1
for key, var1 in nc1.variables.items():
try:
var2 = nc2[key]
if var2.shape != var1.shape:
print(
f"{tcolor.GREEN} Shape {dim1.size} and {dim2.size} differ{tcolor.ENDC}"
)
elif var1[:].any() and var2[:].any():
visclose = np.vectorize(isclose)
var1_valid = var1[:][~np.isnan(var1[:])]
var2_valid = var2[:][~np.isnan(var2[:])]
if var1_valid.any() and var2_valid.any() and var1_valid.size == var2_valid.size:
is_close = visclose(var1_valid, var2_valid, percent_error=percent_error)
if var2_valid.size != np.count_nonzero(is_close):
print(f"{tcolor.GREEN} FP Error: variable {var_key}\n< {var1[:][~is_close]}\n> {var2[:][~is_close]}{tcolor.ENDC}")
ret = -1
elif var1_valid.any() or var2_valid.any():
print(f"{tcolor.GREEN} Variable {var_key} has unequal number of nan values\n< {var1_valid.size}\n> {var2_valid.size}{tcolor.ENDC}")
ret = -1
# if var passed all if statements or both variables contain all nans, return 0
visClose = np.vectorize(isclose)
if np.count_nonzero(np.isnan(var1)) == 0 and np.count_nonzero(
np.isnan(var2)) == 0:
is_close = visClose(var1[:][~np.isnan(var1[:])],
var2[:][~np.isnan(var2[:])])
if var1.size != np.count_nonzero(is_close):
print(
f"{tcolor.GREEN} FP Error: variable {key}\n< {var1[:][~is_close]}\n> {var2[:][~is_close]}{tcolor.ENDC}"
)
ret = -1
except KeyError:
print(f"{tcolor.GREEN} Variable {key} missing{tcolor.ENDC}")
ret = -1
nc1_variables.remove(var_key)
nc2_variables.remove(var_key)
if nc1_variables:
print(f"{tcolor.GREEN} Attributes {nc1_attrs} only in {nc1.filepath()}{tcolor.ENDC}")
ret = -1
if nc2_variables:
print(f"{tcolor.GREEN} Attributes {nc2_attrs} only in {nc2.filepath()}{tcolor.ENDC}")
ret = -1
return ret
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment