Skip to content
Snippets Groups Projects
FofbTool.py 24 KiB
Newer Older
BRONES Romain's avatar
BRONES Romain committed
#!/usr/bin/env python

BRONES Romain's avatar
BRONES Romain committed
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
import tango
import logging
import argparse
import numpy as np
import time


###################################################################################################
#    TANGO DEVICE PROXIES
###################################################################################################

cellnode_subscribers = [ tango.DeviceProxy("ans/dg/fofb-cellnode-{}".format(cell)) for cell in ("C01", "C06", "C09", "C14") ]

centralnode_subscriber = tango.DeviceProxy("ans/dg/fofb-centralnode")

bpmmanager = tango.DeviceProxy("ans/dg/bpm-manager")

fofbcommand = tango.DeviceProxy("ans/dg/fofb-command")

###################################################################################################
#    LOGGER
###################################################################################################
# Install a BasicLogger
logger = logging.getLogger("TestDev")
sh=logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(logging.Formatter("{levelname:8}: {message}", style='{'))


###################################################################################################
#    TANGO DB ATTRIBUTE CONFIG
###################################################################################################

def set_attr_config(cnp):
    if "central" in cnp.name():
        set_attr_config_centralnode(cnp)
    else:
        set_attr_config_cellnode(cnp)


def set_attr_config_cellnode(cnp):

    logger.info("Apply attribute configuration on device {}".format(cnp.name()))

    c = cnp.get_attribute_config("app_version")
    c.description = "FPGA firmware version\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccneth0_gt_reset")
    c.description = "ComCellNode transceiver reset register, write 1 to reset.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.min_value='0'
    c.max_value='7'
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccneth0_reset")
    c.description = "ComCellNode data & serdes reset register, write 0xE0000001 to reset.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.min_value='0'
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccneth0_rx_framerate")
    c.description = "ComCellNode RX frame rate.\n" +c.description
    c.format = '%d'
    c.unit = 'fps'
    c.alarms.min_warning='10000'
    c.alarms.max_warning='11000'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccneth0_tx_framerate")
    c.description = "ComCellNode TX frame rate.\n" +c.description
    c.format = '%d'
    c.unit = 'fps'
    c.alarms.min_warning='10000'
    c.alarms.max_warning='11000'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccneth0_tx_errorrate")
    c.description = "ComCellNode TX erroneous frame rate.\n" +c.description
    c.format = '%d'
    c.unit = 'fps'
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccneth0_gt_status")
    c.description = "ComCellNode ethernet status. Expected value 0x70 or 0x71\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.alarms.min_warning=str(0x6f)
    c.alarms.max_warning=str(0x72)
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnpack0_npackets")
    c.description = "ComCellNode number of packet to send -1. Usually 31 for C14 and 29 for others.\n" +c.description
    c.format = '%d'
    c.unit = 'packets'
    c.min_value='0'
    c.max_value='999'
    c.alarms.min_warning='28'
    c.alarms.max_warning='32'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnpack0_control")
    c.description = "ComCellNode packeter control, write 1 to start.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.min_value='0'
    c.max_value='1'
    c.alarms.min_warning='0'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnpack0_framesize")
    c.description = "ComCellNode output frame size. 330 for C14 else 310.\n" +c.description
    c.format = '%d'
    c.unit = 'bytes'
    c.min_value='0'
    c.max_value='999'
    c.alarms.min_warning='309'
    c.alarms.max_warning='331'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnpack0_error")
    c.description = "ComCellNode packeter protocol error flags.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnpack0_framecount")
    c.description = "ComCellNode number of frame sent.\n" +c.description
    c.format = '%d'
    c.unit = 'frames'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnunpack0_framecount")
    c.description = "ComCellNode number of frame received.\n" +c.description
    c.format = '%d'
    c.unit = 'frames'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnunpack0_control")
    c.description = "ComCellNode unpacketer control, write 1 to start.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.min_value='0'
    c.max_value='1'
    c.alarms.min_warning='0'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnunpack0_error")
    c.description = "ComCellNode unpacketer protocol error flags.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("ccnunpack0_framesize")
    c.description = "ComCellNode input frame size. Usual value = 610.\n" +c.description
    c.format = '%d'
    c.unit = 'bytes'
    c.min_value='0'
    c.max_value='999'
    c.alarms.min_warning='609'
    c.alarms.max_warning='611'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_protocol_status")
    c.description = "ComCellNode ethernet status. Expected value 0x00\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_sfp")
    c.description = "ComBPM SFP status. Expected value 0x00\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_gt_status")
    c.description = "ComBPM transceiver status.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_valid_framerate")
    c.description = "ComBPM RX frame rate.\n" +c.description
    c.format = '%d'
    c.unit = 'fps'
    c.alarms.min_warning='1220000'
    c.alarms.max_warning='1230000'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_invalid_framecount")
    c.description = "ComBPM RX invalid frame rate. Should not increase to quickly (<1/s)\n" +c.description
    c.format = '%d'
    c.unit = 'frames'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_frameseq")
    c.description = "ComBPM number of frames received -1 in last sequence.\n" +c.description
    c.format = '%d'
    c.unit = 'frames'
    c.alarms.min_warning='121'
    c.alarms.max_warning='123'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_reset")
    c.description = "ComBPM protocol reset register, write 1 to reset.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.min_value='0'
    c.max_value='1'
    c.alarms.max_warning='1'
    cnp.set_attribute_config(c)

    c = cnp.get_attribute_config("combpm_gt_control")
    c.description = "ComBPM transceiver reset register, 1 to enable, 5 to reset.\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    c.min_value='0'
    c.max_value='7'
    c.alarms.min_warning='0'
    c.alarms.max_warning='2'
    cnp.set_attribute_config(c)

