From f4ee6c2438ee1970ef82b1340fb8974e597c4f14 Mon Sep 17 00:00:00 2001 From: Georg Date: Fri, 31 Oct 2014 14:50:21 +0100 Subject: [PATCH] implement VSIM Dumpreader tested if methods return right value (except getderived all methods implemented). Using _const.axesidentify for x,y,z,px,py,pz; weight and ID not included. All other methods should behave like for sdf. Neverthelerss additional esting required-- --- .gitignore | 1 + postpic/datareader/__init__.py | 7 +- postpic/datareader/vsimhdf5.py | 147 +++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 postpic/datareader/vsimhdf5.py diff --git a/.gitignore b/.gitignore index 5f66a2db..6daaf5c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *~ *.pyc +*.odt .project .pydevproject *.egg-info diff --git a/postpic/datareader/__init__.py b/postpic/datareader/__init__.py index ed0426c8..321168b5 100644 --- a/postpic/datareader/__init__.py +++ b/postpic/datareader/__init__.py @@ -314,13 +314,18 @@ def chooseCode(code): Args: code : string Possible options are: - - "EPOCH": .sdf files written by EPOCH1D, EPOCH2D or EPOCH3D. - "DUMMY": dummy class creating fake data. + - "EPOCH": .sdf files written by EPOCH1D, EPOCH2D or EPOCH3D. + - "VSIM": .hdf5 files written by VSim. ''' if code in ['EPOCH', 'epoch', 'EPOCH1D', 'EPOCH2D', 'EPOCH3D']: from epochsdf import Sdfreader, Visitreader setdumpreadercls(Sdfreader) setsimreadercls(Visitreader) + elif code in ['VSim', 'VSIM', 'vsim']: + from vsimhdf5 import Hdf5reader + setdumpreadercls(Hdf5reader) + # no Simulationsreader implemented yet elif code in ['DUMMY', 'dummy']: from dummy import Dummyreader, Dummysim setdumpreadercls(Dummyreader) diff --git a/postpic/datareader/vsimhdf5.py b/postpic/datareader/vsimhdf5.py new file mode 100644 index 00000000..254a2da3 --- /dev/null +++ b/postpic/datareader/vsimhdf5.py @@ -0,0 +1,147 @@ +# +# This file is part of postpic. +# +# postpic is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# postpic is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with postpic. If not, see . +# +# Georg Wittig, Stephan Kuschel 2014 + +''' +Reader for HDF5 File format written by the VSim Code: +http://www.txcorp.com/support/vsim-support-menu/vsim-documentation +Dependecies: +h5py The Python actual reader for hdf5 file format. +Georg Wittig, Stephan Kuschel 2014 +''' +from . import Dumpreader_ifc +from . import Simulationreader_ifc +from .. import _const + + +import h5py +import numpy as np +import os + +__all__ = ['Hdf5reader'] + + +class Hdf5reader(Dumpreader_ifc): + ''' + The Reader implementation for HDF5 Data written by the VSim Code. + as argument h5file can be any *.h5 file of the dump of consideration. + ''' + def __init__(self, h5file, **kwargs): + ''' + Initializes the Hdf5reader for a specific h5file. + ''' + super(self.__class__, self).__init__(h5file, **kwargs) + if not os.path.isfile(h5file): + raise IOError('File "' + str(h5file) + '" doesnt exist.') + self.h5file = h5file + filelist = [f for f in os.listdir(".") if f.endswith(".h5")] + self._time = h5py.File(h5file)["time"].attrs["vsTime"] + # all dumped h5 files at one time + self._dumplist = [f for f in filelist + if self._time == h5py.File(f)["time"].attrs["vsTime"]] + + def keys(self): + keys = [] + for f in self._dumplist: + for a in h5py.File(f): + if a not in keys: + keys.append(a) + return keys + + def __getitem__(self, key): + ''' delivers one dataset with the key key.''' + for f in self._dumplist: + for a in h5py.File(f): + if a == key: + return h5py.File(f)[key] + # print " couldn't find key ", key + return None + + def timestep(self): + ''' returns the timestem 0.5*DX/c ''' + temp = self["compGridGlobal"] + NX = temp.attrs["vsNumCells"][0] + xl = temp.attrs["vsLowerBounds"][0] + xu = temp.attrs["vsUpperBounds"][0] + LX = abs(xu-xl) + return 0.5*(LX/NX)/299792458. + + def time(self): + return self._time + + def simdimensions(self): + return self["compGridGlobal"].attrs["vsNumCells"].shape[0] + + def dataE(self, axis, **kwargs): + # x, y, z, px, py, pz same as in sdf. weigt and ID not included. + axis = _const.axesidentify[axis] + try: + return np.float64(self["ElecMultiField"][..., axis]) + except: + return None + + def dataB(self, axis, **kwargs): + # x, y, z, px, py, pz same as in sdf. wweigt and ID not included. + axis = _const.axesidentify[axis] + try: + return np.float64(self["MagMultiField"][..., axis]) + except: + return None + + def grid(self, axis): + ''' returns the array of the positions of all cells on axis = axis. ''' + # x, y, z, px, py, pz same as in sdf. weigt and ID not included. + axis = _const.axesidentify[axis] + temp = self["compGridGlobal"] + return np.linspace(temp.attrs["vsLowerBounds"][axis], + temp.attrs["vsUpperBounds"][axis], temp.attrs["vsNumCells"][axis]) + + def listSpecies(self): + ''' returns all h5 dumps that have a attribute "mass" ''' + SpeciesList = [] + for f in self._dumplist: + for k in h5py.File(f): + for a in h5py.File(f)[k].attrs: + if a == "mass": + SpeciesList.append(k) + break + return SpeciesList + + def getSpecies(self, species, attrib): + ''' + Returns one of the attributes out of (x,y,z,px,py,pz,weight,ID) of + this particle species. + returning None means that this particle property wasnt dumped. + Note that this is different from returning an empty list! + ''' + # x, y, z, px, py, pz same as in sdf. weigt and ID not included. + attrib = _const.axesidentify[attrib] + attrib = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 9: 6, 10: 7}[attrib] + try: + return np.float64(self[species])[:, attrib] + except: + return None + + def getderived(self): + ''' + Returns all Keys starting with "Derived/". + ''' + pass + + def __str__(self): + return '' +