from ObservationSim.ObservationSim import Observation from ObservationSim._util import parse_args, make_run_dirs, generate_pointing_list from pkg_resources import get_distribution import os import yaml import shutil import datetime import importlib import gc gc.enable() def run_sim(): """ Main method for simulation call. Parameters ---------- Catalog : Class a catalog class which is inherited from ObservationSim.MockObject.CatalogBase Returns ---------- None """ # Get version of CSSTSim Package __version__ = get_distribution("CSSTSim").version # Get run datetime now = datetime.datetime.now() 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 # Some default values 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 if "project_cycle" not in config: config["project_cycle"] = 6 if "run_counter" not in config: config["run_counter"] = 0 # Generate lists pointings based on the input pointing list (or default # pointing RA, DEC) and "config["obs_setting"]["run_pointings"]". # "config['obs_setting']['np_cal']"" is the number of CAL pointings which will be # appended to the front. # NOTE: the implementation of gerenating time_stamps is temporary. pointing_list = generate_pointing_list(config=config, pointing_filename=config['obs_setting']['pointing_file'], data_dir=config['obs_setting']['pointing_dir']) # Make the main output directories run_dir = make_run_dirs(work_dir=config['work_dir'], run_name=config['run_name'], pointing_list=pointing_list) # Copy the config file to output directory & Write Run metadata shutil.copy(args.config_file, run_dir) 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") # Initialize the simulation catalog_module = importlib.import_module('Catalog.'+args.catalog) obs = Observation(config=config, Catalog=catalog_module.Catalog, work_dir=config['work_dir'], data_dir=config['data_dir']) # Run simulation obs.runExposure_MPI_PointingList( pointing_list=pointing_list, use_mpi=config["run_option"]["use_mpi"]) if __name__=='__main__': run_sim()