Config.py 3.49 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
2
3
4
import galsim
import os
from astropy.time import Time as asTime

5
def config_dir(config, work_dir=None, data_dir=None):
Fang Yuedong's avatar
Fang Yuedong committed
6
7
8
9
10
11
12
13
14
15
    path_dict = {}
    # Working directory
    if work_dir == None:
        dirname, _ = os.path.split(os.path.abspath(__file__))
        path_dict["work_dir"] = "/".join(dirname.split("/")[:-1]) + "/"
    else:
        path_dict["work_dir"] = work_dir
    
    # Data directory
    if data_dir == None:
Fang Yuedong's avatar
Fang Yuedong committed
16
        # Assume all input datasets are in the work directory
Fang Yuedong's avatar
Fang Yuedong committed
17
18
19
20
21
        path_dict["data_dir"] =os.path.join(path_dict["work_dir"], "data/")
    else:
        path_dict["data_dir"] = data_dir
    # Data sub-catalogs
    # Object catalog direcotry
Fang Yuedong's avatar
Fang Yuedong committed
22
    path_dict["cat_dir"] = os.path.join(path_dict["data_dir"], config["input_path"]["cat_dir"])
Fang Yuedong's avatar
Fang Yuedong committed
23
    # PSF data directory
Fang Yuedong's avatar
Fang Yuedong committed
24
    path_dict["psf_dir"] = os.path.join(path_dict["data_dir"], config["psf_setting"]["psf_dir"])
Fang Yuedong's avatar
Fang Yuedong committed
25
    path_dict["fd_path"] = os.path.join(path_dict["data_dir"], config["psf_setting"]["fd_path"])
Fang Yuedong's avatar
Fang Yuedong committed
26
27
28

    return path_dict

29
def read_config(config_filename):
Fang Yuedong's avatar
Fang Yuedong committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    """Read in a configuration file and return the corresponding dict(s).

    Parameters:
        config_filename:    The name of the configuration file to read.
    Returns:
        (list) of config dicts
    """
    config = {}
    config_file = open(config_filename).readlines()
    nlines = len(config_file)
    for i in range(nlines):
        row = config_file[i].split()
        if len(row) <= 1: continue # blank row
        if not "#" in row:
            if len(row) == 2:
                key, val = row[0:2]
                config.update({key:val})
            else:
                print("!! Something is wrong with parameter '%s'."%row[0])
                return
        elif row.index("#") == 2:
            key, val = row[0:2]
            config.update({key:val})
        elif row.index("#") == 0:
            continue # annotation
        else:
            print("!! Something is wrong with parameter '%s'."%row[0])
            return
58
    config = parse_config(config)
Fang Yuedong's avatar
Fang Yuedong committed
59
60
    return config

61
def parse_config(config):
Fang Yuedong's avatar
Fang Yuedong committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    """Parse the config values to the right type

    Parameters:
        config:     raw config dict 
    Returns:
        Parsed config dict
    """
    config["ra_center"]     = float(config["ra_center"])
    config["dec_center"]    = float(config["dec_center"])
    config["psf_rcont"]     = config["psf_rcont"].split(",")
    config["psfRa"]         = float(config["psf_rcont"][0])
    config["psfCont"]       = float(config["psf_rcont"][1])
    config["image_rot"]     = float(config["image_rot"])*galsim.degrees
    config["sigma_spin"]    = float(config["sigma_spin"])
    config["reduced_g1"]    = float(config["reduced_g1"])
    config["reduced_g2"]    = float(config["reduced_g2"])
    config["rotateEll"]     = float(config["rotateEll"])
    config["reEll"]         = int(config["rotateEll"]/45.0)
    if config["reEll"]==0: config["reIndex"] = "P"
    if config["reEll"]==1: config["reIndex"] = "X"
    if config["reEll"]==2: config["reIndex"] = "N"
    if config["reEll"]==3: config["reIndex"] = "Y"
    config["seed_flat"]     = int(config["seed_flat"])
    config["seed_prnu"]     = int(config["seed_prnu"])
    config["seed_star"]     = int(config["seed_star"])
    config["seed_gal"]      = int(config["seed_gal"])
    config["seed_Av"]       = int(config["seed_Av"])
    config["bias_level"]    = int(config["bias_level"])
    config["df_strength"]   = float(config["df_strength"])
Fang Yuedong's avatar
Fang Yuedong committed
91
    return config