run_sim.py 3.86 KB
Newer Older
1
from ObservationSim.ObservationSim import Observation
2
from ObservationSim._util import parse_args, make_run_dirs, generate_pointing_list
3
from pkg_resources import get_distribution
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
def run_sim():
14
15
    """
    Main method for simulation call.
16
17
18
19
20
21
22
23
24

    Parameters
    ----------
    Catalog : Class
        a catalog class which is inherited from ObservationSim.MockObject.CatalogBase 
    
    Returns
    ----------
        None
25
    """
26
27
28
29
30
31
    # Get version of CSSTSim Package
    __version__ = get_distribution("CSSTSim").version

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

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    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():
                print (key + " : " + str(value))
        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

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

55
    # Some default values
56
57
58
59
    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
60
61
62
63
    if "project_cycle" not in config:
        config["project_cycle"] = 6
    if "run_counter" not in config:
        config["run_counter"] = 0
64

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

    # Make the main output directories
76
    run_dir = make_run_dirs(work_dir=config['work_dir'], run_name=config['run_name'], pointing_list=pointing_list)
77
78
    
    # Copy the config file to output directory & Write Run metadata
79
    shutil.copy(args.config_file, run_dir)
80
81
82
83
84
85
86
87
88
89
    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")
        config_out.write("CSSTSim_version: \"%s\"\n"%__version__)
        date_str = datetime.datetime.strftime(now, '%m/%d/%Y')
        time_str = datetime.datetime.strftime(now, '%H:%M:%S')
        config_out.write("Run_date: \"%s\"\n"%date_str)
        config_out.write("Run_time: \"%s\"\n"%time_str)
        config_out.write("###############################################\n")
90
91

    # Initialize the simulation
92
93
    if args.catalog is not None:
        catalog_module = importlib.import_module('Catalog.'+args.catalog)
94
        obs = Observation(config=config, Catalog=catalog_module.Catalog, work_dir=config['work_dir'], data_dir=config['data_dir'])
95
96
    else:
        catalog_module = None
97
        obs = Observation(config=config, Catalog=None, work_dir=config['work_dir'], data_dir=config['data_dir'])
98
99
100
    
    # Run simulation
    obs.runExposure_MPI_PointingList(
101
        pointing_list=pointing_list,
102
        use_mpi=config["run_option"]["use_mpi"])
103
104

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