Exemple #1
0
def test_py2xtc(tmp_path):

    config = {}
    detname = 'spi_cspad'
    dettype = 'cspad'
    serial_number = '1234'
    namesid = 0

    nameinfo = dc.nameinfo(detname, dettype, serial_number, namesid)
    alg = dc.alg('raw', [1, 2, 3])

    cydgram = dc.CyDgram()

    image_array = np.array([[1, 2, 3, 4], [9, 8, 7, 6]])
    orientations_array = np.array([4, 3, 2, 1])

    runinfo_detname = 'runinfo'
    runinfo_dettype = 'runinfo'
    runinfo_detid = ''
    runinfo_namesid = 1
    runinfo_nameinfo = dc.nameinfo(runinfo_detname, runinfo_dettype,
                                   runinfo_detid, runinfo_namesid)
    runinfo_alg = dc.alg('runinfo', [0, 0, 1])
    runinfo_data = {'expt': 'xpptut15', 'runnum': 14}

    fname = os.path.join(tmp_path, 'junk.xtc2')

    f = open(fname, 'wb')
    for i in range(4):
        my_data = {
            'image': image_array + i,
            'orientations': orientations_array + i
        }

        cydgram.addDet(nameinfo, alg, my_data)
        # only do this for the first two dgrams: name info for config, and
        # the runinfo data for beginrun
        if i < 2: cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)
        timestamp = i
        if (i == 0):
            transitionid = 2  # Configure
        elif (i == 1):
            transitionid = 4  # BeginRun
        else:
            transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        f.write(xtc_bytes)
    f.close()

    from psana import DataSource
    ds = DataSource(files=fname)
    myrun = next(ds.runs())
    assert myrun.expt == runinfo_data['expt']
    assert myrun.runnum == runinfo_data['runnum']
    for nevt, evt in enumerate(myrun.events()):
        assert np.array_equal(evt._dgrams[0].spi_cspad[0].raw.image,
                              image_array + nevt + 2)
        assert np.array_equal(evt._dgrams[0].spi_cspad[0].raw.orientations,
                              orientations_array + nevt + 2)
    assert nevt > 0  #make sure we get events
def convert_hdf5_to_xtc2_with_runinfo():

    import dgramCreate as dc
    import numpy as np
    import os
    import h5py

    nameinfo = dc.nameinfo(DETNAME, DETTYPE, SERNUM, NAMESID)
    alg = dc.alg('raw', [0, 0, 1])

    cydgram = dc.CyDgram()

    #---------- for runinfo

    runinfo_detname = 'runinfo'
    runinfo_dettype = 'runinfo'
    runinfo_detid = ''
    runinfo_namesid = 1
    runinfo_nameinfo = dc.nameinfo(runinfo_detname, runinfo_dettype,
                                   runinfo_detid, runinfo_namesid)
    runinfo_alg = dc.alg('runinfo', [0, 0, 1])
    runinfo_data = {'expt': EXPNAME, 'runnum': RUNNUM}

    #----------

    ifname = FNAME_HDF5
    ofname = FNAME_XTC2
    print('Input file: %s\nOutput file: %s' % (ifname, ofname))

    f = open(ofname, 'wb')
    h5f = h5py.File(ifname)
    waveforms = h5f['waveforms']
    times = h5f['times']
    for nevt, (wfs, times) in enumerate(zip(waveforms, times)):
        my_data = {'waveforms': wfs, 'times': times}

        if nevt<10\
        or nevt<50 and not nevt%10\
        or not nevt%100 :
            print('Event %3d' % nevt)

        #---------- for runinfo
        if nevt < 2:
            cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)

        cydgram.addDet(nameinfo, alg, my_data)
        timestamp = nevt
        pulseid = nevt
        if (nevt == 0): transitionid = 2  # Configure
        elif (nevt == 1): transitionid = 4  # BeginRun
        else: transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        #xtc_bytes = cydgram.get(timestamp, pulseid, transitionid)
        f.write(xtc_bytes)
    f.close()

    print('DO NOT FORGET TO MOVE FILE TO /reg/g/psdm/detector/data2_test/xtc/')
