Move all ETC code to master function

This commit is contained in:
Lukas Klass 2020-09-09 16:53:08 +02:00
parent a2162c89ba
commit 31714c9235
3 changed files with 77 additions and 24 deletions

View File

@ -31,29 +31,17 @@ if __name__ == "__main__":
print("") print("")
print(f.renderText('ESBO-ETC')) print(f.renderText('ESBO-ETC'))
# Set up logging # Initialize the ETC
logger.setLevel(logging.WARNING if args.logging is None else getattr(logging, args.logging.upper())) etc = eetc.esbo_etc(args.config,
logger.addHandler(eetc.SpinnerHandler()) logging.WARNING if args.logging is None else getattr(logging, args.logging.upper()), True)
# Run the computation
res = etc.run()
# Parse Configuration # Print the results
logger.info("Parsing configuration...", extra={"spinning": True}) if hasattr(etc.conf.common, "exposure_time") and hasattr(etc.conf.common, "snr"):
conf = eetc.Configuration(args.config).conf eetc.printSensitivity(etc.conf.common.exposure_time(), etc.conf.common.snr(), res)
elif hasattr(etc.conf.common, "exposure_time"):
# Set up components eetc.printSNR(etc.conf.common.exposure_time(), res)
logger.info("Setting up components...", extra={"spinning": True}) elif hasattr(etc.conf.common, "snr"):
oc_factory = eetc.classes.RadiantFactory(conf.common.wl_bins()) eetc.printExposureTime(res, etc.conf.common.snr())
parent = oc_factory.fromConfigBatch(conf)
sensor_factory = eetc.SensorFactory(parent, conf.common)
detector = sensor_factory.create(conf.instrument.sensor)
# Calculate results
if hasattr(conf.common, "exposure_time") and hasattr(conf.common, "snr"):
sensitivity = detector.getSensitivity(conf.common.exposure_time(), conf.common.snr(), conf.astroscene.target.mag)
eetc.printSensitivity(conf.common.exposure_time(), conf.common.snr(), sensitivity)
elif hasattr(conf.common, "exposure_time"):
snr = detector.getSNR(conf.common.exposure_time())
eetc.printSNR(conf.common.exposure_time(), snr)
elif hasattr(conf.common, "snr"):
exp_time = detector.getExpTime(conf.common.snr())
eetc.printExposureTime(exp_time, conf.common.snr())
logger.info("Finished.", extra={"spinning": False}) logger.info("Finished.", extra={"spinning": False})

View File

@ -1,3 +1,4 @@
from .esbo_etc import esbo_etc
from .Config import * from .Config import *
from .IRadiant import * from .IRadiant import *
from .SpectralQty import * from .SpectralQty import *

View File

@ -0,0 +1,64 @@
import esbo_etc as eetc
from esbo_etc.lib.logger import logger
import logging as log
import astropy.units as u
class esbo_etc:
"""
Top level class of the exposure time calculator
"""
def __init__(self, config: str = "esbo-etc_defaults.xml", logging: int = log.WARNING, spin: bool = False):
"""
Initialize a new exposure time calculator (ETC)
Parameters
----------
config : str
Path to the configuration file
logging : int
Loglevel from package logging
spin : bool
Show a spinner during computations
"""
self.__config = config
self.__logging = logging
self.__spin = spin
self.conf = None
def run(self) -> u.Quantity:
"""
Returns
-------
res: Quantity
The result of the computation. Depending on the input parameters, this will be either a dimensionless
signal-to-noise ratio (SNR), an exposure time in seconds or a sensitivity in apparent magnitues.
"""
# Set up logging
logger.setLevel(log.WARNING if self.__logging is None else self.__logging)
if self.__spin:
logger.addHandler(eetc.SpinnerHandler())
# Parse Configuration
logger.info("Parsing configuration...", extra={"spinning": True})
self.conf = eetc.Configuration(self.__config).conf
# Set up components
logger.info("Setting up components...", extra={"spinning": True})
oc_factory = eetc.classes.RadiantFactory(self.conf.common.wl_bins())
parent = oc_factory.fromConfigBatch(self.conf)
sensor_factory = eetc.SensorFactory(parent, self.conf.common)
detector = sensor_factory.create(self.conf.instrument.sensor)
# Calculate results
res = None
if hasattr(self.conf.common, "exposure_time") and hasattr(self.conf.common, "snr"):
res = detector.getSensitivity(self.conf.common.exposure_time(), self.conf.common.snr(),
self.conf.astroscene.target.mag)
elif hasattr(self.conf.common, "exposure_time"):
res = detector.getSNR(self.conf.common.exposure_time())
elif hasattr(self.conf.common, "snr"):
res = detector.getExpTime(self.conf.common.snr())
return res