From fbc4eaca4cfa537994303a754d42011ec446b809 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Franck=20P=C3=A9rignon?= <franck.perignon@imag.fr>
Date: Mon, 24 Mar 2014 14:14:52 +0000
Subject: [PATCH] Update hdf reader

---
 HySoP/hysop/operator/monitors/reader.py | 77 ++++++++++++++++---------
 1 file changed, 51 insertions(+), 26 deletions(-)

diff --git a/HySoP/hysop/operator/monitors/reader.py b/HySoP/hysop/operator/monitors/reader.py
index 67ce51eb7..4847fea28 100644
--- a/HySoP/hysop/operator/monitors/reader.py
+++ b/HySoP/hysop/operator/monitors/reader.py
@@ -21,7 +21,7 @@ class Reader(Monitoring):
     """
     def __init__(self, variables, topo, prefix,
                  formattype=None, task_id=None,
-                 subset=None):
+                 subset=None, names=None):
         """
         @param variables : list of variables to read.
         @param prefix input file name (without ext)
@@ -65,39 +65,62 @@ class Reader(Monitoring):
             self._slices = self.topo.mesh.iCompute
         self.globalResolution.reverse()
         self.step = self.readHDF5
-
-    @debug
-    @timed_function
-    def apply(self, simulation=None):
-        self.step()
-
-    def finalize(self):
-        Monitoring.finalize(self)
-
-    def readHDF5(self):
         filename = self.prefix + '.h5'
         assert os.path.isfile(filename), 'error, file does not exists'
         if self.topo.size == 1:
-            f = h5py.File(filename, "r")
+            self._file = h5py.File(filename, "r")
         else:
-            f = h5py.File(filename, 'r', driver='mpio', comm=self.topo.comm)
-        # It's necessary to compute the set of indices of the current subset
-        # in global notation
+            self._file = h5py.File(filename, 'r', driver='mpio',
+                                   comm=self.topo.comm)
+        self._globalSlice = []
         if self.subset is None:
             # Note : g_start and g_end do not include ghost points.
             g_start = self.topo.mesh.global_start
             g_end = self.topo.mesh.global_end + 1
-            sl = [slice(g_start[i], g_end[i])
-                  for i in xrange(self.domain.dimension)]
+            self._globalSlice = [slice(g_start[i], g_end[i])
+                                 for i in xrange(self.domain.dimension)]
         else:
             g_start = self.subset.gstart
             # convert self._slices to global position in topo
-            sl = self.topo.toIndexGlobal(self._slices)
+            self._globalSlice = self.topo.toIndexGlobal(self._slices)
             # And shift using global position of the surface
-            sl = [slice(sl[i].start - g_start[i], sl[i].stop - g_start[i])
-                  for i in xrange(self.domain.dimension)]
-        sl.reverse()
-        sl = tuple(sl)
+            self._globalSlice = [slice(self._globalSlice[i].start - g_start[i],
+                                       self._globalSlice[i].stop - g_start[i])
+                                 for i in xrange(self.domain.dimension)]
+        self._globalSlice.reverse()
+        self._globalSlice = tuple(self._globalSlice)
+        if names is None:
+            self.names = {}
+            names = self.dataset_names()
+            i = 0
+            for field in self.variables:
+                self.names[field] = []
+                for d in xrange(field.nbComponents):
+                    self.names[field].append(names[i])
+                    i += 1
+        else:
+            self.names = names
+            for field in self.variables:
+                fname = self.names[field]
+                self.names[field] = [v for v in self.dataset_names()
+                                     if fname in v]
+                assert len(self.names[field]) == field.nbComponents
+
+    @debug
+    @timed_function
+    def apply(self, simulation=None):
+        self.step()
+
+    def dataset_names(self):
+        """
+        Return the list of available names for datasets in
+        the required file.
+        """
+        return self._file.keys()
+
+    def readHDF5(self):
+        # It's necessary to compute the set of indices of the current subset
+        # in global notation
         for field in self.variables:
             df = field.discreteFields[self.topo]
             for d in xrange(df.nbComponents):
@@ -109,10 +132,12 @@ class Reader(Monitoring):
                 #Note FP : temp method --> use the first name is dataset
                 # as data for field. Todo : set a list of names
                 # to be downloaded.
-                currentName = f.keys()[0]
-                ds = f[currentName]
+                currentName = self.names[field][d]
+                ds = self._file[currentName]
                 # In parallel, each proc must write in the proper part
                 # Of the dataset (of site global resolution)
-                df.data[d][self._slices] = ds[sl].T
-        f.close()
+                df.data[d][self._slices] = ds[self._globalSlice].T
 
+    def finalize(self):
+        Monitoring.finalize(self)
+        self._file.close()
-- 
GitLab