This file is indexed.

/usr/lib/python2.7/dist-packages/sardana/macroserver/scan/gscan.py is in python-sardana 1.6.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
#!/usr/bin/env python

##############################################################################
##
## This file is part of Sardana
##
## http://www.sardana-controls.org/
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
## 
## Sardana is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## Sardana is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
## 
## You should have received a copy of the GNU Lesser General Public License
## along with Sardana.  If not, see <http://www.gnu.org/licenses/>.
##
##############################################################################

"""This module contains the class definition for the MacroServer generic
scan"""

__all__ = ["ScanSetupError", "ScanException", "ExtraData", "TangoExtraData",
           "GScan", "SScan", "CScan", "CSScan", "CTScan", "HScan"]

__docformat__ = 'restructuredtext'

import os
import datetime
import operator
import time
import threading
import numpy as np

import PyTango
import taurus
from taurus.core.util.log import Logger
from taurus.core.util.user import USER_NAME
from taurus.core.tango import FROM_TANGO_TO_STR_TYPE

from sardana.util.tree import BranchNode, LeafNode, Tree
from sardana.util.motion import Motor as VMotor
from sardana.util.motion import MotionPath
from sardana.macroserver.msexception import MacroServerException, UnknownEnv, \
    InterruptException
from sardana.macroserver.msparameter import Type
from sardana.macroserver.scan.scandata import ColumnDesc, MoveableDesc, \
    ScanFactory, ScanDataEnvironment
from sardana.macroserver.scan.recorder import OutputRecorder, JsonRecorder, \
    SharedMemoryRecorder, FileRecorder
from sardana.taurus.core.tango.sardana.pool import Ready


class ScanSetupError(Exception):
    pass


class ScanException(MacroServerException):
    pass


class ExtraData(object):
    
    def __init__(self, **kwargs):
        """Expected keywords are:
            - model (str, mandatory): represents data source (ex.: a/b/c/d)
            - label (str, mandatory): column label
            - name (str, optional): unique name (defaults to model)
            - shape (seq, optional): data shape 
            - dtype (numpy.dtype, optional): data type
            - instrument (str, optional): full instrument name"""
        self._label = kwargs['label']
        self._model = kwargs['model']
        if not kwargs.has_key('dtype'):
            kwargs['dtype'] = self.getType()
        if not kwargs.has_key('shape'):
            kwargs['shape'] = self.getShape()
        if not kwargs.has_key('name'):
            kwargs['name'] = self._model
        self._column = ColumnDesc(**kwargs)
    
    def getLabel(self):
        return self._label

    def getName(self):
        return self._label
    
    def getColumnDesc(self):
        return self._column
    
    def getType(self):
        raise Exception("Must be implemented in subclass")
    
    def getShape(self):
        raise Exception("Must be implemented in subclass")

    def read(self):
        raise Exception("Must be implemented in subclass")


class TangoExtraData(ExtraData):
    
    def __init__(self, **kwargs):
        self._attribute = None
        ExtraData.__init__(self, **kwargs)
    
    @property
    def attribute(self):
        if self._attribute is None:
            self._attribute = taurus.Attribute(self._model)
        return self._attribute
        
    def getType(self):
        t = self.attribute.getType()
        if t is None:
            raise Exception("Could not determine type for unknown attribute '%s'" % self._model)
        return FROM_TANGO_TO_STR_TYPE[t]
    
    def getShape(self):
        s = self.attribute.getShape()
        if s is None:
            raise Exception("Could not determine type for unknown attribute '%s'" % self._model)
        return s

    def read(self):
        try:
            return self.attribute.read(cache=False).value
        except InterruptException:
            raise
        except Exception:
            return None


