__main__.py 5.27 KB
Newer Older
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
"""Finesse command line interface

Sean Leavey
<sean.leavey@ligo.org>
"""

from __future__ import print_function

import sys
import numpy as np
import click

from . import __version__
from .finesse import kat as katparser

@click.command(help="Python interface and tools for FINESSE")
@click.argument("file", type=click.File())
18 19
@click.option("--simulate/--no-simulate", is_flag=True, default=True,
              help="Simulate FILE.")
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
20 21 22 23 24 25 26 27 28 29
@click.option("--xstart", type=float,
              help="Simulation start value. If specified, this overrides the xaxis start "
              "value specified in the parsed file.")
@click.option("--xstop", type=float,
              help="Simulation stop value. If specified, this overrides the xaxis stop "
              "value specified in the parsed file.")
@click.option("--xsteps", type=int,
              help="Number of steps to simulate between --start and --stop. If specified, "
              "this overrides the number of xaxis steps specified in the parsed file.")
@click.option("--xscale", type=click.Choice(["lin", "log"]), help="Scaling for the xaxis.")
30
@click.option("--noxaxis", is_flag=True, default=False, help="Switch off x-axis.")
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
31 32
@click.option("--trace", type=click.Choice(["tem", "cavity", "mismatch", "beams", "gouy",
                                            "coupling", "modechanges", "nodes", "all"]),
Sean Leavey's avatar
Sean Leavey committed
33 34
              multiple=True, help="Show simulation trace results. This option can be specified "
                                  "multiple times. The following values are supported: "
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
                                  "'tem': list TEM modes used, "
                                  "'cavity': cavity eigenvalues and other parameters, "
                                  "'mismatch': mode mismatch parameters for the initial setup, "
                                  "'beams': beam parameters for every node, "
                                  "'gouy': Gouy phases for all spaces, "
                                  "'coupling': coupling coefficients for all components, "
                                  "'modechanges': mode matching parameter changes during calculations, "
                                  "'nodes': nodes found during cavity tracing, "
                                  "'all': all trace results.")
@click.option("--maxtem", type=str, help="Maximum transverse electric mode. Can be either "
              "an integer or 'off'.")
@click.option("--ignore-block", "ignored_blocks", multiple=True,
              help="Ignore the specified block. Can be specified multiple times.")
@click.option("--plot/--no-plot", default=True, show_default=True,
              help="Display results as figure.")
@click.option("--save-figure", type=click.File("wb", lazy=False),
              help="Save image of figure to file.")
52
@click.option("--display-graph", is_flag=True, help="Generate and display model node graph.")
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
53
@click.version_option(version=__version__, prog_name="Pykat")
54 55
def cli(file, simulate, xstart, xstop, xsteps, xscale, noxaxis, trace, maxtem, ignored_blocks,
        plot, save_figure, display_graph):
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
56 57 58
    """Base CLI command group"""
    kat = katparser()
    kat.load(file.name)
59
    has_xaxis = hasattr(kat, "xaxis") and not noxaxis
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
60 61 62 63 64

    if ignored_blocks:
        for block in ignored_blocks:
            kat.removeBlock(block)

65 66 67 68 69
    if display_graph:
        from .tools.plotting.graph import NodeGraph
        nodegraph = NodeGraph(kat)
        nodegraph.view_pdf()

70 71 72
    if simulate:
        if xstart is not None or xstop is not None or xsteps is not None or xscale is not None:
            if not has_xaxis:
73 74
                click.echo("Limits can only be overridden when an xaxis is defined in FILE and "
                           "when --noxaxis is unset.", err=True)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                sys.exit(1)
            # Override xaxis.
            limits = kat.xaxis.limits
            set_limits = False
            if xstart is not None:
                limits[0] = xstart
                set_limits = True
            if xstop is not None:
                limits[1] = xstop
                set_limits = True
            if xsteps is not None:
                kat.xaxis.steps = xsteps
            if xscale is not None:
                kat.xaxis.scale = xscale
            
            if set_limits:
                kat.xaxis.limits = np.array(limits).astype(float)

93
        if not has_xaxis:
94
            kat.noxaxis = True
95 96 97 98
            if save_figure is not None:
                click.echo("Cannot plot or save figure without an xaxis defined in FILE.",
                           err=True)
                sys.exit(1)
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
99

100 101
        if maxtem:
            kat.maxtem = maxtem
Sean Leavey's avatar
Add CLI  
Sean Leavey committed
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        if trace:
            traceval = 0
            if "all" in trace:
                traceval = 255
            else:
                traceints = {"tem": 1, "cavity": 2, "mismatch": 4, "beams": 8,
                            "gouy": 16, "coupling": 32, "modechanges": 64, "nodes": 128}
                for tracetype in trace:
                    traceval |= traceints[tracetype]
            kat.trace = traceval

        results = kat.run()

        if kat.trace:
            click.echo(results.stdout)

119 120 121 122
        if has_xaxis and (plot or save_figure is not None):
            results.plot(show=plot, filename=save_figure)
        elif not kat.trace and not display_graph:
            click.echo("No output requested.")