def convert_hdf5_to_xtc2_with_runinfo():

    import dgramCreate as dc
    import numpy as np
    import os
    import h5py

    nameinfo = dc.nameinfo(DETNAME, DETTYPE, SERNUM, NAMESID)
    alg = dc.alg('raw', [0, 0, 1])

    cydgram = dc.CyDgram()

    #---------- for runinfo

    runinfo_detname = 'runinfo'
    runinfo_dettype = 'runinfo'
    runinfo_detid = ''
    runinfo_namesid = 1
    runinfo_nameinfo = dc.nameinfo(runinfo_detname, runinfo_dettype,
                                   runinfo_detid, runinfo_namesid)
    runinfo_alg = dc.alg('runinfo', [0, 0, 1])
    runinfo_data = {'expt': EXPNAME, 'runnum': RUNNUM}

    #----------

    ifname = FNAME_HDF5
    ofname = FNAME_XTC2
    print('Input file: %s\nOutput file: %s' % (ifname, ofname))

    f = open(ofname, 'wb')
    h5f = h5py.File(ifname)
    #waveforms = h5f['waveforms']
    #times = h5f['times']
    raw = h5f['raw']
    #for nevt,(wfs,times) in enumerate(zip(waveforms,times)):
    for nevt, nda in enumerate(raw):
        my_data = {'array': nda}
        #'times': times

        if do_print(nevt): print('Event %3d' % nevt, ' nda.shape:', nda.shape)

        #---------- for runinfo
        if nevt < 2:
            cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)

        cydgram.addDet(nameinfo, alg, my_data)
        timestamp = nevt
        pulseid = nevt
        if (nevt == 0): transitionid = 2  # Configure
        elif (nevt == 1): transitionid = 4  # BeginRun
        else: transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        #xtc_bytes = cydgram.get(timestamp, pulseid, transitionid)
        f.write(xtc_bytes)
    f.close()
Exemple #4
0
    def getBlock(self, *, transitionid, add_names, add_shapes_data):
        my_data = {}
        for motor in self.motors:
            my_data.update({motor.name: motor.position})
            # derive step_docstring from step_value
            if motor.name == 'step_value':
                docstring = f'{{"detname": "{self.detname}", "scantype": "{self.scantype}", "step": {motor.position}}}'
                my_data.update({'step_docstring': docstring})

        detname       = 'scan'
        dettype       = 'scan'
        serial_number = '1234'
        namesid       = 253     # STEPINFO = 253 (psdaq/drp/drp.hh)
        nameinfo      = dc.nameinfo(detname,dettype,serial_number,namesid)

        alg           = dc.alg('raw',[2,0,0])

        self.cydgram.addDet(nameinfo, alg, my_data)

        # create dgram
        timestamp    = 0
        xtc_bytes    = self.cydgram.getSelect(timestamp, transitionid, add_names=add_names, add_shapes_data=add_shapes_data)
        logging.debug('transitionid %d dgram is %d bytes (with header)' % (transitionid, len(xtc_bytes)))

        # remove first 12 bytes (dgram header), and keep next 12 bytes (xtc header)
        return xtc_bytes[12:]
