aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fg21sim
blob: 0effbc713d8af80267997d55fa2cc69173a46f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
# -*- mode: python -*-
#
# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
# MIT license

"""
Simulate the low-frequency radio foregrounds for the 21cm EoR signal.
"""

import os
import sys
import argparse
import logging

import numpy as np

from fg21sim.configs import configs, validate_configs
from fg21sim.utils import setup_logging
from fg21sim.galactic import Synchrotron as GalacticSynchrotron
from fg21sim.galactic import FreeFree as GalacticFreeFree
from fg21sim.galactic import SuperNovaRemnants as GalacticSNR


def main():
    parser = argparse.ArgumentParser(
        description="Simulate the radio foregrounds for 21cm EoR signal")
    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()

    configs.read_userconfig(args.config)
    if os.environ.get("DEBUG_FG21SIM"):
        print("DEBUG: Current configurations:", configs._config,
              sep="\n", file=sys.stderr)
    validate_configs(configs)

    if args.quiet:
        log_stream = ""
    else:
        log_stream = 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)))

    frequencies = np.array(configs.frequencies, ndmin=1)
    freq_unit = configs.getn("frequency/unit")
    logger.info("Simulation frequencies: "
                "{min:.2f} - {max:.2f} {unit} (#{num:d})".format(
                    min=frequencies.min(), max=frequencies.max(),
                    num=len(frequencies), unit=freq_unit))

    # XXX/TODO: move the supported components and id's to a module/file
    fg_components = configs.getn("common/components")
    logger.info("Enabled simulation components: {0}".format(
        ", ".join(fg_components)))
    fg_maps = {}

    # Galactic synchrotron
    id_gsync = "galactic/synchrotron"
    if id_gsync in fg_components:
        logger.info("Simulating the Galactic synchrotron foreground ...")
        gsynchrotron = GalacticSynchrotron(configs)
        hpmap_gsync = gsynchrotron.simulate(frequencies)
        logger.info("Done simulate Galactic synchrotron foreground!")
        fg_maps[id_gsync] = hpmap_gsync

    # Galactic free-free
    id_gfree = "galactic/freefree"
    if id_gfree in fg_components:
        logger.info("Simulating the Galactic free-free foreground ...")
        gfreefree = GalacticFreeFree(configs)
        hpmap_gfree = gfreefree.simulate(frequencies)
        logger.info("Done simulate Galactic free-free foreground!")
        fg_maps[id_gfree] = hpmap_gfree

    # Galactic supernova remnants
    id_gsnr = "galactic/snr"
    if id_gsnr in fg_components:
        logger.info("Simulating the Galactic supernova remnants emission ...")
        gsnr = GalacticSNR(configs)
        hpmap_gsnr = gsnr.simulate(frequencies)
        logger.info("Done simulate Galactic supernova remnants foreground!")
        fg_maps[id_gsnr] = hpmap_gsnr


if __name__ == "__main__":
    main()