def set_attr_config_centralnode(cnp):

    logger.info("Apply attribute configuration on device {}".format(cnp.name()))

    c = cnp.get_attribute_config("app_version")
    c.description = "FPGA firmware version\n" +c.description
    c.format = '0x%X'
    c.unit = ' '
    cnp.set_attribute_config(c)

    for n in range(4):
        c = cnp.get_attribute_config("ccneth{}_gt_reset".format(n))
        c.description = "ComCellNode transceiver reset register, write 1 to reset.\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.min_value='0'
        c.max_value='7'
        c.alarms.max_warning='1'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccneth{}_reset".format(n))
        c.description = "ComCellNode data & serdes reset register, write 0xE0000001 to reset.\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.min_value='0'
        c.alarms.max_warning='1'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccneth{}_rx_framerate".format(n))
        c.description = "ComCellNode RX frame rate.\n" +c.description
        c.format = '%d'
        c.unit = 'fps'
        c.alarms.min_warning='10000'
        c.alarms.max_warning='11000'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccneth{}_tx_framerate".format(n))
        c.description = "ComCellNode TX frame rate.\n" +c.description
        c.format = '%d'
        c.unit = 'fps'
        c.alarms.min_warning='10000'
        c.alarms.max_warning='11000'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccneth{}_tx_errorrate".format(n))
        c.description = "ComCellNode TX erroneous frame rate.\n" +c.description
        c.format = '%d'
        c.unit = 'fps'
        c.alarms.max_warning='1'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccneth{}_gt_status".format(n))
        c.description = "ComCellNode ethernet status. Expected value 0x70 or 0x71\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.alarms.min_warning=str(0x6f)
        c.alarms.max_warning=str(0x72)
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnpack{}_npackets".format(n))
        c.description = "ComCellNode number of packet to send -1. Usual value 99.\n" +c.description
        c.format = '%d'
        c.unit = 'packets'
        c.min_value='0'
        c.max_value='999'
        c.alarms.min_warning='98'
        c.alarms.max_warning='100'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnpack{}_control".format(n))
        c.description = "ComCellNode packeter control, write 1 to start.\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.min_value='0'
        c.max_value='1'
        c.alarms.min_warning='0'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnpack{}_framesize".format(n))
        c.description = "ComCellNode output frame size. Usual value 610.\n" +c.description
        c.format = '%d'
        c.unit = 'bytes'
        c.min_value='0'
        c.max_value='999'
        c.alarms.min_warning='609'
        c.alarms.max_warning='611'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnpack{}_error".format(n))
        c.description = "ComCellNode packeter protocol error flags.\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.alarms.max_warning='1'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnpack{}_framecount".format(n))
        c.description = "ComCellNode number of frame sent.\n" +c.description
        c.format = '%d'
        c.unit = 'frames'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnunpack{}_framecount".format(n))
        c.description = "ComCellNode number of frame received.\n" +c.description
        c.format = '%d'
        c.unit = 'frames'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnunpack{}_control".format(n))
        c.description = "ComCellNode unpacketer control, write 1 to start.\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.min_value='0'
        c.max_value='1'
        c.alarms.min_warning='0'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnunpack{}_error".format(n))
        c.description = "ComCellNode unpacketer protocol error flags.\n" +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.alarms.max_warning='1'
        cnp.set_attribute_config(c)

        c = cnp.get_attribute_config("ccnunpack{}_framesize".format(n))
        c.description = "ComCellNode input frame size. 330 from C14 else 310.\n" +c.description
        c.format = '%d'
        c.unit = 'bytes'
        c.min_value='0'
        c.max_value='999'
        c.alarms.min_warning='309'
        c.alarms.max_warning='331'
        cnp.set_attribute_config(c)

    for p in "xy":

        c = cnp.get_attribute_config("corr_control_{}".format(p))
        c.description = "Correction controller for {} plan. 1 is feedback running, 2 is reset correctors.\n".format(p.upper()) +c.description
        c.format = '0x%X'
        c.unit = ' '
        c.min_value='0'
        c.max_value='2'
        cnp.set_attribute_config(c)

        for n in "12":
            for cf in ("a","b","ic","d"):
                c = cnp.get_attribute_config("corr_k{}{}_{}".format(n,cf,p))
                c.description = "Correction controller coefficient {}, filter {} plan\n".format(n,cf,p) +c.description
                c.format = '%d'
                c.unit = ' '
                cnp.set_attribute_config(c)