Exemple #5
0
def translate_xtc_demo(ifname, det_type='cspad', offset=1):
    xtcfile = '%s.xtc2' % ifname.rstrip('.json')
    print('Get data from file: %s' % ifname)
    print('Save data  in file: %s' % xtcfile)

    try:
        os.remove(xtcfile)
    except:
        pass

    lcls1_xtc = load_json(ifname)

    cfg_namesId = 0
    cfg_alg = dc.alg('cfg', [1, 2, 3])
    cfg_ninfo = dc.nameinfo('my' + det_type, det_type, 'serialnum1234',
                            cfg_namesId)

    evt_namesId = 1
    evt_alg = dc.alg('raw', [4, 5, 6])
    evt_ninfo = dc.nameinfo('my' + det_type, det_type, 'serialnum1234',
                            evt_namesId)

    # This is needed for some reason.
    # Perhaps a collision of lcls1 xtc "version" with lcls2 counterpart
    try:
        lcls1_xtc[0]['version.'] = lcls1_xtc[0]['version']
        del lcls1_xtc[0]['version']
    except KeyError:
        pass

    cydgram = dc.CyDgram()

    with open(xtcfile, 'wb') as f:
        # the order of these two lines must match the of the namesId's above
        cydgram.addDet(cfg_ninfo, cfg_alg, lcls1_xtc[0])
        cydgram.addDet(evt_ninfo, evt_alg, lcls1_xtc[offset])
        df = cydgram.get(0, 0)
        f.write(df)

        # this currently duplicates the first dgram
        for event_dgram in lcls1_xtc[offset:]:
            cydgram.addDet(evt_ninfo, evt_alg, event_dgram)
            df = cydgram.get(0, 0)
            f.write(df)
Exemple #6
0
def setupXtc(step=None):
    d = {'step_value': 0., 'step_docstring': ''}
    if step and len(step) == 3:
        d['step_value'] = step[1]
        d['step_docstring'] = step[2]
    print(f'setupXtc {d}')

    nameinfo = dc.nameinfo('scan', 'scan', '1234', 253)
    alg = dc.alg('raw', [2, 0, 0])
    cydgram = dc.CyDgram()
    cydgram.addDet(nameinfo, alg, d)
    return cydgram
def translate_xtc_demo(job_type):
    event_file = '%s_evts.xtc' % job_type

    ninfo = dc.nameinfo('DsdCsPad', 'cspad', 'detnum1234', 0)
    alg = dc.alg('raw', [1, 2, 3])

    cydgram = dc.CyDgram()

    with open(event_file, 'wb') as f:
        while True:
            work = consumer_receiver.recv_json()
            event_dict, timestamp = munge_json(work)
            if event_dict is None: break
            cydgram.addDet(ninfo, alg, event_dict)
            df = cydgram.get(timestamp, 0, 0)
            f.write(df)

    print("Output: ", event_file)
Exemple #8
0
def test_py2xtc(tmp_path):

    config = {}
    detname = 'spi_cspad'
    dettype = 'cspad'
    serial_number = '1234'
    namesid = 0

    nameinfo = dc.nameinfo(detname, dettype, serial_number, namesid)
    alg = dc.alg('raw', [1, 2, 3])

    cydgram = dc.CyDgram()

    image_array = np.array([[1, 2, 3, 4], [9, 8, 7, 6]])
    orientations_array = np.array([4, 3, 2, 1])

    fname = os.path.join(tmp_path, 'junk.xtc2')

    f = open(fname, 'wb')
    for i in range(3):
        my_data = {
            'image': image_array + i,
            'orientations': orientations_array + i
        }

        cydgram.addDet(nameinfo, alg, my_data)
        timestamp = i
        if (i == 0):
            transitionid = 2  # Configure
        else:
            transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        f.write(xtc_bytes)
    f.close()

    from psana import DataSource
    ds = DataSource(files=fname)
    myrun = next(ds.runs())
    for nevt, evt in enumerate(myrun.events()):
        assert np.array_equal(evt._dgrams[0].spi_cspad[0].raw.image,
                              image_array + nevt + 1)
        assert np.array_equal(evt._dgrams[0].spi_cspad[0].raw.orientations,
                              orientations_array + nevt + 1)
    assert nevt > 0  #make sure we get events
