def main():
    """
    Entry point for command 'impact-settings'.
    """
    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 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 = build_settings(layout, args.latticepath, start=args.start, end=args.end)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error building settings:", e, file=sys.stderr)
        return 1


    try:
        if args.settingspath is not None:
            with open(args.settingspath, "w") as fp:
                json.dump(settings, fp, indent=2)
        else:
            json.dump(settings, sys.stdout, indent=2)
    except Exception as e:
        print("Error writing settings:", e, file=sys.stderr)
        return 1

    return 0
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-model'.
    """
    args = parser.parse_args(sys.argv[2:])

    def rm_temp_dir(path):
        if args.workpath == None:
            shutil.rmtree(path)

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


    if (args.resultpath != None) and os.path.exists(args.resultpath):
        print("Error: destination result path already exists:", args.resultpath, 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 configuration:", e, file=sys.stderr)
        return 1


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

    lattice.outputMode = OUTPUT_MODE_END

    try:
        result_dir = run_lattice(lattice, config=config, data_dir=args.datapath, work_dir=args.workpath)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error running lattice:", e, file=sys.stderr)
        return 1

    try:
        result = build_result(impact="FRIB", directory=result_dir, keep=True)
    except Exception as e:
        if args.verbosity > 0: traceback.print_exc()
        print("Error building result:", e, file=sys.stderr)
        rm_temp_dir(result_dir)
        return 1


    spos = result.getSPosition()

    energy = result.getEnergy()

    xorbit = result.getOrbit("X")
    yorbit = result.getOrbit("Y")

    xrms = result.getBeamRms("X")
    yrms = result.getBeamRms("Y")
    #zrms = result.getBeamRms("Z")

    xalpha = result.getTwissAlpha("X")
    yalpha = result.getTwissAlpha("Y")

    xemit = result.getEmittance("X")
    yemit = result.getEmittance("Y")
    zemit = result.getEmittance("Z")

    if args.plot:
        try:
            plt.figure(figsize=(16,10), dpi=80)
            plt.subplot(221)
            plt.title("Beam Orbit")
            plt.plot(spos, xorbit, 'r-', label="X")
            plt.plot(spos, yorbit, 'b-', label="Y")
            plt.xlabel("S [m]")
            plt.ylabel("Beam Position [m]")
            plt.legend(loc="upper left")
            plt.grid()

            plt.subplot(222)
            plt.title("Beam RMS")
            plt.plot(spos, xrms, 'r-', label="X")
            plt.plot(spos, yrms, 'b-', label="Y")
            #plt.plot(zrms[:,0], zrms[:,1], 'g-', label="Z")
            plt.xlabel("S [m]")
            plt.ylabel("Beam RMS [m]")
            plt.legend(loc="upper left")
            plt.grid()

            plt.subplot(223)
            plt.title("Beam Energy")
            plt.plot(spos, energy, 'r-')
            plt.xlabel("S [m]")
            plt.ylabel("Beam Energy [MeV]")
            plt.grid()

            plt.subplot(224)
            plt.title("Beam Emittance")
            plt.plot(spos, xemit, 'r-', label="X")
            plt.plot(spos, yemit, 'b-', label="Y")
            #plt.plot(zemit[:,0], zemit[:,1], 'g-', label="Z")
            plt.xlabel("S [m]")
            plt.ylabel("Beam Emittance [m-rad]")
            plt.legend(loc="upper left")
            plt.grid()

            if args.resultpath == None:
                plt.show()
            else:
                plt.savefig(args.resultpath)

        except Exception as e:
            if args.verbosity > 0: traceback.print_exc()
            print("Error generating plot: ", e, file=sys.stderr)

    else:
        try:
            if args.resultpath == None:
                csvfile = sys.stdout
            else:
                csvfile = open(args.resultpath, "w")
            csvfile.write("#  i  name   s   energy   codx   cody  rmsx  rmsy  alphax alphay emittancex emittancey emittancez TM\r\n")
            csvfile.write("#           [m]   [eV]     [m]    [m]   [m]   [m]                                                   \r\n")

            for idx in xrange(len(lattice.elements)):
                csvfile.write(str(idx))
                csvfile.write("  ")
                csvfile.write(lattice.elements[idx].name)
                csvfile.write("  ")
                csvfile.write(str(spos[idx]))
                csvfile.write("  ")
                csvfile.write(str(energy[idx]))
                csvfile.write("  ")
                csvfile.write(str(xorbit[idx]))
                csvfile.write("  ")
                csvfile.write(str(yorbit[idx]))
                csvfile.write("  ")
                csvfile.write(str(xrms[idx]))
                csvfile.write("  ")
                csvfile.write(str(yrms[idx]))
                csvfile.write("  ")
                csvfile.write(str(xalpha[idx]))
                csvfile.write("  ")
                csvfile.write(str(yalpha[idx]))
                csvfile.write("  ")
                csvfile.write(str(xemit[idx]))
                csvfile.write("  ")
                csvfile.write(str(yemit[idx]))
                csvfile.write("  ")
                csvfile.write(str(zemit[idx]))
                csvfile.write("  ")
                csvfile.write("0.0")
                csvfile.write("\r\n")

        except Exception as e:
            print("Error writing CSV result: ", e, file=sys.stderr)

        finally:
            if csvfile != sys.stdout: csvfile.close()

    rm_temp_dir(result_dir)

    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