class GScan(Logger):
    """Generic Scan object. 
    The idea is that the scan macros create an instance of this Generic Scan, 
    supplying in the constructor a reference to the macro that created the scan,
    a generator function pointer, a list of moveable items, an extra 
    environment and a sequence of constrains.
    
    If the referenced macro is hookable, 'pre-scan' and 'post-scan' hook hints
    will be used to execute callables before the start and after the end of the
    scan, respectively
    
    The generator must be a function yielding a dictionary with the following
    content (minimum) at each step of the scan:
      - 'positions'  : In a step scan, the position where the moveables should go
      - 'integ_time' : In a step scan, a number representing the integration time for the step 
                     (in seconds)
      - 'integ_time' : In a continuous scan, the time between acquisitions
      - 'pre-move-hooks' : (optional) a sequence of callables to be called in strict order before starting to move.
      - 'post-move-hooks': (optional) a sequence of callables to be called in strict order after finishing the move.
      - 'pre-acq-hooks'  : (optional) a sequence of callables to be called in strict order before starting to acquire.
      - 'post-acq-hooks' : (optional) a sequence of callables to be called in strict order after finishing acquisition but before recording the step.
      - 'post-step-hooks' : (optional) a sequence of callables to be called in strict order after finishing recording the step.
      - 'hooks' : (deprecated, use post-acq-hooks instead)
      - 'point_id' : a hashable identifing the scan point.
      - 'check_func' : (optional) a list of callable objects. callable(moveables, counters)
      - 'extravalues': (optional) a dictionary containing the values for each extra info
                       field. The extra information fields must be described in
                       extradesc (passed in the constructor of the Gscan) 
    
    
    The moveables must be a sequence Motion or MoveableDesc objects.
    
    The environment is a dictionary of extra environment to be added specific
    to the macro in question.
    
    Each constrain must be a callable which must receive a two parameters: the 
    current point and the next point. It should return True or False
    
    The extradesc optional argument consists of a list of ColumnDesc objects
    which describe the data fields that will be filled using step['extravalues'],
    where step is what the generator yields.
    
    The Generic Scan will create:
      - a ScanData
      - DataHandler with the following recorders:
        - OutputRecorder (depends on 'OutputCols' environment variable)
        - SharedMemoryRecorder (depends on 'SharedMemory' environment variable)
        - FileRecorder (depends on 'ScanDir' and 'ScanData' environment variables)
      - ScanDataEnvironment with the following contents:
        - 'serialno' : a integer identifier for the scan operation
        - 'user' : the user which started the scan
        - 'title' : the scan title (build from macro.getCommand)
        - 'datadesc' : a seq<ColumnDesc> describing each column of data 
                     (labels, data format, data shape, etc)
        - 'estimatedtime' : a float representing an estimation for 
                          the duration of the scan (in seconds). Negative means
                          the time estimation is known not to be accurate. Anyway,
                          time estimation has 'at least' semantics.
        - 'total_scan_intervals' : total number of scan intervals. Negative means
                                   the estimation is known not to be accurate. In
                                   this case, estimation has 'at least' semantics.
        - '' : a datetime.datetime representing the start of the scan
        - 'instrumentlist' : a list of Instrument objects containing info
                            about the physical setup of the motors, counters,...
        - <extra environment> given in the constructor
        (at the end of the scan, extra keys 'endtime' and 'deadtime' will be added
        representing the time at the end of the scan and the dead time)

        This object is passed to all recorders at the beginning and at the end 
        of the scan (when startRecordList and endRecordList is called)
    
    At each step of the scan, for each Recorder, the writeRecord method will
    be called with a Record object as parameter. The Record.data member will be
    a dictionary containing:
      - 'point_nb' : the point number of the scan
      - for each column of the scan (motor or counter), a key with the 
      corresponding column name will contain the value"""
    
    MAX_SCAN_HISTORY = 20
    
    env = ('ActiveMntGrp', 'ExtraColumns' 'ScanDir', 'ScanFile', 'SharedMemory', 'OutputCols')
    
    def __init__(self, macro, generator=None, moveables=[], env={}, constraints=[], extrainfodesc=[]):
        self._macro = macro
        self._generator = generator
        self._extrainfodesc = extrainfodesc
        
        #nasty hack to make sure macro has access to gScan as soon as possible 
        self._macro._gScan = self #TODO: CAUTION! this may be causing a circular reference! 
        
        self._moveables, moveable_names = [], []
        for moveable in moveables:
            if not isinstance(moveable, MoveableDesc):
                moveable = MoveableDesc(moveable=moveable)
            moveable_names.append(moveable.moveable.getName())
            self._moveables.append(moveable)
        
        name = self.__class__.__name__
        self.call__init__(Logger, name)
        
        # ----------------------------------------------------------------------
        # Setup motion objects
        # ----------------------------------------------------------------------
        self._motion = macro.getMotion(moveable_names)

        # ----------------------------------------------------------------------
        # Find the measurement group
        # ----------------------------------------------------------------------
        try:
            mnt_grp_name = macro.getEnv('ActiveMntGrp')
        except UnknownEnv:
            mnt_grps = macro.getObjs(".*", type_class=Type.MeasurementGroup)
            if len(mnt_grps) == 0:
                raise ScanSetupError('No Measurement Group defined')
            mnt_grp = mnt_grps[0]
            macro.info("ActiveMntGrp not defined. Using %s", mnt_grp)
            macro.setEnv('ActiveMntGrp', mnt_grp.getName())
        else:
            if not isinstance(mnt_grp_name, (str, unicode)):
                t = type(mnt_grp_name).__name__
                raise TypeError("ActiveMntGrp MUST be string. It is '%s'" % t)

            mnt_grp = macro.getObj(mnt_grp_name,
                                   type_class=Type.MeasurementGroup)

        if mnt_grp is None:
            raise ScanSetupError("ActiveMntGrp has invalid value: '%s'"
                                 % mnt_grp_name)

        self._master = mnt_grp.getTimer()

        if self._master is None:
            raise ScanSetupError('%s has no timer defined' % mnt_grp.getName())
        
        self._measurement_group = mnt_grp
        
        # ----------------------------------------------------------------------
        # Setup extra columns
        # ----------------------------------------------------------------------
        self._extra_columns = self._getExtraColumns()
        
        # ----------------------------------------------------------------------
        # Setup data management
        # ----------------------------------------------------------------------
        
        # Generate data handler
        data_handler = ScanFactory().getDataHandler()
        
        # The Scan data object
        data = ScanFactory().getScanData(data_handler)

        # The Output recorder (if any)
        output_recorder = self._getOutputRecorder()

        # The Output recorder (if any)
        json_recorder = self._getJsonRecorder()
        
        # The File recorders (if any)
        file_recorders = self._getFileRecorders()
        
        # The Shared memory recorder (if any)
        shm_recorder = self._getSharedMemoryRecorder(0)
        shm_recorder_1d = None
        if shm_recorder is not None:
            shm_recorder_1d = self._getSharedMemoryRecorder(1)
        
        data_handler.addRecorder(output_recorder)
        data_handler.addRecorder(json_recorder)
        for file_recorder in file_recorders:
            data_handler.addRecorder(file_recorder)
        data_handler.addRecorder(shm_recorder)
        data_handler.addRecorder(shm_recorder_1d)
        
        self._data = data
        self._data_handler = data_handler
                
        # ----------------------------------------------------------------------
        # Setup environment
        # ----------------------------------------------------------------------
        self._setupEnvironment(env)

    def _getExtraColumns(self):
        ret = []
        try:
            cols = self.macro.getEnv('ExtraColumns')
        except InterruptException:
            raise
        except:
            self.info('ExtraColumns is not defined')
            return ret
        
        try:
            for i, kwargs in enumerate(cols):
                kw = dict(kwargs)
                try:
                    if kw.has_key('instrument'):
                        instrument = self._macro.getObj(kw['instrument'],
                            type_class=Type.Instrument)
                        if instrument:
                            kw['instrument'] = instrument
                    ret.append(TangoExtraData(**kw))
                except InterruptException:
                    raise
                except Exception, colexcept:
                    colname = kw.get('label', str(i))
                    self.macro.warning("Extra column %s is invalid: %s",
                                       colname, str(colexcept))
        except InterruptException:
            raise
        except Exception:
            self.macro.warning('ExtraColumns has invalid value. Must be a '
                               'sequence of maps')
        return ret

    def _getJsonRecorder(self):
        try:
            json_enabled = self.macro.getEnv('JsonRecorder')
            if json_enabled:
                return JsonRecorder(self.macro)
        except InterruptException:
            raise
        except Exception:
            pass
        self.info('JsonRecorder is not defined. Use "senv JsonRecorder '
                  'True" to enable it')

    def _getOutputRecorder(self):
        cols = None
        try:
            cols = self.macro.getEnv('OutputCols')
        except InterruptException:
            raise
        except:
            pass
        return OutputRecorder(self.macro, cols=cols, number_fmt='%g')
    
    def _getFileRecorders(self):
        macro = self.macro
        try:
            scan_dir = macro.getEnv('ScanDir')
        except InterruptException:
            raise
        except Exception:
            macro.warning('ScanDir is not defined. This operation will not be '
                          'stored persistently. Use Use "expconf" (or "senv ScanDir '
                          '<abs directory>") to enable it')
            return ()
        
        if not isinstance(scan_dir, (str, unicode)):
            scan_dir_t = type(scan_dir).__name__
            raise TypeError("ScanDir MUST be string. It is '%s'" % scan_dir_t)
        
        try:
            file_names = macro.getEnv('ScanFile')
        except InterruptException:
            raise
        except Exception:
            macro.warning('ScanFile is not defined. This operation will not '
                          'be stored persistently. Use "expconf" (or "senv ScanFile <scan '
                          'file(s)>") to enable it')
            return ()

        if isinstance(file_names, (str, unicode)):
            file_names = (file_names,)
        elif not operator.isSequenceType(file_names):
            scan_file_t = type(file_names).__name__
            raise TypeError("ScanFile MUST be string or sequence of strings."\
                            " It is '%s'" % scan_file_t)
            
        file_recorders = []
        for file_name in file_names:
            abs_file_name = os.path.join(scan_dir, file_name)
            try:
                file_recorder = FileRecorder(abs_file_name, macro=macro)
                file_recorders.append(file_recorder)
            except InterruptException:
                raise
            except Exception:
                macro.warning("Error creating recorder for %s", abs_file_name)
                macro.debug("Details:", exc_info=1)
        
        if len(file_recorders) == 0:
            macro.warning("No valid recorder found. This operation will not be "
                          " stored persistently")
        return file_recorders
    
    def _getSharedMemoryRecorder(self, eid):
        macro, mg, shm = self.macro, self.measurement_group, False
        try:
            shm = macro.getEnv('SharedMemory')
        except InterruptException:
            raise
        except Exception:
            self.info('SharedMemory is not defined. Use "senv '
                      'SharedMemory sps" to enable it')
            return
        
        if not shm: 
            return
        
        kwargs = {}
        # For now we only support SPS shared memory format
        if shm.lower() == 'sps':
            cols  = 1                            # Point nb column
            cols += len(self.moveables)          # motor columns
            ch_nb = len(mg.getChannels())
            oned_nb = 0
            array_prefix = mg.getName().upper()
            
            try:
                oned_nb = len(mg.OneDExpChannels)
            except InterruptException:
                raise
            except:
                oned_nb = 0
            
            twod_nb = 0
            try:
                twod_nb = len(mg.TwoDExpChannels)
            except InterruptException:
                raise
            except:
                twod_nb = 0
            
            if eid == 0:
                cols += (ch_nb - oned_nb - twod_nb)    # counter/timer & 0D channel columns
            elif eid == 1:
                cols = 1024
                
            if eid == 0:
                kwargs.update({ 'program' : macro.getDoorName(),
                                  'array' : "%s_0D" % array_prefix,
                                  'shape' : (cols, 4096) } )
            elif eid == 1:
                if oned_nb == 0:
                    return
                else:
                    kwargs.update({ 'program' : macro.getDoorName(),
                                  'array' : "%s_1D" % array_prefix,
                                  'shape' : (cols, 99) } )
            
        shmRecorder = SharedMemoryRecorder(shm, **kwargs)
        if shmRecorder is None:
            self.info('SharedMemory %s is not available'%shm)
        return shmRecorder
    
    def _secsToTimedelta(self, secs):
        days, secs = divmod(secs, 86400)
        # we don't have to care about microseconds because if secs is a float
        # timedelta will do it for us
        return datetime.timedelta(days, secs)
    
    def _timedeltaToSecs(self, td):
        return 86400*td.days + td.seconds + 1E-6*td.microseconds
    
    def _setupEnvironment(self, additional_env):
        try:
            serialno = self.macro.getEnv("ScanID") + 1
        except UnknownEnv:
            serialno = 1
        self.macro.setEnv("ScanID", serialno)
            
        env = ScanDataEnvironment(
                { 'serialno' : serialno,
                      'user' : USER_NAME, #TODO: this should be got from self.measurement_group.getChannelsInfo()
                     'title' : self.macro.getCommand() } )
        
        # Initialize the data_desc list (and add the point number column)
        data_desc = [
            ColumnDesc(name='point_nb', label='#Pt No', dtype='int64')
        ]
        
        # add motor columns
        ref_moveables = []
        for moveable in self.moveables:
            data_desc.append(moveable)
            if moveable.is_reference:
                ref_moveables.insert(0, moveable.name)
        
        if not ref_moveables and len(self.moveables):
            ref_moveables.append(data_desc[-1].name)
        env['ref_moveables'] = ref_moveables
        
        # add master column
        master = self._master
        instrument = master['instrument']
        
        #add channels from measurement group
        channels_info = self.measurement_group.getChannelsInfo()
        counters = []
        for ci in channels_info:
            instrument = ci.instrument or ''
            try:
                instrumentFullName = self.macro.findObjs(instrument, type_class=Type.Instrument)[0].getFullName()
            except InterruptException:
                raise
            except:
                instrumentFullName = ''
            #substitute the axis placeholder by the corresponding moveable.
            plotAxes = []
            i = 0
            for a in ci.plot_axes:
                if a == '<mov>':
                    plotAxes.append(ref_moveables[i])
                    i += 1
                else: plotAxes.append(a)
                
            #create the ColumnDesc object
            column = ColumnDesc(name=ci.full_name,
                                label=ci.label,
                                dtype=ci.data_type,
                                shape=ci.shape,
                                instrument=instrumentFullName,
                                source=ci.source,
                                output=ci.output,
                                conditioning=ci.conditioning,
                                normalization=ci.normalization,
                                plot_type=ci.plot_type,
                                plot_axes=plotAxes,
                                data_units=ci.unit)
            data_desc.append(column)
            counters.append(column.name)
        counters.remove(master['full_name'])
        env['counters'] = counters
        
        for extra_column in self._extra_columns:
            data_desc.append(extra_column.getColumnDesc())
        # add extra columns 
        data_desc += self._extrainfodesc
        data_desc.append(ColumnDesc(name='timestamp', label='dt', dtype='float64'))
        
        env['datadesc'] = data_desc
        
        #set the data compression default
        try:
            env['DataCompressionRank'] = self.macro.getEnv('DataCompressionRank')
        except UnknownEnv:
            env['DataCompressionRank'] = -1
        
        #set the sample information
        #@todo: use the instrument API to get this info
        try: 
            env['SampleInfo'] = self.macro.getEnv('SampleInfo')
        except UnknownEnv:
            env['SampleInfo'] = {}
            
        #set the source information
        #@todo: use the instrument API to get this info
        try:
            env['SourceInfo'] = self.macro.getEnv('SourceInfo')
        except UnknownEnv:
            env['SourceInfo'] = {}
        
        #take the pre-scan snapshot
        try:
            preScanSnapShot = self.macro.getEnv('PreScanSnapshot')
        except UnknownEnv:
            preScanSnapShot = []
        env['preScanSnapShot'] = self.takeSnapshot(elements=preScanSnapShot)
        
        env['macro_id'] = self.macro.getID()
        try:
            env['ScanFile'] = self.macro.getEnv('ScanFile')
        except InterruptException:
            raise
        except:
            env['ScanFile'] = None
        try:
            env['ScanDir'] = self.macro.getEnv('ScanDir')
        except InterruptException:
            raise
        except:
            env['ScanDir'] = None
        env['estimatedtime'], env['total_scan_intervals'] = self._estimate()
        env['instrumentlist'] = self._macro.findObjs('.*', type_class=Type.Instrument) 

        #env.update(self._getExperimentConfiguration) #add all the info from the experiment configuration to the environment
        env.update(additional_env)
        self._env = env
        
        # Give the environment to the ScanData
        self.data.setEnviron(env)

    def takeSnapshot(self, elements=[]):
        '''reads the current values of the given elements
        
        :param elements: (list<str,str>) list of tuples of label,src for the elements to read
                         (can be pool elements or Taurus attribute names).
        
        :return: (list<ColumnDesc>) a list of :class:`ColumnDesc`, each including a 
                 "pre_scan_value" attribute with the read value for that attr
        '''
        manager = self.macro.getManager()
        all_elements_info = manager.get_elements_with_interface('Element')
        ret = []
        for src, label in elements:
            try:
                if src in all_elements_info:
                    ei = all_elements_info[src]
                    column = ColumnDesc(name=ei.full_name,
                                        label=label,
                                        instrument=ei.instrument,
                                        source=ei.source)
                else:
                    column = ColumnDesc(name=src,
                                        label=label,
                                        source=src)

                v = PyTango.AttributeProxy(column.source).read().value  #@Fixme: Tango-centric. It should work for any Taurus Attribute
                column.pre_scan_value = v
                column.shape = np.shape(v)
                column.dtype = getattr(v, 'dtype', np.dtype(type(v))).name
                ret.append(column)
            except:
                self.macro.warning('Error taking pre-scan snapshot of %s (%s)', label, src)
                self.debug('Details:', exc_info=1)
        return ret

    def get_virtual_motors(self):
        ret = []
        for moveable in self.moveables:
            try:
                v_motor = VMotor.fromMotor(moveable.moveable)
            except:
                #self.debug("Details:", exc_info=1)
                v_motor = VMotor(min_vel=0, max_vel=float('+inf'),
                                 accel_time=0, decel_time=0)
            ret.append(v_motor)
        return ret

    MAX_ITER = 100000

    def _estimate(self, max_iter=None):
        with_time = hasattr(self.macro, "getTimeEstimation")
        with_interval = hasattr(self.macro, "getIntervalEstimation")
        if with_time and with_interval:
            t, i = self.macro.getTimeEstimation(), self.macro.getIntervalEstimation()
            return t, i
        
        max_iter = max_iter or self.MAX_ITER
        iterator = self.generator()
        total_time = 0.0
        interval_nb = 0
        try:
            if not with_time:
                start_pos = self.motion.readPosition(force=True)
                v_motors = self.get_virtual_motors()
                motion_time, acq_time = 0.0, 0.0
                while interval_nb < max_iter:
                    step = iterator.next()
                    end_pos = step['positions']
                    max_path_duration = 0.0
                    for v_motor, start, stop in zip(v_motors, start_pos, end_pos):
                        path = MotionPath(v_motor, start, stop)
                        max_path_duration = max(max_path_duration, path.duration)
                    integ_time = step.get("integ_time", 0.0)
                    acq_time += integ_time
                    motion_time += max_path_duration 
                    total_time += integ_time + max_path_duration
                    interval_nb += 1
                    start_pos = end_pos
                if with_interval:
                    interval_nb = self.macro.getIntervalEstimation()
            else:
                while interval_nb < max_iter:
                    step = iterator.next()
                    interval_nb += 1
                total_time = self.macro.getTimeEstimation()
        except StopIteration:
            return total_time, interval_nb
        # max iteration reached.
        return -total_time, -interval_nb
    
    @property
    def data(self):
        return self._data
    
    @property
    def macro(self):
        return self._macro

    @property
    def measurement_group(self):
        return self._measurement_group

    @property
    def generator(self):
        return self._generator

    @property
    def motion(self):
        return self._motion
    
    @property
    def moveables(self):
        return self._moveables
    
    @property
    def steps(self):
        if not hasattr(self, '_steps'):
            self._steps = enumerate(self.generator())
        return self._steps
    
    def start(self):
        self.do_backup()
        env = self._env
        env['startts'] = ts = time.time()
        env['starttime'] = datetime.datetime.fromtimestamp(ts)
        env['acqtime'] = 0
        env['motiontime'] = 0
        env['deadtime'] = 0
        self.data.start()

    def end(self):
        env = self._env
        env['endts'] = end_ts = time.time()
        env['endtime'] = datetime.datetime.fromtimestamp(end_ts)
        total_time = end_ts - env['startts']
        estimated = env['estimatedtime']
        acq_time = env['acqtime']
        #env['deadtime'] = 100.0 * (total_time - estimated) / total_time
        
        env['deadtime'] = total_time - acq_time
        if 'delaytime' in env:
            env['motiontime'] = total_time - acq_time - env['delaytime']
        elif 'motiontime' in env:
            env['delaytime'] = total_time - acq_time - env['motiontime']
                  
        self.data.end()
        try:
            scan_history = self.macro.getEnv('ScanHistory')
        except UnknownEnv:
            scan_history = []
        
        scan_file = env['ScanFile']
        if isinstance(scan_file, (str, unicode)):
            scan_file = scan_file,
        
        names = [ col.name for col in env['datadesc'] ]
        history = dict(startts=env['startts'], endts=env['endts'],
                       estimatedtime=env['estimatedtime'],
                       deadtime=env['deadtime'], title=env['title'],
                       serialno=env['serialno'], user=env['user'],
                       ScanFile=scan_file, ScanDir=env['ScanDir'],
                       channels=names)
        scan_history.append(history)
        while len(scan_history) > self.MAX_SCAN_HISTORY:
            scan_history.pop(0)
        self.macro.setEnv('ScanHistory', scan_history)

    def scan(self):
        for _ in self.step_scan():
            pass
        
    def step_scan(self):
        self.start()
        try:
            ex = None
            try:
                for i in self.scan_loop():
                    self.macro.pausePoint()
                    yield i
            except ScanException, e:
                #self.macro.warning(e.msg)
                ex = e
            self.end()
            if not ex is None: raise e
        finally:
            self.do_restore()
                
    def scan_loop(self):
        raise NotImplementedError('Scan method cannot be called by '
                                  'abstract class')

    def do_backup(self):
        try:
            if hasattr(self.macro, 'do_backup'):
                self.macro.do_backup()
        except:
            self.macro.warning("Failed to execute macro 'do_backup'")
            self.debug("Details:", exc_info=1)
        
    def do_restore(self):
        try:
            if hasattr(self.macro, 'do_restore'):
                self.macro.do_restore()
        except:
            self.macro.warning("Failed to execute macro 'do_restore'")        
            self.debug("Details:", exc_info=1)


