_util.py 4.24 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
2
3
import numpy as np
import os
from datetime import datetime
Fang Yuedong's avatar
Fang Yuedong committed
4
import argparse
5
6
7
from astropy.time import Time

from ObservationSim.Config import Pointing
Fang Yuedong's avatar
Fang Yuedong committed
8
9
10
11
12
13
14

def parse_args():
    '''
    Parse command line arguments. Many of the following
    can be set in the .yaml config file as well.
    '''
    parser = argparse.ArgumentParser()
Fang Yuedong's avatar
Fang Yuedong committed
15
    parser.add_argument('--config_file', type=str, required=True, help='.yaml config file for simulation settings.')
16
    parser.add_argument('--catalog', type=str, help='name of the catalog interface class to be loaded.')
Fang Yuedong's avatar
Fang Yuedong committed
17
18
19
    parser.add_argument('-c', '--config_dir', type=str, help='Directory that houses the .yaml config file.')
    parser.add_argument('-d', '--data_dir', type=str, help='Directory that houses the input data.')
    parser.add_argument('-w', '--work_dir', type=str, help='The path for output.')
Fang Yuedong's avatar
Fang Yuedong committed
20
    return parser.parse_args()
Fang Yuedong's avatar
Fang Yuedong committed
21

22
23
24
25
26
27
28
29
30
31
32
33
34
def generate_pointing_list(config, pointing_filename=None, data_dir=None):
    pointing_list = []

    # Only valid when the pointing list does not contain time stamp column
    t0 = datetime(2021, 5, 25, 12, 0, 0)
    delta_t = 10. # Time elapsed between exposures (minutes)

    # Calculate starting time(s) for CAL exposures
    # NOTE: temporary implementation
    t = datetime.timestamp(t0)
    ipoint = 0

    run_pointings = config['obs_setting']['run_pointings']
35
36
37
38
    if "obs_config_file" in config['obs_setting']:
        obs_config_file = config['obs_setting']["obs_config_file"]
    else:
        obs_config_file = None
39
    
40
41
42
    if pointing_filename and data_dir:
        pointing_file = os.path.join(data_dir, pointing_filename)
        f = open(pointing_file, 'r')
43
44
        # for _ in range(1):
        #     header = f.readline()
45
46
        iline = 0
        for line in f:
47
48
            if len(line.strip()) == 0 or line[0]=='#':
                continue
49
50
            if run_pointings and iline not in run_pointings:
                iline += 1
Fang Yuedong's avatar
Fang Yuedong committed
51
                ipoint += 1
52
                continue
53
54
            line = line.strip()
            columns = line.split()
55
            pointing = Pointing(obs_config_file=obs_config_file)
56
            pointing.read_pointing_columns(columns=columns, id=ipoint)
57
58
59
60
            t += delta_t * 60.
            pointing_list.append(pointing)
            iline += 1
            ipoint += 1
61
62
        f.close()
    else:
63
64
65
66
        if config["obs_setting"]["exp_time"]:
            exp_time = config["obs_setting"]["exp_time"]
        else:
            exp_time = 150.
67
68
69
70
71
72
        pointing = Pointing(
            id=ipoint,
            ra=config["obs_setting"]["ra_center"],
            dec=config["obs_setting"]["dec_center"],
            img_pa=config["obs_setting"]["image_rot"],
            timestamp=t,
Fang Yuedong's avatar
Fang Yuedong committed
73
            exp_time=exp_time,
74
75
            pointing_type='SCI',
            obs_config_file=obs_config_file
76
77
78
79
80
            )
        t += delta_t * 60.
        pointing_list.append(pointing)
        ipoint += 1
    return pointing_list
81

82
def make_run_dirs(work_dir, run_name, pointing_list):
83
84
85
86
87
88
89
90
91
92
93
    if not os.path.exists(work_dir):
        try:
            os.makedirs(work_dir, exist_ok=True)
        except OSError:
            pass
    imgDir = os.path.join(work_dir, run_name)
    if not os.path.exists(imgDir):
        try:
            os.makedirs(imgDir, exist_ok=True)
        except OSError:
            pass
94
    return imgDir
95

Fang Yuedong's avatar
Fang Yuedong committed
96
def makeSubDir_PointingList(path_dict, config, pointing_ID=0):
Fang Yuedong's avatar
Fang Yuedong committed
97
98
99
100
101
102
    imgDir = os.path.join(path_dict["work_dir"], config["run_name"])
    if not os.path.exists(imgDir):
        try:
            os.makedirs(imgDir, exist_ok=True)
        except OSError:
            pass
103
    prefix = "MSC_" + str(pointing_ID).rjust(8, '0')
Fang Yuedong's avatar
Fang Yuedong committed
104
105
106
107
108
109
110
    subImgdir = os.path.join(imgDir, prefix)
    if not os.path.exists(subImgdir):
        try:
            os.makedirs(subImgdir, exist_ok=True)
        except OSError:
            pass
    return subImgdir, prefix
Fang Yuedong's avatar
Fang Yuedong committed
111

112
113
def get_shear_field(config):
    if not config["shear_setting"]["shear_type"] in ["constant", "catalog"]:
Fang Yuedong's avatar
Fang Yuedong committed
114
        raise ValueError("Please set a right 'shear_method' parameter.")
Fang Yuedong's avatar
Fang Yuedong committed
115

Fang Yuedong's avatar
Fang Yuedong committed
116
117
118
119
120
    if config["shear_setting"]["shear_type"] == "constant":
        g1 = config["shear_setting"]["reduced_g1"]
        g2 = config["shear_setting"]["reduced_g2"]
        nshear = 1
        # TODO logging
Fang Yuedong's avatar
Fang Yuedong committed
121
122
123
    else:
        g1, g2 = 0., 0.
        nshear = 0
Fang Yuedong's avatar
Fang Yuedong committed
124
    return g1, g2, nshear