###################################################################################################
#    OPERATION FUNCTIONS
###################################################################################################

def cellnode_stop_ccn(cnp):
    logger.info("Stopping CCN on {}".format(cnp.name()))
    cnp["ccnpack0_control"] = False
    cnp["ccnunpack0_control"] = False

def cellnode_start_ccn(cnp):
    logger.info("Starting CCN on {}".format(cnp.name()))
    cnp["ccneth0_reset"] = 0x60000001 # impossible to write 0xE0000001
    cnp["ccneth0_gt_reset"] = 1
    cnp["ccneth0_gt_reset"] = 0
    cnp["ccneth0_reset"] = 0
    cnp["ccnpack0_control"] = True
    cnp["ccnunpack0_control"] = True

def cellnode_ack_ccn(snp):
    logger.info("Ack CCN error on {}".format(cnp.name()))
    cnp["ccnpack0_reset_error"] = True
    cnp["ccnunpack0_reset_error"] = True
    cnp["ccnpack0_reset_error"] = False
    cnp["ccnunpack0_reset_error"] = False

def centralnode_stop_ccn(cnp):
    logger.info("Stopping CCN on {}".format(cnp.name()))
    for n in range(4):
        cnp["ccnpack{}_control".format(n)] = False
        cnp["ccnunpack{}_control".format(n)] = False

def centralnode_start_ccn(cnp):
    logger.info("Starting CCN on {}".format(cnp.name()))
    for n in range(4):
        cnp["ccneth{}_reset".format(n)] = 0x60000001 # impossible to write 0xE0000001
        cnp["ccneth{}_gt_reset".format(n)] = 1
        cnp["ccneth{}_gt_reset".format(n)] = 0
        cnp["ccneth{}_reset".format(n)] = 0
        cnp["ccnpack{}_control".format(n)] = True
        cnp["ccnunpack{}_control".format(n)] = True
        cnp["ccnpack{}_reset_error".format(n)] = True
        cnp["ccnunpack{}_reset_error".format(n)] = True
        cnp["ccnpack{}_reset_error".format(n)] = False
        cnp["ccnunpack{}_reset_error".format(n)] = False

def cellnode_configure_ccn(cnp, nbpm=None, npsc=100):
    """
    Configure the ComCellNode block.

    PARAMETERS
    ----------
    nbpm:
        Number of BPM allowed by the filter, hence the number of expected BPM packets.
        If None, auto detect the number from the combpm_filter_table attribute.
    npsc:
        Number of total PSC, hence the number of expected PSC packets.

    """

    if nbpm is None:
        # Get number of BPM activated in the filter
        nbpm = int((cnp["combpm_filter_table"].value == 0x80).sum())
        logger.debug("{} bpm allowed in the filter on {}".format(nbpm, cnp.name()))

    maclen = 10*nbpm+10
    logger.debug("Configure packeter framesize (mac length) to {}".format(maclen))
    cnp["ccnpack0_framesize"] = maclen

    logger.debug("Configure packeter npackets to {}".format(nbpm-1))
    cnp["ccnpack0_npackets"] = nbpm-1

    maclen = npsc*6+10
    logger.debug("Configure unpacketer framesize (mac length) to {}".format(maclen))
    cnp["ccnunpack0_framesize"] = maclen

    logger.info("Configuration of CCN done on {}.".format(cnp.name()))

