Commit d0231087 authored by Sean Leavey's avatar Sean Leavey

Support saving the input and output files in the CLI, and report locations

parent fe81f44a
......@@ -4,9 +4,8 @@ Sean Leavey
<sean.leavey@ligo.org>
"""
from __future__ import print_function
import sys
import os
import numpy as np
import click
......@@ -59,18 +58,21 @@ from .finesse import kat as katparser
@click.option("--save-figure", type=click.File("wb", lazy=False),
help="Save image of figure to file.")
@click.option("--display-graph", is_flag=True, help="Generate and display model node graph.")
@click.option("--save-input", is_flag=True, default=False,
help="Save generated Finesse input file.")
@click.option("--save-output", is_flag=True, default=False,
help="Save generated Finesse output file.")
@click.option("--finesse-dir", type=click.Path(exists=True, file_okay=False, dir_okay=True),
envvar='FINESSE_DIR', help="Path to directory containing the Finesse 'kat' "
"executable. If not specified, the environment variable FINESSE_DIR is used.")
@click.version_option(version=__version__, prog_name="Pykat")
def cli(file, simulate, xstart, xstop, xsteps, xscale, noxaxis, trace, powers, maxtem,
deriv_h, lambda0, ignored_blocks, plot, save_figure, display_graph, finesse_dir):
deriv_h, lambda0, ignored_blocks, plot, save_figure, display_graph, save_input,
save_output, finesse_dir):
"""Base CLI command group"""
if finesse_dir is None:
# Use default as required by kat object.
finesse_dir = ""
else:
click.echo("Using kat binary in %s" % finesse_dir)
kat = katparser(katdir=finesse_dir)
kat.load(file.name)
has_xaxis = hasattr(kat, "xaxis") and not noxaxis
......@@ -141,10 +143,10 @@ def cli(file, simulate, xstart, xstop, xsteps, xscale, noxaxis, trace, powers, m
powerval |= powerints[powertype]
kat.parse("powers %i" % powerval)
results = kat.run()
results = kat.run(save_output=save_output, save_kat=save_input)
if kat.trace or powers:
click.echo(results.raw_output)
if (kat.trace or powers) and results.rundata:
click.echo(results.rundata)
if has_xaxis and (plot or save_figure is not None):
results.plot(show=plot, filename=save_figure)
......
......@@ -365,16 +365,22 @@ class KatRun(object):
def __init__(self):
self._unfreeze()
self.runtime = None
self.save_input = False
self.save_output = False
self.StartDateTime = datetime.datetime.now()
self.x = None
self.stdout = None
self.__stdout = None
self.stderr = None
self.__rundata = None
self.runDateTime = None
self.y = None
self.xlabel = None
self.ylabels = None
self.infile = None
self.outfile = None
self.katScript = None
self.katVersion = None
self.katPath = None
self.yaxis = None
self._freeze()
......@@ -641,16 +647,29 @@ class KatRun(object):
return fig
@property
def raw_output(self):
"""Contents of stdout with the Finesse banner removed."""
def stdout(self):
"""Standard output from Finesse."""
return self.__stdout
@stdout.setter
def stdout(self, stdout):
self.__stdout = stdout
# Extract run data from stdout.
# Remove everything above the second occurrance of a line with
# 72 dashes (the bottom of the Finesse banner).
output = self.stdout.split("-" * 72)
if len(output) <= 2:
# No dashes found - return what we have.
return self.stdout
output = "".join(output[2:])
return output
rundata = stdout.split("-" * 72)
if len(rundata) <= 2:
# No dashes found - nothing to do.
rundata = ""
else:
rundata = "".join(rundata[2:])
self.__rundata = rundata
@property
def rundata(self):
"""Contents of stdout with the Finesse banner removed."""
return self.__rundata
def saveKatRun(self, filename):
with open(filename,'w') as outfile:
......@@ -2088,6 +2107,8 @@ class kat(object):
r.katScript = "".join(self.generateKatScript())
r.katScript += "time\n"
r.katPath = kat_exec
if (plot==None):
# ensure we don't do any plotting. That should be handled
# by user themselves
......@@ -2104,7 +2125,6 @@ class kat(object):
katfile = open( filepath, 'w' )
katfile.writelines(r.katScript)
katfile.flush()
pipe_name = katfile.name + str(uuid.uuid4())
......@@ -2187,7 +2207,7 @@ class kat(object):
(tag, line) = v
if tag == "version":
r.katVersion = line
r.katVersion = line.strip()
elif tag == "progress" and self.verbose:
var = line.split("\t")
......@@ -2242,6 +2262,10 @@ class kat(object):
path = os.path.split(katfile.name)[0]
outfile = root[0] + ".out"
pb.finish()
if self.verbose: print("Used Finesse %s at %s" % (r.katVersion, r.katPath))
traceData = None
if getTraceData:
......@@ -2298,20 +2322,6 @@ class kat(object):
finally:
ifile.close()
if save_output:
newoutfile = "{0}.out".format(base)
cwd = os.path.os.getcwd()
newoutfile = os.path.join(cwd,newoutfile)
if os.path.isfile(newoutfile):
os.remove(newoutfile)
os.rename(outfile, newoutfile)
if self.verbose: print ("\nOutput data saved to '{0}'".format(newoutfile))
# can't see why this check is needed, causes problems when only detectors
# not parsed as pykat objects are used
#if len(self.detectors.keys()) > 0:
......@@ -2330,6 +2340,25 @@ class kat(object):
r.ylabels = [s.strip() for s in hdr[1:]]
#r.ylabels = map(str.strip, hdr[1:]) // replaced 090415 adf
r.save_output = save_output
if save_output:
newoutfile = "{0}.out".format(base)
cwd = os.path.os.getcwd()
newoutfile = os.path.join(cwd,newoutfile)
if os.path.isfile(newoutfile):
os.remove(newoutfile)
os.rename(outfile, newoutfile)
r.outfile = newoutfile
if self.verbose: print("Output data saved to '{0}'".format(newoutfile))
r.save_input = save_kat
if save_kat:
if kat_name is None:
kat_name = "pykat_output"
......@@ -2342,7 +2371,9 @@ class kat(object):
os.rename(katfile.name, newkatfile)
if self.verbose: print ("Kat file saved to '{0}'".format(newkatfile))
r.infile = newkatfile
if self.verbose: print("Kat file saved to '{0}'".format(newkatfile))
katfile.close()
perfData = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment