PYTHON: Rewrite subroutine amun_dataset().

Signed-off-by: Grzegorz Kowal <grzegorz@amuncode.org>
This commit is contained in:
Grzegorz Kowal 2018-11-06 12:45:07 -02:00
parent 43e5b0b5f2
commit 5b28e11182

View File

@ -166,11 +166,11 @@ def amun_coordinate(fname, iname):
def amun_dataset(fname, vname, shrink = 1, progress = False):
'''
Subroutine to reads dataset from AMUN HDF5 snapshots.
Subroutine to read datasets from AMUN HDF5 snapshots.
Arguments:
fname - the AMUN HDF5 file name;
fname - the HDF5 file name;
vname - the variable name;
shrink - the shrink factor (must be the power of 2 and not larger
than the block size);
@ -188,12 +188,7 @@ def amun_dataset(fname, vname, shrink = 1, progress = False):
if not amun_compatible(fname):
return False
# open the file
#
f = h5.File(fname, 'r')
# get the file path
#
try:
dname = op.dirname(fname)
if progress:
@ -201,27 +196,32 @@ def amun_dataset(fname, vname, shrink = 1, progress = False):
# get attributes necessary to reconstruct the domain
#
g = f['attributes'].attrs
# get the set of equations used to perform the simulation
# and the equation of state
#
eqsys = g.get('eqsys')[0].astype(str)
eos = g.get('eos')[0].astype(str)
# get the snapshot number, the number of domain files, and the number of blocks
#
nr = g.get('isnap')[0]
nc = g.get('nprocs')[0]
nl = g.get('nleafs')[0]
eqsys = amun_attribute(fname, 'eqsys')
eos = amun_attribute(fname, 'eos')
nr = amun_attribute(fname, 'isnap')
nc = amun_attribute(fname, 'nprocs')
nl = amun_attribute(fname, 'nleafs')
if eos == 'adi':
gm = g.get('gamma')[0]
gm = amun_attribute(fname, 'gamma')
# prepare array to hold data
#
bm = amun_attribute(fname, 'dims')
if bm[2] > 1:
ndims = 3
else:
ndims = 2
rm = amun_attribute(fname, 'rdims')
ng = amun_attribute(fname, 'nghosts')
ml = amun_attribute(fname, 'maxlev')
# build the list of supported variables
#
variables = []
f = h5.File(fname, 'r')
for var in f['variables'].keys():
variables.append(var)
f.close()
# add derived variables if possible
#
@ -264,19 +264,6 @@ def amun_dataset(fname, vname, shrink = 1, progress = False):
print('The requested variable cannot be extracted from the file datasets!')
return False
# prepare array to hold data
#
bm = g.get('dims')
if bm[2] > 1:
ndims = 3
else:
ndims = 2
rm = g.get('rdims')
ng = g.get('nghosts')
ml = g.get('maxlev')[0]
f.close()
# check if the shrink parameter is correct
#
sh = bm[0:ndims].min()
@ -296,16 +283,14 @@ def amun_dataset(fname, vname, shrink = 1, progress = False):
for n in range(nc):
fname = 'p%06d_%05d.h5' % (nr, n)
lname = op.join(dname, fname)
f = h5.File(lname, 'r')
g = f['attributes'].attrs
dblocks = g.get('dblocks')[0]
dblocks = amun_attribute(lname, 'dblocks')
if dblocks > 0:
g = f['coordinates']
levels = g['levels'][()]
coords = g['coords'][()]
dx = g['dx'][()]
dy = g['dy'][()]
dz = g['dz'][()]
levels = amun_coordinate(lname, 'levels')
coords = amun_coordinate(lname, 'coords')
dx = amun_coordinate(lname, 'dx')
dy = amun_coordinate(lname, 'dy')
dz = amun_coordinate(lname, 'dz')
f = h5.File(lname, 'r')
g = f['variables']
if vname == 'velo':
dataset = np.sqrt(g['velx'][:,:,:,:]**2 \
@ -429,6 +414,8 @@ def amun_dataset(fname, vname, shrink = 1, progress = False):
else:
dataset = g[vname][:,:,:,:]
f.close()
# rescale all blocks to the effective resolution
#
for l in range(dblocks):
@ -455,14 +442,14 @@ def amun_dataset(fname, vname, shrink = 1, progress = False):
% (vname, fname, nb, nl))
sys.stdout.flush()
f.close()
if (progress):
sys.stdout.write('\n')
sys.stdout.flush()
# return the dataset
#
except:
print("Dataset '%s' cannot be retrieved from '%s'!" % (vname, fname))
ret = False
return ret