_util.py 5.93 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
16
17
18
19
    parser.add_argument('--config_file', type=str, required=True, help='.yaml config file for simulation settings.')
    parser.add_argument('--catalog', type=str, required=True, help='name of the catalog interface class to be loaded.')
    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
35
36
37
38
39
40
41
42
43
44
45
46
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)
    ncal = config['obs_setting']['np_cal']
    ipoint = 0
    for i in range(ncal):
        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,
            pointing_type='CAL')
        t += 3 * delta_t * 60. # 3 calibration exposures for each pointing
        pointing_list.append(pointing)
        ipoint += 1

    run_pointings = config['obs_setting']['run_pointings']
Fang Yuedong's avatar
Fang Yuedong committed
47
48
49
50
    if config["obs_setting"]["exp_time"]:
        exp_time = config["obs_setting"]["exp_time"]
    else:
        exp_time = 150.
51
    
52
53
54
55
56
57
58
    if pointing_filename and data_dir:
        pointing_file = os.path.join(data_dir, pointing_filename)
        f = open(pointing_file, 'r')
        for _ in range(1):
            header = f.readline()
        iline = 0
        for line in f:
59
60
            if run_pointings and iline not in run_pointings:
                iline += 1
Fang Yuedong's avatar
Fang Yuedong committed
61
                ipoint += 1
62
                continue
63
64
            line = line.strip()
            columns = line.split()
Fang Yuedong's avatar
Fang Yuedong committed
65
            pointing = Pointing(exp_time=exp_time)
66
67
68
69
70
            pointing.read_pointing_columns(columns=columns, id=ipoint, t=t)
            t += delta_t * 60.
            pointing_list.append(pointing)
            iline += 1
            ipoint += 1
71
72
        f.close()
    else:
73
74
75
76
77
78
        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
79
            exp_time=exp_time,
80
81
82
83
84
85
            pointing_type='MS'
            )
        t += delta_t * 60.
        pointing_list.append(pointing)
        ipoint += 1
    return pointing_list
86

87
def make_run_dirs(work_dir, run_name, pointing_list):
88
89
90
91
92
93
94
95
96
97
98
99
    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
    prefix = "MSC_"
100
101
    for pointing in pointing_list:
        fname=prefix + str(pointing.id).rjust(7, '0')
102
103
104
105
106
107
        subImgDir = os.path.join(imgDir, fname)
        if not os.path.exists(subImgDir):
            try:
                os.makedirs(subImgDir, exist_ok=True)
            except OSError:
                pass
108
    return imgDir
109

Fang Yuedong's avatar
Fang Yuedong committed
110
def imgName(tt=0):
Fang Yuedong's avatar
Fang Yuedong committed
111
112
113
114
115
116
117
118
    ut = datetime.utcnow()
    eye, emo, eda, eho, emi, ese = str(ut.year), str(ut.month), str(ut.day), str(ut.hour), str(ut.minute), str(ut.second)
    emse = str(ut.microsecond)
    if int(emo)<10: emo = "0%s"%emo
    if int(eda)<10: eda = "0%s"%eda
    if int(eho)<10: eho = "0%s"%eho
    if int(emi)<10: emi = "0%s"%emi
    if int(ese)<10: ese = "0%s"%ese
Fang Yuedong's avatar
Fang Yuedong committed
119

Fang Yuedong's avatar
Fang Yuedong committed
120
121
122
123
124
125
126
127
    if tt==0:
        namekey = "CSST%s%s%sT%s%s%s"%(eye,emo,eda,eho,emi,ese)
    elif tt==1:
        namekey = "%s-%s-%sT%s:%s:%s.%s"%(eye,emo,eda,eho,emi,ese,emse)
    elif tt==2:
        namekey = "%s%s%s%s%s%s"%(eye,emo,eda,eho,emi,ese)
    else:
        raise ValueError("!!! Give a right 'tt' value.")
Fang Yuedong's avatar
Fang Yuedong committed
128

Fang Yuedong's avatar
Fang Yuedong committed
129
    return namekey
Fang Yuedong's avatar
Fang Yuedong committed
130
131

def makeSubDir_PointingList(path_dict, config, pointing_ID=0):
Fang Yuedong's avatar
Fang Yuedong committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    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
    prefix = "MSC_" + str(pointing_ID).rjust(7, '0')
    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
146

147
def get_shear_field(config, shear_cat_file=None):
Fang Yuedong's avatar
Fang Yuedong committed
148
    if not config["shear_setting"]["shear_type"] in ["constant", "extra", "catalog"]:
Fang Yuedong's avatar
Fang Yuedong committed
149
        raise ValueError("Please set a right 'shear_method' parameter.")
Fang Yuedong's avatar
Fang Yuedong committed
150

Fang Yuedong's avatar
Fang Yuedong committed
151
152
153
154
155
    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
156
    elif config["shear_setting"]["shear_type"] == "extra":
Fang Yuedong's avatar
Fang Yuedong committed
157
158
        # TODO logging
        if not os.path.exists(shear_cat_file):
Fang Yuedong's avatar
Fang Yuedong committed
159
            raise ValueError("Cannot find external shear catalog file.")
Fang Yuedong's avatar
Fang Yuedong committed
160
161
162
163
164
        try:
            shearCat = np.loadtxt(shear_cat_file)
            nshear = shearCat.shape[0]
            g1, g2 = shearCat[:, 0], shearCat[:, 1]
        except:
Fang Yuedong's avatar
Fang Yuedong committed
165
            print("Failed to read the shear catalog file.")
Fang Yuedong's avatar
Fang Yuedong committed
166
167
            print("Setting to no shear.")
            g1, g2 = 0., 0.
Fang Yuedong's avatar
Fang Yuedong committed
168
169
170
    else:
        g1, g2 = 0., 0.
        nshear = 0
Fang Yuedong's avatar
Fang Yuedong committed
171
    return g1, g2, nshear