Exemple #9
0
def translate_xtc_demo(job_type, offset=1):
    configure_file = '%s_configure.xtc' % job_type
    event_file = '%s_evts.xtc' % job_type

    try:
        os.remove(FILE_NAME)
    except:
        pass

    lcls1_xtc = load_json("%s.json" % job_type)

    alg = dc.alg('raw', [1, 2, 3])

    ninfo = dc.nameinfo('DscCsPad', 'cspad', 'detnum1234', 0)

    # This is needed for some reason.
    # Perhaps a collision of lcls1 xtc "version" with lcls2 counterpart
    try:
        lcls1_xtc[0]['version.'] = lcls1_xtc[0]['version']
        del lcls1_xtc[0]['version']
    except KeyError:
        pass

    cydgram = dc.CyDgram()

    with open(configure_file, 'wb') as f:
        cydgram.addDet(ninfo, alg, lcls1_xtc[0])
        df = cydgram.get()
        f.write(df)

    del cydgram
    cydgram = dc.CyDgram()

    with open(event_file, 'wb') as f:
        for event_dgram in lcls1_xtc[offset:]:
            cydgram.addDet(ninfo, alg, event_dgram)
            df = cydgram.get()
            f.write(df)

    ds_cfg = DgramManager(configure_file)
    ds_evt = DgramManager(event_file)
Exemple #10
0
def test_py2xtc(tmp_path):

    config = {}
    detname = 'cspad'
    dettype = 'cspad'
    serial_number = '1234'
    namesid = 0

    nameinfo = dc.nameinfo(detname, dettype, serial_number, namesid)
    alg = dc.alg('raw', [1, 2, 3])

    cydgram = dc.CyDgram()

    image_array = np.array([[1, 2, 3, 4], [9, 8, 7, 6]])
    orientations_array = np.array([4, 3, 2, 1])

    runinfo_detname = 'runinfo'
    runinfo_dettype = 'runinfo'
    runinfo_detid = ''
    runinfo_namesid = 1
    runinfo_nameinfo = dc.nameinfo(runinfo_detname, runinfo_dettype,
                                   runinfo_detid, runinfo_namesid)
    runinfo_alg = dc.alg('runinfo', [0, 0, 1])
    runinfo_data = {'expt': 'cxid9114', 'runnum': 95}

    fname = os.path.join(tmp_path, 'junk.xtc2')

    f = open(fname, 'wb')

    xtc_file = "/gpfs/alpine/proj-shared/chm137/data/LD91/old_data.xtc2"
    ds = DataSource(files=xtc_file)
    run = next(ds.runs())
    det = run.Detector('cspad')
    for i, evt in enumerate(run.events()):
        my_data = {
            'raw': det.raw.raw(evt),
            'photonEnergy': det.raw.photonEnergy(evt)
        }

        cydgram.addDet(nameinfo, alg, my_data)
        # only do this for the first two dgrams: name info for config, and
        # the runinfo data for beginrun
        if i < 2: cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)
        timestamp = i
        if (i == 0):
            transitionid = 2  # Configure
        elif (i == 1):
            transitionid = 4  # BeginRun
        else:
            transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        f.write(xtc_bytes)

        if i == 10: break

    f.close()

    ds = DataSource(files=fname)
    myrun = next(ds.runs())
    assert myrun.expt == runinfo_data['expt']
    assert myrun.runnum == runinfo_data['runnum']
    for nevt, evt in enumerate(myrun.events()):
        assert evt._dgrams[0].cspad[0].raw.raw.shape == (32, 185, 388)

    assert nevt > 0  #make sure we get events
Exemple #11
0
FILE_NAME = 'data.xtc'
NUM_ELEM = 1


def generate_event(num_elem, zero):
    # data = np.array([0])*num_elem
    data = [np.full((1, 1), zero)] * num_elem
    # data = [np.random.random((2,3,3)) for x in range(num_elem)]
    names = ['det%i' % zero for i in range(num_elem)]
    algs = [alg] * num_elem

    event_dict = dict(zip(names, zip(data, algs)))
    return event_dict


alg = dc.alg('alg', [0, 0, 0])
alg2 = dc.alg('alg2', [0, 0, 0])

ninfo = dc.nameinfo('xpphsd', 'cspad', 'detnum1234', 0)
ninfo2 = dc.nameinfo('xpphsd', 'cspad', 'detnum1234', 1)

cydgram = dc.CyDgram()

