-
Notifications
You must be signed in to change notification settings - Fork 1
/
limaacq.mac
604 lines (529 loc) · 16.6 KB
/
limaacq.mac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#%TITLE% limaacq.mac
#%NAME% LIMA CCD acquisition macro
#%DESCRIPTION%
# Acquisition macros for LIMA CCDs
#
#%END%
#%CATEGORY% Detection, Ccd
#%UU% <exposure_time> [<nb_frames>] [<interval_time>] [<display_rate>]
#%MDESC%
# Take a serie of images in multi-frame mode using active lima CCD device(s) (active lima ccd
# has to be set using %B%limaon/off%B% . Check it with %B%limalist%B%) %BR%
# %BR% %B%exposure_time%B% : exposure time per frame (sec)
# %BR% %B%nb_frames%B% : total number of frames to acquire (%B%0%B% for LIVE mode )
# %BR% %B%interval_time%B% : interval time between frames programmed on the ccd controller
# %BR% %B%display_rate%B% : by default, spec reads about 4 frames/sec to update shared memory for online display.
# You can change that and set it to: %B%0%B% to have no image reading, %B%-1%B% to force reading all images, %B%>0%B% to change the approximate reading rate.
# %BR% %BR%
# For %B%LIVE%B% mode use %B%nb_frames = 0%B% and %B%Ctr-C%B% to stop
# %BR% %BR%
# Saving of frame can be enabled through ccdmenu
def limatake '{
local _expo _nimg _rate _it_time
local nccd ccds[] _in_pars[]
local npar pars[] ipar
local nval vals[]
if (!$#) {
print "Usage: $0 <exposure_time> [<nb_frames>] [<interval_time>] [<display_rate>] [<limapar=value> ...]"
print " LIVE mode: <nb_frames> = 0 (Ctr-C to stop)"
exit
}
# -- active ccds
nccd= _lima_getactive(ccds)
if (!nccd) {
_lima_error; printf("No active lima ccd. Use limaon first.\n")
exit
}
# -- default
_expo= 1.0
_nimg= 1
_it_time= 0.
_rate= 0.25*nccd
# -- read input parameters
npar= split("$*", pars)
for (ipar= 0; ipar<npar; ipar++) {
if (index(pars[ipar], "=")>0) {
nval= split(pars[ipar], vals, "=")
if (nval == 2) {
_in_pars[vals[0]]= vals[1]
} else {
printf("Wrong input parameters #%d [%s]\n", ipar, pars[ipar])
exit
}
} else {
if (ipar == 0) _expo=1.*pars[0]
else if (ipar == 1) _nimg=int(pars[1])
else if (ipar == 2) _it_time= 1.*pars[2]
else if (ipar == 3) _rate= 1.*pars[3]
else {
printf("Wrong input parameters #%d [%s]\n", ipar, pars[ipar])
exit
}
}
}
# -- number of images
if (_nimg <= 0) {
_nimg= 0
print "LIVE mode is running (hint: Ctr-C to stop)"
}
# -- open GUI
_limatake_display(ccds)
# -- acquisition
_limatake(ccds, _expo, _nimg, _it_time, _rate, _in_pars)
}'
#%IU%
def _limatake_display(ccds) '{
#WARNING: use of ccd display macros !!
local iccd
for (iccd=0; iccd<ccds["nb"]; iccd++) {
ccdunit= LIMA_DEV[ccds[iccd]]["unit"]
if (CCD_GUI[ccdunit]["on"]) {
ccd_display_start(ccdunit)
}
}
}'
#%IU%
def _limatake(ccds, _expo, _nimg, _it_time, _rate, _in_pars) '{
unglobal LIMATAKE LIMATAKE_BACK
local iccd _set_pars[] parname parval
global LIMATAKE[] LIMATAKE_BACK[]
LIMATAKE["nb"]= 0
# --- lima pars to be set
_set_pars["latency_time"]= _it_time
_set_pars["acq_trigger_mode"]= "INTERNAL_TRIGGER"
for (parname in _in_pars) {
_set_pars[parname]= _in_pars[parname]
}
cdef("cleanup_once", "_limatake_cleanup;", "limatake")
# --- init lima pars and internal pars
for (iccd=0; iccd<ccds["nb"]; iccd++) {
ccdname= ccds[iccd]
LIMATAKE["nb"] += 1
LIMATAKE[iccd]["ccdname"]= ccdname
# --- set lima parameters, backup previous values
for (parname in _set_pars) {
parval= _limapar_get(ccdname, parname)
if (parval != _set_pars[parname]) {
LIMATAKE_BACK[ccdname][parname]= parval
_limapar_set(ccdname, parname, _set_pars[parname])
}
}
# --- internal variables
LIMATAKE[iccd]["savmode"]= _limaacq_checksaving(ccds[iccd])
LIMATAKE[iccd]["autosave"]= (LIMATAKE[iccd]["savmode"]!="MANUAL")
LIMATAKE[iccd]["autoheader"]= (LIMATAKE[iccd]["savmode"]=="AUTO_HEADER")
LIMATAKE[iccd]["nacq"]= -1
LIMATAKE[iccd]["nhead"]= -1
LIMATAKE[iccd]["nsave"]= -1
LIMATAKE[iccd]["nread"]= -1
LIMATAKE[iccd]["readtime"]= 0
}
# --- prepare acq
for (iccd=0; iccd<ccds["nb"]; iccd++) {
if (_limaacq_prepare(ccds[iccd], _expo, _nimg) == -1) {
_limatake_cleanup
return
}
}
# --- update common header if needed
for (iccd=0; iccd<ccds["nb"]; iccd++) {
if (LIMATAKE[iccd]["autosave"]) {
if (LIMATAKE[iccd]["autoheader"]) {
_limaacq_reset_frame_header(ccds[iccd])
}
_limaacq_update_common_header(ccds[iccd])
}
}
# --- start acq
for (iccd=0; iccd<ccds["nb"]; iccd++) {
if (_limaacq_start(ccds[iccd]) == -1) {
_limatake_cleanup
return
}
}
_limatake_wait(_rate)
_limatake_cleanup
cdef("cleanup_once", "", "limatake", "delete")
}'
#%IU%
def _limatake_cleanup '{
local iccd ccdname parname
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
# --- stop ccd
ccdname= LIMATAKE[iccd]["ccdname"]
_limaacq_stop(ccdname)
# --- send last frame headers if needed
LIMATAKE[iccd]["nacq"]= _limapar_get(ccdname, "last_image_ready")
if (LIMATAKE[iccd]["autoheader"]) {
if (LIMATAKE[iccd]["nacq"] > LIMATAKE[iccd]["nhead"]) {
_limaacq_update_frame_header(ccdname, LIMATAKE[iccd]["nhead"], LIMATAKE[iccd]["nacq"])
LIMATAKE[iccd]["nhead"]= LIMATAKE[iccd]["nacq"]
}
}
# --- reset previous acq parameters
_limapar_set(ccdname, "acq_nb_frames", 1)
for (parname in LIMATAKE_BACK[ccdname]) {
_limapar_set(ccdname, parname, LIMATAKE_BACK[ccdname][parname])
}
}
}'
#%IU% (ccdname, rate)
def _limatake_wait(rate) '{
local iccd acqrunning
acqrunning= 1
_limatake_wait_update()
_limatake_wait_msg(1)
while (acqrunning) {
acqrunning= 0
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
acqrunning += _limaacq_running(LIMATAKE[iccd]["ccdname"])
}
if (acqrunning) {
_limatake_wait_update()
_limatake_wait_msg()
_limatake_wait_read(rate)
sleep(COUNTERSPOLLTIME)
}
}
_limatake_wait_update()
_limatake_wait_msg()
if (rate!=0) {
_limatake_readlast()
}
printf("\n")
}'
#%IU%
#%MDESC%
# Update last image number acquired/saved during a limatake acquisition
#%BR% if saving_mode = %B%AUTO_HEADER%B%, update already acquired frame headers
#
def _limatake_wait_update() '{
local iccd ccdname
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
ccdname= LIMATAKE[iccd]["ccdname"]
LIMATAKE[iccd]["lastacq"]= LIMATAKE[iccd]["nacq"]
LIMATAKE[iccd]["nacq"]= _limapar_get(ccdname, "last_image_ready")
if (LIMATAKE[iccd]["autoheader"]) {
if (LIMATAKE[iccd]["nacq"] > LIMATAKE[iccd]["nhead"]) {
_limaacq_update_frame_header(ccdname, LIMATAKE[iccd]["nhead"], LIMATAKE[iccd]["nacq"])
LIMATAKE[iccd]["nhead"]= LIMATAKE[iccd]["nacq"]
}
}
if (LIMATAKE[iccd]["autosave"]) {
LIMATAKE[iccd]["nsave"]= _limapar_get(ccdname, "last_image_saved")
}
}
}'
#%IU%
#%MDESC%
# Read images if necessary during a limatake acquisition
#
def _limatake_wait_read(rate) '{
if (rate==-1) {
finished= 0
while (!finished) {
finished= 1
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
if (LIMATAKE[iccd]["nread"]!=LIMATAKE[iccd]["nacq"]) {
_limaacq_read(LIMATAKE[iccd]["ccdname"], LIMATAKE[iccd]["nread"]+1)
LIMATAKE[iccd]["nread"]= LIMATAKE[iccd]["nread"]+1
}
if (LIMATAKE[iccd]["nread"]!=LIMATAKE[iccd]["nacq"]) {
finished= 0
}
}
_limatake_wait_update()
_limatake_wait_msg()
}
}
else if (rate>0.) {
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
if (LIMATAKE[iccd]["nacq"]>=0) {
tps= time()
if ((tps - LIMATAKE[iccd]["readtime"]) > rate) {
_limaacq_read(LIMATAKE[iccd]["ccdname"], LIMATAKE[iccd]["nacq"])
LIMATAKE[iccd]["nread"]= LIMATAKE[iccd]["nacq"]
LIMATAKE[iccd]["readtime"]= tps
_limatake_wait_update()
_limatake_wait_msg()
}
}
}
}
}'
#%IU%
#%MDESC%
# read last image of an acquisition
#
def _limatake_readlast() '{
local iccd
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
if (LIMATAKE[iccd]["nacq"]>=0) {
_limaacq_read(LIMATAKE[iccd]["ccdname"], LIMATAKE[iccd]["nacq"])
LIMATAKE[iccd]["nread"]= LIMATAKE[iccd]["nacq"]
_limatake_wait_msg()
}
}
}'
#%IU%
#%MDESC%
# Print message during limatake acquisition
#
def _limatake_wait_msg(doclear) '{
local iccd cy msg
if (doclear) {
for (iccd= 0; iccd<LIMATAKE["nb"]; iccd++) print
}
for (iccd=0; iccd<LIMATAKE["nb"]; iccd++) {
msg= sprintf("\[md\]> %-8.8s\[me\] Acquire #%d ", LIMATAKE[iccd]["ccdname"], LIMATAKE[iccd]["nacq"])
if (LIMATAKE[iccd]["nread"]!=-1)
msg= msg sprintf("Display #%d ", LIMATAKE[iccd]["nread"])
if (LIMATAKE[iccd]["nsave"]!=-1)
msg= msg sprintf("Save #%d", LIMATAKE[iccd]["nsave"])
cy= -1*LIMATAKE["nb"] + iccd
tty_move(0, cy, msg)
}
}'
#%IU% (ccdname, framenr)
#%MDESC%
# WORKAROUND: read image through taco device, waiting for a
# tango interface which support DevEncoded
#
def _limaacq_read(ccdname, framenr) '{
local ccdunit arrname
ccdunit= LIMA_DEV[ccdname]["unit"]
arrname= LIMA_DEV[ccdname]["data"]
image_par(ccdunit, "get_offset", framenr)
image_get(ccdunit, @arrname)
}'
# PURE_LIMA
# The under-test image reading using DevEncoded format
# def _limaacq_read(ccdname, framenr) '{
# local ccdunit arrname
# ccdunit= LIMA_DEV[ccdname]["unit"]
# arrname= LIMA_DEV[ccdname]["data"]
# tango_io(LIMA_DEV[ccdname]["control"],"readImage",framenr, @arrname)
# }'
#%IU% (ccdname, expo, nimg)
#%MDESC%
# Prepare an acquisition of <nimg> frames with <expo> exposure time
#
def _limaacq_prepare(ccdname, expo, nimg) '{
local ret back_tout prep_tout
_limapar_set(ccdname, "acq_nb_frames", nimg)
_limapar_set(ccdname, "acq_expo_time", expo)
prep_tout= LIMA_DEV[ccdname]["prepare_timeout"]
if (prep_tout>0.) {
back_tout= tango_get(LIMA_DEV[ccdname]["control"], "timeout")
tango_put(LIMA_DEV[ccdname]["control"], "timeout", prep_tout)
}
TANGO_ERR = -1
ret = tango_io(LIMA_DEV[ccdname]["control"], "prepareAcq")
if (ret == -1) {
_lima_tango_error ccdname LIMA_DEV[ccdname]["control"]
}
if (prep_tout>0.) {
tango_put(LIMA_DEV[ccdname]["control"], "timeout", back_tout)
}
if (ret == -1) {
return -1
}
_limaacq_arrayinit(ccdname)
return 0
}'
#%IU% (ccdname)
#%MDESC%
# Start an acquisition and hook a stop in cleanup_once
#
def _limaacq_start(ccdname) '{
local cmd key ret
cmd= sprintf("_limaacq_stop(\"%s\"); ", ccdname)
key= sprintf("run_%s", ccdname)
cdef("cleanup_once", cmd, key)
TANGO_ERR = -1
ret = tango_io(LIMA_DEV[ccdname]["control"], "startAcq")
if (ret == -1) {
_lima_tango_error ccdname LIMA_DEV[ccdname]["control"]
return -1
}
return 0
}'
#%IU$ (ccdname)
#%MDESC%
# stop an acquisition
#
def _limaacq_stop(ccdname) '{
local ret
TANGO_ERR = -1
ret = tango_io(LIMA_DEV[ccdname]["control"], "stopAcq")
cdef("cleanup_once", "", sprintf("run_%s", ccdname), "delete")
if (ret == -1) {
_lima_tango_error ccdname LIMA_DEV[ccdname]["control"]
}
}'
#%IU% (ccdname)
#%MDESC%
# Return 1 when an acquisition is running, 0 otherwise
#
def _limaacq_running(ccdname) '{
local acqstatus
acqstatus= _limapar_get(ccdname, "acq_status")
if (acqstatus == "Running") {
return 1
} else if (acqstatus == "Fault") {
_lima_error ccdname ; printf("Acquisiton in fault state\n")
}
return 0
}'
# --- versions of array init based on ccd.mac ---
def _limaacq_arrayname(ccdname) '{
return sprintf("image_data%d", LIMA_DEV[ccdname]["unit"])
}'
def _limaacq_arrayinit(ccdname) '{
ccd_createarray LIMA_DEV[ccdname]["unit"]
LIMA_DEV[ccdname]["data"]= _limaacq_arrayname(ccdname)
}'
# --- versions of array init pure lima - not used for now ---
# PURE_LIMA
# def _limaacq_arrayname(ccdname) '{
# return sprintf("IMAGE_%s", ccdname)
# }'
# def _limaacq_arrayinit(ccdname) '{
# local signed nbytes xdim ydim arrname
# if (_limapar_get(ccdname, "image_sizes", -1)!=4) {
# _lima_error ccdname ; printf("Cannot get image size\n")
# return (-1)
# }
# signed= LIMA_VALS[0]
# nbytes= LIMA_VALS[1]
# xdim= LIMA_VALS[2]
# ydim= LIMA_VALS[3]
# arrname= _limaacq_arrayname(ccdname)
# if (nbytes == 1) {
# if (signed)
# shared byte array @arrname[xdim][ydim]
# else
# shared ubyte array @arrname[xdim][ydim]
# }
# else if (nbytes == 2) {
# if (signed)
# shared short array @arrname[xdim][ydim]
# else
# shared ushort array @arrname[xdim][ydim]
# } else if (nbytes == 4) {
# if (signed)
# shared long array @arrname[xdim][ydim]
# else
# shared ulong array @arrname[xdim][ydim]
# } else if (nbytes == 8) {
# shared double array @arrname[xdim][ydim]
# } else {
# _lima_error ccdname ; printf("Invalid data array type (%d,%d)\n", signed, nbytes)
# }
# array_op("tag", @arrname, "image")
# LIMA_DEV[ccdname]["data"]= arrname
#}'
#%IU% (ccdname)
#%MDESC%
# If saving mode is not %B%MANUAL%B%, print out next filename, saving mode and format
# %BR% Return current saving_mode
#
def _limaacq_checksaving(ccdname) '{
local savmode savfmt savdir savpre savsuf savnum savidxfmt idx
savmode= _limapar_get(ccdname, "saving_mode")
if (savmode != "MANUAL") {
savfmt= _limapar_get(ccdname, "saving_format")
savidxfmt= _limapar_get(ccdname, "saving_index_format")
savdir= _limapar_get(ccdname, "saving_directory")
savpre= _limapar_get(ccdname, "saving_prefix")
savsuf= _limapar_get(ccdname, "saving_suffix")
savnum= _limapar_get(ccdname, "saving_next_number")
_lima_trace ccdname
idx = sprintf(savidxfmt, savnum)
printf("Saving in %s/%s%s%s [%s-%s]\n", savdir, savpre, idx, savsuf, savmode, savfmt)
}
return savmode
}'
cdef("user_limaacq_frame_header")
#%IU% (ccdname, from_idx, to_idx)
#%MDESC%
# Update frame headers from image <from_idx> to image <to_idx> (same header)
# %BR% Header should be filled by beamline macro using %B%user_limaacq_frame_header%B%
# by adding pairs of name/value in global associative array %B%LIMA_FRAME_HEADER%B%
# %BR% The global array is deleted after each update.
# %BR% Two time stamps are automatically added : %B%frame_header_time%B% which gives the
# time the macro has been called, and %B%frame_header_took%B% gives in sec the time
# spent in user_limaacq_frame_header.
# %BR% A minimal frame header is always sent so that, even if user_limaacq_frame_header
# is not filling LIMA_FRAME_HEADER, the saving_mode AUTO_HEADER will always work
#
def _limaacq_update_frame_header(ccdname, from_idx, to_idx) '{
global LIMA_FRAME_HEADER[]
local headarr[] headstr nhead
local begt endt
begt= time()
user_limaacq_frame_header
endt= time()-begt
headstr= sprintf("frame_header_time=%f\n", begt)
headstr= sprintf("%sframe_header_took=%f\n", headstr, endt)
for (name in LIMA_FRAME_HEADER) {
headstr= sprintf("%s%s=%s\n", headstr, name, LIMA_FRAME_HEADER[name])
}
nhead= 0
for (fidx=from_idx; fidx<=to_idx; fidx++, nhead++) {
headarr[nhead]= sprintf("%d;%s", fidx, headstr)
}
tango_io(LIMA_DEV[ccdname]["control"], "setImageHeader", headarr)
unglobal LIMA_FRAME_HEADER
}'
#%IU% (ccdname)
#%MDESC%
# Reset frame headers
#
def _limaacq_reset_frame_header(ccdname) '{
tango_io(LIMA_DEV[ccdname]["control"], "resetFrameHeaders")
}'
cdef("user_limaacq_common_header")
#%IU% (ccdname)
#%MDESC%
# Update lima common header at the beginning of an acquisition.
# %BR% to set values in that header, cdef a beamline macro to
# %B%user_limaacq_common_header%B% and fill global ass. array
# %B%LIMA_COMMON_HEADER%B% with pairs of name/value
# %BR% The global array is deleted after each call
# %BR% Two time stamps are automatically added: %B%common_header_time%B%
# gives the time the macro has been called, and %B%common_header_took%B%
# gives in sec the time to execute beamline user_limaacq_common_header
#
def _limaacq_update_common_header(ccdname) '{
global LIMA_COMMON_HEADER[]
local headarr[] nhead begt endt
begt= time()
user_limaacq_common_header
endt= time() - begt
nhead= 0
for (name in LIMA_COMMON_HEADER) {
nhead++
}
if (nhead) {
nhead= 0
headarr[nhead++]= sprintf("common_header_time=%f", begt)
headarr[nhead++]= sprintf("common_header_took=%f", endt)
for (name in LIMA_COMMON_HEADER) {
headarr[nhead++]= sprintf("%s=%s", name, LIMA_COMMON_HEADER[name])
}
_limapar_set(ccdname, "saving_common_header", headarr, -1)
}
unglobal LIMA_COMMON_HEADER
}'
#%IU% (ccdname)
#%MDESC%
# Reset common headers
#
def _limaacq_reset_common_header(ccdname) '{
tango_io(LIMA_DEV[ccdname]["control"], "resetCommonHeader")
}'
#%MACROS%
#%IMACROS%
#%TOC%
#%AUTHOR% E.Papillon