From 540baa9e463b58a07c35fd8ee5db87de8ce427cf Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Tue, 10 Mar 2020 12:53:39 +0000 Subject: [PATCH] re-factored common counter code to Counter class --- slic/daq/bscounter.py | 30 ++++-------------------------- slic/daq/counter.py | 40 ++++++++++++++++++++++++++++++++++++++++ slic/daq/dbcounter.py | 30 ++++-------------------------- slic/daq/diacounter.py | 1 - 4 files changed, 48 insertions(+), 53 deletions(-) create mode 100644 slic/daq/counter.py diff --git a/slic/daq/bscounter.py b/slic/daq/bscounter.py index 0b7973ed..7438b9e7 100644 --- a/slic/daq/bscounter.py +++ b/slic/daq/bscounter.py @@ -1,13 +1,9 @@ -from datetime import datetime, timedelta -import os import zmq from bsread.h5 import receive from bsread.avail import dispatcher -from .acquisition import Acquisition -from .basecounter import BaseCounter -from .utils import can_create_file, fix_hdf5_filename +from .counter import Counter @@ -17,28 +13,10 @@ def bsread_to_h5(filename, channels, n_pulses=100, queue_size=100, mode=zmq.SUB) -class BSCounter(BaseCounter): +class BSCounter(Counter): - def __init__(self, default_channels=None, default_path="."): - self.default_channels = default_channels - self.default_path = default_path - - - def acquire(self, filename=None, channels=None, use_default_path=True, **kwargs): - if filename and use_default_path: - filename = os.path.join(self.default_path, filename) - - filename = fix_hdf5_filename(filename) - - if not can_create_file(filename): - return - - if not channels: - print("No channels specified, using default channel list.") - channels = self.default_channels - - acq = lambda: bsread_to_h5(filename, channels, **kwargs) - return Acquisition(acq, hold=False) + def _acquire(self, *args, **kwargs): + bsread_to_h5(*args, **kwargs) diff --git a/slic/daq/counter.py b/slic/daq/counter.py new file mode 100644 index 00000000..24313b62 --- /dev/null +++ b/slic/daq/counter.py @@ -0,0 +1,40 @@ +import os +from abc import abstractmethod + +from .basecounter import BaseCounter +from .utils import can_create_file, fix_hdf5_filename + +from .acquisition import Acquisition + + + +class Counter(BaseCounter): + + def __init__(self, default_channels=None, default_path="."): + self.default_channels = default_channels + self.default_path = default_path + + + def acquire(self, filename=None, channels=None, use_default_path=True, **kwargs): + if filename and use_default_path: + filename = os.path.join(self.default_path, filename) + + filename = fix_hdf5_filename(filename) + + if not can_create_file(filename): + return + + if not channels: + print("No channels specified, using default channel list.") + channels = self.default_channels + + acq = lambda: self._acquire(filename, channels, **kwargs) + return Acquisition(acq, hold=False) + + + @abstractmethod + def _acquire(self): + raise NotImplementedError + + + diff --git a/slic/daq/dbcounter.py b/slic/daq/dbcounter.py index 6978cde4..309f8637 100644 --- a/slic/daq/dbcounter.py +++ b/slic/daq/dbcounter.py @@ -1,12 +1,8 @@ from datetime import datetime, timedelta -import os -import zmq import data_api as dapi -from .acquisition import Acquisition -from .basecounter import BaseCounter -from .utils import can_create_file, fix_hdf5_filename +from .counter import Counter @@ -30,28 +26,10 @@ def dapi_get(channels, start_time_delta=None, end_time_delta=None): -class DBCounter(BaseCounter): +class DBCounter(Counter): - def __init__(self, default_channels=None, default_path="."): - self.default_channels = default_channels - self.default_path = default_path - - - def acquire(self, filename=None, channels=None, use_default_path=True, **kwargs): - if filename and use_default_path: - filename = os.path.join(self.default_path, filename) - - filename = fix_hdf5_filename(filename) - - if not can_create_file(filename): - return - - if not channels: - print("No channels specified, using default channel list.") - channels = self.default_channels - - acq = lambda: dapi_to_h5(filename, channels, **kwargs) - return Acquisition(acq, hold=False) + def _acquire(self, *args, **kwargs): + dapi_to_h5(*args, **kwargs) diff --git a/slic/daq/diacounter.py b/slic/daq/diacounter.py index d6a38ad3..8fa2912b 100644 --- a/slic/daq/diacounter.py +++ b/slic/daq/diacounter.py @@ -68,7 +68,6 @@ class DIACounter(BaseCounter): self.detector_config = { "timing": "trigger", - # FIXME: HARDCODED: For Alvra JF4.5 it's 0.000005, Bernina is using 0.00001 "exptime": 0.000005, "cycles": self.n_frames,