with open(FILE_NAME, 'wb') as f:
    for _ in range(10):
        cydgram.addDet(ninfo, alg, generate_event(NUM_ELEM, 0))
        cydgram.addDet(ninfo2, alg2, generate_event(NUM_ELEM, 0))
        # pydgram.writeToFile()
        xtc_byte = cydgram.get()
        f.write(xtc_byte)
Exemple #12
0
def extend_xtc(tmp_path, seed_dgrams, n_total_dgrams):
    print(
        f"Writing out {n_total_dgrams} from {len(seed_dgrams)} seeded dgrams")
    config = {}
    detname = 'cspad'
    dettype = 'cspad'
    serial_number = '1234'
    namesid = 0

    nameinfo = dc.nameinfo(detname, dettype, serial_number, namesid)
    alg = dc.alg('raw', [1, 2, 3])

    cydgram = dc.CyDgram()

    image_array = np.array([[1, 2, 3, 4], [9, 8, 7, 6]])
    orientations_array = np.array([4, 3, 2, 1])

    runinfo_detname = 'runinfo'
    runinfo_dettype = 'runinfo'
    runinfo_detid = ''
    runinfo_namesid = 1
    runinfo_nameinfo = dc.nameinfo(runinfo_detname, runinfo_dettype,
                                   runinfo_detid, runinfo_namesid)
    runinfo_alg = dc.alg('runinfo', [0, 0, 1])
    runinfo_data = {'expt': 'cxid9114', 'runnum': 95}

    fname = os.path.join(tmp_path, 'junk.xtc2')

    f = open(fname, 'wb')

    i = 0
    cn_seed_dgrams = 0
    while i < n_total_dgrams:
        if cn_seed_dgrams == len(seed_dgrams):
            cn_seed_dgrams = 0

        my_data = seed_dgrams[cn_seed_dgrams]
        cydgram.addDet(nameinfo, alg, my_data)

        # only do this for the first two dgrams: name info for config, and
        # the runinfo data for beginrun
        if i < 2: cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)
        timestamp = i
        if (i == 0):
            transitionid = 2  # Configure
        elif (i == 1):
            transitionid = 4  # BeginRun
        else:
            transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        f.write(xtc_bytes)

        cn_seed_dgrams += 1
        i += 1

    f.close()

    ds = DataSource(files=fname)
    myrun = next(ds.runs())
    assert myrun.expt == runinfo_data['expt']
    assert myrun.runnum == runinfo_data['runnum']
    for nevt, evt in enumerate(myrun.events()):
        assert evt._dgrams[0].cspad[0].raw.raw.shape == (32, 185, 388)

    assert nevt > 0  #make sure we get events
Exemple #13
0
import dgramCreate as dc
import numpy as np
import os
import h5py

config = {}
detname = 'xpp_ipm4'
dettype = 'ipm'
serial_number = '1234'
namesid = 0

nameinfo = dc.nameinfo(detname,dettype,serial_number,namesid)
alg = dc.alg('raw',[0,0,1])

cydgram = dc.CyDgram()

fname = 'xcsami.xtc2'

f = open(fname,'wb')
h5f = h5py.File('/reg/d/psdm/xcs/xcslq2515/scratch/snelson/xcslq2515_Run061.h5', 'r')
ipm4 = h5f['ipm4']['sum']
for nevt,ipmval in enumerate(ipm4):
    my_data = {
        'sum': ipmval,
    }

    cydgram.addDet(nameinfo, alg, my_data)
    timestamp = nevt
    pulseid = nevt
    if (nevt==0):
        transitionid = 2  # Configure
Exemple #14
0
SLOWUPDATE_FREQ = 100  # every 100 L1 , insert one slowupdate (with 0 array for fake bg)

file_in = "/reg/neh/home/monarin/lcls2/psana/psana/tests/test_timetool.xtc2"
fd = os.open(file_in, os.O_RDONLY)
file_in_size = os.path.getsize(file_in)
ds = DataSource(files=file_in)
run = next(ds.runs())

f = open("junk.xtc2", "wb")

