-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyse_uao.py
executable file
·1210 lines (966 loc) · 38.9 KB
/
analyse_uao.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import csv
import os, time, getopt, sys, calendar, re
import operator, argparse
from functools import reduce
from collections import defaultdict
parser = argparse.ArgumentParser('UAO log analysis')
parser.add_argument('day', type=str, help='UTC date YYYYMMDD format')
parser.add_argument('side', type=str, help='side (R or L)')
parser.add_argument('logdir', type=str, help='directory where log files are stored')
parser.add_argument('--wfs', type=str, default='FLAO', help='WFS type: FLAO, LBTI, etc')
parser.add_argument('--mode', type=str, default='FLAOAO', help='WFS mode: FLAOAO, FLAOTT, etc')
parser.add_argument('--start_time', type=str, default='02:00:00', help='Consider logs starting at this time (GMT) of each day')
parser.add_argument('--end_time', type=str, default='12:00:00', help='Consider logs before this time (GTM) of each day')
parser.add_argument('--html', action='store_true', default=False, help='generate html output')
parser.add_argument('--outdir', type=str, default='.', help='output directory for csv files (default: %(default)s)')
parser.add_argument('--verbose', action='store_true', default=False, help='verbose output')
args = parser.parse_args()
repeatedErrors = defaultdict(int)
if args.side not in ['R', 'L']:
print()
print('Argument --side must be L or R')
print()
sys.exit(2)
def logfilename(process, num=0):
y = args.day[0:4]
m = args.day[4:6]
d = args.day[6:8]
path = '%s/%s/%s/%s.%s.%s%04d.log' % (y, m, d, process, args.side, args.day, num)
return os.path.join(args.logdir, path)
def log_timestamp(line):
fields = line.split('|')
timestamp, microsec = fields[3].split('.')
return calendar.timegm(time.strptime(timestamp, '%Y-%m-%d %H:%M:%S')) + float(microsec)/1e6
def julianDayFromUnix(timestamp):
return (timestamp / 86400.0) + 2440587.5;
def timeStr(t):
return time.strftime('%Y%m%d %H:%M:%S', time.gmtime(t))
def dayStr(t):
return time.strftime('%Y%m%d', time.gmtime(t))
def hourStr(t):
return time.strftime('%H:%M:%S', time.gmtime(t))
def logfile(name, grep=None):
''''
Returns a file-like object to read a logfile
'''
for n in range(10000):
filename= logfilename(name, num=n)
filenamegz = filename+'.gz'
if os.path.exists(filename):
if args.verbose:
print('Reading: '+filename)
if grep is not None:
cmd = 'grep "%s" %s' % (grep, filename)
return os.popen(cmd)
else:
return file(filename, 'r')
if os.path.exists(filenamegz):
if args.verbose:
print('Reading: '+filenamegz)
if grep is not None:
cmd = 'gzip -cd %s | grep "%s"' % (filenamegz, grep)
return os.popen(cmd)
else:
cmd = 'gzip -cd %s' % filenamegz
return os.popen(cmd)
raise Exception('Cannot find log file: '+filename)
def search(name, string=None, mindiff=1, getDict=False):
found = logfile(name, grep=string)
prev=0
found2={}
p = re.compile('\> \. (.*)')
for f in found:
now = log_timestamp(f)
if now-prev>= mindiff:
if not now in found2:
found2[now] = f.strip()
else:
try:
fields = f.split('|')
m = p.search(fields[4])
if m:
found2[now] += m.group(1)
except IndexError as e:
print('Malformed line: '+f)
else:
if args.verbose:
print('Rejected '+f.strip())
prev=now
if getDict:
return found2
found3 = []
for k in sorted(found2.keys()):
found3.append(found2[k])
return found3
def myRound(x, ndigits=0):
'''Returns a rounded number where -0.0 is set to 0'''
y = round(x, ndigits)
if y == 0.0: # This yields True for both 0.0 and -0.0
y = 0.0
return y
class Event:
def __init__(self, name, t, details=None):
self.name = name
self.t = t
self.details = details
def htmlHeader(self):
return '<tr><th>Timestamp</th><th>Event</th><th>Details</th></tr>'
def htmlRow(self):
return '<tr><td>%s</td><td>%s</td><td>%s</td></tr>' % ( timeStr(self.t), self.name, self.details)
class SkipFrameEvent(Event):
def __init__(self, t, details=''):
Event.__init__( self, 'SkipFrame', t, details)
@staticmethod
def fromLogLine(line):
t = log_timestamp(line)
line = line.replace('->', '--') # Avoid extra ">"
log, msg = line.split('>')
return SkipFrameEvent( t, msg)
class FailedActuatorEvent(Event):
def __init__(self, t, details, actno=None):
Event.__init__( self, 'FailedActuator', t, details)
self.actno = actno
@staticmethod
def fromLogLine(line):
t = log_timestamp(line)
pattern = 'Failing actuator detected N. (\d+)(.*)'
found = re.findall( pattern, line)
if len(found) > 0:
actno, reason = found[0]
log, msg = line.split('>')
return FailedActuatorEvent( t, 'Act: %s - %s' % (actno, msg), int(actno))
class RIPEvent(Event):
def __init__(self, t, details):
Event.__init__( self, 'RIP', t, details)
@staticmethod
def fromLogLine(line):
t = log_timestamp(line)
try:
procname, other = line.split('_')
if procname=='fastdiagn':
procname = 'FastDiagnostic'
if procname=='housekeeper':
procname = 'HouseKeeper'
return RIPEvent( t, 'Detected by %s' % procname)
except ValueError:
return RIPEvent( t, '')
class ArbCmd:
def __init__(self, name, args='', start_time=None, end_time=None, success=None, errstr=''):
self.name = name
self.args = args
self.start_time = start_time
self.end_time = end_time
self.success = success
self.wasIllegal = False
self.errstr = errstr
self.floatPattern ='[-+]?\d*\.\d+|d+'
self.wfsPattern ='wfsSpec = (\w+)WFS'
self.magPattern ='expectedStarMagnitude = (%s)' % self.floatPattern
self.refXPattern = 'roCoordX = (%s)' % self.floatPattern
self.refYPattern = 'roCoordY = (%s)' % self.floatPattern
self.modePattern = 'mode = (\w+)'
self.wfs = ''
self.mode = ''
self.mag = 0
self.faultCount = 0
self.recloseCount = 0
self.faultTimes = []
self.recloseTimes = []
self.reclosing = False
self.inFault = False
if name=='PresetAO':
_ = self.details()
def report(self):
time_str = timeStr(self.start_time)
if self.success is True:
success_str = 'Success'
elif self.success is False:
success_str = 'Failure: %s' % self.errstr
else:
success_str = 'Unknown'
return '%s %s %s' % (time_str, success_str, ' - '.join(self.details()))
def errorString(self):
try:
if self.name == 'PresetAO':
str = self.errstr
replaces = [('presetAO:', ''),
('WARNING -', ''),
('RETRY:', ''),
('(-20004) WFSARB_ARG_ERROR', ''),
('(-5001) TIMEOUT_ERROR', ''),
('(-5002) VALUE_OUT_OF_RANGE_ERROR', '')]
for r in replaces:
str = str.replace(r[0], r[1])
return str.strip()
except:
pass
return self.errstr
def is_instrument_preset(self):
if not self.name == 'PresetAO':
return False
# Make sure mag, refX and refY are there
_ = self.details()
return self.refX != 0 or self.refY != 0
def details2(self):
details2=[]
try:
if self.name == 'OffsetSequence':
details2.append('Time paused: %.1fs' % self.time_paused())
if self.name == 'ExposureSequence':
details2.append('Time exposing: %.1fs' % self.time_exposing())
except Exception as e:
print(e)
return details2
def details(self):
details=[]
try:
if self.name == 'OffsetSequence' or self.name == 'OffsetXY':
coords = map( float, re.findall( self.floatPattern, self.args))
if len(coords) ==2:
details.append('X=%.2f, Y=%.2f mm' % (coords[0], coords[1]))
if self.name == 'PresetAO':
wfs = re.findall( self.wfsPattern, self.args)[0]
mag = float(re.findall( self.magPattern, self.args)[0])
refX = float(re.findall( self.refXPattern, self.args)[0])
refY = float(re.findall( self.refYPattern, self.args)[0])
mode = re.findall(self.modePattern, self.args)[0]
details.append('%s, star mag= %.1f, posXY= %.1f, %.1f mm, mode = %s' % (wfs, mag, myRound(refX, 1), myRound(refY, 1), mode))
if hasattr(self, 'intervention'):
if self.intervention == True:
interventionDesc = 'Intervention mode'
else:
interventionDesc = 'Automatic mode'
else:
interventionDesc = 'Intervention/automatic mode unknown'
self.wfs = wfs
self.mag = mag
self.refX = refX
self.refY = refY
self.mode = mode
details.append(interventionDesc)
if self.name == 'CompleteObs':
tt = self.total_time()
ot = self.total_open_time()
if tt != 0:
per = ot*100/tt
else:
per = 0
s = '%s, open shutter: %ds (%d%%)' % (self.wfs, ot, per)
details.append(s)
if self.name == 'Acquire':
details=[]
if hasattr(self, 'estimatedMag'):
details.append('Estimated magnitude: %.1f' % self.estimatedMag)
if hasattr(self, 'hoBinning'):
details.append('Ccd39 binning: %d' % self.hoBinning)
if hasattr(self, 'hoSpeed'):
details.append('Loop speed: %d Hz' % self.hoSpeed)
# print 'Loop speed: %d Hz' % self.hoSpeed
except Exception as e:
print(e)
return details
def get_AOARB_cmds():
import re
lines = search('AOARB', string='MAIN', mindiff=0)
cmds=[]
curCmd=None
startCmdFlao = 'FSM (status'
startCmdUao = 'Request:'
p1 = re.compile('Request: (.*?)\((.*)\)')
p2 = re.compile('Request: (.*)')
p3 = re.compile('has received command \d+ \((.*)\)') # FLAO command
endCmdFlao = ' successfully completed'
endCmdUao = 'Status after command:'
exceptionStr = '[AOException]'
illegalCmdStr = 'Illegal command for state'
loopFaultStr = 'Loop fault detected. Opening loop.'
recloseStr = 'Request: ReCloseLoop()'
closedStr = 'Status after command: AOArbitrator.LoopClosed'
retryCmdStr = 'Optical gain is not one'
interventionStr = 'Intervention:'
readyForStartStr = 'Status after command: AOArbitrator.ReadyForStartAO'
estimatedMagStr = 'Estimated magnitude from ccd39: '
hoBinningStr = 'HO binning : '
hoSpeedStr1 = 'HO speed : '
hoSpeedStr2 = 'Updating from WFS preset: freq= '
lastAcquireRef=None
lastPreset=None
lastErrorTime=None
for line in lines:
try:
if ((startCmdFlao in line) or (startCmdUao in line)) and (recloseStr not in line):
# Skip this command, that has no effect on FSM
if 'getLastImage' in line or recloseStr in line:
continue
if curCmd is not None:
if lastPreset:
curCmd.wfs = lastPreset.wfs
curCmd.mode = lastPreset.mode
curCmd.mag = lastPreset.mag
cmds.append(curCmd)
curCmd = None
t = log_timestamp(line)
m1 = p1.search(line)
m2 = p2.search(line)
m3 = p3.search(line)
args = ''
try:
if m1:
name = m1.group(1)
args = m1.group(2)
elif m2:
name = m2.group(1)
elif m3:
name = m3.group(1)
else:
print('Malformed request: '+line)
continue
except IndexError as e:
print('Malformed request: '+line)
continue
default_success = None
curCmd = ArbCmd(name=name, args=args, start_time=t, end_time=None, success=default_success, errstr='')
if name == 'PresetAO':
lastPreset = curCmd
if name == 'AcquireRefAO':
lastAcquireRef = curCmd
elif ((endCmdFlao in line) or (endCmdUao in line)) and not curCmd.reclosing:
t = log_timestamp(line)
curCmd.end_time = t
curCmd.success = True
curCmd.errstr = ''
if readyForStartStr in line:
if lastAcquireRef is not None:
lastAcquireRef.end_time = t
continue # TODO remove?
elif exceptionStr in line:
pos = line.index(exceptionStr)
curCmd.errstr = line[pos+len(exceptionStr):].strip()
curCmd.success = False
if lastErrorTime:
if retryCmdStr in line:
if curCmd.start_time - lastErrorTime < 30:
lastErrorTime = curCmd.start_time
repeatedErrors[curCmd.name] += 1
curCmd = None
continue
# curCmd.success = True
lastErrorTime = curCmd.start_time
elif illegalCmdStr in line:
pos = line.index(illegalCmdStr)
curCmd.errstr = line[pos:].strip()
curCmd.success = False
curCmd.wasIllegal = True
elif loopFaultStr in line:
if not curCmd.inFault:
t = log_timestamp(line)
curCmd.faultCount += 1
curCmd.inFault = True
curCmd.faultTimes.append(t)
elif recloseStr in line:
if curCmd.inFault:
curCmd.reclosing = True
elif closedStr in line:
if curCmd.reclosing:
t = log_timestamp(line)
curCmd.recloseCount += 1
curCmd.recloseTimes.append(t)
curCmd.inFault = False
curCmd.reclosing = False
# Detect intervention mode in Presets
elif interventionStr in line:
pos = line.index(interventionStr) + len(interventionStr)
interv = line[pos+1:pos+6]
if interv[0:4] == 'True':
curCmd.intervention=True
else:
curCmd.intervention=False
# Detect magnitude estimation in AcquireRef
elif estimatedMagStr in line:
pos = line.index(estimatedMagStr)
curCmd.estimatedMag = float(line[pos+len(estimatedMagStr):])
elif hoBinningStr in line:
pos = line.index(hoBinningStr)
curCmd.hoBinning = int(line[pos+len(hoBinningStr):])
elif hoSpeedStr1 in line:
pos = line.index(hoSpeedStr1)
curCmd.hoSpeed = float(line[pos+len(hoSpeedStr1):])
elif hoSpeedStr2 in line:
pos = line.index(hoSpeedStr2)
curCmd.hoSpeed = float(line[pos+len(hoSpeedStr2):])
except Exception as e:
#if args.verbose:
print(e)
# Store last command
if curCmd is not None:
if lastPreset:
curCmd.wfs = lastPreset.wfs
curCmd.mode = lastPreset.mode
curCmd.mag = lastPreset.mag
cmds.append(curCmd)
return cmds
class OffsetSequence(ArbCmd):
'''A Pause - Offset - Resume sequence'''
def __init__(self, pause, resume, **kwargs):
ArbCmd.__init__(self, name='OffsetSequence', **kwargs)
self.pause = pause
self.resume = resume
def time_paused(self):
if self.pause and self.resume:
return self.resume.start_time - self.pause.end_time
else:
return 0
class ExposureSequence(ArbCmd):
'''A scientific exposure between Resume and Pause'''
def __init__(self, resume, pause, **kwargs):
ArbCmd.__init__(self, name='ExposureSequence', **kwargs)
self.pause = pause
self.resume = resume
def time_exposing(self):
if self.pause and self.resume:
return self.pause.start_time - self.resume.end_time
else:
return 0
class CompleteObs(ArbCmd):
def __init__(self, *args, **kwargs):
ArbCmd.__init__(self, *args, **kwargs)
self.cmds = []
def total_closed_time(self):
tTime = 0
last_closed_time = self.startAOtime
t2 = None
#print self.startAOtime
#print self.recloseTimes
#print self.faultTimes
for t1, t2 in zip(self.faultTimes, self.recloseTimes):
tTime += t1 - last_closed_time
last_closed_time = t2
if len(self.faultTimes)==len(self.recloseTimes) and t2:
tTime += self.end_time - t2
if tTime==0:
tTime = self.total_time()
return tTime
def total_time(self):
'''Total observation time from start of PresetAO to end of StopAO'''
if self.end_time is None or self.start_time is None:
return 0
return self.end_time - self.start_time
def total_open_time(self):
'''Total time available from instrument'''
return self.total_time() - self.setup_duration() - self.offsets_overhead()
def setup_duration(self):
'''Total setup time from start of PresetAO to end of StartAO'''
startao = list(filter(lambda x: x.name in ['StartAO', 'Start AO'], self.cmds))[0]
return startao.end_time - self.start_time
def ao_setup_overhead(self):
'''Total AO time from start of PresetAO to end of StartAO'''
ao_time = 0
is_intervention = False
for cmd in self.cmds:
if cmd.name in 'CenterStar CenterPupils CheckFlux CloseLoop'.split():
is_intervention = True
for cmd in self.cmds:
if cmd.name in 'Acquire Done'.split():
continue
if cmd.end_time is None or cmd.start_time is None:
continue
if is_intervention and cmd.name == 'AcquireRefAO': # Avoid double counting acquisition commands
continue
if cmd.end_time is not None and cmd.start_time is not None:
this_cmd_time = cmd.end_time - cmd.start_time
ao_time += this_cmd_time
if cmd.name in ['StartAO', 'Start AO']:
return ao_time
return 0
def telescope_overhead(self):
'''Telescope overhead during setup time'''
return self.setup_duration() - self.ao_setup_overhead()
def offsets_overhead(self):
'''Time spent executing offsets'''
offsets_time = 0
for cmd in self.cmds:
if cmd.name in ['PauseAO', 'Pause']:
pause_time = cmd.start_time
if cmd.name in ['ResumeAO', 'Resume']:
resume_time = cmd.end_time
offsets_time += resume_time - pause_time
return offsets_time
def total_ao_overhead(self):
'''Time spent executing AO commands'''
return self.ao_setup_overhead() + self.offsets_overhead()
def detectCompleteObs(cmds):
'''
Detect a complete observations series: PresetAO, Acquire,
StarAO, and Stop.
'''
inObs = False
inPreset = False
newCmds = []
for cmd in cmds:
if cmd.name == 'PresetAO' and cmd.is_instrument_preset():
inPreset = True
inObs = False
obsCmd = CompleteObs(name='CompleteObs', start_time = cmd.start_time)
obsCmd.wfs = cmd.wfs
obsCmd.mode = cmd.mode
obsCmd.mag = cmd.mag
obsCmd.cmds.append(cmd)
obsCmd.hoSpeed = 0
if hasattr(cmd, 'hoSpeed'):
obsCmd.hoSpeed = cmd.hoSpeed
elif inPreset is True and cmd.name == 'Cancel':
inPreset = False
inObs = False
elif inObs is True:
obsCmd.cmds.append(cmd)
obsCmd.faultCount += cmd.faultCount
obsCmd.recloseCount += cmd.recloseCount
obsCmd.faultTimes += cmd.faultTimes
obsCmd.recloseTimes += cmd.recloseTimes
if hasattr(cmd, 'hoSpeed'):
obsCmd.hoSpeed = cmd.hoSpeed
if cmd.name in ['Stop', 'StopAO']:
obsCmd.end_time = cmd.end_time
# If we are here, we got a PresetAO, a startAO and a stopAO
# so we declare it a success
obsCmd.success = True
newCmds.append(obsCmd)
inObs = False
elif inPreset is True:
obsCmd.cmds.append(cmd)
if cmd.name in ['StartAO', 'Start AO']:
inPreset = False
inObs = True
obsCmd.startAOtime = cmd.start_time
if hasattr(cmd, 'hoSpeed'):
obsCmd.hoSpeed = cmd.hoSpeed
newCmds.append(cmd)
return newCmds
def detectAcquires(cmds):
'''
Detect sequences of AcquireRefAO and subcommands and group them
into a meta 'Acquire' command
'''
newCmds = []
inAcquire = False
for cmd in cmds:
if cmd.name == 'AcquireRefAO':
inAcquire = True
acquireCmd = ArbCmd(name='Acquire', start_time=cmd.start_time)
acquireCmd.success = cmd.success
acquireCmd.errstr = cmd.errstr
acquireCmd.wfs = cmd.wfs
acquireCmd.mode = cmd.mode
acquireCmd.mag = cmd.mag
acquireDone = False
elif inAcquire is True:
if cmd.name == 'CheckFlux':
# acquireCmd.success = acquireCmd.success and cmd.success
acquireCmd.errstr += cmd.errstr
acquireCmd.end_time = cmd.end_time
if hasattr(cmd, 'estimatedMag'):
acquireCmd.estimatedMag = cmd.estimatedMag
if hasattr(cmd, 'hoBinning'):
acquireCmd.hoBinning = cmd.hoBinning
if hasattr(cmd, 'hoSpeed'):
acquireCmd.hoSpeed = cmd.hoSpeed
elif cmd.name in \
['CenterPupils', 'CenterStar', 'CloseLoop', 'OptimizeGain',
'ReCloseLoop', 'getLastImage', 'ApplyOpticalGain']:
# acquireCmd.success = acquireCmd.success and cmd.success
acquireCmd.errstr += cmd.errstr
acquireCmd.end_time = cmd.end_time
elif cmd.name == 'Done':
acquireCmd.success = acquireCmd.success and cmd.success
acquireCmd.errstr += cmd.errstr
acquireCmd.end_time = cmd.end_time
acquireDone = True
if cmd.success==True:
newCmds.append(acquireCmd)
inAcquire=False
else:
if not acquireDone:
acquireCmd.success = False
acquireCmd.errstr += ' Command not completed'
newCmds.append( acquireCmd)
inAcquire = False
newCmds.append(cmd)
return newCmds
def detectOffsets(cmds):
'''
Detect Pause-Offset-Resume sequences and build a meta 'Offset' command
for each of them.
'''
if len(cmds)<3:
return cmds
newCmds = []
for n in range(len(cmds)-2):
if cmds[n].name == 'Pause' and \
cmds[n+1].name == 'OffsetXY' and \
cmds[n+2].name == 'Resume':
t0 = cmds[n+0].start_time
t1 = cmds[n+2].end_time
success = reduce(operator.and_, [x.success for x in cmds[n:n+3]])
errstr = ' '.join([x.errstr for x in cmds[n:n+3]])
args = cmds[n+1].args
cmd = OffsetSequence(cmds[n], cmds[n+2], args=args, start_time=t0, end_time=t1, success=success, errstr=errstr)
newCmds.append(cmd)
elif cmds[n].name == 'Pause' and \
cmds[n+1].name == 'OffsetXY':
# Last command is not a Resume
t0 = cmds[n+0].start_time
t1 = cmds[n+1].end_time
success = reduce(operator.and_, [x.success is True for x in cmds[n:n+2]])
errstr = ' '.join([x.errstr for x in cmds[n:n+2]])
args = cmds[n+1].args
if success:
success = False
errstr = 'Resume was not sent'
cmd = OffsetSequence(cmds[n], None, args=args, start_time=t0, end_time=t1, success=success, errstr=errstr)
newCmds.append(cmd)
elif cmds[n].name == 'Pause' and \
cmds[n+1].name == 'Resume':
t0 = cmds[n].start_time
t1 = cmds[n+1].end_time
success = cmds[n].success and cmds[n+1].success
errstr = cmds[n].errstr + ' ' + cmds[n+1].errstr
args = ''
cmd = OffsetSequence(cmds[n], cmds[n+1], args=args, start_time=t0, end_time=t1, success=success, errstr=errstr)
newCmds.append(cmd)
elif cmds[n].name == 'Pause':
# Next command is not an OffsetXY nor a Resume
t0 = cmds[n].start_time
t1 = cmds[n].end_time
success = cmds[n].success
errstr = cmds[n].errstr
args = ''
if success:
success = False
errstr = 'no OffsetXY or Resume'
cmd = OffsetSequence(cmds[n], None, args=args, start_time=t0, end_time=t1, success=success, errstr=errstr)
newCmds.append(cmd)
elif cmds[n].name == 'Resume' and \
cmds[n+1].name == 'Pause':
t0 = cmds[n].start_time
t1 = cmds[n].end_time
success = cmds[n].success and cmds[n+1].success
errstr = cmds[n].errstr + ' ' + cmds[n+1].errstr
cmd = ExposureSequence(cmds[n], cmds[n+1], args=args, start_time=t0, end_time=t1, success=success, errstr=errstr)
newCmds.append(cmd)
newCmds.append(cmds[n])
return newCmds
def cmdsByName(cmds, name):
return filter(lambda x: x.name == name, cmds)
def outputEvents(title, events, sort=True, complete_list=None):
if sort:
ev = {}
sortedEvents = []
for e in events:
ev[e.t] = e
for k in sorted(ev.keys()):
sortedEvents.append(ev[k])
else:
sortedEvents = events
if not args.html:
print()
print(title)
print('Total: %d' % len(sortedEvents))
for e in sortedEvents:
print('%s %s %s' % (timeStr(e.t), e.name, e.details))
else:
print('<HR>')
print('<H2>%s</H2>' % title)
print('<p>Total: %d</p>' % len(sortedEvents))
if len(sortedEvents)>0:
print('<table id="aotable">')
print(sortedEvents[0].htmlHeader())
for e in sortedEvents:
print(e.htmlRow())
if complete_list is not None:
complete_list[timeStr(e.t)] = e.htmlRow()
print('</table>')
else:
print('<p>')
def output_cmd(title, found, complete_list=None):
found = list(found)
success = len([f for f in found if f.success])
illegal = len([f for f in found if f.wasIllegal])
success_rate = 0
if len(found)>0:
success_rate = 0
den = float(len(found)-illegal)
if den>0:
success_rate = float(success) / den
if not args.html:
print()
print(title)
print('Total: %d - Success rate: %d%%' % (len(found), int(success_rate*100)))
for f in found:
print(f.report())
else:
print('<HR>')
print('<H2>%s</H2>' % title)
print('<p>Total: %d - Success rate: %d%%</p>' % (len(found), int(success_rate*100)))
if len(found)>0:
print('<p>')
print('<table id="aotable">')
print('<tr><th>Time</th><th>Command</th><th>Ex. time (s)</th><th style="width: 300px">Result</th><th>Details</th><th>More details</th></tr>')
for cmd in found:
strtime = timeStr(cmd.start_time)
if (cmd.end_time is not None) and (cmd.start_time is not None):
elapsed = '%5.1f s' % (cmd.end_time - cmd.start_time,)
else:
elapsed = 'Unknown'
if cmd.success is True:
errstr = 'Success'
else:
errstr = cmd.errorString()
row = '<tr><td>%s</td><td>%s</td><td>%s</td><td style="width: 300px">%s</td><td>%s</td><td>%s</td></tr>' % \
(strtime, cmd.name, elapsed, errstr, '<br>'.join(cmd.details()), '<br>'.join(cmd.details2()))
print(row)
if complete_list is not None:
complete_list[strtime] = row
if len(found)>0:
print('</table>\n')
print('</p>')
return success_rate
def update_cmd_success_csv(cmds):
csvfilename = os.path.join(args.outdir, 'cmd_succes%s.csv' % args.side)
# read csv
if os.path.exists(csvfilename):
with open(csvfilename, 'r') as csvfile:
data = list(csv.reader(csvfile, delimiter=','))
else:
data = []
cmds = list(cmds)
if len(cmds) < 1:
return
# Remove anything matching this day/cmd (assumes all cmds are equal)
data = filter(lambda row: (row[0] != args.day) or (row[2] != cmds[0].name), data)
# Remove header if any
data = list(filter(lambda row: row[0] != 'day', data))
cmd_attempts = {}
cmd_illegals = {}
cmd_success = {}
days = {}
for cmd in cmds:
cmd_attempts[cmd.name] = 0
cmd_illegals[cmd.name] = 0
cmd_success[cmd.name] = 0
# Add our data
for cmd in cmds:
if cmd.wfs != args.wfs or cmd.mode != args.mode:
continue
if cmd.wasIllegal:
cmd_illegals[cmd.name] = int(cmd_illegals[cmd.name]) + 1
continue
if cmd.end_time is None or cmd.start_time is None:
continue
if hourStr(cmd.start_time) < args.start_time or hourStr(cmd.start_time) > args.end_time:
continue
cmd_attempts[cmd.name] = int(cmd_attempts[cmd.name]) + 1
if not cmd.success:
continue
cmd_success[cmd.name] = int(cmd_success[cmd.name]) + 1
days[cmd.name] = dayStr(cmd.start_time)
for cmd in days.keys():
row = (days[cmd], cmd, cmd_attempts[cmd], cmd_success[cmd], cmd_illegals[cmd], repeatedErrors[cmd])
data.append(row)
data.sort(key=lambda x: x[0])
hdr = ('day', 'command', 'attempts', 'successes', 'illegals', 'repeatedErrors')
data = [hdr]+data
# Save csv
with open(csvfilename, 'w') as csvfile:
csv.writer(csvfile, delimiter=',').writerows(data)
def update_cmd_csv(cmds):
csvfilename = os.path.join(args.outdir, 'cmd_%s.csv' % args.side)
failedcsvfilename = os.path.join(args.outdir, 'cmd_failed_%s.csv' % args.side)
# read csv
if os.path.exists(csvfilename):
with open(csvfilename, 'r') as csvfile:
data = list(csv.reader(csvfile, delimiter=','))
else:
data = []
if os.path.exists(failedcsvfilename):
with open(failedcsvfilename, 'r') as csvfile:
dataf = list(csv.reader(csvfile, delimiter=','))
else:
dataf = []
cmds = list(cmds)
if len(cmds) < 1:
return
# Remove anything matching this day/cmd (assumes all cmds are equal)
data = filter(lambda row: (row[0] != args.day) or (row[2] != cmds[0].name), data)
# Remove header if any
data = list(filter(lambda row: row[0] != 'day', data))
# Add our data