This commit is contained in:
gac-furka
2025-09-04 11:21:05 +02:00
parent 51186aba2b
commit bf1060b656
20 changed files with 662 additions and 35 deletions
+7
View File
@@ -0,0 +1,7 @@
channel_x = Channel('S10BD01-DBPM020:X1')
channel_y = Channel('SINSB01-RSYS:SET-BEAM-PHASE')
av = create_averager(channel_x, 10, 0.1)
#lscan(channel_y, av, 70., 90., 20, latency=0.5)
+61
View File
@@ -0,0 +1,61 @@
N_POSITION_BAND = 10.0
# EInkommentieren fuer Messungen Mit PSCR136
# Auskommentieren fuer Messungen Mit PCEL
#
cam_server.start("SATES21-CAMS-PATT1", True)
cam_server.stream.waitCacheChange(-1)
i= cam_server.stream.getChild("x_fwhm")
i_avg = create_averager(i, 30, interval = -1)
# muss auskommentiert bleiben
#Keithley = Channel("SATOP31-CSSU-PCEL1381:READOUT",'d', monitored ='True')
#_avg = create_averager(Keithley, 5, interval = -1)
def beam_ok():
I0 = caget ("SATFE10-PEPG046:PHOTON-ENERGY-PER-PULSE-AVG")
if I0 > 100:
return "Yes"
else :
return "No"
def before_sampling(rec):
while beam_ok() == "No":
time.sleep(0.1)
print(".")
def after_sampling(rec):
if beam_ok() == "No":
rec.invalidate()
i0 = Channel("SATFE10-PEPG046:PHOTON-ENERGY-PER-PULSE-AVG",'d')
class VLSSGM(RegisterBase):
def doWrite(self, value):
caput ("SATOP11-OSGM087:SetEnergy", value)
time.sleep(0.2)
cawait('SATOP11-OSGM087:MOVING', 1, timeout = 20.0, type = 'i')
def doRead(self):
return caget("SATOP11-OSGM087:photonenergy")
athos = VLSSGM()
athos.initialize()
#lscan( athos, [i0,i_avg], 520.0, 540, 1.0, 0.5, setpoints=True,zigzag=True) # Scans mit PSCR136
#lscan( athos, [i0,K_avg], 833.5, 837, 0.05, 0.5, setpoints=True,zigzag=True) # Scans mit PCEL
#caput ("ATHOS:SetEnergy", 680.0)
tscan( [i_avg], 10, 1 , passes=1)
+5
View File
@@ -0,0 +1,5 @@
add_device(Stream("st", "sf-daqsync-15.psi.ch:9006", SocketType.PULL), True)
st.start()
show_panel(st)
bscan(st, 10, -1, save=False, lazy=True)
+15
View File
@@ -0,0 +1,15 @@
CAMERA = "SATES30-RIXS-CAM01"
CAMERA = "SATES31-CAMS187-RIXS1"
CAMERA = "SATES30-CAMS182-GIGE5"
CAMERA_URL = caget(CAMERA + ":BSREADCONFIG").replace("tcp://daq", "tcp://")
#CAMERA_URL ="tcp://129.1.247.78:8199"
print CAMERA_URL
st1 = Stream("st1", CAMERA_URL, SocketType.PULL)
#st1.debug=True
st1.initialize()
st1.start()
st1.waitCacheChange(1000)
show_panel(st1)
#add_device(st1.getChildren()[0], True)
+30
View File
@@ -0,0 +1,30 @@
CAMERA = "SATES30-RIXS-CAM01"
CAMERA_URL = caget(CAMERA + ":BSREADCONFIG").replace("tcp://daq", "tcp://")
SAMPLES = 3
TIMEOUT = 10.0
CHANNEL_IMG = CAMERA+":FPICTURE"
add_device(Stream("cs", CAMERA_URL, SocketType.PULL), True)
#image = cs.addMatrix(CAMERA+":FPICTURE")
cs.createMatrix = True
cs.start()
cs.waitCacheChange(3000)
image = cs.getChild(CHANNEL_IMG)
add_device(image, True)
PLOT_TYPES={CHANNEL_IMG:"ch.psi.pshell.plot.MatrixPlotRenderer"}
#bscan(cs, 5, None)
bscan(cs, SAMPLES, TIMEOUT, enabled_plots=[cs])
#add_device(st1.getChildren()[0], True)
mscan(image, image, SAMPLES, TIMEOUT, plot_types=PLOT_TYPES)
tscan (image, 3, 1.0, plot_types=PLOT_TYPES)
+19
View File
@@ -0,0 +1,19 @@
#Add device: will be visible in Device tab -> device panel is opened double clicking the device name.
#In the device panel you can plot data double-clicking each chanel.
add_device (Stream("st", cam_server.getStream("SATES30-RIXS-CAM01_proc"), SocketType.PULL), True)
st.start()
#Open the panel programatically
show_panel(st)
#Saves and displays 5 messages
bscan(st, 5)
#Saves and displays messages for 3 seconds, plotting arrays as line plots (1d)
bscan(st, -1, 3.0, line_plots=list(st.getChildren()))
#Just saves the stream, no display (Good idea if 100Hz)
bscan(st, -1, 3.0, display=False)
#Just displays the stream, no saving
bscan(st, 5, -1, save=False, line_plots=list(st.getChildren()))
+214
View File
@@ -0,0 +1,214 @@
#include "module.c"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const char *CHANNEL_NAMES[] = {"EVENT_NUM", "EVENT_I", "EVENT_J", "EVENT_CHARGE", "EVENT_ETA_X", "EVENT_ETA_Y", "EVENT_I_INTERP", "EVENT_J_INTERP"};
const int MAX_NUM_EVENTS= 100; // max number of events per frame
const int EVENT_CHANNELS = 7;
struct events_double func_ph_1d_double( double *frame, int i_dim, int j_dim, double *th_m);
//struct decl.
struct events_double {
double **evnt_ijc;
int evnt_num;
};
//def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata=None):
PyObject *process(PyObject *self, PyObject *args)
{
PyArrayObject *image;
long pulse_id;
PyObject /*double*/ *timestamp;
long seconds, nanos;
PyArrayObject *x_axis;
PyArrayObject *y_axis;
PyObject *pars;
PyObject *bsdata;
//if (!PyArg_ParseTuple(args, "OldOOO|O", &image, &pulse_id, &timestamp, &x_axis, &y_axis, &pars, &bsdata))
if ( !PyArg_ParseTuple(args, "OlOOOO|O", &image, &pulse_id, &timestamp, &x_axis, &y_axis, &pars, &bsdata) ||
!PyArg_ParseTuple(timestamp, "ll", &seconds, &nanos) )
return NULL;
if (pulse_id < 0) {
PyErr_SetString(moduleErr, "Invalid Pulse ID");
return NULL;
}
//Acessing image
int element_size = image->descr->elsize;
int dims = image->nd;
int size_x = image->dimensions[1];
int size_y = image->dimensions[0];
unsigned short* img_data = (unsigned short*)image->data;
int i,j,l;
int i_dim=size_y;
int j_dim=size_x;
double *threshold = malloc(i_dim*j_dim*sizeof(double));
for(i=0; i<i_dim;i++) {
for(j=0;j<j_dim;j++) {
threshold[i*j_dim+j]=10.0;
}
}
//background (all matrices are indexed in 1d)
double *background = malloc(i_dim*j_dim*sizeof(double));
for(i=0; i<i_dim;i++) {
for(j=0;j<j_dim;j++) {
background[i*j_dim+j]=210.0;
}
}
double *frameBKsub = malloc(i_dim*j_dim*sizeof(double));
for(i=0; i<i_dim;i++) {
for(j=0;j<j_dim;j++) {
frameBKsub[i*j_dim+j]=(double)img_data[i*j_dim+j] - background[i*j_dim+j];
}
}
struct events_double evns1d = func_ph_1d_double( frameBKsub, i_dim, j_dim, threshold);
//Create return dictionary
PyObject *ret = PyDict_New();
PyObject* camera_name = PyDict_GetItemString(pars, "camera_name");
const char * camera_name_str = PyUnicode_AsUTF8(camera_name);
char channel_name[200];
//PyDict_SetItemString(ret, "camera_name", camera_name);
sprintf(channel_name, "%s:%s",camera_name_str, CHANNEL_NAMES[0]);
PyDict_SetItemString(ret, channel_name, PyLong_FromLong(evns1d.evnt_num));
for (int i=0; i<EVENT_CHANNELS; i++){
sprintf(channel_name, "%s:%s",camera_name_str, CHANNEL_NAMES[i+1]);
npy_intp arr_dims[1] = {MAX_NUM_EVENTS};
PyObject *parr =PyArray_SimpleNewFromData(1, arr_dims, NPY_FLOAT64, evns1d.evnt_ijc);
PyDict_SetItemString(ret, channel_name, parr);
}
for (l = 0; l < EVENT_CHANNELS; l++) {
free(evns1d.evnt_ijc[l]);
}
free(evns1d.evnt_ijc);
free(frameBKsub);
free(background);
free(threshold);
return ret;
}
struct events_double func_ph_1d_double( double *frame, int i_dim, int j_dim, double *th_m)
{
//int th= 50;
int i, j, l=0, m=0, n=0, evt_m_i=0, evt_i=0 ;
int dist_i, dist_j;
double charge_evt;
int th;
double **evt_p= (double**)malloc(EVENT_CHANNELS * sizeof(double*));
for (l = 0; l < EVENT_CHANNELS; l++) {
evt_p[l] = (double*)malloc(MAX_NUM_EVENTS * sizeof(double));}
/*Counter variables for the loop*/
double charge = 0;
double eta_x = 0;
double eta_y = 0;
double i_interp = 0;
double j_interp = 0;
double C[]= { 4.37805097e-03, 2.43266401e-01, -7.81479328e+00, 7.70533057e+01, -2.37906124e+02, 3.42988113e+02, -2.35979731e+02, 6.24074562e+01};
double D[]= {-2.05180773e-04, 4.35314696e-01, -1.32557223e+01, 1.19990373e+02, -3.80696758e+02, 5.81427096e+02, -4.30789104e+02, 1.23894761e+02};
for(i=0; i<i_dim-1; i++) {
if (evt_i>=MAX_NUM_EVENTS){
break;
}
for(j=0;j<j_dim-1;j++) {
if (evt_i>=MAX_NUM_EVENTS){
break;
}
// 2x2 version
charge = frame[i*j_dim+j]+frame[(i+1)*j_dim+j] + frame[i*j_dim+(j+1)]+frame[(i+1)*j_dim+j+1];
//pixel by pixel threshold
th = th_m[i*j_dim +j];
//check if charge above threshold
if(charge>th) {
eta_x = (frame[(i+1)*j_dim + j ]+frame[(i+1)*j_dim + (j+1)])/charge;
eta_y = (frame[ i*j_dim + (j+1)]+frame[(i+1)*j_dim + (j+1)])/charge;
i_interp = i + (C[0] + C[1]*eta_x+ C[2]*pow(eta_x,2) + C[3]*pow(eta_x,3) + C[4]*pow(eta_x,4)+ C[5]*pow(eta_x,5) + C[6]*pow(eta_x,6) + C[7]*pow(eta_x,7));
j_interp = j + (D[0] + D[1]*eta_y+ D[2]*pow(eta_y,2) + D[3]*pow(eta_y,3) + D[4]*pow(eta_y,4)+ D[5]*pow(eta_y,5) + D[6]*pow(eta_y,6) + D[7]*pow(eta_y,7));
// 1st case: first event
if(evt_i==0){
evt_p[0][evt_i] = i;
evt_p[1][evt_i] = j;
evt_p[2][evt_i] = charge;
evt_p[3][evt_i] = eta_x;
evt_p[4][evt_i] = eta_y;
evt_p[5][evt_i] = i_interp; //
evt_p[6][evt_i] = j_interp; //
evt_i++;
} else {
// 2nd case: not 1st event. we check if it is a neighbourg of the previos events and if charge is larger
n=0;
evt_m_i = evt_i;
for(m=0; m<evt_m_i; m++) {
dist_i = abs(evt_p[0][evt_i-1-m] - i); //fix here
dist_j = abs(evt_p[1][evt_i-1-m] - j);
charge_evt = evt_p[2][evt_i-1-m];
if( dist_i< 2 && dist_j<2) {
if(charge_evt < charge) {
//2nd case: not 1st event, but neigboor of previos event, this event has more charge
evt_p[0][evt_i-1-m] = i;
evt_p[1][evt_i-1-m] = j;
evt_p[2][evt_i-1-m] = charge;
evt_p[3][evt_i-1-m] = eta_x;
evt_p[4][evt_i-1-m] = eta_y;
evt_p[5][evt_i-1-m] = i_interp;
evt_p[6][evt_i-1-m] = j_interp;
} else {
//3d case not 1st event, but neigboor of previos event, this event has less charge
}
} else {
// not a neighbor of the m event
n++;
//now we check if it is not a neighborg of any previous event
if((n)==evt_i) {
evt_p[0][evt_i] = i;
evt_p[1][evt_i] = j;
evt_p[2][evt_i] = charge;
evt_p[3][evt_i] = eta_x;
evt_p[4][evt_i] = eta_y;
evt_p[5][evt_i] = i_interp; //
evt_p[6][evt_i] = j_interp; //
evt_i++;
}
}
}
}
}
}
}
struct events_double events_i_d;
events_i_d.evnt_ijc = evt_p;
events_i_d.evnt_num = evt_i;
return events_i_d;
}