# tmodet
detname = 'tmott'
dettype = 'ttdet'
serial_number = 'tt_detector_identification_placeholder'
namesid = 0
nameinfo = dc.nameinfo(detname, dettype, serial_number, namesid)
alg = dc.alg('ttalg', [0, 0, 1])
cydgram = dc.CyDgram()
data = {'data': np.zeros((4272, ), dtype=np.uint8)}
cydgram.addDet(nameinfo, alg, data)

# runinfo detector
runinfo_detname = 'runinfo'
runinfo_dettype = 'runinfo'
runinfo_detid = ''
runinfo_namesid = 1
runinfo_nameinfo = dc.nameinfo(runinfo_detname, runinfo_dettype, runinfo_detid,
                               runinfo_namesid)
runinfo_alg = dc.alg('runinfo', [0, 0, 1])
runinfo_data = {'expt': 'tmo', 'runnum': 1}
cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)
Exemple #15
0
def convert_hdf5_to_xtc2_with_runinfo():

    import dgramCreate as dc  # .../psana/peakFinder/dgramCreate.pyx (Lord, why it is here?)
    import numpy as np
    import os
    import h5py

    if RAW in LIST_SAVE:
        #xxxxxx_nameinfo = dc.nameinfo(DETNAME, DETTYPE, SERNUM, NAMESID)
        #raw_nameinfo = dc.nameinfo(DETNAME, DETTYPE, SERNUM, NAMESID)
        raw_nameinfo = dc.nameinfo('xtcav', 'camera', '1234', RAW)
        raw_alg = dc.alg('raw', [0, 0, 1])

    if RUNINFO in LIST_SAVE:
        runinfo_nameinfo = dc.nameinfo('runinfo', 'runinfo', '', RUNINFO)
        runinfo_alg = dc.alg('runinfo', [0, 0, 1])

    if EBEAM in LIST_SAVE:
        ebeam_nameinfo = dc.nameinfo('ebeam', 'ebeam', '', EBEAM)
        ebeam_alg = dc.alg('valsebm', [0, 0, 1])

    if EVENTID in LIST_SAVE:
        eventid_nameinfo = dc.nameinfo('eventid', 'eventid', '', EVENTID)
        eventid_alg = dc.alg('valseid', [0, 0, 1])

    if GASDET in LIST_SAVE:
        gasdet_nameinfo = dc.nameinfo('gasdetector', 'gasdetector', '', GASDET)
        gasdet_alg = dc.alg('valsgd', [0, 0, 1])

    if XTCAVPARS in LIST_SAVE:
        xtcav_nameinfo = dc.nameinfo('xtcavpars', 'xtcavpars', '', XTCAVPARS)
        xtcav_alg = dc.alg('valsxtp', [0, 0, 1])

    #----------

    cydgram = dc.CyDgram()

    ifname = FNAME_HDF5
    ofname = FNAME_XTC2
    print('Input file: %s\nOutput file: %s' % (ifname, ofname))

    f = open(ofname, 'wb')
    h5f = h5py.File(ifname, 'r')
    eventid = h5f['EventId']
    ebeam = h5f['EBeam']
    gasdet = h5f['GasDetector']
    xtcav = h5f['XTCAV']
    raw = h5f['raw']

    for nevt, nda in enumerate(raw):

        if nevt > EVENTS:
            print('Reached maximum number of events %s' % EVENTS)
            break

        #if nevt < EVSKIP :
        #   print('  - skip event')
        #   continue

        #print('Event %3d'%nevt, ' nda.shape:', nda.shape)

        if do_print(nevt):
            print('Event %3d' % nevt, ' nda.shape:', nda.shape)

            print('       -> RUNINFO    EXPNAME    :', EXPNAME)
            print('       -> RUNINFO    RUNNUM     :', RUNNUM)
            print('  HDF5 -> EVENTID    exp        :',
                  eventid['experiment'][nevt].decode())
            print('  HDF5 -> EVENTID    run        :', eventid['run'][nevt])
            print('  HDF5 -> EVENTID    time       :', eventid['time'][nevt])
            print('  HDF5 -> EBEAM      charge     :', ebeam['Charge'][nevt])
            print('  HDF5 -> EBEAM      dumpcharge :',
                  ebeam['DumpCharge'][nevt])
            print('  HDF5 -> GASDET     f_11_ENRC  :',
                  gasdet['f_11_ENRC'][nevt])
            print('  HDF5 -> XTCAVPARS  Beam_energy:',
                  xtcav['XTCAV_Beam_energy_dump_GeV'][nevt])
            print_ndarr(nda, '  HDF5 RAW raw:')

        if RUNINFO in LIST_SAVE:
            if nevt < 2:
                runinfo_data = {'expt': EXPNAME, 'runnum': RUNNUM}
                cydgram.addDet(runinfo_nameinfo, runinfo_alg, runinfo_data)

        if EBEAM in LIST_SAVE:
            cydgram.addDet(ebeam_nameinfo, ebeam_alg, {\
              'Charge'    : ebeam['Charge'    ][nevt],\
              'DumpCharge': ebeam['DumpCharge'][nevt],\
              'XTCAVAmpl' : ebeam['XTCAVAmpl' ][nevt],\
              'XTCAVPhase': ebeam['XTCAVPhase'][nevt],\
              'PkCurrBC2' : ebeam['PkCurrBC2' ][nevt],\
              'L3Energy'  : ebeam['L3Energy'  ][nevt],\
              #'cpohack'   : np.array([3]),\
            })

        if EVENTID in LIST_SAVE:
            cydgram.addDet(eventid_nameinfo, eventid_alg, {\
              'experiment': eventid['experiment'][nevt].decode(),\
              'run'       : eventid['run'       ][nevt],\
              'fiducials' : eventid['fiducials' ][nevt],\
              'time'      : eventid['time'      ][nevt],\
            })

        if GASDET in LIST_SAVE:
            cydgram.addDet(gasdet_nameinfo, gasdet_alg, {\
              'f_11_ENRC': gasdet['f_11_ENRC'][nevt],\
              'f_12_ENRC': gasdet['f_12_ENRC'][nevt],\
              'f_21_ENRC': gasdet['f_21_ENRC'][nevt],\
              'f_22_ENRC': gasdet['f_22_ENRC'][nevt],\
              'f_63_ENRC': gasdet['f_63_ENRC'][nevt],\
              'f_64_ENRC': gasdet['f_64_ENRC'][nevt],\
            })

        if XTCAVPARS in LIST_SAVE:
            cydgram.addDet(xtcav_nameinfo, xtcav_alg, {\
              'XTCAV_Analysis_Version'      : xtcav['XTCAV_Analysis_Version'      ][nevt],\
              'XTCAV_ROI_sizeX'             : xtcav['XTCAV_ROI_sizeX'             ][nevt],\
              'XTCAV_ROI_sizeY'             : xtcav['XTCAV_ROI_sizeY'             ][nevt],\
              'XTCAV_ROI_startX'            : xtcav['XTCAV_ROI_startX'            ][nevt],\
              'XTCAV_ROI_startY'            : xtcav['XTCAV_ROI_startY'            ][nevt],\
              'XTCAV_calib_umPerPx'         : xtcav['XTCAV_calib_umPerPx'         ][nevt],\
              'OTRS_DMP1_695_RESOLUTION'    : xtcav['OTRS:DMP1:695:RESOLUTION'    ][nevt],\
              'XTCAV_strength_par_S'        : xtcav['XTCAV_strength_par_S'        ][nevt],\
              'OTRS_DMP1_695_TCAL_X'        : xtcav['OTRS:DMP1:695:TCAL_X'        ][nevt],\
              'XTCAV_Amp_Des_calib_MV'      : xtcav['XTCAV_Amp_Des_calib_MV'      ][nevt],\
              'SIOC_SYS0_ML01_AO214'        : xtcav['SIOC:SYS0:ML01:AO214'        ][nevt],\
              'XTCAV_Phas_Des_calib_deg'    : xtcav['XTCAV_Phas_Des_calib_deg'    ][nevt],\
              'SIOC_SYS0_ML01_AO215'        : xtcav['SIOC:SYS0:ML01:AO215'        ][nevt],\
              'XTCAV_Beam_energy_dump_GeV'  : xtcav['XTCAV_Beam_energy_dump_GeV'  ][nevt],\
              'REFS_DMP1_400_EDES'          : xtcav['REFS:DMP1:400:EDES'          ][nevt],\
              'XTCAV_calib_disp_posToEnergy': xtcav['XTCAV_calib_disp_posToEnergy'][nevt],\
              'SIOC_SYS0_ML01_AO216'        : xtcav['SIOC:SYS0:ML01:AO216'        ][nevt],\
            })

        if RAW in LIST_SAVE:
            cydgram.addDet(raw_nameinfo, raw_alg, {'array': nda})

        t_sec_nsec = eventid['time'][nevt]
        timestamp = time_stamp(t_sec_nsec)
        t_sec, t_nsec = t_sec_nsec
        timestamp = timestamp if t_sec else nevt

        transitionid = None
        if (nevt == 0): transitionid = 2  # Configure
        elif (nevt == 1): transitionid = 4  # BeginRun
        else: transitionid = 12  # L1Accept
        xtc_bytes = cydgram.get(timestamp, transitionid)
        f.write(xtc_bytes)
    f.close()
