-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaq_main2.py
executable file
·227 lines (201 loc) · 6.78 KB
/
daq_main2.py
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
#!/opt/conda_envs/collection-2018-1.0/bin/ipython -i
##!/opt/conda_envs/lsdc_dev3/bin/ipython -i
###!/usr/bin/python -Wignore
#from __future__ import (absolute_import, division, print_function,unicode_literals)
import string
import sys
import os
import time
import _thread
import db_lib
import daq_macros
from daq_macros import *
import daq_lib
from daq_lib import *
import daq_utils
from robot_lib import *
import det_lib
import beamline_support
import beamline_lib
from beamline_lib import *
#import stateModule
import atexit
sitefilename = ""
global command_list,immediate_command_list,z
command_list = []
immediate_command_list = []
z = 25
def execute_command(command_s):
### from daq_macros import * #this is so macros can be reloaded on-the-fly
# refresh_screen(0,0)
exec(command_s)
def pybass_init():
global message_string_pv
# db_lib.db_connect()
daq_utils.init_environment()
init_var_channels()
# init_diffractometer()
det_lib.init_detector()
daq_lib.message_string_pv = beamline_support.pvCreate(daq_utils.beamlineComm + "message_string")
daq_lib.gui_popup_message_string_pv = beamline_support.pvCreate(daq_utils.beamlineComm + "gui_popup_message_string")
if (1):
# if (daq_lib.has_beamline): # for now
# try:
beamline_lib.read_db()
print("init mots")
# beamline_support.init_motors()
init_mots() #for now
print("init done mots")
init_diffractometer()
# init_counters() #for now
# newfile("scandata")
## except CaChannelException as status:
## print(ca.message(status))
## gui_message("EPICS motor Initialization Error. Exit and try again. If problem persists, EPICS may need to be restarted.")
try:
sitefilename = os.environ["LSDC_SITE_FILE"]
except KeyError:
gui_message("\$LSDC_SITE_FILE not defined. Questionable defaults in effect.")
if (sitefilename != ""):
if (os.path.exists(sitefilename) == 0):
error_msg = "\$LSDC_SITE_FILE: %s does not exist. Questionable defaults in effect." % sitefilename
gui_message(error_msg)
else:
process_command_file(sitefilename)
def process_command_file(command_file_name):
echo_s = "reading %s\n" % command_file_name
print(echo_s)
command_file = open(command_file_name,"r")
while 1:
command = command_file.readline()
if not command:
break
else:
input_tokens = string.split(command)
if (len(input_tokens)>0):
command_string = "%s(" % input_tokens[0]
for i in range(1,len(input_tokens)):
command_string = command_string + "\"" + input_tokens[i] + "\""
if (i != (len(input_tokens)-1)):
command_string = command_string + ","
command_string = command_string + ")"
print(command_string)
try:
exec(command_string);
except NameError:
error_string = "Unknown command: " + command_string
print(error_string)
except SyntaxError:
print("Syntax error")
except KeyError:
print("Key error")
# refresh_screen(1,0)
command_file.close()
def process_immediate_commands(frequency):
# global immediate_command_list
while (1):
if (len(immediate_command_list) > 0):
process_input(immediate_command_list.pop(0))
# daq_utils.broadcast_output("Command> ")
## print "Command> "
time.sleep(frequency)
def process_commands(frequency):
while (1):
if (len(command_list) > 0):
process_input(command_list.pop(0))
# daq_utils.broadcast_output("Command> ")
## print "Command> "
###### time.sleep(frequency)
plt.pause(frequency)
def print_status_thread(frequency):
global count_list,ring_intensity
previous_image_started = 0
percent_done = 0
shutter_dead_time = .6
while 1:
time.sleep(frequency)
current_percent_done = daq_lib.get_field("state_percent")
if (daq_lib.image_started > 0):
if (start_time == 0 or daq_lib.image_started != previous_image_started):
previous_image_started = daq_lib.image_started
start_time = time.time()
now = time.time()
total_time = float(daq_lib.image_started)
if (total_time>0.0):
percent_done = int(.5+((now-start_time)/total_time*100))
else:
percent_done = 0
else:
start_time = 0
percent_done = 0
if (percent_done != current_percent_done):
daq_lib.set_field("state_percent",percent_done)
def comm_cb(value=None, char_value=None, **kw):
command = char_value
command_list.append(command)
def comm2_cb(value=None, char_value=None, **kw):
command = char_value
if not (command == "\n"):
immediate_command_list.append(command)
def process_input(command_string):
if (command_string == ""):
return
# daq_utils.broadcast_output(time.ctime(time.time()))
if (command_string == "q"):
sys.exit()
daq_lib.broadcast_output(time.ctime(time.time()) + "\n" + command_string)
try:
daq_lib.set_field("program_state","Program Busy")
execute_command(command_string)
except NameError:
error_string = "Unknown command: " + command_string
print(error_string)
except SyntaxError:
print("Syntax error")
except KeyError:
print("Key error")
except TypeError:
print("Type error")
except AttributeError:
print("Attribute Error")
except KeyboardInterrupt:
abort_data_collection()
print("Interrupt caught by daq server\n")
if (command_string != "pause_data_collection()" and command_string != "continue_data_collection()" and command_string != "abort_data_collection()" and command_string != "unmount_after_abort()" and command_string != "no_unmount_after_abort()"):
daq_lib.set_field("program_state","Program Ready")
def run_server():
# _thread.start_new_thread(process_commands,(.05,))
_thread.start_new_thread(process_immediate_commands,(.25,))
comm_pv = beamline_support.pvCreate(daq_utils.beamlineComm + "command_s")
beamline_support.pvPut(comm_pv,"\n")
immediate_comm_pv = beamline_support.pvCreate(daq_utils.beamlineComm + "immediate_command_s")
beamline_support.pvPut(immediate_comm_pv,"\n")
comm_pv.add_callback(comm_cb)
immediate_comm_pv.add_callback(comm2_cb)
process_commands(0.5)
## daq_lib.refresh_screen(0,1)
# while 1:
# if (len(command_list) > 0):
# command_s = command_list.pop(0)
# process_input(command_s)
## daq_utils.broadcast_output("Command> ")
# print "Command> "
# time.sleep(.05)
#@atexit.register
#def goodbye():
# comm_s = "killall -KILL daq_main2.py"
# os.system(comm_s)
def main(mode):
pybass_init()
# stateModule.initStateControl(daq_utils.beamlineComm)
if (mode=="gui"):
run_server()
else:
# pass
lsdcHome = os.environ["LSDCHOME"]
os.system(lsdcHome+"/daq_main2.py gui&")
if (len(sys.argv)>1):
if (sys.argv[1] == "gui"):
main(mode="gui")
else:
main(mode="shell")