#!/usr/bin/env python3 # -*- mode: python -*- # # Copyright (c) 2016-2017 Weitian LI # MIT license """ Simulate the low-frequency radio foregrounds for the 21cm EoR signal. """ import os import sys import argparse import logging import time from fg21sim.configs import configs from fg21sim.utils import setup_logging, cosmo def main(): parser = argparse.ArgumentParser( description="Simulate the radio foregrounds for 21cm EoR signal", epilog="Set environment variable 'DEBUG_FG21SIM=1' to force debug!") parser.add_argument("config", help="user configuration file") parser.add_argument("-l", "--log", dest="loglevel", default=None, choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], help="set the log level") parser.add_argument("-L", "--logfile", default=None, help="filename where to save the log messages") parser.add_argument("-Q", "--quiet", action="store_true", help="be quiet so do not log messages to screen") args = parser.parse_args() t1_start = time.perf_counter() t2_start = time.process_time() configs.read_userconfig(args.config) if os.environ.get("DEBUG_FG21SIM"): print("DEBUG: Current configurations:", configs._config, sep="\n", file=sys.stderr) configs.check_all() log_stream = "" if args.quiet else None setup_logging(dict_config=configs.logging, level=args.loglevel, stream=log_stream, logfile=args.logfile) tool = os.path.basename(sys.argv[0]) logger = logging.getLogger(tool) logger.info("COMMAND: {0}".format(" ".join(sys.argv))) # Save/backup current configurations configfile_dump = os.path.splitext(configs.userconfig)[0] + "_dump.conf" configs.save(configfile_dump, clobber=True, backup=True) logger.info("Saved current configurations to file: %s" % configfile_dump) # Setup cosmology model logger.info("Setup cosmology model with parameters from configs ...") cosmo.setup(**configs.cosmology) logger.info("Importing modules + Numba JIT, waiting ...") from fg21sim.foregrounds import Foregrounds fg = Foregrounds(configs) fg.preprocess() fg.simulate() fg.postprocess() t1_stop = time.perf_counter() t2_stop = time.process_time() logger.info("Elapsed time: {0:.3f} (s)".format(t1_stop - t1_start)) logger.info("CPU process time: {0:.3f} (s)".format(t2_stop - t2_start)) if __name__ == "__main__": main()