Skip to content

Commit

Permalink
Add hdf5 surface fields reading
Browse files Browse the repository at this point in the history
  • Loading branch information
amorison committed Sep 8, 2020
1 parent c8f7a9e commit 13467f5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
16 changes: 12 additions & 4 deletions stagpy/_step.py
Expand Up @@ -235,10 +235,10 @@ def __getitem__(self, name):
for fld_name, fld in zip(fld_names, fields):
if self._header['xyp'] == 0:
if not self.geom.twod_yz:
newline = (fld[:1, :, :, :] + fld[-1:, :, :, :]) / 2
newline = (fld[:1, ...] + fld[-1:, ...]) / 2
fld = np.concatenate((fld, newline), axis=0)
if not self.geom.twod_xz:
newline = (fld[:, :1, :, :] + fld[:, -1:, :, :]) / 2
newline = (fld[:, :1, ...] + fld[:, -1:, ...]) / 2
fld = np.concatenate((fld, newline), axis=1)
self._set(fld_name, fld)
return self._data[name]
Expand Down Expand Up @@ -277,8 +277,16 @@ def _get_raw_data(self, name):
for filestem, list_fvar in self._filesh5.items():
if name in list_fvar:
break
if filestem in phyvars.SFIELD_FILES_H5:
xmff = 'Data{}.xmf'.format(
'Bottom' if name.endswith('bot') else 'Surface')
_ = self.geom
header = self._header
else:
xmff = 'Data.xmf'
header = None
parsed_data = stagyyparsers.read_field_h5(
self.step.sdat.hdf5 / 'Data.xmf', filestem, self.step.isnap)
self.step.sdat.hdf5 / xmff, filestem, self.step.isnap, header)
return list_fvar, parsed_data

def _set(self, name, fld):
Expand Down Expand Up @@ -406,7 +414,7 @@ def __init__(self, istep, sdat):
self.fields = _Fields(self, phyvars.FIELD, phyvars.FIELD_EXTRA,
phyvars.FIELD_FILES, phyvars.FIELD_FILES_H5)
self.sfields = _Fields(self, phyvars.SFIELD, [],
phyvars.SFIELD_FILES, [])
phyvars.SFIELD_FILES, phyvars.SFIELD_FILES_H5)
self.tracers = _Tracers(self)
self._isnap = UNDETERMINED

Expand Down
14 changes: 14 additions & 0 deletions stagpy/phyvars.py
Expand Up @@ -107,6 +107,20 @@
('cr', ['crust']),
))

SFIELD_FILES_H5 = OrderedDict((
('BottomTopography', ['topo_bot']),
('SurfaceTopography', ['topo_top']),
('BottomGeoid', ['geoid_bot']),
('TopGeoid', ['geoid_top']),
('BottomCSGeoid', ['topo_g_bot']),
('TopCSGeoid', ['topo_g_top']),
('BottomHeatFlux', ['fbot']),
('TopHeatFlux', ['ftop']),
('BottomHFSpectrum', ['fsbot']),
('TopHFSpectrum', ['fstop']),
('CrustThickness', ['crust']),
))

Varr = namedtuple('Varr', ['description', 'kind', 'dim'])
RPROF = OrderedDict((
('r', Varr('Radial coordinate', 'Radius', 'm')),
Expand Down
12 changes: 10 additions & 2 deletions stagpy/stagyyparsers.py
Expand Up @@ -16,7 +16,7 @@
import h5py

from .error import ParsingError
from .phyvars import FIELD_FILES_H5
from .phyvars import FIELD_FILES_H5, SFIELD_FILES_H5


def _tidy_names(names, nnames, extra_names=None):
Expand Down Expand Up @@ -744,7 +744,7 @@ def _flds_shape(fieldname, header):
"""Compute shape of flds variable."""
shp = list(header['nts'])
shp.append(header['ntb'])
if len(FIELD_FILES_H5[fieldname]) == 3:
if len(FIELD_FILES_H5.get(fieldname, [])) == 3:
shp.insert(0, 3)
# extra points
header['xp'] = int(header['nts'][0] != 1)
Expand Down Expand Up @@ -813,10 +813,15 @@ def read_field_h5(xdmf_file, fieldname, snapshot, header=None):
fld = fld.reshape((shp[0], shp[1], 1, shp[2]))
if header['rcmb'] < 0:
fld = fld[(1, 2, 0), ...]
elif fieldname in SFIELD_FILES_H5:
fld = fld.reshape((1, npc[0], npc[1], 1))
elif header['nts'][1] == 1: # cart XZ
fld = fld.reshape((1, shp[0], 1, shp[1]))
ifs = [icore // np.prod(header['ncs'][:i]) % header['ncs'][i] *
npc[i] for i in range(3)]
if fieldname in SFIELD_FILES_H5:
ifs[2] = 0
npc[2] = 1
if header['zp']: # remove top row
fld = fld[:, :, :, :-1]
flds[:,
Expand All @@ -828,6 +833,9 @@ def read_field_h5(xdmf_file, fieldname, snapshot, header=None):

flds = _post_read_flds(flds, header)

if fieldname in SFIELD_FILES_H5:
# remove z component
flds = flds[..., 0, :]
return (header, flds) if data_found else None


Expand Down

0 comments on commit 13467f5

Please sign in to comment.