diff --git a/ess/TraceWin.py b/ess/TraceWin.py
index be112bef3cdd4a4f850113cae8c94bb7f2be9636..9ff5895f12dd14f76b7335a3cac7fc437febded9 100644
--- a/ess/TraceWin.py
+++ b/ess/TraceWin.py
@@ -649,6 +649,78 @@ class density_file:
             self.Np+=o.Np
             self.Nrun+=o.Nrun
 
+    def savetohdf(self,filename='Density.h5',group='TraceWin'):
+        '''
+        Saves data to HDF5
+        '''
+        import h5py
+
+        fout = h5py.File(filename,'w')
+        group = fout.create_group(group)
+
+        # header attributes..
+        group.attrs['version']=self.version
+        group.attrs['year']=self.year
+        group.attrs['Nrun']=self.Nrun
+        group.attrs['vlong']=self.vlong
+
+        length=len(self.z)
+
+        partran = sum(self.Np)>0
+
+        # one number per location
+        arrays=     ['z', 'nelp', 'ib', 'Np', 'Xouv', 'Youv']
+        array_units=['m',     '', 'mA',   '',    'm',    'm']
+        if self.version>=8:
+            arrays +=      ['energy_accept', 'phase_ouv_pos', 'phase_ouv_neg']
+            array_units += [           'eV',           'deg',           'deg']
+        if partran:
+            arrays +=      [ 'lost2', 'Minlost', 'Maxlost', 'powlost2', 'Minpowlost', 'Maxpowlost']
+            array_units += [      '',        '',        '',      'W*w',          'W',          'W']
+
+        # 7 numbers per location..
+        coordinates=   ['moy', 'moy2', '_max', '_min']
+        coordinate_units=['m',  'm*m',   'm',   'm']
+        if self.version>=5 and partran:
+            coordinates += ['rms_size','rms_size2']
+            coordinate_units += [  'm',      'm*m']
+        if self.version>=6 and partran:
+            coordinates += ['min_pos_moy', 'max_pos_moy']
+            coordinate_units += [     'm',           'm']
+
+
+        for val,unit in zip(arrays,array_units):
+            data_set=group.create_dataset(val, (length,), dtype='f')
+            data_set[...]=getattr(self,val)
+            if unit:
+                data_set.attrs['unit']=unit
+
+        for val,unit in zip(coordinates,coordinate_units):
+            data_set=group.create_dataset(val, (length,7), dtype='f')
+            data_set[...]=getattr(self,val)
+            if unit:
+                data_set.attrs['unit']=unit
+
+        if self.version>=7 and partran:
+            # 3 numbers per location..
+            emit_data=['rms_emit', 'rms_emit2']
+            emit_units=['m*rad', 'm*m*rad*rad']
+            for val,unit in zip(emit_data,emit_units):
+                data_set=group.create_dataset(val, (length,3), dtype='f')
+                data_set[...]=getattr(self,val)
+                if unit:
+                    data_set.attrs['unit']=unit
+        if partran:
+            # 3 numbers per location..
+            data=['lost', 'powlost']
+            units=['', 'W']
+            for val,unit in zip(data, units):
+                data_set=group.create_dataset(val, (length,self.Nrun), dtype='f')
+                data_set[...]=getattr(self,val)
+                if unit:
+                    data_set.attrs['unit']=unit
+
+
 
 class remote_data_merger:
     def __init__(self, base='.'):