reset_overall.py 3.68 KB
Newer Older
Wei Chengliang's avatar
Wei Chengliang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from flask import Flask, render_template, request, redirect
import yaml
import ast

app = Flask(__name__)

key_type_map = {  
    'work_dir': str,
    'run_name': str,
    'project_cycle': int,
    'run_counter': int,
    'run_option':{
        'out_cat_only': bool,
    },
    'catalog_options':{
        'input_path':{
            'cat_dir': str,
            'star_cat': str,
            'galaxy_cat': str,
        },
        'SED_templates_path':{
            'star_SED': str,
            'galaxy_SED': str,
            'AGN_SED': str,
        },
        'star_only': bool, 
        'galaxy_only': bool,
        'rotateEll': float,
        'enable_mw_ext_gal': bool,
        'planck_ebv_map':str,
    },
    'obs_setting':{
        'pointing_file': str,
        'obs_config_file': str,
        'run_pointings': list,
        'enable_astrometric_model': bool,
        'cut_in_band': str,
        'mag_sat_margin': float,
        'mag_lim_margin': float,
    },
    'psf_setting':{
        'psf_model': str,
        'psf_pho_dir': str,
        'psf_sls_dir': str,
    },
    'shear_setting':{
        'shear_type': str,
        'reduced_g1': float,
        'reduced_g2': float,
    },
    'output_setting':{
        'output_format': str,
        'shutter_output': bool,
        'prnu_output': bool,
    },
    'random_seeds':{
        'seed_poisson': int,
        'seed_CR': int,
        'seed_flat': int,
        'seed_prnu': int,
        'seed_gainNonUniform': int,
        'seed_biasNonUniform': int,
        'seed_rnNonUniform': int,
        'seed_badcolumns': int,
        'seed_defective': int,
        'seed_readout': int,
    },
}
def convert_dict_values(d, key_type_map):  
    for key, value in d.items():  
        if isinstance(value, dict):  
            convert_dict_values(value, key_type_map[key])  
        elif key in key_type_map:  
            if key_type_map[key] is int:
                d[key] = int(value)
            if key_type_map[key] is float:
                d[key] = float(value)
            if key_type_map[key] is bool:
                if d[key].lower() == 'yes' or d[key].lower() == 'true':
                    d[key] = True
                else:
                    d[key] = False
            if key_type_map[key] is str:
                d[key] = str(value)
            if key_type_map[key] is list:
                d[key] = ast.literal_eval(value)

def load_yaml():
    with open('templates/config_overall.yaml', 'r') as file:
        return yaml.safe_load(file)

def save_yaml(data):
    convert_dict_values(data, key_type_map) 
    with open('config_reset/config_overall_reset.yaml', 'w') as file:
        yaml.dump(data, file, default_flow_style=False, sort_keys=False)

def render_form(data, parent_key=''):
    form_html = ''
    for key, value in data.items():
        full_key = f"{parent_key}.{key}" if parent_key else key
        if isinstance(value, dict):  # 处理字典
            form_html += f"<div class='block'><h2>{key}</h2>{render_form(value, full_key)}</div>"
        else:
            form_html += f"<label for='{full_key}'>{key}:</label>"
            form_html += f"<input type='text' id='{full_key}' name='{full_key}' value='{value}'><br>"
    return form_html

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        data = load_yaml()
        for key in request.form:
            keys = key.split('.')
            temp = data
            for k in keys[:-1]:
                temp = temp[k]
            temp[keys[-1]] = request.form[key]
        save_yaml(data)
        return redirect('/')

    data = load_yaml()
    form_html = render_form(data)
    return render_template('index_overall.html', form_html=form_html)

if __name__ == '__main__':
    app.run(debug=True)