def main():
    """
    Entry point for command 'impact-lattice'.
    """
    args = parser.parse_args(sys.argv[2:])

    if args.verbosity == 1:
        logging.getLogger().setLevel(logging.INFO)
    elif args.verbosity > 1:
        logging.getLogger().setLevel(logging.DEBUG)


    if (args.latticepath != None) and os.path.exists(args.latticepath):
        print("Destination file already exists: {}".format(args.latticepath), file=sys.stderr)
        return 1

    if (args.chanmap != None) and os.path.exists(args.chanmap):
        print("Destination file already exists: {}".format(args.chanmap), file=sys.stderr)
        return 1

    if (args.latdata != None) and os.path.exists(args.latdata):
        print("Destination file already exists: {}".format(args.latdata), file=sys.stderr)
        return 1


    try:
        mconfig, submach = loadMachineConfig(args.machine, args.submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading machine configuration:", e, file=sys.stderr)
        return 1


    try:
        layout = loadLayout(args.layoutpath, mconfig, submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading layout:", e, file=sys.stderr)
        return 1


    try:
        settings = loadSettings(args.settingspath, mconfig, submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading settings:", e, file=sys.stderr)
        return 1


    try:
        config = loadLatticeConfig(args.configpath, mconfig, submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading lattice configuration:", e, file=sys.stderr)
        return 1


    try:
        lat = impact.build_lattice(layout, config=config, settings=settings,
                                   start=args.start, end=args.end, template=args.template)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error building lattice:", e, file=sys.stderr)
        return 1


    if args.chanmap != None:
        try:
            channels = loadChannels(args.cfsurl, args.cfstag, mconfig, submach)
        except Exception as e:
            if args.verbosity > 0: traceback.print_exc()
            print("Error loading channels:", e, file=sys.stderr)
            return 1

        with open(args.chanmap, "w") as fp:
            _write_channel_map(layout, lat, channels, fp)

    if args.latdata != None:
        with open(args.latdata, "w") as fp:
            _write_lattice_data(lat, fp)

    if args.latticepath != None:
        name, _ = os.path.splitext(args.latticepath)
        maps = name + ".map"
        with open(args.latticepath, "w") as fp, open(maps, "w") as fmp:
            lat.write(fp, fmp, withElemData=args.with_elem_data)
    else:
        lat.write(sys.stdout, withElemData=args.with_elem_data)

    return 0
def main():
    """
    Entry point for command 'impact-vastart'.
    """
    args = parser.parse_args(sys.argv[2:])

    if args.verbosity == 1:
        logging.getLogger().setLevel(logging.INFO)
    elif args.verbosity > 1:
        logging.getLogger().setLevel(logging.DEBUG)


    try:
        mconfig, submach = loadMachineConfig(args.machine, args.submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error readings machine configuration:", e, file=sys.stderr)
        return 1


    try:
        layout = loadLayout(args.layoutpath, mconfig, submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading layout:", e, file=sys.stderr)
        return 1


    try:
        settings = loadSettings(args.settingspath, mconfig, submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading settings:", e, file=sys.stderr)
        return 1


    try:
        config = loadLatticeConfig(args.configpath, mconfig, submach)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error loading configuration:", e, file=sys.stderr)
        return 1


    channels = loadChannels(args.cfsurl, None, mconfig, submach)


    try:
        va = build_virtaccel(layout, config=config, channels=channels, settings=settings,
                             start=args.start, end=args.end, data_dir=args.datapath, work_dir=args.workpath)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error building virtual accelerator:", e, file=sys.stderr)
        return 1


    try:
        va.start(True)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error starting virtual accelerator:", e, file=sys.stderr)
        return 1


    try:
        va.wait()
    except KeyboardInterrupt:
        va.stop()
        va.wait()
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error executing virtual accelerator:", e, file=sys.stderr)
        return 1


    return 0