From a2bac70ddc24f3880f36eb52848078de3ecf368d Mon Sep 17 00:00:00 2001 From: LukasK13 Date: Tue, 19 May 2020 09:41:03 +0200 Subject: [PATCH] Show progress information, print results --- esbo_etc/esbo-etc.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/esbo_etc/esbo-etc.py b/esbo_etc/esbo-etc.py index c547d4c..9090c7a 100644 --- a/esbo_etc/esbo-etc.py +++ b/esbo_etc/esbo-etc.py @@ -2,9 +2,13 @@ import esbo_etc as eetc import argparse import logging import sys -import numpy as np +from logging_spinner import SpinnerHandler +from pyspin.spin import Spin1 +from pyfiglet import Figlet +from rich import console, markdown if __name__ == "__main__": + # Parse arguments parser = argparse.ArgumentParser(prog="esbo_etc/esbo-etc.py", description='Exposure time calculator for ESBO-DS') parser.add_argument("-c", "--config", dest='config', default="esbo-etc_defaults.xml", metavar="config.xml", help="path to the configuration file") @@ -13,24 +17,47 @@ if __name__ == "__main__": default="output") parser.add_argument("-v", "--version", action="version", version="ESBO-ETC version 1.0.0", help="show version information") + parser.add_argument("-m", "--manual", action="store_true", dest="manual", + help="print the user manual from the readme") args, _ = parser.parse_known_args() # fix for PyCharm python console + # Print manual from README.md + if args.manual: + console = console.Console() + with open("README.md") as readme: + markdown = markdown.Markdown(readme.read()) + console.print(markdown) + exit(0) + + # Print title + f = Figlet(font='slant') + print(f.renderText('ESBO-ETC')) + + # Set up logging logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING if args.logging is None else getattr( logging, args.logging.upper()), stream=sys.stdout) + logger = logging.getLogger('root') + logger.addHandler(SpinnerHandler(spin_style=Spin1)) + # Parse Configuration + logging.getLogger("root").info("Parsing configuration...", extra={"user_waiting": True}) conf = eetc.Configuration(args.config).conf + # Set up components + logging.getLogger("root").info("Setting up components...", extra={"user_waiting": True}) oc_factory = eetc.classes.RadiantFactory(conf.common.wl_bins()) parent = oc_factory.fromConfigBatch(conf) sensor_factory = eetc.SensorFactory(parent, conf.common) imager = sensor_factory.create(conf.instrument.sensor) + # Calculate results if hasattr(conf.common, "exposure_time") and hasattr(conf.common, "snr"): sensitivity = imager.getSensitivity(conf.common.exposure_time(), conf.common.snr(), conf.astroscene.target.mag) - print("The limiting apparent magnitude is: " + str(np.array2string(sensitivity.value, precision=2)) + " mag.") + eetc.printSensitivity(conf.common.exposure_time(), conf.common.snr(), sensitivity) elif hasattr(conf.common, "exposure_time"): snr = imager.getSNR(conf.common.exposure_time()) - print("The SNR is: " + str(np.array2string(snr.value, precision=2)) + ".") + eetc.printSNR(conf.common.exposure_time(), snr) elif hasattr(conf.common, "snr"): exp_time = imager.getExpTime(conf.common.snr()) - print("The necessary exposure time is: " + str(np.array2string(exp_time.value, precision=2)) + " s.") + eetc.printExposureTime(exp_time, conf.common.snr()) + logging.getLogger("root").info("Finished.", extra={"user_waiting": False})