def centralnode_configure_ccn(cnp, nbpm=[30,30,30,32], npsc=100):
    """
    Configure the ComCellNode block.

    PARAMETERS
    ----------
    nbpm: list(int)
        Number of BPM packet received on each interface.
    npsc:
        Number of total PSC, hence the number of expected PSC packets.

    """

    for n in range(4):

        maclen = npsc*6+10
        logger.debug("Configure packeter {} framesize (mac length) to {}".format(n, maclen))
        cnp["ccnpack{}_framesize".format(n)] = maclen

        logger.debug("Configure packeter {}  npackets to {}".format(n, npsc-1))
        cnp["ccnpack{}_npackets".format(n)] = npsc-1

        maclen = 10*nbpm[n]+10
        logger.debug("Configure unpacketer {} framesize (mac length) to {}".format(n, maclen))
        cnp["ccnunpack{}_framesize".format(n)] = maclen

    logger.info("Configuration of CCN done on {}.".format(cnp.name()))


def cellnode_stop_combpm(cnp):
    logger.info("Stopping ComBpm on {}".format(cnp.name()))
    cnp["combpm_reset"] = 1
    cnp["combpm_gt_control"] = 0x5

def cellnode_start_combpm(cnp):
    logger.info("Starting ComBpm on {}".format(cnp.name()))
    cnp["combpm_reset"] = 0
    cnp["combpm_gt_control"] = 0x1
    cnp["combpm_reset_error"] = True
    cnp["combpm_reset_error"] = False

def cellnode_configure_combpm(cnp):

    # Filter list depends on the cellnode
    filterlist = {
            "C01":np.array(list(range(1,23))+list(range(115,123))),
            "C06":np.array(range(23,53)),
            "C09":np.array(range(53,83)),
            "C14":np.array(range(83,115)),
            }

    for k in filterlist.keys():
        if k in cnp.name():
            logger.debug("Activate BPMs {} in the filter for {}".format(filterlist[k], cnp.name()))
            f = cnp["combpm_filter_table"].value
            f[:] = 0
            f[np.array(filterlist[k])] = 0x80
            cnp["combpm_filter_table"] = f

    logger.info("Configuration of ComBpm done on {}.".format(cnp.name()))

def cellnode_configure_comcorr(cnp, enable=True):

    pscidlist = {
        'C01':[50,100,49,99,255,255,255,255,2,52,1,51,255,255,255,255,56,6,3,53,4,54,5,55,10,60,7,57,8,58,9,59],
        'C06':[12,62,11,61,255,255,255,255,14,64,13,63,255,255,255,255,68,18,15,65,16,66,17,67,22,72,19,69,20,70,21,71],
        'C09':[24,74,23,73,255,255,255,255,26,76,25,75,255,255,255,255,80,30,27,77,28,78,29,79,34,84,31,81,32,82,33,83],
        'C14':[36,86,35,85,37,87,38,88,40,90,39,89,255,255,255,255,94,44,41,91,42,92,43,93,48,98,45,95,46,96,47,97]
        }

    for k in pscidlist.keys():
        if k in cnp.name():
            logger.debug("Set PSCIDs {} in the filter for {}".format(pscidlist[k], cnp.name()))
            f= cnp["comcorr_line_id"].value
            f[:] = pscidlist[k]
            f[:] = f+ 0x10000*bool(enable)
            logger.debug("Set comcorr_line_id {} for {}".format(f, cnp.name()))
            cnp["comcorr_line_id"] = f

    logger.info("Configuration of ComCorr done on {}.".format(cnp.name()))

def centralnode_configure_corr(cnp):

    # Legacy
    cnp["corr_k1a_x"]=256
    cnp["corr_k1b_x"]=0
    cnp["corr_k1ic_x"]=64
    cnp["corr_k1d_x"]=16300

    cnp["corr_k1a_y"]=256
    cnp["corr_k1b_y"]=0
    cnp["corr_k1ic_y"]=64
    cnp["corr_k1d_y"]=16300

    # Unitary
    cnp["corr_k2a_x"]=128
    cnp["corr_k2b_x"]=0
    cnp["corr_k2ic_x"]=8192
    cnp["corr_k2d_x"]=0

    cnp["corr_k2a_y"]=128
    cnp["corr_k2b_y"]=0
    cnp["corr_k2ic_y"]=8192
    cnp["corr_k2d_y"]=0

    f= cnp["corr_pscid"].value
    f[0:100] = range(1,101)
    cnp["corr_pscid"] = f

    logger.info("Configuration of Corr done on {}.".format(cnp.name()))

