run_sim.py 3.87 KB
Newer Older
1
from ObservationSim.ObservationSim import Observation
2
from ObservationSim._util import parse_args, make_run_dirs, generate_pointing_list
Fang Yuedong's avatar
Fang Yuedong committed
3
from importlib.metadata import version
4
5
import os
import yaml
6
import shutil
7
import datetime
Fang Yuedong's avatar
Fang Yuedong committed
8
import importlib
9
10
11
12

import gc
gc.enable()

Fang Yuedong's avatar
Fang Yuedong committed
13

Fang Yuedong's avatar
Fang Yuedong committed
14
def run_sim():
15
16
    """
    Main method for simulation call.
17
18
19
20
21

    Parameters
    ----------
    Catalog : Class
        a catalog class which is inherited from ObservationSim.MockObject.CatalogBase 
Fang Yuedong's avatar
Fang Yuedong committed
22

23
24
25
    Returns
    ----------
        None
26
    """
Fang Yuedong's avatar
Fang Yuedong committed
27
28
    # Get version of the Package
    __version__ = version("csst_msc_sim")
29
30
31
32

    # Get run datetime
    now = datetime.datetime.now()

33
34
35
36
37
38
39
40
41
    args = parse_args()
    if args.config_dir is None:
        args.config_dir = ''
    args.config_dir = os.path.abspath(args.config_dir)
    args.config_file = os.path.join(args.config_dir, args.config_file)
    with open(args.config_file, "r") as stream:
        try:
            config = yaml.safe_load(stream)
            for key, value in config.items():
Fang Yuedong's avatar
Fang Yuedong committed
42
                print(key + " : " + str(value))
43
44
45
46
47
48
49
50
51
52
        except yaml.YAMLError as exc:
            print(exc)

    # Overwrite the data and working directories
    # if they are specified by the command line inputs
    if args.data_dir is not None:
        config['data_dir'] = args.data_dir
    if args.work_dir is not None:
        config['work_dir'] = args.work_dir

53
54
55
    if not ("data_dir" in config):
        config["data_dir"] = None

56
    # Some default values
57
58
59
60
    if "mag_sat_margin" not in config["obs_setting"]:
        config["obs_setting"]["mag_sat_margin"] = -2.5
    if "mag_lim_margin" not in config["obs_setting"]:
        config["obs_setting"]["mag_lim_margin"] = 1.0
61
62
63
64
    if "project_cycle" not in config:
        config["project_cycle"] = 6
    if "run_counter" not in config:
        config["run_counter"] = 0
65

Fang Yuedong's avatar
Fang Yuedong committed
66
    # Generate lists pointings based on the input pointing list (or default
67
    # pointing RA, DEC) and "config["obs_setting"]["run_pointings"]".
Fang Yuedong's avatar
Fang Yuedong committed
68
    # "config['obs_setting']['np_cal']"" is the number of CAL pointings which will be
69
    # appended to the front.
70
    # NOTE: the implementation of gerenating time_stamps is temporary.
71
72
73
    pointing_dir = None
    if "pointing_dir" in config['obs_setting']:
        pointing_dir = config['obs_setting']["pointing_dir"]
Fang Yuedong's avatar
Fang Yuedong committed
74
75
    pointing_list = generate_pointing_list(
        config=config, pointing_filename=config['obs_setting']['pointing_file'], data_dir=pointing_dir)
76
77

    # Make the main output directories
Fang Yuedong's avatar
Fang Yuedong committed
78
79
80
    run_dir = make_run_dirs(
        work_dir=config['work_dir'], run_name=config['run_name'], pointing_list=pointing_list)

81
    # Copy the config file to output directory & Write Run metadata
82
    shutil.copy(args.config_file, run_dir)
83
84
85
86
    run_meta = os.path.join(run_dir, "run_metadata.yaml")
    with open(run_meta, "w") as config_out:
        config_out.write("\n")
        config_out.write("###############################################\n")
Fang Yuedong's avatar
Fang Yuedong committed
87
88
        config_out.write(
            "csst_msc_sim package version: \"%s\"\n" % __version__)
89
90
        date_str = datetime.datetime.strftime(now, '%m/%d/%Y')
        time_str = datetime.datetime.strftime(now, '%H:%M:%S')
Fang Yuedong's avatar
Fang Yuedong committed
91
92
        config_out.write("Run_date: \"%s\"\n" % date_str)
        config_out.write("Run_time: \"%s\"\n" % time_str)
93
        config_out.write("###############################################\n")
94
95

    # Initialize the simulation
96
97
    if args.catalog is not None:
        catalog_module = importlib.import_module('Catalog.'+args.catalog)
Fang Yuedong's avatar
Fang Yuedong committed
98
99
        obs = Observation(config=config, Catalog=catalog_module.Catalog,
                          work_dir=config['work_dir'], data_dir=config['data_dir'])
100
101
    else:
        catalog_module = None
Fang Yuedong's avatar
Fang Yuedong committed
102
103
104
        obs = Observation(config=config, Catalog=None,
                          work_dir=config['work_dir'], data_dir=config['data_dir'])

105
    # Run simulation
106
    obs.runExposure_MPI_PointingList(pointing_list=pointing_list)
107

Fang Yuedong's avatar
Fang Yuedong committed
108
109

if __name__ == '__main__':
Fang Yuedong's avatar
Fang Yuedong committed
110
    run_sim()