class SScan(GScan):
    """Step scan"""
    
    def scan_loop(self):
        lstep = None
        macro = self.macro
        scream = False
        
        if hasattr(macro, "nr_points"):
            nr_points = float(macro.nr_points)
            scream = True
        else:
            yield 0.0
        
        if hasattr(macro, 'getHooks'):
            for hook in macro.getHooks('pre-scan'):
                hook()
        
        self._sum_motion_time = 0
        self._sum_acq_time = 0
        
        for i, step in self.steps:
            # allow scan to be stopped between points
            macro.checkPoint()
            self.stepUp(i, step, lstep)
            lstep = step
            if scream:
                yield ((i + 1) / nr_points) * 100.0

        if hasattr(macro, 'getHooks'):
            for hook in macro.getHooks('post-scan'):
                hook()

        if not scream:
            yield 100.0
            
        self._env['motiontime'] = self._sum_motion_time
        self._env['acqtime'] = self._sum_acq_time
        
    def stepUp(self, n, step, lstep):
        motion, mg = self.motion, self.measurement_group
        startts = self._env['startts']
        
        #pre-move hooks
        for hook in step.get('pre-move-hooks',()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except:
                pass
        
        # Move
        self.debug("[START] motion")
        move_start_time = time.time()
        try:
            state, positions = motion.move(step['positions'])
            self._sum_motion_time += time.time() - move_start_time
        except InterruptException:
            raise
        except:
            self.dump_information(n, step)
            raise
        self.debug("[ END ] motion")
        
        curr_time = time.time()
        dt = curr_time - startts
        
        #post-move hooks
        for hook in step.get('post-move-hooks',()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except:
                pass
        
        # allow scan to be stopped between motion and data acquisition
        self.macro.checkPoint()
        
        if state != Ready:
            self.dump_information(n, step)
            m = "Scan aborted after problematic motion: " \
                "Motion ended with %s\n" % str(state)
            raise ScanException({ 'msg' : m })
        
        #pre-acq hooks
        for hook in step.get('pre-acq-hooks',()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except: pass
        
        integ_time = step['integ_time']
        # Acquire data
        self.debug("[START] acquisition")
        state, data_line = mg.count(integ_time)
        for ec in self._extra_columns:
            data_line[ec.getName()] = ec.read()
        self.debug("[ END ] acquisition")
        self._sum_acq_time += integ_time

        #post-acq hooks
        for hook in step.get('post-acq-hooks', ()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except:
                pass
        
        #hooks for backwards compatibility:
        if step.has_key('hooks'):
            self.macro.info('Deprecation warning: you should use '
                            '"post-acq-hooks" instead of "hooks" in the step '
                            'generator')
            for hook in step.get('hooks', ()):
                hook()
                try:
                    step['extrainfo'].update(hook.getStepExtraInfo())
                except InterruptException:
                    raise
                except:
                    pass
        
        # Add final moveable positions
        data_line['point_nb'] = n
        data_line['timestamp'] = dt
        for i, m in enumerate(self.moveables):
            data_line[m.moveable.getName()] = positions[i]
        
        #Add extra data coming in the step['extrainfo'] dictionary
        if step.has_key('extrainfo'): data_line.update(step['extrainfo'])
        
        self.data.addRecord(data_line)
    
        #post-step hooks
        for hook in step.get('post-step-hooks', ()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except:
                pass
        
    def dump_information(self, n, step):
        moveables = self.motion.moveable_list
        msg = ["Report: Stopped at step #" + str(n) + " with:"]
        for moveable in moveables:
            msg.append(moveable.information())
        self.macro.info("\n".join(msg))
        

class CScan(GScan):
    """Continuous scan abstract class. Implements helper methods."""
    
    def __init__(self, macro, generator=None, moveables=[],
                 env={}, constraints=[], extrainfodesc=[]):
        GScan.__init__(self, macro, generator=generator,
                       moveables=moveables, env=env, constraints=constraints,
                       extrainfodesc=extrainfodesc)
        self._current_waypoint_finished = False
        self._all_waypoints_finished = False
        self.motion_event = threading.Event()
        self.motion_end_event = threading.Event()
        data_structures = self.populate_moveables_data_structures(moveables)
        self._moveables_trees, \
        physical_moveables_names, \
        self._physical_moveables = data_structures
        # The physical motion object contains only physical motors - no pseudo
        # motors (in case the pseudomotors are involved in the scan,
        # it comprarises the underneath physical motors)
        # This is due to the fact that the CTScan coordinates the
        # pseudomotors' underneeth physical motors on on their constant
        # velocity in contrary to the the CScan which do not coordinate them
        self._physical_motion = self.macro.getMotion(physical_moveables_names)
        
    def populate_moveables_data_structures(self, moveables):
        '''Populates moveables data structures.
        :param moveables: (list<Moveable>) data structures will be generated 
                          for these moveables
        :return (moveable_trees, physical_moveables_names, physical_moveables)
                - moveable_trees (list<Tree>) - each tree represent one Moveables
                            with its hierarchy of inferior moveables.
                - physical_moveables_names (list<str> - list of the names of the 
                            physical moveables. List order is important and preserved.
                - physical_moveables (list<Moveable> - list of the moveable objects.
                            List order is important and preserved.'''
        
        def generate_moveable_node(macro, moveable):
            '''Function to generate a moveable data structures based on moveable object.
            Internally can be recursively called if moveable is a PseudoMotor.
            :param moveable: moveable object
            :return (moveable_node, physical_moveables_names, physical_moveables)
                - moveable_node (BaseNode) - can be a BranchNode if moveable is a PseudoMotor
                                      or a LeafNode if moveable is a PhysicalMotor.
                - physical_moveables_names (list<str> - list of the names of the 
                            physical moveables. List order is important and preserved.
                - physical_moveables (list<Moveable> - list of the moveable objects.
                            List order is important and preserved.'''
            moveable_node = None
            physical_moveables_names = []
            physical_moveables = []
            moveable_type = moveable.getType()
            if moveable_type == "PseudoMotor":
                moveable_node = BranchNode(moveable)    
                moveables_names = moveable.elements                
                sub_moveables = [macro.getMoveable(name) \
                                 for name in moveables_names]
                for sub_moveable in sub_moveables:
                    sub_moveable_node, \
                    _physical_moveables_names, \
                    _physical_moveables = generate_moveable_node(macro,
                                                                 sub_moveable)
                    physical_moveables_names += _physical_moveables_names
                    physical_moveables += _physical_moveables
                    moveable_node.addChild(sub_moveable_node)
            elif moveable_type == "Motor":
                moveable_node = LeafNode(moveable)
                moveable_name = moveable.getName()
                physical_moveables_names.append(moveable_name)
                physical_moveables.append(moveable)
            return moveable_node, physical_moveables_names, physical_moveables
        
        moveable_trees = []
        physical_moveables_names = [] 
        physical_moveables = []
                
        for moveable in moveables:
            moveable_root_node, _physical_moveables_names, _physical_moveables = \
                      generate_moveable_node(self.macro, moveable.moveable) 
            moveable_tree = Tree(moveable_root_node)
            moveable_trees.append(moveable_tree)
            physical_moveables_names += _physical_moveables_names
            physical_moveables += _physical_moveables 
        return moveable_trees, physical_moveables_names, physical_moveables
    
    def get_moveables_trees(self):
        '''Returns reference to the list of the moveables trees'''
        return self._moveables_trees
                    
    def on_waypoints_end(self, restore_positions=None):
        """To be called by the waypoint thread to handle the end of waypoints
        (either because no more waypoints or because a macro abort was
        triggered)"""
        self.set_all_waypoints_finished(True)
        if restore_positions is not None:
            self._setFastMotions()
            self.macro.info("Correcting overshoot...")
            self.motion.move(restore_positions)
        self.do_restore()
        self.motion_end_event.set()        
        self.motion_event.set()
            
    def go_through_waypoints(self, iterate_only=False):
        """Go through the different waypoints."""
        try:
            self._go_through_waypoints()
        except:
            self.macro.error("An error occured moving to waypoints. Aborting...")
            self.macro.debug("Details:", exc_info=1)
            self.on_waypoints_end()

    def _go_through_waypoints(self):
        """Internal, unprotected method to go through the different waypoints."""
        raise NotImplementedError("_go_through_waypoints must be implemented " +
                            "in CScan derived classes")
                
    def waypoint_estimation(self):
        """Internal, unprotected method to go through the different waypoints."""
        motion, waypoints = self.motion, self.generator()
        total_duration = 0
        #v_motors = self.get_virtual_motors()
        curr_positions, last_end_positions = motion.readPosition(force=True), None
        for i, waypoint in enumerate(waypoints):
            start_positions = waypoint.get('start_positions', last_end_positions)
            positions = waypoint['positions']
            if start_positions is None:
                last_end_positions = positions
                continue
            
            waypoint_info = self.prepare_waypoint(waypoint, start_positions,
                                                  iterate_only=True)
            motion_paths, delta_start, acq_duration = waypoint_info
    
            start_path, end_path = [] , []
            for path in motion_paths:
                start_path.append(path.initial_user_pos)
                end_path.append(path.final_user_pos)
                       
            # move from last waypoint to start position of this waypoint
            first_duration = 0
            if i == 1:
                # first waypoint means, moving from current position to the
                # start of first waypoint
                initial = curr_positions
            else:
                initial = start_positions
            for _path, start, end in zip(motion_paths, initial, start_path):
                v_motor = _path.motor
                path = MotionPath(v_motor, start, end)
                first_duration = max(first_duration, path.duration)
                        
            # move from waypoint start position to waypoint end position
            second_duration = 0
            for _path, start, end in zip(motion_paths, start_path, end_path):
                v_motor = _path.motor
                path = MotionPath(v_motor, start, end)
                second_duration = max(second_duration, path.duration)
            
            total_duration += first_duration + second_duration
            
            last_end_positions = end_path
        
        # add correct overshoot time
        overshoot_duration = 0
        for _path, start, end in zip(motion_paths, last_end_positions, positions):
            v_motor = _path.motor
            path = MotionPath(v_motor, start, end)
            overshoot_duration = max(overshoot_duration, path.duration)
        
        total_duration += overshoot_duration
        return total_duration       
    
    def prepare_waypoint(self, waypoint, start_positions, iterate_only=False):
        raise NotImplementedError("prepare_waypoint must be implemented in " + 
                                  "CScan derived classes")
        
    def set_all_waypoints_finished(self, v):
        self._all_waypoints_finished = v

    def do_backup(self):
        super(CScan, self).do_backup()
        self._backup = backup = []
        for moveable in self._physical_moveables:
            # first backup all motor parameters
            motor = moveable
            try:
                velocity = motor.getVelocity()
                accel_time = motor.getAcceleration()
                decel_time = motor.getDeceleration()
                motor_backup = dict(moveable=moveable, velocity=velocity,
                                    acceleration=accel_time,
                                    deceleration=decel_time)
                self.debug("Backup of %s", motor)
            except AttributeError:
                motor_backup = None
            backup.append(motor_backup)
           
    def do_restore(self):
        super(CScan, self).do_restore()
        # restore changed motors to initial state
        for motor_backup in self._backup:
            if motor_backup is None:
                continue
            try:
                motor = motor_backup['moveable']
                motor.setVelocity(motor_backup['velocity'])
                motor.setAcceleration(motor_backup['acceleration'])
                motor.setDeceleration(motor_backup['deceleration'])
                self.debug("Restored %s", motor)
            except:
                self.macro.warning("Failed to restore %s", motor)
                self.debug("Details:", exc_info=1)
                
    def _setFastMotions(self, motors=None):
        '''make given motors go at their max speed and accel'''
        if motors is None:
            motors = [b.get('moveable') for b in self._backup if b is not None]
            
        for motor in motors:
            try:
                motor.setVelocity(self.get_max_top_velocity(motor))
                motor.setAcceleration(self.get_min_acc_time(motor))
                motor.setDeceleration(self.get_min_dec_time(motor))
                self.debug("%s put into fast motion", motor)
            except:
                self.macro.warning("Failed to put %s into fast motion", motor)
                self.debug("Details:", exc_info=1)
                
    def get_max_top_velocity(self, motor):
        """Helper method to find the maximum top velocity for the motor.
        If the motor doesn't have a defined range for top velocity,
        then use the current top velocity"""
        
        top_vel_obj = motor.getVelocityObj()
        min_top_vel, max_top_vel = top_vel_obj.getRange()
        try:
            max_top_vel = float(max_top_vel)
        except ValueError:
            try:
                #hack to avoid recursive velocity reduction
                self._maxVelDict = getattr(self,'_maxVelDict',{})
                if not motor in self._maxVelDict:
                    self._maxVelDict[motor] = motor.getVelocity()
                max_top_vel = self._maxVelDict[motor]
            except AttributeError:
                pass
        return max_top_vel
    
    def get_min_acc_time(self, motor):
        """Helper method to find the minimum acceleration time for the motor.
        If the motor doesn't have a defined range for the acceleration time,
        then use the current acceleration time"""
        
        acc_time_obj = motor.getAccelerationObj()
        min_acc_time, max_acc_time = acc_time_obj.getRange()
        try:
            min_acc_time = float(min_acc_time)
        except ValueError:
            min_acc_time = motor.getAcceleration()
        return min_acc_time
    
    def get_min_dec_time(self, motor):
        """Helper method to find the minimum deceleration time for the motor.
        If the motor doesn't have a defined range for the acceleration time,
        then use the current acceleration time"""
        
        dec_time_obj = motor.getDecelerationObj()
        min_dec_time, max_dec_time = dec_time_obj.getRange()
        try:
            min_dec_time = float(min_dec_time)
        except ValueError:
            min_dec_time = motor.getDeceleration()
        return min_dec_time
 
    def set_max_top_velocity(self, motor):
        """Helper method to set the maximum top velocity for the motor to 
        its maximum allowed limit."""
        
        v = self.get_max_top_velocity(motor)
        try:
            motor.setVelocity(v)
        except:
            pass

                
class CSScan(CScan):
    """Continuous scan controlled by software"""
    
    def __init__(self, macro, waypointGenerator=None, periodGenerator=None,
                 moveables=[], env={}, constraints=[], extrainfodesc=[]):
        CScan.__init__(self, macro, generator=waypointGenerator,
                       moveables=moveables, env=env, constraints=constraints,
                       extrainfodesc=extrainfodesc)
        self._periodGenerator = periodGenerator
        

    def _calculateTotalAcquisitionTime(self):
        return None
        
    @property
    def period_generator(self):
        return self._periodGenerator
    
    @property
    def period_steps(self):
        if not hasattr(self, '_period_steps'):
            self._period_steps = enumerate(self.period_generator())
        return self._period_steps
    
    def prepare_waypoint(self, waypoint, start_positions, iterate_only=False):
        slow_down = waypoint.get('slow_down', 1)
        positions = waypoint['positions']
        
        duration, cruise_duration, delta_start = 0, 0, 0
        ideal_paths, real_paths = [], []
        for i, (moveable, position) in enumerate(zip(self.moveables, positions)):
            motor = moveable.moveable
            
            coordinate = True
            try:
                base_vel, top_vel = motor.getBaseRate(), motor.getVelocity()
                accel_time, decel_time = motor.getAcceleration(), motor.getDeceleration()

                if slow_down > 0:
                    # find and set the maximum top velocity for the motor. 
                    # If the motor doesn't have a defined range for top velocity,
                    # then use the current top velocity
                    max_top_vel = self.get_max_top_velocity(motor)
                    if not iterate_only:
                        motor.setVelocity(max_top_vel)
                else:
                    max_top_vel = top_vel
            except AttributeError:
                if not iterate_only:
                    self.macro.warning("%s motion will not be coordinated", motor)
                base_vel, top_vel, max_top_vel = 0, float('+inf'), float('+inf')
                accel_time, decel_time = 0, 0
                coordinate = False

            last_user_pos = start_positions[i]
                        
            real_vmotor = VMotor(min_vel=base_vel, max_vel=max_top_vel,
                                 accel_time=accel_time,
                                 decel_time=decel_time)
            real_path = MotionPath(real_vmotor, last_user_pos, position)
            real_path.moveable = moveable            
            real_path.apply_correction = coordinate
            
            # Find the cruise duration of motion at top velocity. For this create a
            # virtual motor which has instantaneous acceleration and deceleration
            ideal_vmotor = VMotor(min_vel=base_vel, max_vel=max_top_vel,
                                  accel_time=0, decel_time=0)
            
            # create a path which will tell us which is the cruise duration of this
            # motion at top velocity
            ideal_path = MotionPath(ideal_vmotor, last_user_pos, position)
            ideal_path.moveable = moveable
            ideal_path.apply_correction = coordinate
            
            # if really motor is moving in this waypoint
            if ideal_path.displacement > 0:
                # recalculate time to reach maximum velocity
                delta_start = max(delta_start, accel_time)
            
            # recalculate cruise duration of motion at top velocity
            cruise_duration = max(cruise_duration, ideal_path.duration)
            duration = max(duration, real_path.duration)
            
            ideal_paths.append(ideal_path)
            real_paths.append(real_path)
            
        if slow_down <= 0:
            return real_paths, 0, duration
        
        # after finding the duration, introduce the slow down factor added
        # by the user
        cruise_duration /= slow_down
        
        if cruise_duration == 0:
            cruise_duration = float('+inf')
        
        # now that we have the appropriate top velocity for all motors, the
        # cruise duration of motion at top velocity, and the time it takes to
        # recalculate 
        for path in ideal_paths:
            vmotor = path.motor
            # in the case of pseudo motors or not moving a motor...
            if not path.apply_correction or path.displacement == 0:
                continue
            moveable = path.moveable
            motor = moveable.moveable
            new_top_vel = path.displacement / cruise_duration
            vmotor.setMaxVelocity(new_top_vel)
            accel_t, decel_t = motor.getAcceleration(), motor.getDeceleration()
            base_vel = vmotor.getMinVelocity()
            vmotor.setAccelerationTime(accel_t)
            vmotor.setDecelerationTime(decel_t)
            disp_sign = path.positive_displacement and 1 or -1
            new_initial_pos = path.initial_user_pos - accel_t * 0.5 * disp_sign * (new_top_vel + base_vel) - disp_sign * new_top_vel * (delta_start - accel_t)
            path.setInitialUserPos(new_initial_pos)
            new_final_pos = path.final_user_pos + disp_sign * vmotor.displacement_reach_min_vel
            path.setFinalUserPos(new_final_pos)
        
        return ideal_paths, delta_start, cruise_duration

    
    def go_through_waypoints(self, iterate_only=False):
        """go through the different waypoints."""
        try:
            self._go_through_waypoints()
        except Exception, e:
            self.macro.error("An error occured moving to waypoints. Aborting...")
            self.macro.debug("Details:", exc_info=1)
            self.on_waypoints_end()
            raise e

    def _go_through_waypoints(self):
        """Internal, unprotected method to go through the different waypoints."""
        macro, motion, waypoints = self.macro, self.motion, self.steps
        self.macro.debug("_go_through_waypoints() entering...")
        
        last_positions = None
        for _, waypoint in waypoints:
            self.macro.debug("Waypoint iteration...")
            start_positions = waypoint.get('start_positions')
            positions = waypoint['positions']
            if start_positions is None:
                start_positions = last_positions
            if start_positions is None:
                last_positions = positions
                continue
            
            waypoint_info = self.prepare_waypoint(waypoint, start_positions)
            motion_paths, delta_start, acq_duration = waypoint_info
            
            self.acq_duration = acq_duration
                        
            #execute pre-move hooks
            for hook in waypoint.get('pre-move-hooks',[]): 
                hook()
    
            start_pos, final_pos = [] , []
            for path in motion_paths:
                start_pos.append(path.initial_user_pos)
                final_pos.append(path.final_user_pos)

            if macro.isStopped():
                self.on_waypoints_end()
                return 
                      
            # move to start position
            self.macro.debug("Moving to start position: %s" % repr(start_pos))
            motion.move(start_pos)
            
            if macro.isStopped():
                self.on_waypoints_end()
                return
            
            # prepare motor(s) with the velocity required for synchronization
            for path in motion_paths:
                if not path.apply_correction:
                    continue
                vmotor = path.motor
                motor = path.moveable.moveable
                motor.setVelocity(vmotor.getMaxVelocity())

            if macro.isStopped():
                self.on_waypoints_end()
                return
                        
            self.timestamp_to_start = time.time() + delta_start
            self.motion_event.set()
            
            # move to waypoint end position
            motion.move(final_pos)

            self.motion_event.clear()

            if macro.isStopped():
                return self.on_waypoints_end()
            
            #execute post-move hooks
            for hook in waypoint.get('post-move-hooks',[]):
                hook()
            
            if start_positions is None:  
                last_positions = positions
        
        self.on_waypoints_end(positions)

    
    def scan_loop(self):
        motion, mg, waypoints = self.motion, self.measurement_group, self.steps
        macro = self.macro
        manager = macro.getManager()
        scream = False
        motion_event = self.motion_event
        startts = self._env['startts']
        
        sum_delay = 0
        sum_integ_time = 0
        
        if hasattr(macro, "nr_points"):
            nr_points = float(macro.nr_points)
            scream = True
        else:
            yield 0.0

        moveables = [ m.moveable for m in self.moveables ]
        period_steps = self.period_steps
        point_nb, step = -1, None
        data = self.data
        
        if hasattr(macro, 'getHooks'):
            for hook in macro.getHooks('pre-scan'):
                hook()
        
        # start move & acquisition as close as possible
        # from this point on synchronization becomes critical
        manager.add_job(self.go_through_waypoints)
        
        while not self._all_waypoints_finished:
        
            # wait for motor to reach start position
            motion_event.wait()
            
            # allow scan to stop
            macro.checkPoint()
            
            if self._all_waypoints_finished:
                break
            
            # wait for motor to reach max velocity
            start_time = time.time()
            deltat = self.timestamp_to_start - start_time
            if deltat > 0:
                time.sleep(deltat)
            curr_time = acq_start_time = time.time()
            integ_time = 0
            
            # Acquisition loop: acquire consecutively until waypoint asks to
            # stop or we see that we will enter deceleration time in next
            # acquisition
            while motion_event.is_set():

                # allow scan to stop
                macro.checkPoint()
            
                try:
                    point_nb, step = period_steps.next()
                except StopIteration:
                    self._all_waypoints_finished = True
                    break

                integ_time = step['integ_time']
                
                # If there is no more time to acquire... stop!
                elapsed_time = time.time() - acq_start_time
                if elapsed_time + integ_time > self.acq_duration:
                    motion_event.clear()
                    break;                
                
                #pre-acq hooks
                for hook in step.get('pre-acq-hooks',()):
                    hook()
                    try:
                        step['extrainfo'].update(hook.getStepExtraInfo())
                    except InterruptException:
                        self._all_waypoints_finished = True
                        raise
                    except: pass

                # allow scan to stop
                macro.checkPoint()
                
                positions = motion.readPosition(force=True)

                dt = time.time() - startts

                # Acquire data
                self.debug("[START] acquisition")
                state, data_line = mg.count(integ_time)
                
                sum_integ_time += integ_time
                
                # allow scan to stop
                macro.checkPoint()
                
                # After acquisition, test if we are asked to stop, probably because
                # the motor are stopped. In this case discard the last acquisition
                if not self._all_waypoints_finished:
                    for ec in self._extra_columns:
                        data_line[ec.getName()] = ec.read()
                    self.debug("[ END ] acquisition")
                    
                    #post-acq hooks
                    for hook in step.get('post-acq-hooks',()):
                        hook()
                        try:
                            step['extrainfo'].update(hook.getStepExtraInfo())
                        except InterruptException:
                            self._all_waypoints_finished = True
                            raise
                        except:
                            pass

                    # Add final moveable positions
                    data_line['point_nb'] = point_nb
                    data_line['timestamp'] = dt
                    for i, m in enumerate(self.moveables):
                        data_line[m.moveable.getName()] = positions[i]
                    
                    #Add extra data coming in the step['extrainfo'] dictionary
                    if step.has_key('extrainfo'): data_line.update(step['extrainfo'])
                    
                    self.data.addRecord(data_line)
                    
                    if scream:
                        yield ((point_nb + 1) / nr_points) * 100.0
                else:
                    break
                old_curr_time = curr_time
                curr_time = time.time()
                sum_delay += (curr_time - old_curr_time) - integ_time

        self.motion_end_event.wait()

        if hasattr(macro, 'getHooks'):
            for hook in macro.getHooks('post-scan'):
                hook()
        
        
        env = self._env
        env['acqtime'] = sum_integ_time
        env['delaytime'] = sum_delay
        
        if not scream:
            yield 100.0   


class CTScan(CScan):
    '''Continuous scan controlled by hardware trigger signals. 
    Sequence of trigger signals is programmed in time. '''
    
    class ExtraTrigger:
        '''Helper class and temporary solution for configuring trigger device.
           It is used to configure any Tango device name implementing:
           +) following attributes: 
           - InitialDelayTime [s] - delay time from calling Start to generating first pulse 
           - HighTime [s] - time interval while signal will maintain its high state
           - LowTime [s] - time interval while signal will maintain its low state
           - SampPerChan - nr of pulses to be generated
           - IdleState - state (high or low) which signal will take after the Start command
                         and which will maintain during the InitialDelayTime.  
           +) following commands:
           - Start 
           - Stop)'''
         
        MIN_HIGH_TIME = 0.0000002
        MIN_TIME_PER_TRIGGER = 0.000001
    
        def __init__(self, macro):
            self.macro = macro
            
            triggerDeviceName = self.macro.getEnv("TriggerDevice")
            self.master = None
            self.slaves = []
            masterName = None
            slaveNames = []
            
            if isinstance(triggerDeviceName, str):
                masterName = triggerDeviceName
            elif isinstance(triggerDeviceName, list):
                masterName = triggerDeviceName[0]
                slaveNames = triggerDeviceName[1:]
            
            for name in slaveNames:
                slave = PyTango.DeviceProxy(name)
                self.slaves.append(slave)
            if masterName != None:
                self.master = PyTango.DeviceProxy(masterName)        
    
        def configure(self, scanTime=None, nrOfTriggers=None, idleState="Low", lowTime=None, highTime=None, delayTime=0):
            if not None in (scanTime, nrOfTriggers, delayTime, idleState):
                timePerTrigger = scanTime / nrOfTriggers
                if timePerTrigger < self.MIN_TIME_PER_TRIGGER:
                    raise Exception("scanTime is not long enough to manage this amount of triggers")
                highTime = self.MIN_HIGH_TIME
                lowTime = timePerTrigger - highTime
            elif not None in (lowTime, highTime, delayTime, nrOfTriggers, idleState):
                pass
            else:
                raise Exception("Missing parameters.")
            
            self.master.write_attribute("InitialDelayTime", delayTime)
            self.master.write_attribute("HighTime", highTime) # 162.5 ns
            self.master.write_attribute("LowTime", lowTime) # 2.75 ms
            self.master.write_attribute("SampPerChan", long(nrOfTriggers))
            self.master.write_attribute("IdleState", idleState)
            self.master.write_attribute("SampleTimingType", "Implicit")
    
            for slave in self.slaves:
                slave.write_attribute("HighTime", highTime) # 162.5 ns
                slave.write_attribute("LowTime", lowTime) # 2.75 ms
                slave.write_attribute("SampPerChan", long(nrOfTriggers))
                slave.write_attribute("IdleState", idleState)
                slave.write_attribute("SampleTimingType", "Implicit")
                
            return timePerTrigger
    
        def getConfiguration(self):
            return None, None, None, None
    
        def start(self):
            for slave in self.slaves:
                self.macro.debug("Staring  %s" % slave.name())
                slave.Start()
            if self.master != None:
                self.master.Start()
    
        def stop(self):
            for slave in self.slaves:
                self.macro.debug("Stopping  %s" % slave.name())
                slave.Stop()
            if self.master != None:
                self.master.Stop()

    
    class ExtraMntGrp:
        '''Helper class and temporary solution for configuring experimental channels.
        It assumes that experimental channels are implementing:
        +) following attributes:
        - Data - an array of acquired data
        - TriggerMode - Soft or Gate - to configure/unconfigure channel for hardware trigger
        - NrOfTriggers - to specify how many hardware triggers and acquistions will be done
        - SamplingFrequency - have sence only for sampling experimental channels
        +) following SendToCtrl strings:
        - "pre-start"
        - "start"
        - "pre-stop"
        - "stop"'''
        
        def __init__(self, macro):
            self.macro = macro
            activeMntGrpName = self.macro.getEnv("ActiveMntGrp")
            self.mntGrp = self.macro.getMeasurementGroup(activeMntGrpName)        
            self.activeChannels = []
            self.nrOfTriggers = 0
            channels = self.mntGrp.getChannels()
            for channel in channels:
                channelName = channel["name"]
                expChannel = self.macro.getExpChannel(channelName)
                expChannel.getHWObj().set_timeout_millis(120000) #in case of readout of position channels, it can take really long...
                self.activeChannels.append(expChannel)
    
        def isMoving(self):
            for channel in self.activeChannels:
                if channel.State() == PyTango.DevState.MOVING:
                    return True
            return False
    
        def start(self):
            for channel in self.activeChannels:
                pool = channel.getPoolObj()
                ctrlName = channel.getControllerName()
                axis = channel.getAxis()
                self.macro.debug("Pre-starting controller: %s, axis: %d", ctrlName, axis)
                pool.SendToController([ctrlName, 'pre-start %d' % axis])
    
            for channel in self.activeChannels:
                pool = channel.getPoolObj()
                ctrlName = channel.getControllerName()
                axis = channel.getAxis()
                self.macro.debug("Starting controller: %s, axis: %d", ctrlName, axis)
                pool.SendToController([ctrlName, 'start %d' % axis])
     
        def stop(self):
            for channel in self.activeChannels:
                pool = channel.getPoolObj()
                ctrlName = channel.getControllerName()
                axis = channel.getAxis()
                self.macro.debug("Pre-stopping controller: %s, axis: %d", ctrlName, axis)
                pool.SendToController([ctrlName, 'pre-stop %d' % axis])
    
            for channel in self.activeChannels:
                pool = channel.getPoolObj()
                ctrlName = channel.getControllerName()
                axis = channel.getAxis()
                self.macro.debug("Stopping controller: %s, axis: %d", ctrlName, axis)
                pool.SendToController([ctrlName, 'stop %d' % axis])
    
        def getDataList(self):
            dataList = [ {"point_nb" : i, "timestamp" : 0} for i in xrange(self.nrOfTriggers) ]
            for channel in self.activeChannels:
                dataDesc = channel.getFullName()
                channelData = channel.getAttribute("Data").read().value
                for i, data in enumerate(channelData):
                    dataList[i][dataDesc] = data
            return dataList
    
        def setSamplingFrequency(self, freq):
            for channel in self.activeChannels:
                channel.getAttribute('SamplingFrequency').write(freq)
    
        def setAcquisitionTime(self, acqTime):
            for channel in self.activeChannels:
                channel.getAttribute('AcquisitionTime').write(acqTime)
    
        def setTriggerMode(self, mode):
            if mode not in ["soft", "gate"]:
                raise Exception("Trigger mode must be either soft or gate.")
            for channel in self.activeChannels:
                channel.getAttribute('TriggerMode').write(mode)
    
        def setNrOfTriggers(self, nrOfTriggers):
            self.nrOfTriggers = nrOfTriggers
            for channel in self.activeChannels:
                channel.getAttribute('NrOfTriggers').write(nrOfTriggers)
    
        def configure(self, nrOfTriggers, acqTime, timePerTrigger, sampFreq=-1, triggerMode="gate"):
            self.macro.debug("acqTime: %s" % acqTime)
            if timePerTrigger == None:
                raise Exception("TimePerTrigger attribute must be set")
            acqTime = timePerTrigger * acqTime / 100.0
            self.setTriggerMode(triggerMode)
            self.setNrOfTriggers(nrOfTriggers)
            self.setSamplingFrequency(sampFreq)
            self.setAcquisitionTime(acqTime)
            self.macro.debug("MG: nrOfTriggers: %s, timePerTrigger: %s, acqTime: %s, sampFreq: %s" % (nrOfTriggers,timePerTrigger,acqTime,sampFreq))
    
        def getConfiguration(self):
            return None
    
        def setConfiguration(self, configuration):
            pass
    
    def __init__(self, macro, generator=None,
                 moveables=[], env={}, constraints=[], extrainfodesc=[]):
        CScan.__init__(self, macro, generator=generator,
                       moveables=moveables, env=env, constraints=constraints,
                       extrainfodesc=extrainfodesc)
        self._measurement_group = self.ExtraMntGrp(macro)
        self.extraTrigger = self.ExtraTrigger(macro)            
        
    def prepare_waypoint(self, waypoint, start_positions, iterate_only=False):
        '''Prepare list of MotionPath objects per each physical motor. 
        :param waypoint: (dict) waypoint dictionary with necessary information
        :param start_positions: (list<float>) list of starting position per each
                                 physical motor
        :return (ideal_paths, acc_time, active_time)
                - ideal_paths: (list<MotionPath> representing motion attributes 
                               of each physical motor)
                - acc_time: acceleration time which will be used during the scan
                            it corresponds to the longest acceleration time of 
                            all the motors
                - active_time: time interval while all the physical motors will
                               maintain constant velocity'''

        positions = waypoint['positions']
        active_time = waypoint["active_time"]
        
        ideal_paths = []
        
        max_acc_time, max_dec_time = 0, 0                
        for moveable, end_position in zip(self._physical_moveables, positions):
            motor = moveable
            self.macro.debug("Motor: %s" % motor.getName())
            self.macro.debug("AccTime: %f" % self.get_min_acc_time(motor))
            self.macro.debug("DecTime: %f" % self.get_min_dec_time(motor))
            max_acc_time = max(self.get_min_acc_time(motor), max_acc_time)
            max_dec_time = max(self.get_min_dec_time(motor), max_dec_time)
            
        acc_time = max_acc_time
        dec_time = max_dec_time
            
        for moveable, start_position, end_position in \
                      zip(self._physical_moveables, start_positions, positions):
            base_vel = moveable.getBaseRate()        
            ideal_vmotor = VMotor(accel_time=acc_time,
                                  decel_time=dec_time,
                                  min_vel=base_vel)
            ideal_path = MotionPath(ideal_vmotor, 
                                    start_position, 
                                    end_position, 
                                    active_time)            
            ideal_path.moveable = moveable
            ideal_path.apply_correction = True        
            ideal_paths.append(ideal_path)                                 
        
        return ideal_paths, acc_time, active_time
    
    def _go_through_waypoints(self):
        """Internal, unprotected method to go through the different waypoints.
           It controls all the three objects: motion, trigger and measurement
           group."""
        macro, motion, waypoints = self.macro, self._physical_motion, self.steps
        self.macro.debug("_go_through_waypoints() entering...")
        
        last_positions = None
        for _, waypoint in waypoints:
            self.macro.debug("Waypoint iteration...")
            start_positions = waypoint.get('start_positions')
            positions = waypoint['positions']
            if start_positions is None:
                start_positions = last_positions
            if start_positions is None:
                last_positions = positions
                continue
            
            waypoint_info = self.prepare_waypoint(waypoint, start_positions)
            motion_paths, delta_start, acq_duration = waypoint_info
            
            self.acq_duration = acq_duration
                        
            #execute pre-move hooks
            for hook in waypoint.get('pre-move-hooks',[]): 
                hook()
    
            start_pos, final_pos = [] , []
            for path in motion_paths:
                start_pos.append(path.initial_user_pos)
                final_pos.append(path.final_user_pos)
            
            if macro.isStopped():
                self.on_waypoints_end()
                return

            self.__mntGrpConfigured = False
            self.__triggerConfigured = False
            self.__mntGrpStarted = False
            self.__triggerStarted = False
            
            #validation of parameters
            for start, end in zip(self.macro.starts, self.macro.finals):
                if start == end:
                    raise Exception("Start and End can not be equal.")

            startTimestamp = time.time()
        
            #extra pre configuration
            if hasattr(macro, 'getHooks'):
                for hook in macro.getHooks('pre-configuration'):
                    hook()
            self.macro.checkPoint()
    
            #configuring trigger lines
            oldHighTime, oldLowTime, oldDelay, oldNrOfTriggers = \
                                        self.extraTrigger.getConfiguration()
            self.__triggerConfigured = True
            timePerTrigger = self.extraTrigger.configure(delayTime=delta_start,
                                           scanTime=acq_duration,
                                           nrOfTriggers=self.macro.nr_of_points)
            self.macro.checkPoint()
    
            #configuring measurementGroup
            self.mntGrpConfiguration = self._measurement_group.getConfiguration()
            self.__mntGrpConfigured = True
            self._measurement_group.configure(self.macro.nr_of_points, 
                                       self.macro.acq_time, 
                                       timePerTrigger)
            self.macro.checkPoint()
    
            #extra post configuration
            if hasattr(macro, 'getHooks'):
                for hook in macro.getHooks('post-configuration'):
                    hook()
            self.macro.checkPoint()
    
            endTimestamp = time.time()
            self.macro.info("Configuration took %s time." % repr(endTimestamp - startTimestamp))

            # move to start position
            self.macro.debug("Moving to start position: %s" % repr(start_pos))
            motion.move(start_pos)
            
            if macro.isStopped():
                self.on_waypoints_end()
                return
            
            # prepare motor(s) to move with their maximum velocity
            for path in motion_paths:
                motor = path.moveable
                self.macro.debug("Motor: %s" % motor.getName())
                self.macro.debug("Velocity: %f" % path.max_vel)
                self.macro.debug("AccTime: %f" % path.max_vel_time)
                self.macro.debug("DecTime: %f" % path.min_vel_time)
                #TODO: check why we have 0 here
                #if 0 in [path.max_vel, path.max_vel_time, path.min_vel_time]:
                #    continue
                motor.setVelocity(path.max_vel)
                motor.setAcceleration(path.max_vel_time)                
                motor.setDeceleration(path.min_vel_time)
                
            if macro.isStopped():
                self.on_waypoints_end()
                return
            
            if hasattr(macro, 'getHooks'):
                for hook in macro.getHooks('pre-start'):
                    hook()
            self.macro.checkPoint()
    
            self.macro.debug("Starting measurement group")
            self.__mntGrpStarted = True
            self._measurement_group.start()            
            
            self.timestamp_to_start = time.time() + delta_start
                        
            self.motion_event.set()
            
            # move to waypoint end position
            self.macro.debug("Moving to waypoint position: %s" % repr(final_pos))
            self.macro.debug("Starting triggers")
            self.__triggerStarted = True
            self.extraTrigger.start()
            motion.move(final_pos)
                        
            self.motion_event.clear()

            if macro.isStopped():
                self.on_waypoints_end()
                return
            
            #execute post-move hooks
            for hook in waypoint.get('post-move-hooks',[]):
                hook()
                    
            self.macro.debug("Waiting for measurement group to finish")            
            while self._measurement_group.isMoving():
                self.macro.checkPoint()
                time.sleep(0.1)
                
            self.macro.debug("Getting data")                
            data_list = self._measurement_group.getDataList()
            
            def populate_ideal_positions():
                moveables = self.moveables
                nr_of_points = self.macro.nr_of_points
                starts = self.macro.starts
                finals = self.macro.finals
                positions_records = [{} for i in xrange(nr_of_points)]
                
                for moveable, start, final in zip(moveables, starts, finals):
                    name = moveable.moveable.getName()
                    for point_nr, position in enumerate(np.linspace(start, \
                                                        final, nr_of_points)):
                        positions_records[point_nr][name] = position    
                    
                return positions_records
            
            #TODO: decide what to do with moveables
            position_list = populate_ideal_positions()

            self.macro.debug("Storing data")
            for data_dict, position_dict in zip(data_list,position_list):
                data_dict.update(position_dict)
                self.data.addRecord(data_dict)    
            
            if start_positions is None:  
                last_positions = positions
        
        self.on_waypoints_end(positions)
        
    def on_waypoints_end(self, restore_positions=None):
        """To be called by the waypoint thread to handle the end of waypoints
        (either because no more waypoints or because a macro abort was
        triggered)

        .. todo:: Unify this method for all the continuous scans. Hint: use
                  the motion property and return the _physical_motion member
                  instead of _motion or in both cases: CSScan and CTScan
                  coordinate the physical motors' velocit.
        """
        self.macro.debug("on_waypoints_end() entering...")
        self.set_all_waypoints_finished(True)
        if restore_positions is not None:
            self._setFastMotions()
            self.macro.info("Correcting overshoot...")
            self._physical_motion.move(restore_positions)
        self.do_restore()
        self.motion_end_event.set()        
        self.motion_event.set()
        self.cleanup()

    def scan_loop(self):        
        macro = self.macro
        manager = macro.getManager()
        scream = False
        motion_event = self.motion_event
        startts = self._env['startts']
        
        sum_delay = 0
        sum_integ_time = 0
        
        if hasattr(macro, "nr_points"):
            nr_points = float(macro.nr_points)
            scream = True
        else:
            yield 0.0

        moveables = [ m.moveable for m in self.moveables ]
        
        point_nb, step = -1, None
        data = self.data
        
        if hasattr(macro, 'getHooks'):
            for hook in macro.getHooks('pre-scan'):
                hook()
                
        self.go_through_waypoints()
        
        
        if hasattr(macro, 'getHooks'):
            for hook in macro.getHooks('post-scan'):
                hook()
        
        env = self._env
        env['acqtime'] = sum_integ_time
        env['delaytime'] = sum_delay
        
        if not scream:
            yield 100.0   

    def cleanup(self):
        '''This method is responsible for restoring state of measurement group 
        and trigger to its state before the scan.'''
        startTimestamp = time.time()

        if self.__mntGrpStarted:
            self.debug("Stopping measurement group")
            try:
                self._measurement_group.stop()
            except Exception, e:
                self.warning("Exception while trying to stop measurement group.")
                self.debug(e)

        if self.__triggerStarted:
            self.debug("Stopping triggers")
            try:
                self.extraTrigger.stop()
            except Exception, e:
                self.warning("Exception while trying to stop trigger.")
                self.debug(e)

        if hasattr(self.macro, 'getHooks'):
            for hook in self.macro.getHooks('pre-cleanup'):
                self.debug("Executing pre-cleanup hook")
                try:
                    hook()
                except Exception, e:
                    self.warning("Exception while trying to execute a pre-cleanup hook")
                    self.debug(e)

        if self.__mntGrpConfigured:
            self.debug("Restoring configuration of measurement group")
            try:
                self._measurement_group.setConfiguration(self.mntGrpConfiguration)
                #TODO: mntGrp configuration should contain also: nrOfTriggers, acqTime, sampling frequency
            except:
                self.warning("Exception while trying to restore measurement group parameters")
                self.debug(e)

        if hasattr(self.macro, 'getHooks'):
            for hook in self.macro.getHooks('post-cleanup'):
                self.debug("Executing post-cleanup hook")
                try:
                    hook()
                except Exception, e:
                    self.warning("Exception while trying to execute a post-cleanup hook")
                    self.debug(e)

        endTimestamp = time.time()
        self.debug("Cleanup took %s time." % repr(endTimestamp - startTimestamp))


class HScan(SScan):
    """Hybrid scan"""
    
    def stepUp(self, n, step, lstep):
        motion, mg = self.motion, self.measurement_group
        startts = self._env['startts']
        
        #pre-move hooks
        for hook in step.get('pre-move-hooks',()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except:
                pass
                
        positions, integ_time = step['positions'], step['integ_time']
        
        try:
            m_ID = motion.startMove(positions)
            mg_ID = mg.startCount(integ_time)    
        except InterruptException:
            raise
        except:
            self.dump_information(n, step)
            raise

        try:
            motion.waitMove(id=m_ID)
            mg.waitCount(id=mg_ID)
        except InterruptException:
            raise
        except:
            self.dump_information(n, step)
            raise
        self._sum_acq_time += integ_time
        
        curr_time = time.time()
        dt = curr_time - startts
                
        m_state, m_positions = motion.readState(), motion.readPosition()       
         
        if m_state != Ready:
            self.dump_information(n, step)
            m = "Scan aborted after problematic motion: " \
                "Motion ended with %s\n" % str(m_state)
            raise ScanException({ 'msg' : m })

        data_line = mg.getValues()
        
        # Add final moveable positions
        data_line['point_nb'] = n
        data_line['timestamp'] = dt
        for i, m in enumerate(self.moveables):
            data_line[m.moveable.getName()] = m_positions[i]
        
        #Add extra data coming in the step['extrainfo'] dictionary
        if step.has_key('extrainfo'): data_line.update(step['extrainfo'])
        
        self.data.addRecord(data_line)
    
        #post-step hooks
        for hook in step.get('post-step-hooks',()):
            hook()
            try:
                step['extrainfo'].update(hook.getStepExtraInfo())
            except InterruptException:
                raise
            except:
                pass
        
    def dump_information(self, n, step):
        moveables = self.motion.moveable_list
        msg = ["Report: Stopped at step #" + str(n) + " with:"]
        for moveable in moveables:
            msg.append(moveable.information())
        self.macro.info("\n".join(msg))