source: fact/tools/PyDimCtrl/ScriptsForPyDimCtrl.py@ 14463

Last change on this file since 14463 was 14463, checked in by neise, 12 years ago
removed drafted schedule and tried some minor printing issues
File size: 27.3 KB
Line 
1#!/usr/bin/python -tti
2
3import time
4from factdimserver import *
5import numpy as np
6import types
7import sys
8
9last_drive_kwargs = {}
10last_drive_method = None
11
12bias_calibration = {}
13
14report_length = 0
15
16def wait_nice(self, state_num, timeout=None):
17 global report_length
18 if not hasattr(self, 'stn'):
19 raise TypeError(self.name+' has no CMD called STATE')
20 if timeout == None:
21 timeout = float('inf')
22 else:
23 timeout = float(timeout)
24 start = time.time()
25 intermed = time.time()-1.
26 while not self.stn == state_num:
27 time.sleep(0.1)
28 if time.time() - intermed >= 1.:
29 report = str(fad_control.events()[0]) + ' events @ ' + str( ftm_control.trigger_rates()[3]) + ' Hz'
30
31 sys.stdout.write('\r'+' '*report_length)
32 sys.stdout.flush()
33 sys.stdout.write('\r'+report)
34 sys.stdout.flush()
35 report_length = len(report)
36
37 intermed = time.time()
38
39 if time.time() >= start+timeout:
40 report_length=0
41 print
42 return False
43 report_length=0
44 print
45 return True
46
47fad_control.wait_nice = types.MethodType( wait_nice, fad_control)
48
49
50def FadConnectCrate( crate ):
51 cratenum = None
52
53 if crate == 'all':
54 print "connecting to all crates"
55 for i in range( 40 ):
56 time.sleep(3.8)
57 fad_control.connect(i)
58 print "... done"
59 else:
60 try:
61 cratenum = int(crate)
62 except ValueError as e:
63 print "cannot convert crate parameter to integer. crate=", crate
64 print e
65 raise
66
67 if cratenum != None:
68 print "connecting to crate", cratenum
69 for i in range(cratenum*10, (cratenum+1)*10 ):
70 time.sleep(3.8)
71 fad_control.connect(i)
72 print "... done"
73
74
75def FadDisconnectCrate( crate ):
76 cratenum = None
77
78 if crate == 'all':
79 print "connecting to all crates"
80 for i in range( 40 ):
81 fad_control.disconnect(i)
82 print "... done"
83 else:
84 try:
85 cratenum = int(crate)
86 except ValueError as e:
87 print "cannot convert crate parameter to integer. crate=", crate
88 print e
89 raise
90
91 if cratenum != None:
92 print "connecting to crate", cratenum
93 for i in range(cratenum*10, (cratenum+1)*10 ):
94 print 'disconnecting from i', time.time()
95 fad_control.disconnect(i)
96 print "... done"
97
98
99
100def IsReadyForDataTaking():
101 """ Checking the system statuses if they are ready for data taking
102 """
103
104 print "--------------------------------------"
105 print "Checking the system statuses of:"
106 print "FEEDBACK, BIAS, FAD and DRIVE_CONTROL"
107 print "--------------------------------------"
108
109 print "...waiting for FEEDBACK"
110 print " to be in state 12: CurrentControl"
111 feedback.wait(12)
112
113 print "...waiting for BIAS_CONTROL"
114 print " to be in state 9: VoltageOn"
115 bias_control.wait(9)
116
117 print "...waiting for FAD_CONTROL"
118 print " to be in state 4: Connected"
119 fad_control.wait(4)
120
121 print "...waiting for drive_CONTROL"
122 print " to be in state 8: Tracking"
123 drive_control.wait(8)
124
125 print "...system statuses OK"
126 print "--------------------------------------"
127
128def NotReadyForDataTaking( servers_n_targets = [ (feedback, 12),
129 (bias_control, 9),
130 (fad_control, 4) ] ):
131 """ Checking the system statuses if they are ready for data taking
132 return list of servers, which are NOT ready for Data Taking
133 so one can use this function like this
134 if not NotReadyForDataTaking():
135 # freak out
136 else:
137 # start data taking
138 """
139 not_ready = []
140
141 print "--------------------------------------"
142 print "Checking the system statuses of:"
143 for server,target in servers_n_targets:
144 print server.__name__ , ','
145 print
146 print "--------------------------------------"
147
148 for n, server, target in enumerate(servers_n_targets):
149 if server.stn != target:
150 print server.__name__, "NOT in state ", target
151 not_ready.apppend((server,target))
152
153 return not_ready
154
155
156
157
158def PrepareBiasForDataTaking():
159 """ should have the original behaviour, no new fancy features
160 check feedback state before switching BIAS ON and ramping up to nominal Voltage
161 """
162
163 start = time.time()
164 while (feedback.stn != 12):
165 time.sleep(0.1)
166 if time.time() > start + 10.:
167 print "==================================================="
168 print " feedback is not in state 'CurrentControl' "
169 print " OPERATOR: "
170 print " goto feedback console and check the state of "
171 print " feedback by typing [st] to find out what the"
172 print " current state means and maybe needs to be done"
173 print " this script will wait for state 'CurrentControl'"
174 print "==================================================="
175 feedback.wait(12)
176
177 bias_control.set_global_dac(1)
178
179 start = time.time()
180 while (bias_control.stn != 9):
181 time.sleep(0.1)
182 if time.time() > start + 10.:
183 print '==================================================='
184 print ' switching on bias not successfull'
185 print ' biasctrl is not in state "VoltageOn"'
186 print ''
187 print ' OPERATOR:'
188 print ' goto biasctrl console and check the state of'
189 print ' biasctrl by typing [st] to find out what the'
190 print ' current state means and maybe needs to be done'
191 print ''
192 print ' this script will wait for state "VoltageOn"'
193 print '==================================================='
194 bias_control.wait(9)
195
196 bias_control.wait(5)
197 bias_control.wait(9)
198
199 print "bias is on, and feedback-program is working, but we wait 45sec for the current readings..."
200 time.sleep(45)
201 print "...done"
202
203
204def StopTracking():
205 """ should have the original behaviour, no new fancy features
206 stop drivectrl tracking the current source
207 """
208 drive_control.stop()
209 drive_control.wait(6) #Armed
210
211 print "Drive Armed"
212 print "Tracking Stopped"
213
214
215def SwitchOnBias():
216 """ should have the original behaviour, no new fancy features
217 bring Feedback to state CurrentControIdle and switch on Bias
218 """
219 print ' switching on current controll feedback'
220 feedback.stop()
221 print ' ... starting current control feedback '
222 feedback.start_current_control(0.)
223 feedback.enable_output(1) # 1 means True here ... this needs improvement.
224
225 print ' ...waiting for FEEDBACK to be in state 9: CurrentCtrlIdle'
226 feedback.wait(9)
227 print '... feedback is running.'
228
229 print ' switching bias on by, setting DAC to 1 globally'
230 bias_control.set_global_dac(1)
231
232 print ' ...waiting for BIAS to be in state 9: VoltageOn'
233 bias_control.wait(9)
234 print ' ...1 DAC globally set'
235
236
237 print ' ...waiting for BIAS to be in state 5: Ramping'
238 bias_control.wait(5)
239 print ' ...ramping to nominal voltage'
240
241 print ' ...waiting for BIAS to be in state 9: VoltageOn'
242 bias_control.wait(9)
243 print ' ...bias on'
244
245 print ' waiting 45sec for the current control to stabilize...'
246 time.sleep(45.)
247 print ' ... done, bias on'
248
249
250 # the feedback should be in state 'CurrentCtrlIdle'(9) now since 30.05.12
251 # if feedback.stn != 9:
252 # print "feedback is in state:", feedback.sts , "(", feedback.stn, ")"
253 # print "but is should be in state CurrentCtrlIdle (9)"
254 # print "aborting"
255 # return
256
257
258
259def WaitForTracking( verbose = False):
260 """ Wait for drivectrl to reply that its tracking the given source
261 """
262 if verbose:
263 print "...waiting for DRIVE_CONTROL"
264 print " to be in state 7: Moving"
265 drive_control.wait(7)
266 if verbose:
267 print "...moving"
268
269 if verbose:
270 print "...waiting for DRIVE_CONTROL"
271 print " to be in state 8: Tracking"
272 drive_control.wait(8)
273 if verbose:
274 print "...tracking requested wobble position"
275
276 if verbose:
277 print "waiting 10 sec for drive to calm down"
278 print "and tracking beeing stable"
279 time.sleep(10)
280
281
282
283def Take( time, num_events, runtype, verbose=False):
284 """ more general version of e.g. TakePedestalOnRun
285 Note: One has to check, if Ready for *Take* by oneself
286 """
287 runtype += '\0'
288
289 if verbose:
290 print ' taking', runtype,'. ', num_events, 'events in', time, 'seconds'
291 mcp.start(time, num_events, runtype)
292
293 if verbose:
294 print '...waiting for FAD to be in state 8: Writing Data'
295 fad_control.wait(8) # Writing Data
296 if verbose:
297 print '...waiting for FAD to be in state 4: Connected'
298 fad_control.wait_nice(4) # Connected
299 if verbose:
300 print '... done'
301
302
303def TakeData():
304 """ taking a Data Set (1x Pedestal On, 1x LPext, 4x5min DataRun)
305 """
306 # take a Pedestal run
307 IsReadyForDataTaking()
308 print 'taking External Light Pulser with BIAS on 1000 ...'
309 Take(-1, 1000, 'pedestal')
310
311 # take a ExtLP run
312 IsReadyForDataTaking()
313 print 'taking External Light Pulser with BIAS on 1000 ...'
314 Take(-1, 1000, 'light-pulser-ext')
315
316 #Data Taking with Full Trigger Area (4x5min)
317 for run in range(4):
318 print 'taking data run', run+1, 'out of 4'
319 IsReadyForDataTaking()
320 Take( 300, -1, 'data')
321
322def TakeDrsCalibration():
323 """ script for DRS-Calibration before Data taking
324 """
325 print 'script for DRS-Calibration before Data taking'
326 print 'starting up...'
327
328 feedback.enable_output(1)
329 # Making sure bias is off, before the DRS calibration starts
330 bias_control.set_zero_voltage()
331 print '...ramping Voltage down'
332 print ' ...waiting for BIAS to be in state 7: Voltage Off'
333 bias_control.wait(7) #VoltageOff
334 print '...BIAS voltage is switched off'
335
336 # starting the DRS calibration
337 fad_control.start_drs_calibration()
338
339 # taking first DRS:Pedestal with 1000 Events and ROI 1024
340 print 'taking DRS:Pedestal 1000 ...'
341 fad_control.wait(4) #Connected
342 Take(-1, 1000, 'drs-pedestal')
343
344 # taking DRS:Gain with 1000 Events and ROI 1024
345 print ' taking DRS:Gain 1000 ...'
346 fad_control.wait(4) #Connected
347 Take(-1, 1000, 'drs-gain')
348
349 # taking DRS:Pedestal 1000 Events and ROI 1024
350 print 'taking DRS:Pedestal 1000 ...'
351 fad_control.wait(4) #Connected
352 Take(-1, 1000, 'drs-pedestal')
353
354 print ' ... done'
355
356 # taking again a DRS:Pedestal with 1000 Events and ROI 1024 for a crosscheck of calculated calibrations constants
357 print ' taking crosscheck DRS:Pedestal 1000 ...'
358 fad_control.set_file_format(2)
359 fad_control.wait(4) #Connected
360 Take(-1, 1000, 'drs-pedestal')
361
362
363 # taking DRS:Time with 1000 Events and ROI 1024
364 print ' taking DRS:Time 1000 ...'
365 fad_control.wait(4) #Connected
366 Take(-1, 1000, 'drs-time')
367
368
369 # taking DRS:Time upshifted 1000 Events and ROI 1024
370 print 'taking DRS:Time upshifted 1000 ...'
371 fad_control.wait(4) #Connected
372 Take(-1, 1000, 'drs-time-upshifted')
373
374 # taking a Pedestal with 1000 Events and ROI 300 for secondary baseline...
375 print 'taking Pedestal 1000 for secondary baseline... with ROI=300'
376 fad_control.reset_secondary_drs_baseline()
377 fad_control.wait(4) #Connected
378 Take(-1, 1000, 'pedestal')
379
380 # taking crosscheck Pedestal 1000 Events and ROI 300
381 print ' taking crosscheck Pedestal 1000 ...with ROI=300'
382 fad_control.set_file_format(2)
383 fad_control.wait(4) #Connected
384 Take(-1, 1000, 'pedestal')
385
386 print '----------------------------------------------------'
387 print 'This is the end of the'
388 print 'DRS-Calibration before Data taking'
389 print '----------------------------------------------------'
390
391
392def FirstDrsCalib( SkipCurrentCalib=False ):
393 """ performs the everything, which is done in the FirstDrsCalib Script as well .
394 """
395 # As a First step we want to calibrate the current, which are read from the bias crate,
396 # and not take a DRS calibration, as it is mentioned in the data taking page...
397 # so for this we should get the feedback and biasctrl programs into known states
398 # I think it is good to try a RECONNECT to the bias, and make sure the voltage is off
399 # Since we do not know, what the feedback program is doing at the moment, we should as well,
400 # tell it to keep its mouth shut ... just to be sure, we know whats going on
401 print "stopping feedback"
402 feedback.stop()
403
404 time.sleep(2)
405 # stopping should always be possible, and end in state 'Connected'(6)
406 print " ...waiting for FEEDBACK to be in state 6: Connected"
407 feedback.wait(6)
408 print "..done"
409
410 #BIAS_CONTROL/RECONNECT
411 # If we were disconnected, and this was the first try of the night, the bias_ctrl should
412 # be in state 'VoltageOff'(7) more or less immediately
413 #.s BIAS_CONTROL 3
414 #.s BIAS_CONTROL 7 5000
415 # if these assumptions are all wrong, then we might have been properly connected anyway,
416 # and just have to ramp down... lets do it, but wait forever, in case it does not work
417 print " switching off bias"
418 bias_control.set_zero_voltage()
419 time.sleep(2)
420 print " ...waiting for BIAS to be in state 7: VoltageOff"
421 bias_control.wait(7)
422 print " ...done"
423
424 if not SkipCurrentCalib:
425 # in case we reach this line, the voltages are all off, and the feedback does not do anything
426 # So lets do the current calibration, therefor we tell the bias crate to ramp up just 1 single DAC count(~22mV)
427 # the result of this action is, to get bias_ctrl into the state 'VoltageOn'(9), but since we only go one DAC count it shouldn't take long
428 print " setting bias globally to 1 DAC"
429 bias_control.set_global_dac(1)
430
431 time.sleep(2)
432 print " ...waiting for BIAS to be in state 9: VoltageOn"
433 bias_control.wait(9)
434 print " ...done"
435
436 # now we may tell the feedback program to calibrate the currents ...
437 # I do not understand, if I have to explicitely allow the feedback program to generate output,
438 # or if it just produces output...
439 # As far as I understand, the feedback output enable status is the same,
440 # as it was before I send the STOP command... so it is unknown at this point.
441 # and in addition enabling or disabling the output, when STOPed is not possible as far as I know...
442 # I try to enable it anyway.
443 print " enabling output for feedback"
444 feedback.enable_output(1)
445 time.sleep(2)
446 print " ...done"
447
448 print " calibrating bias crate current readings..."
449 feedback.calibrate_currents()
450 time.sleep(5)
451 # in order to find out when the calibration ends, we have to wait for the transistion from state
452 # 'Calibrating'(13) back to 'Connected'(6)
453 print " ...waiting for FEEDBACK to be in state 13: Calibrating"
454 feedback.wait(13)
455 print " ...waiting for FEEDBACK to be in state 6: Connected"
456 feedback.wait(6)
457
458 # Thomas Bretz told me, that the feedback, after this is step has disabled its output
459 # and is in the mode, we might call 'temperature control' even there is no temerature beeing controlled.
460 # I don't know where the voltage is ... in order to perform the calibration, the feedback had to
461 # ramp up to 2V below the operational voltage, i.e. about 1V below the breakdown voltage
462
463 # We want to take a DRS amplitude calibration so we have to ramp down the bias voltage.
464 # this 10sec wait is needed in order for the bias not to disconect all the time...
465 print " ... current calibration done"
466 time.sleep(10)
467
468 print " switching off bias"
469 bias_control.set_zero_voltage()
470 time.sleep(5)
471 print " ...waiting for BIAS to be in state 7: VoltageOff"
472 bias_control.wait(7)
473 print " ...done"
474
475 # So now we can take the 3 runs, which are called DRS amplitude calibration:
476 # A pedestal run with ROI=1024
477 # A gain calibration run with ROI=1024
478 # and a second pedestal run, with the same ROI as our next data will be, i.e. ROI=300 in this case
479 print "taking DRS:Pedestal 1000 ..."
480 print "==================================================="
481 print "OPERATOR: "
482 print "observe Events tab and make sure there are no patches "
483 print "with strange behaviour, which can be caused "
484 print "by DRS-CHIP Problems"
485 print "==================================================="
486
487 fad_control.start_drs_calibration()
488 time.sleep(0.5)
489 Take( -1, 1000, 'drs-pedestal')
490
491 print "taking DRS:Gain 1000 ..."
492 Take( -1, 1000, 'drs-gain')
493
494 time.sleep(2)
495 if GetDrsCalibGainRms():
496 print
497 print 'First DRS Calib Script will be aborted'
498 print 'operator, please power cycle FACT'
499 return False
500
501 print "taking Pedestal 1000 ..."
502 Take( -1, 1000, 'pedestal')
503
504 # okay this is the DRS calibration for the next few runs.
505 # we are now asked to take again a pedestal run, which can be used, to
506 # calculate the electronics noise for instance ... since the shutter is closed and the
507 # voltage is off .. there should not be alot of signal in it :-)
508 print "taking crosscheck Pedestal 1000 ..."
509 fad_control.set_file_format(2)
510 Take(-1, 1000, 'pedestal')
511
512 # now we want to take a run, with dark counts events
513 # so we need to ramp up the voltage
514 # we want to use the 'current control' more so we give the commands for this...
515 print "switching on current controll feedback ..."
516 feedback.stop()
517 feedback.start_current_control(0.0)
518 feedback.enable_output(1)
519 # the feedback should be in state 'CurrentControl'(12) now
520 # the feedback should be in state 'CurrentCtrlIdle'(9) now since 30.05.12
521 print "...waiting for FEEDBACK to be in state 9: CurrentCtrlIdle"
522 feedback.wait(9)
523 print "... done"
524 print "switching on bias"
525 # now we give the feedback a hint, that it may ramp ...
526 bias_control.set_global_dac(1)
527 # after this command the bias_ctrl should be in state 'VoltageOn'(9) after a second or so
528 print "...waiting for BIAS to be in state 9: VoltageOn"
529 bias_control.wait(9)
530 print "...1 DAC globally set"
531 # then usually it takes some time until the feedback has enough information to really start controlling the voltage
532 # when the feedback actually kicks in, the bias is first in state 'Ramping'(5) for some seconds and finally in 'VoltageOn'(9)
533 # again
534 print "...waiting for BIAS to be in state 5: Ramping"
535 bias_control.wait(5)
536 print "...ramping to nominal voltage"
537 print "...waiting for BIAS to be in state 9: VoltageOn"
538 bias_control.wait(9)
539 print "...bias on"
540 # here we should wait 45 sec in order for the current control to get enough current readings and temp readings to stabilize..
541 print "waiting 45sec for the current control to stabilize..."
542 time.sleep(45)
543 print "... done"
544
545 # so now we can take the dark count run ...
546 # this might be changed in the future ... either the number of events or the the ROI might be changed
547 # then the DRS calibration above, and the pedestal run in between have to be changed as well.
548 print "taking Pedestal with BIAS on 3000 ..."
549 Take(-1, 3000, 'pedestal')
550
551 # at the end the bias voltage should be ramped down, since in a few seconds a shifter wit ha flashlight
552 # will come out to open the shutter...
553 print "switching OFF bias ..."
554 bias_control.set_zero_voltage()
555 print "...waiting for BIAS to be in state 7: VoltageOff"
556 bias_control.wait(7)
557 print "...done"
558 print "This is the end of First DRS Calibration"
559 print "----------------------------------------------------"
560 print ">"
561
562def Ratescan( ra=None, dec=None, sourcename=None):
563 """
564# call it by: .x ScriptsForDimCtrl/Ratescan.dim mode=<trackmode> ra=<Right ascension> dec=<Declination> source=<source_name>
565# mode=0: Manual tracking Mode: set tracking in drivectrl manually
566# mode=1: Coordinate Mode: scripts sends tracking command to drivectrl with the given RaDec coordinates
567# mode=2: source Mode: scripts sends tracking command to drivectrl with the given source_name
568
569 """
570 print '======================================'
571 print 'RATESCAN'
572 print '======================================'
573 print 'Preparing Drive'
574
575
576 if None == ra and None == dec and None == sourcename:
577 print 'Manual tracking Mode'
578 print '---------------------'
579 print 'OPERATOR'
580 print 'change tracking in drivectrl manually'
581 print 'script will wait for drive'
582 print 'to be in state tracking'
583 elif None != ra or None != dec:
584 try:
585 ra = float(ra)
586 dec = float(dec)
587 except TypeError:
588 raise
589
590 print '...stop tracking'
591 StopTracking()
592 print '...change tracking of telescope to:'
593 print '...Ra = ', ra
594 print '...Dec = ', dec
595 drive_control.track( ra, dec)
596 elif None != sourcename:
597 print '...stop tracking'
598 StopTracking()
599 print '...change tracking of telescope to:', sourcename
600 sourcename += '\0'
601 drive_control.track_source( 0, 0, sourcename)
602 else:
603 print 'type(ra)', type(ra), '\tra', ra
604 print 'type(dec)', type(dec), '\tdec', dec
605 print 'type(sourcename)', type(sourcename), '\tsourcename', sourcename
606 raise ValueError('RateScan does not know what to do with its parameters. Bug!')
607 return False
608
609 IsReadyForDataTaking()
610
611 print 'Starting Ratescan'
612 print '...waiting for Ratescan'
613 print ' to be in state 4: Connected'
614
615
616 if not rate_scan.wait(4, timeout=5.): #Connected
617 # we went into timeout!
618 print 'Rate_Scan not in correct state'
619 print 'OPERATOR:'
620 print '+ check connection to ftm control'
621 print 'we went into to 5sec. timeout while waiting for RATE_SCAN to be in state Connected'
622 print 'aborting'
623 return False
624
625 rate_scan.start_threshold_scan( 50, 1000, -10)
626 if not rate_scan.wait( 6, timeout=10.): # Statename???
627 # we went into timeout
628 print 'ratescan not started'
629 print 'we went into 10sec. timeout while waiting for RATE_SCAN to start the Scan'
630 print 'aborting'
631 return False
632
633 print '...processing ratescan'
634 if not rate_scan.wait( 4, timeout=2700.): # Connected
635 # we went into timeout
636 print 'we went into 2700sec. timeout while waiting for RATE_SCAN to finish'
637 print 'aborting'
638 return False
639
640 print 'Ratescan finished successfully'
641 return True
642
643
644def ResetCrate( crate_num ):
645 """ Reset Crate
646 crate_num = 0,1,2 or 3 the number of the crate to reset.
647 crate_num = 'all' is NOT YET SUPPORTED
648 """
649 c = int(crate_num)
650
651 print '======================================'
652 print 'Crate-Reset for crate ', c
653 print '======================================'
654
655 print '...resetting MCP'
656 mcp.reset()
657 time.sleep(5.)
658 print '...diconnecting FAD boards of crate ', c
659 FadDisconnectCrate( c )
660 time.sleep(2.)
661
662 print '...disconnecting All FTUs'
663 ftm_control.enable_ftu( -1, 0) # -1 for all, and 0 for False
664 time.sleep(2.)
665
666 print '...checking state of FTM_Control'
667 print '...waiting for state 3: Idle'
668 if not ftm_control.wait(3, 2.): # Idle
669 print '...stopping trigger'
670 ftm_control.stop_trigger()
671 ftm_control.wait(3) # wait for Idle endlessly
672
673 print '...resetting crate'
674 ftm_control.reset_crate( c )
675 time.sleep(2.)
676
677 print '...connecting All FTUs'
678 ftm_control.enable_ftu( -1, 1) # -1 for all, and 1 for yes, or True
679 time.sleep(4.)
680
681 print '...pinging FTUs'
682 ftm_control.ping()
683
684 print '...connecting FAD boards of crate', c
685 FadConnectCrate(c)
686 print '======================================'
687 print 'Crate-Reset for crate', c, 'finished'
688 print '======================================'
689
690def GetDrsCalibGainRms():
691#drs_calibration(self) method of factdimserver.FAD_CONTROL instance
692#DESC in SERVICE_DESC is empty ?!
693#I:1;I:3;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:163840;F:163840
694 data = fad_control.drs_calibration()
695 N1 = data[0]
696 N3 = data[1:4]
697 OffsetMean = np.array(data[4:4+1024*1440]).reshape(1440,1024)
698 OffsetRms = np.array(data[4+1024*1440:4+1024*1440*2]).reshape(1440,1024)
699 GainMean = np.array(data[4+1024*1440*2:4+1024*1440*3]).reshape(1440,1024)
700 GainRms = np.array(data[4+1024*1440*3:4+1024*1440*4]).reshape(1440,1024)
701
702 gr = GainRms.mean(axis=1)
703 lala = np.zeros(len(gr)/9)
704 for i,v in enumerate(lala):
705 lala[i] = gr[i*9:(i+1)*9].mean()
706
707 # outliers
708 mean = lala.mean()
709 std = lala.std()
710
711 print 'Mean DRS GainRms value:', mean
712 print 'std:', std
713 outs = np.where( lala > mean+7*std)[0]
714 if len(outs) > 0:
715 print 'WARNING possible DRS underflow detected!!!'
716 for out in outs:
717 out = int(out)
718 crate= out/40
719 board = (out-40*crate)/4
720 chip = out-40*crate-4*board
721 print 'possible DRS underflow in DRS:', crate, board, chip, '--> Mean-GainRms:', lala[out]
722 return outs
723 else:
724 return False
725
726
727
728
729def GetBiasCalibration():
730 cali = feedback.calibration()
731 bias_calibration['Time'] = time.time()
732 bias_calibration['Calibration'] = cali
733
734def GetBiasCurrent(verbose = False):
735 """ return median, std, max and min current
736 """
737 if 'Time' in bias_calibration:
738 cali = bias_calibration['Calibration']
739 else:
740 GetBiasCalibration()
741 cali = bias_calibration['Calibration']
742
743 r = np.array(cali[2*416:2*416+320])
744
745 bias = bias_control
746
747 I = np.array(bias.current()[0:320], dtype=float)
748 II = I/4096. * 5000
749 V = np.array(bias.voltage()[0:320])
750 if len(sys.argv) > 1:
751 i = int(sys.argv[1])
752 else: i=0
753# print 'I:', I[i], 'dac\t', II[i], 'uA'
754# print 'V:', V[i]
755# print ''
756# print 'GUI offset:', V[i]/r[i]*1e6
757# print 'GUI feedback:', II[0] - V[0]/r[0]*1e6
758
759 GUII = II-V/r*1e6
760 if verbose:
761 print 'median', np.median(GUII)
762 print 'mean', GUII.mean()
763 # print 'rms', ((GUII- GUII.mean())**2).sum()
764 print 'std', GUII.std()
765 print 'max', GUII.max()
766 print 'min', GUII.min()
767
768 return GUII.mean(), GUII.std(), GUII.max(), GUII.min()
769
770
771
772
773def TrackSourceWobbleX( sourcename, wobble_pos ):
774 """ general Tracking function
775 """
776 wp = int(wobble_pos)
777 if wp != 1 and wp != 2:
778 raise ValueError('wobble_pos *must* be 1 or 2')
779
780 if sourcename not in sourcedict:
781 print sourcedict.keys()
782 raise ValueError('sourcename: '+ sourcename +' must be in sourcedict.')
783
784 print 'moving telescope to wobble position ', wp, 'of ', sourcename
785 print '...waiting for DRIVE_CONTROL'
786 print ' to be in state 6: Armed'
787 drive_control.stop()
788 drive_control.wait(6) #Armed
789 print 'DRIVE: ARMED'
790 time.sleep(5.)
791
792 wobble_offset = sourcedict[sourcename]['wobble_offset']
793 wobble_angle = sourcedict[sourcename]['wobble_angle'+str(wp)]
794 sourcename += '\0'
795
796 last_drive_method = drive_control.track_source
797 last_drive_kwargs = { 'wobble_offset' : wobble_offset,
798 'wobble_angle' : wobble_angle,
799 'source_name' : sourcename }
800 kwa = last_drive_kwargs
801 last_drive_method(kwa['wobble_offset'], kwa['wobble_angle'], kwa['source_name'])
802
803 print '... done'
804
805source_list = [
806 ['Crab', 0.6 , 50, -130],
807 ["1ES 2344+51.4", 0.6 , 90, -90 ],
808 ["Mrk 501", 0.6, -22, -22+180 ],
809 ["Mrk 421", 0.6, 90, -90 ],
810 ["1ES 1218+304", 0.6, -5, -5+180 ],
811 ["1ES 1959+650", 0.6, 155, 155-180 ],
812 ["Dark Patch 2", 0.6 , 90, -90 ],
813 ["Dark Patch 3", 0.6 , 90, -90 ],
814 ["H 1426+428", 0.6 , 90, -90 ],
815 ["IC 310", 0.6, -18, -18+180 ],
816 ["PKS 2155-304", 0.6, 90, -90 ] ]
817
818sourcedict = {}
819
820def make_sourcedict():
821 for s in source_list:
822 sourcedict[s[0]] = {}
823 sourcedict[s[0]]['wobble_offset'] = s[1]
824 sourcedict[s[0]]['wobble_angle1'] = s[2]
825 sourcedict[s[0]]['wobble_angle2'] = s[3]
826
827
828def TakeSource( name ):
829 if name not in sourcedict:
830 print name, 'not in dict of sources, possible sources are:'
831 print sorted(sourcedict.keys())
832 raise ValueError('wrong source name')
833
834 TrackSourceWobbleX( name, 1) # Track Wobble pos 1 of source
835 WaitForTracking()
836 # Take a DRS-Calibration before beginning to take physics Data
837 TakeDrsCalibration()
838 # check feedback state before switching BIAS ON and ramping up to nominal Voltage
839 PrepareBiasForDataTaking()
840 # taking a Data Set (1x Pedestal 1000 Bias On, 1x LPext 1000, 4x5min DataRun)
841 TakeData()
842 StopTracking()
843
844 TrackSourceWobbleX( name, 2) # Track Wobble pos 2 of source
845 WaitForTracking()
846 # taking a Data Set (1x Pedestal 1000 Bias On, 1x LPext 1000, 4x5min DataRun)
847 TakeData()
848
849 StopTracking()
850
851
852
853
854
855
856def PrintCurrents( x = 1.0):
857 while True:
858 time.sleep( x )
859 print time.strftime('%d %b %Y %H:%M:%S UTC', time.gmtime()), GetBiasCurrent()
860
861
862if __name__ == '__main__':
863 print 'Welcome to PyDimCtrl'
864 make_sourcedict()
865 print
866 print 'possible sources:'
867 print sorted( sourcedict.keys() )
Note: See TracBrowser for help on using the repository browser.