From ce89f22a5187e137243db015bf3b4720b19bb221 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Le=20Guyader?= <loic.le.guyader@xfel.eu>
Date: Wed, 8 Apr 2020 11:23:35 +0200
Subject: [PATCH] Clean up remaining scan_variable.nc file saving code

---
 DSSC.py | 35 ++++++++---------------------------
 1 file changed, 8 insertions(+), 27 deletions(-)

diff --git a/DSSC.py b/DSSC.py
index 90373df..52eb8a1 100644
--- a/DSSC.py
+++ b/DSSC.py
@@ -106,9 +106,9 @@ class DSSC:
         # create a dummy scan variable for dark run
         # for other type or run, use DSSC.define_run function to overwrite it
         self.scan = xr.DataArray(np.ones_like(self.run.train_ids), dims=['trainId'],
-                                 coords={'trainId': self.run.train_ids})
+                                 coords={'trainId': self.run.train_ids}).to_dataset(
+                        name='scan_variable')
         self.scan_vname = 'dummy'
-        self.vds_scan = None
         
     def define_scan(self, vname, bins):
         """
@@ -121,30 +121,24 @@ class DSSC:
         """
 
         if type(vname) is dict:
-            self.scan = self.run.get_array(vname['source'], vname['key'])
+            scan = self.run.get_array(vname['source'], vname['key'])
         elif type(vname) is str:
             if vname not in tb.mnemonics:
                 raise ValueError(f'{vname} not found in the ToolBox mnemonics table')
-            self.scan = self.run.get_array(tb.mnemonics[vname]['source'], tb.mnemonics[vname]['key'])
+            scan = self.run.get_array(tb.mnemonics[vname]['source'], tb.mnemonics[vname]['key'])
         else:
             raise ValueError(f'vname should be a string or a dict. We got {type(vname)}')
             
         if (type(bins) is int) or (type(bins) is float):
-            self.scan = bins * np.round(self.scan / bins)
+            scan = bins * np.round(scan / bins)
         else:
             # TODO: digitize the data
             raise ValueError(f'To be implemented')
         self.scan_vname = vname
-        
-        self.vds_scan = os.path.join(self.tempdir, 'scan_variable.nc')
-        if os.path.isfile(self.vds_scan):
-            os.remove(self.vds_scan)
-            
-        self.scan = self.scan.to_dataset(name='scan_variable')
+       
+        self.scan = scan.to_dataset(name='scan_variable')
         self.scan['xgm_pumped'] = self.xgm[:, :self.nbunches:2].mean('dim_0')
         self.scan['xgm_unpumped'] = self.xgm[:, 1:self.nbunches:2].mean('dim_0')
-        self.scan.to_netcdf(self.vds_scan, group='data')
-        self.scan.close()
 
         self.scan_counts = xr.DataArray(np.ones(len(self.scan['scan_variable'])),
                                         dims=['scan_variable'],
@@ -297,15 +291,6 @@ class DSSC:
         """ Bin the DSSC data by the predifined scan type (DSSC.define()) using multiprocessing
         
         """
-        if self.vds_scan is None:
-            # probably a dark run with a dummy scan variable
-            self.vds_scan = os.path.join(self.tempdir, 'scan_variable.nc')
-            if os.path.isfile(self.vds_scan):
-                os.remove(self.vds_scan)
-            
-            self.scan = self.scan.to_dataset(name='scan_variable')
-            self.scan.to_netcdf(self.vds_scan, group='data')
-            self.scan.close()
 
         # get available memory in GB, we will try to use 80 % of it
         max_GB = psutil.virtual_memory().available/1024**3
@@ -316,10 +301,6 @@ class DSSC:
         
         print('processing', self.chunksize, 'trains per chunk')
                    
-        # load scan variable
-        scan = xr.load_dataset(self.vds_scan, group='data')['scan_variable']
-        scan.name = 'scan'
- 
         jobs = []
         for m in range(16):
             jobs.append(dict(
@@ -327,7 +308,7 @@ class DSSC:
             fpt=self.fpt,
             vdf_module=os.path.join(self.tempdir, f'dssc{m}_vds.h5'),
             chunksize=self.chunksize,
-            scan=scan,
+            scan=self.scan['scan_variable'],
             nbunches=self.nbunches,
             run_nr=self.run_nr,
             do_pulse_mean=do_pulse_mean
-- 
GitLab