def fofbcommand_writeref(what='current'):
    if what=="current":
        fofbcommand["x_ref_orbit"] = (bpmmanager.xmeanorbit*1e6).astype(int)
        fofbcommand["y_ref_orbit"] = (bpmmanager.zmeanorbit*1e6).astype(int)

    if what=="oldref":
        fofbcommand["x_ref_orbit"] = (bpmmanager.xreforbit*1e6).astype(int)
        fofbcommand["y_ref_orbit"] = (bpmmanager.zreforbit*1e6).astype(int)

    if what=="zeros":
        fofbcommand["x_ref_orbit"] = np.zeros(122, dtype=int)
        fofbcommand["y_ref_orbit"] = np.zeros(122, dtype=int)

def fofbcommand_writemat(filename='respmat.npy'):

    fofbcommand["x_inv_resp_mat"] = np.hstack([np.zeros((50, 1)), np.load("respmat.npy")[50:], np.zeros((50,5))]).astype(int)
    fofbcommand["y_inv_resp_mat"] = np.hstack([np.zeros((50, 1)), np.load("respmat.npy")[:50], np.zeros((50,5))]).astype(int)

###################################################################################################
#
###################################################################################################

if __name__ == '__main__':

    parser = argparse.ArgumentParser("FofbTool")
    parser.add_argument("--log", default="info",
            help="Log level (error, warning, info, debug)")
    parser.add_argument("--init", action="store_true",
            help="Run init devices.")
    parser.add_argument("--confDS", action="store_true",
            help="Configure the Device servers, by applying attribute configuration.")
    parser.add_argument("--stop", action="store_true",
            )
    parser.add_argument("--conf", action="store_true",
            )
    parser.add_argument("--start", action="store_true",
            )
    parser.add_argument("--disable-corr", action="store_true",
            help="Disable cellnode corrector output frames."
            )
    parser.add_argument("--respmat", default=None,
            help="Configure response matrix from file"
            )
    parser.add_argument("node", default="allnodes", nargs='*',
            choices=('c01', 'c06', 'c09', 'c14', 'central', 'allcells', 'allnodes'),
            help="Node to apply the selected actions. Default is all.")

    args = parser.parse_args()

    #### Install logger
    # Remove handler previously attached
    for hdlr in logger.handlers:
        logger.removeHandler(hdlr)
    # Attach this handler
    logger.addHandler(sh)
    # Set log level from args
    logger.setLevel(getattr(logging, args.log.upper()))
    ####

    logger.debug(args)


    #### Node selection
    nodes = []
    if 'allnodes' in args.node:
        nodes = cellnode_subscribers + [centralnode_subscriber,]
    if 'allcells' in args.node:
        nodes = nodes +  cellnode_subscribers
    if 'central' in args.node:
        nodes.append(centralnode_subscriber)
    if 'c01' in args.node:
        nodes.append(cellnode_subscribers[0])
    if 'c06' in args.node:
        nodes.append(cellnode_subscribers[1])
    if 'c09' in args.node:
        nodes.append(cellnode_subscribers[2])
    if 'c14' in args.node:
        nodes.append(cellnode_subscribers[3])

    nodes = list(set(nodes))
    logger.debug("Perform actions on {}".format(nodes))
    ####

    if args.init:
        for cnp in nodes:
            logger.info("Apply command init on {}".format(cnp.name()))
            cnp.init()

    if args.confDS:
        for cnp in nodes:
            set_attr_config(cnp)

    if args.stop:
        for cnp in cellnode_subscribers:
            cellnode_stop_ccn(cnp)
            cellnode_stop_combpm(cnp)
        centralnode_stop_ccn(centralnode_subscriber)

    if not args.respmat is None:
        centralnode_configure_respmat(centralnode_subscriber, args.respmat)



    if args.conf:
        for cnp in cellnode_subscribers:
            cellnode_configure_combpm(cnp)
            cellnode_configure_comcorr(cnp, not args.disable_corr)
            time.sleep(2)
            cellnode_configure_ccn(cnp)
        centralnode_configure_ccn(centralnode_subscriber)
        centralnode_configure_corr(centralnode_subscriber)
    elif args.disable_corr:
        for cnp in cellnode_subscribers:
            cellnode_configure_comcorr(cnp, not args.disable_corr)

    if args.start:
        for cnp in cellnode_subscribers:
            cellnode_start_ccn(cnp)
            cellnode_start_combpm(cnp)
            cellnode_ack_ccn(cnp)
        centralnode_start_ccn(centralnode_subscriber)