From 49cb67e3064ea8aeaaeb6f170dbf00b58908bd4b Mon Sep 17 00:00:00 2001 From: Chris Milne Date: Fri, 22 Jun 2018 14:06:38 +0200 Subject: [PATCH] updated DIAClient to match bernina --- devices_general/alvradetectors.py | 529 +++++++++++++++++++++--------- 1 file changed, 372 insertions(+), 157 deletions(-) diff --git a/devices_general/alvradetectors.py b/devices_general/alvradetectors.py index b69108d..030b780 100755 --- a/devices_general/alvradetectors.py +++ b/devices_general/alvradetectors.py @@ -10,24 +10,20 @@ import subprocess import h5py from time import sleep from threading import Thread +from datetime import datetime from ..acquisition.utilities import Acquisition -import subprocess - - try: import sys, os - tpath = "/sf/alvra/config/jungfrau/envs/jungfrau_client/lib/python3.6/site-packages/" # os.path.dirname(__file__) - sys.path.insert(0,os.path.join(tpath,'/jungfrau_utils')) - - #tpath = "/sf/alvra/config/src/python" - #sys.path.insert(0,os.path.join(tpath,'/detector_integration_api')) + tpath = os.path.dirname(__file__) + sys.path.insert(0,os.path.join(tpath,'../../detector_integration_api')) + #ask Leo(2018.03.14): + #sys.path.insert(0,os.path.join(tpath,'../../jungfrau_utils')) from detector_integration_api import DetectorIntegrationClient - print("DONE") except: - print('NB: JF detector integration could not be imported!') - + print('NB: detector integration could not be imported!') + _cameraArrayTypes = ['monochrome','rgb'] @@ -138,159 +134,358 @@ class DiodeDigitizer: -class JF: - def __init__(self, Id): - self.writer_config = "" - self.backend_config = "" - self.detector_config = "" - self.Id = Id - self.api_address = self.Id - self.client = DetectorIntegrationClient(self.Id) - - def reset(self): - self.client.reset() - pass - - def get_status(self): - status = self.client.get_status() - return status - - def get_config(self): - config = self.client.get_config() - return config - - def set_config(self, pedestal_fname = '/sf/alvra/data/res/p16581/pedestal_20171210_1628_res.h5/', fname = "/sf/alvra/data/raw/p16581/JF.h5", N = 1000): - - self.reset() - self.detector_config = { - "timing": "trigger", - "exptime": 0.000005, - "delay" : 0.001992, # this is the magic aldo number - "frames" : 1, - "cycles": N} - - - self.writer_config = { - "output_file": fname, - "process_uid": 16581, - "process_gid": 16581, - "dataset_name": "jungfrau/data", - "n_messages": N} - - self.backend_config = { - "n_frames": N, - "gain_corrections_filename": "/sf/alvra/config/jungfrau/jungfrau_4p5_gaincorrections_v0.h5", - "gain_corrections_dataset": "gains", - "pede_corrections_filename": pedestal_fname, - "pede_corrections_dataset": "gains", - "activate_corrections_preview": True} - - - DetectorIntegrationClient.set_config(self,self.writer_config, self.backend_config, self.detector_config) - pass - - def record(self,file_name,Npulses): - self.detector_config.update(dict(cycles=Npulses)) - self.writer_config.update(dict(output_file=file_name)) - self.reset() - DetectorIntegrationClient.set_config(self,self.writer_config, self.backend_config, self.detector_config) - self.client.start() - - def check_running(self,time_interval=.5): - cfg = self.get_config() - running = False - while not running: - if self.get_status()['status'][-7:]=='RUNNING': - running = True - break - else: - sleep(time_interval) - - def check_still_running(self,time_interval=.5): - cfg = self.get_config() - running = True - while running: - if not self.get_status()['status'][-7:]=='RUNNING': - running = False - break - else: - sleep(time_interval) - - def start(self): - self.client.start() - print("start acquisition") - pass - - def stop(self): - self.client.stop() - print("stop acquisition") - pass - - def config_and_start_test(self): - self.reset() - self.set_config() - self.start() - pass - - def acquire(self,file_name=None,Npulses=100): - file_name += '_JF4p5M.h5' - def acquire(): - self.reset() - self.detector_config.update(dict(cycles=Npulses)) - self.writer_config.update(dict(output_file=file_name)) - self.set_config(f = file_name, N = Npulses) - self.client.start() - self.check_running() - self.check_still_running() - - return Acquisition(acquire=acquire,acquisition_kwargs={'file_names':[file_name], 'Npulses':Npulses},hold=False) - - - - def wait_done(self): - self.check_running() - self.check_still_running() +# class JF: +# def __init__(self, Id): +# self.writer_config = "" +# self.backend_config = "" +# self.detector_config = "" +# self.Id = Id +# self.api_address = self.Id +# self.client = DetectorIntegrationClient(self.Id) +# +# def reset(self): +# self.client.reset() +# pass +# +# def get_status(self): +# status = self.client.get_status() +# return status +# +# def get_config(self): +# config = self.client.get_config() +# return config +# +# def set_config(self, pedestal_fname = '/sf/alvra/data/res/p16581/pedestal_20171210_1628_res.h5/', fname = "/sf/alvra/data/raw/p16581/JF.h5", N = 1000): +# +# self.reset() +# self.detector_config = { +# "timing": "trigger", +# "exptime": 0.000005, +# "delay" : 0.001992, # this is the magic aldo number +# "frames" : 1, +# "cycles": N} +# +# +# self.writer_config = { +# "output_file": fname, +# "process_uid": 16581, +# "process_gid": 16581, +# "dataset_name": "jungfrau/data", +# "n_messages": N} +# +# self.backend_config = { +# "n_frames": N, +# "gain_corrections_filename": "/sf/alvra/config/jungfrau/jungfrau_4p5_gaincorrections_v0.h5", +# "gain_corrections_dataset": "gains", +# "pede_corrections_filename": pedestal_fname, +# "pede_corrections_dataset": "gains", +# "activate_corrections_preview": True} +# +# +# DetectorIntegrationClient.set_config(self,self.writer_config, self.backend_config, self.detector_config) +# pass +# +# def record(self,file_name,Npulses): +# self.detector_config.update(dict(cycles=Npulses)) +# self.writer_config.update(dict(output_file=file_name)) +# self.reset() +# DetectorIntegrationClient.set_config(self,self.writer_config, self.backend_config, self.detector_config) +# self.client.start() +# +# def check_running(self,time_interval=.5): +# cfg = self.get_config() +# running = False +# while not running: +# if self.get_status()['status'][-7:]=='RUNNING': +# running = True +# break +# else: +# sleep(time_interval) +# +# def check_still_running(self,time_interval=.5): +# cfg = self.get_config() +# running = True +# while running: +# if not self.get_status()['status'][-7:]=='RUNNING': +# running = False +# break +# else: +# sleep(time_interval) +# +# def start(self): +# self.client.start() +# print("start acquisition") +# pass +# +# def stop(self): +# self.client.stop() +# print("stop acquisition") +# pass +# +# def config_and_start_test(self): +# self.reset() +# self.set_config() +# self.start() +# pass +# +# def acquire(self,file_name=None,Npulses=100): +# file_name += '_JF4p5M.h5' +# def acquire(): +# self.reset() +# self.detector_config.update(dict(cycles=Npulses)) +# self.writer_config.update(dict(output_file=file_name)) +# self.set_config(f = file_name, N = Npulses) +# self.client.start() +# self.check_running() +# self.check_still_running() +# +# return Acquisition(acquire=acquire,acquisition_kwargs={'file_names':[file_name], 'Npulses':Npulses},hold=False) +# +# +# +# def wait_done(self): +# self.check_running() +# self.check_still_running() -def parseChannelListFile(fina): - out = [] - with open(fina,'r') as f: - done = False - while not done: - d = f.readline() - if not d: - done=True - if len(d)>0: - if not d.isspace(): - if not d[0]=='#': - out.append(d.strip()) - return out +# def parseChannelListFile(fina): +# out = [] +# with open(fina,'r') as f: +# done = False +# while not done: +# d = f.readline() +# if not d: +# done=True +# if len(d)>0: +# if not d.isspace(): +# if not d[0]=='#': +# out.append(d.strip()) +# return out +# class DIAClient: +# def __init__(self, Id, api_address = "http://sf-daq-2:10000"): +# self._api_address = api_address +# self.client = DetectorIntegrationClient(api_address) +# print("\nDetector Integration API on %s" % api_address) +# # No pgroup by default +# self.pgroup = 0 +# self.n_frames = 100 +# self.jf_name = "JF 4.5M" +# self.pede_file = "" +# self.gain_file = "" +# self.update_config() +# +# def update_config(self, ): +# self.writer_config = { +# "output_file": "/sf/alvra/data/raw/p%d/test_data.h5" % self.pgroup, +# "user_id": self.pgroup, +# "n_frames": self.n_frames, +# "general/user": str(self.pgroup), +# "general/process": __name__, +# "general/created": str(datetime.now()), +# "general/instrument": self.jf_name, +# # "general/correction": "test" +# } +# +# self.backend_config = { +# "n_frames": self.n_frames, +# "bit_depth": 16, +# "gain_corrections_filename": self.gain_file, # "/sf/alvra/config/jungfrau/jungfrau_4p5_gaincorrections_v0.h5", +# #"gain_corrections_dataset": "gains", +# #"pede_corrections_filename": "/sf/alvra/data/res/p%d/pedestal_20171210_1628_res.h5" % self.pgroup, +# #"pede_corrections_dataset": "gains", +# #"pede_mask_dataset": "pixel_mask", +# #"activate_corrections_preview": True, +# "is_HG0": True +# } +# +# if self.pede_file != "": +# self.backend_config["gain_corrections_filename"] = self.gain_file # "/sf/alvra/config/jungfrau/jungfrau_4p5_gaincorrections_v0.h5", +# self.backend_config["gain_corrections_dataset"] = "gains" +# self.backend_config["pede_corrections_filename"] = self.pede_file # "/sf/alvra/data/res/p%d/pedestal_20171210_1628_res.h5" % self.pgroup, +# self.backend_config["pede_corrections_dataset"] = "gains" +# self.backend_config["pede_mask_dataset"] = "pixel_mask" +# self.backend_config["activate_corrections_preview"] = True +# +# self.detector_config = { +# "timing": "trigger", +# "exptime": 0.000005, +# "cycles": self.n_frames, +# #"delay" : 0.001992, +# "frames" : 1, +# "dr": 16, +# } +# +# # Not needed anymore? +# #default_channels_list = parseChannelListFile( +# # '/sf/alvra/config/com/channel_lists/default_channel_list') +# +# self.bsread_config = { +# 'output_file': '/sf/alvra/data/raw/p%d/test_bsread.h5' % self.pgroup, +# 'user_id': self.pgroup, +# "general/user": str(self.pgroup), +# "general/process": __name__, +# "general/created": str(datetime.now()), +# "general/instrument": self.jf_name, +# #'Npulses':100, +# #'channels': default_channels_list +# } +# # self.default_channels_list = jungfrau_utils.load_default_channel_list() +# +# def reset(self): +# self.client.reset() +# #pass +# +# def get_status(self): +# return self.client.get_status() +# +# def get_config(self): +# config = self.client.get_config() +# return config +# +# def set_pgroup(self, pgroup): +# self.pgroup = pgroup +# self.update_config() +# +# def set_bs_channels(self, ): +# print("Please update /sf/alvra/config/com/channel_lists/default_channel_list and restart all services on the DAQ server") +# +# def set_config(self): +# self.reset() +# self.client.set_config({"writer": self.writer_config, "backend": self.backend_config, "detector": self.detector_config, "bsread": self.bsread_config}) +# +# def check_still_running(self, time_interval=.5): +# cfg = self.get_config() +# running = True +# while running: +# if not self.get_status()['status'][-7:] == 'RUNNING': +# running = False +# break +# # elif not self.get_status()['status'][-20:]=='BSREAD_STILL_RUNNING': +# # running = False +# # break +# else: +# sleep(time_interval) +# +# def take_pedestal(self, n_frames, analyze=True, n_bad_modules=0, update_config=True): +# import jungfrau_utils as ju +# directory = '/sf/alvra/data/raw/p%d/' % self.pgroup +# filename = "pedestal_%s.h5" % datetime.now().strftime("%Y%m%d_%H%M") +# ju.jungfrau_run_pedestals.run(self._api_address, filename, directory, self.pgroup, 0.1, self.detector_config["exptime"], +# n_frames, 1, analyze, n_bad_modules) +# +# if update_config: +# self.pede_file = filename.replace("raw/", "res/").replace(".h5", "_res.h5") +# print("Pedestal file updated to %s" % self.pede_file) +# return self.pede_file +# +# def start(self): +# self.client.start() +# print("start acquisition") +# pass +# +# def stop(self): +# self.client.stop() +# print("stop acquisition") +# pass +# +# def config_and_start_test(self): +# self.reset() +# self.set_config() +# self.start() +# pass +# +# def wait_for_status(self,*args,**kwargs): +# return self.client.wait_for_status(*args,**kwargs) +# +# def acquire(self, file_name=None, Npulses=100, JF_factor=1, bsread_padding=0): +# """ +# JF_factor? +# bsread_padding? +# """ +# file_rootdir = '/sf/alvra/data/raw/p%d/' % self.pgroup +# +# if file_name is None: +# print("Not saving any data, as file_name is not set") +# file_name_JF = "/dev/null" +# file_name_bsread = "/dev/null" +# else: +# file_name_JF = file_rootdir +file_name + '_JF4p5M.h5' +# file_name_bsread = file_rootdir + file_name + '.h5' +# +# if self.pgroup == 0: +# raise ValueError("Please use set_pgroup() to set a pgroup value.") +# +# def acquire(): +# self.n_frames = Npulses * JF_factor +# self.update_config() +# #self.detector_config.update({ +# # 'cycles': n_frames}) +# self.writer_config.update({ +# 'output_file': file_name_JF, +# # 'n_messages': n_frames +# }) +# #self.backend_config.update({ +# # 'n_frames': n_frames}) +# self.bsread_config.update({ +# 'output_file':file_name_bsread, +# # 'Npulses': Npulses + bsread_padding +# }) +# +# self.reset() +# self.set_config() +# print(self.get_config()) +# self.client.start() +# done = False +# +# while not done: +# stat = self.get_status() +# if stat['status'] =='IntegrationStatus.FINISHED': +# done = True +# if stat['status'] == 'IntegrationStatus.BSREAD_STILL_RUNNING': +# done = True +# if stat['status'] == 'IntegrationStatus.INITIALIZED': +# done = True +# if stat['status'] == 'IntegrationStatus.DETECTOR_STOPPED': +# done = True +# sleep(.1) +# +# return Acquisition(acquire=acquire, acquisition_kwargs={'file_names': [file_name_bsread, file_name_JF], 'Npulses': Npulses},hold=False) +# +# def wait_done(self): +# self.check_running() +# self.check_still_running() + class DIAClient: - def __init__(self, Id, api_address = "http://sf-daq-2:10000"): + def __init__(self, Id, instrument=None, api_address = "http://sf-daq-2:10000", jf_name="JF_4.5M"): + self.Id = Id self._api_address = api_address self.client = DetectorIntegrationClient(api_address) print("\nDetector Integration API on %s" % api_address) # No pgroup by default self.pgroup = 0 self.n_frames = 100 - self.jf_name = "JF 4.5M" + self.jf_name = jf_name self.pede_file = "" self.gain_file = "" + self.instrument = instrument + if instrument is None: + print("ERROR: please configure the instrument parameter in DIAClient") self.update_config() + def update_config(self, ): self.writer_config = { - "output_file": "/sf/alvra/data/raw/p%d/test_data.h5" % self.pgroup, + "output_file": "/sf/%s/data/p%d/raw/test_data.h5" % (self.instrument, self.pgroup), "user_id": self.pgroup, "n_frames": self.n_frames, "general/user": str(self.pgroup), "general/process": __name__, "general/created": str(datetime.now()), - "general/instrument": self.jf_name, + "general/instrument": self.instrument, # "general/correction": "test" } @@ -303,7 +498,8 @@ class DIAClient: #"pede_corrections_dataset": "gains", #"pede_mask_dataset": "pixel_mask", #"activate_corrections_preview": True, - "is_HG0": True + # FIXME: HARDCODED!!! + "is_HG0": False } if self.pede_file != "": @@ -313,10 +509,18 @@ class DIAClient: self.backend_config["pede_corrections_dataset"] = "gains" self.backend_config["pede_mask_dataset"] = "pixel_mask" self.backend_config["activate_corrections_preview"] = True + else: + self.backend_config["pede_corrections_dataset"] = "gains" + self.backend_config["pede_mask_dataset"] = "pixel_mask" + self.backend_config["gain_corrections_filename"] = "" + self.backend_config["pede_corrections_filename"] = "" + self.backend_config["activate_corrections_preview"] = False self.detector_config = { - "timing": "trigger", - "exptime": 0.000005, + "timing": "trigger", + + # FIXME: HARDCODED + "exptime": 0.000010, "cycles": self.n_frames, #"delay" : 0.001992, "frames" : 1, @@ -328,12 +532,12 @@ class DIAClient: # '/sf/alvra/config/com/channel_lists/default_channel_list') self.bsread_config = { - 'output_file': '/sf/alvra/data/raw/p%d/test_bsread.h5' % self.pgroup, + 'output_file': '/sf/%s/data/p%d/raw/test_bsread.h5' % (self.instrument, self.pgroup), 'user_id': self.pgroup, "general/user": str(self.pgroup), "general/process": __name__, "general/created": str(datetime.now()), - "general/instrument": self.jf_name, + "general/instrument": self.instrument, #'Npulses':100, #'channels': default_channels_list } @@ -355,7 +559,7 @@ class DIAClient: self.update_config() def set_bs_channels(self, ): - print("Please update /sf/alvra/config/com/channel_lists/default_channel_list and restart all services on the DAQ server") + print("Please update /sf/%s/config/com/channel_lists/default_channel_list and restart all services on the DAQ server" % self.instrument) def set_config(self): self.reset() @@ -375,14 +579,23 @@ class DIAClient: sleep(time_interval) def take_pedestal(self, n_frames, analyze=True, n_bad_modules=0, update_config=True): - import jungfrau_utils as ju - directory = '/sf/alvra/data/raw/p%d/' % self.pgroup + from jungfrau_utils.scripts.jungfrau_run_pedestals import run as jungfrau_utils_run + directory = '/sf/%s/data/p%d/raw/JF_pedestal/' % (self.instrument, self.pgroup) + if not os.path.exists(directory): + print("Directory %s not existing, creating it" % directory) + os.makedirs(directory) + + res_dir = directory.replace("/raw/", "/res/") + if not os.path.exists(res_dir): + print("Directory %s not existing, creating it" % res_dir) + os.makedirs(res_dir) filename = "pedestal_%s.h5" % datetime.now().strftime("%Y%m%d_%H%M") - ju.jungfrau_run_pedestals.run(self._api_address, filename, directory, self.pgroup, 0.1, self.detector_config["exptime"], - n_frames, 1, analyze, n_bad_modules) + period = 0.04 + jungfrau_utils_run(self._api_address, filename, directory, self.pgroup, period, self.detector_config["exptime"], + n_frames, 1, analyze, n_bad_modules, self.instrument, self.jf_name) if update_config: - self.pede_file = filename.replace("raw/", "res/").replace(".h5", "_res.h5") + self.pede_file = (directory + filename).replace("raw/", "res/").replace(".h5", "_res.h5") print("Pedestal file updated to %s" % self.pede_file) return self.pede_file @@ -407,17 +620,19 @@ class DIAClient: def acquire(self, file_name=None, Npulses=100, JF_factor=1, bsread_padding=0): """ - JF_factor? + JF_factor? bsread_padding? """ - file_rootdir = '/sf/alvra/data/raw/p%d/' % self.pgroup + file_rootdir = '/sf/%s/data/p%d/raw/' % (self.instrument, self.pgroup) if file_name is None: + # FIXME /dev/null crashes the data taking (h5py can't close /dev/null and crashes) print("Not saving any data, as file_name is not set") - file_name_JF = "/dev/null" - file_name_bsread = "/dev/null" + file_name_JF = file_rootdir + "DelMe" + '_JF4p5M.h5' + file_name_bsread = file_rootdir + "DelMe" + '.h5' else: - file_name_JF = file_rootdir +file_name + '_JF4p5M.h5' + # FIXME hardcoded + file_name_JF = file_rootdir + file_name + '_JF4p5M.h5' file_name_bsread = file_rootdir + file_name + '.h5' if self.pgroup == 0: @@ -441,7 +656,7 @@ class DIAClient: self.reset() self.set_config() - print(self.get_config()) + #print(self.get_config()) self.client.start() done = False