Exemple #16
0
def test_py2xtc_scan(tmp_path):

    config = {}
    detname = 'spi_cspad'
    dettype = 'cspad'
    serial_number = '1234'
    namesid = 0

    nameinfo = dc.nameinfo(detname, dettype, serial_number, namesid)
    alg = dc.alg('raw', [1, 2, 3])

    cydgram = dc.CyDgram()

    motor1_array = np.array([1.0, 2.0, 3.0])
    motor2_array = np.array([4.0, 5.0, 6.0])

    fname = os.path.join(tmp_path, 'junk.xtc2')

    my_data = {'motor1': motor1_array, 'motor2': motor2_array}
    timestamp = 0
    transitionid = _transitionId['Configure']

    f = open(fname, 'wb')

    len_list = []
    for xx in range(4):
        cydgram.addDet(nameinfo, alg, my_data)
        xtc_bytes = cydgram.get(timestamp, transitionid)
        len_list.append(len(xtc_bytes))
        f.write(xtc_bytes)

    for xx in range(2):
        cydgram.addDet(nameinfo, alg, my_data)
        xtc_bytes = cydgram.getSelect(timestamp,
                                      transitionid,
                                      add_names=True,
                                      add_shapes_data=True)
        len_list.append(len(xtc_bytes))
        f.write(xtc_bytes)

    for xx in range(2):
        cydgram.addDet(nameinfo, alg, my_data)
        xtc_bytes = cydgram.getSelect(timestamp,
                                      transitionid,
                                      add_names=False,
                                      add_shapes_data=True)
        len_list.append(len(xtc_bytes))
        f.write(xtc_bytes)

    for xx in range(2):
        cydgram.addDet(nameinfo, alg, my_data)
        xtc_bytes = cydgram.getSelect(timestamp,
                                      transitionid,
                                      add_names=True,
                                      add_shapes_data=False)
        len_list.append(len(xtc_bytes))
        f.write(xtc_bytes)

    cydgram.addDet(nameinfo, alg, my_data)
    xtc_bytes = cydgram.getSelect(timestamp,
                                  transitionid,
                                  add_names=False,
                                  add_shapes_data=False)
    len_list.append(len(xtc_bytes))
    f.write(xtc_bytes)

    print("len_list: %s" % len_list)

    header_size = 24  # 24 = dgram (12) + xtc (12)
    assert len_list[0] > len_list[1]
    assert len_list[1] == len_list[2] == len_list[3]
    assert len_list[0] == len_list[4] == len_list[5]
    assert len_list[3] == len_list[6] == len_list[7]
    assert len_list[8] == len_list[9]
    assert len_list[10] == header_size
    assert len_list[8] + len_list[6] - header_size == len_list[0]

    f.close()