Read and Write Matrices using Kontrol’s EZCA Wrapper

We often want to get a matrix from the EPICS record, manipulate it, and then write it back to the EPICS record. But, this can be difficult since the Easy Channel Access (EZCA) only have functions to read and write one single element at a time.

Here, Kontrol provides a EZCA wrapper to allow reading and writing matrices more easily. We will demonstrate using a trivial matrix "K1:VIS-SRM_BF_SEISALIGN_{1,2,3}_{1,2,3}".

Here’s the original matrix and let’s access it.

title

[1]:
import kontrol
import numpy as np

# Define an Ezca instance
srm = kontrol.Ezca("VIS-SRM")  # Argument is the prefix after "K1:".

# Get matrix. Make sure you're doing this at the k1ctr workstations, or else you will get a Null array.
bf_seisalign = srm.get_matrix("BF_SEISALIGN")
bf_seisalign
[1]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

Now, let’s manipulate it and put it back

[2]:
decouple_matrix = np.array([[1, 0.1, 0.01], [-0.01, 1, 0.1], [0.01, -0.1, 1]])
new_bf_seisalign = bf_seisalign @ decouple_matrix  # here, @ is the matrix multiplication operator, just in case you don't know.
new_bf_seisalign
[2]:
array([[ 1.  ,  0.1 ,  0.01],
       [-0.01,  1.  ,  0.1 ],
       [ 0.01, -0.1 ,  1.  ]])

Now put it back

[3]:
srm.put_matrix(new_bf_seisalign, "BF_SEISALIGN")
K1:VIS-SRM_BF_SEISALIGN_1_2 => 0.1
K1:VIS-SRM_BF_SEISALIGN_1_3 => 0.01
K1:VIS-SRM_BF_SEISALIGN_2_1 => -0.01
K1:VIS-SRM_BF_SEISALIGN_2_3 => 0.1
K1:VIS-SRM_BF_SEISALIGN_3_1 => 0.01
K1:VIS-SRM_BF_SEISALIGN_3_2 => -0.1

TA-DA!

title2