PYTHON: Rewrite amun_compatible().

Signed-off-by: Grzegorz Kowal <grzegorz@amuncode.org>
This commit is contained in:
Grzegorz Kowal 2019-11-22 17:39:49 -03:00
parent ec6ea2cbf7
commit 325208559e

View File

@ -37,6 +37,7 @@ import numpy as np
import os.path as op import os.path as op
import sys import sys
def amun_compatible(fname): def amun_compatible(fname):
''' '''
Subroutine checks if the HDF5 file is AMUN compatible. Subroutine checks if the HDF5 file is AMUN compatible.
@ -47,39 +48,28 @@ def amun_compatible(fname):
Return values: Return values:
ret - True or False; True or False;
Examples: Examples:
comp = amun_compatible('p000010_00000.h5') comp = amun_compatible('p000010_00000.h5')
''' '''
try: with h5.File(fname, 'r') as f:
f = h5.File(fname, 'r') if 'codes' in f.attrs:
if f.attrs['code'].astype(str) == "AMUN":
# check if the file is written in the AMUN format or at least contains return True
# necessary groups else:
# print("'%s' contains attribute 'code'," % fname, \
ret = True " but it is not 'AMUN'!")
if 'code' in f.attrs: return False
if f.attrs['code'].astype(str) != "AMUN": elif 'attributes' in f and 'coordinates' in f and \
print("'%s' contains attribute 'code'", \ 'variables' in f:
" but it is not set to 'AMUN'!" % fname) return True
ret = False else:
elif not 'attributes' in f or \ print("'%s' misses one of these groups:" % fname, \
not 'coordinates' in f or \ "'attributes', 'coordinates' or 'variables'!")
not 'variables' in f: return False
print("'%s' misses one of these groups: ", \
"'attributes', 'coordinates' or 'variables'!" % fname)
ret = False
f.close()
except:
print("It seems '%s' is not an HDF5 file!" % fname)
ret = False
return ret
def amun_attribute(fname, aname): def amun_attribute(fname, aname):