reset_obs.py 3.42 KB
Newer Older
Wei Chengliang's avatar
Wei Chengliang committed
1
2
3
4
5
6
7
8
9
10
11
12
from flask import Flask, render_template, request, redirect
import yaml
import ast

app = Flask(__name__)


key_type_map = {
    'obs_type': str,
    'obs_type_code': str,
    'obs_id': str,
    'run_chips': list,
Wei Chengliang's avatar
Wei Chengliang committed
13
14
    'call_sequence': {
        'scie_obs': {
Wei Chengliang's avatar
Wei Chengliang committed
15
16
17
18
          'shutter_effect': bool,
          'flat_fielding': bool,
          'field_dist': bool,
        },
Wei Chengliang's avatar
Wei Chengliang committed
19
        'sky_background': {
Wei Chengliang's avatar
Wei Chengliang committed
20
21
22
23
24
25
26
            'shutter_effect': bool,
            'flat_fielding': bool,
            'enable_straylight_model': bool,
            'flat_level': None,
            'flat_level_filt': None,
        },
        'PRNU_effect': {},
Wei Chengliang's avatar
Wei Chengliang committed
27
        'cosmic_rays': {
Wei Chengliang's avatar
Wei Chengliang committed
28
29
            'save_cosmic_img':  bool,
        },
Wei Chengliang's avatar
Wei Chengliang committed
30
        'poisson_and_dark': {
Wei Chengliang's avatar
Wei Chengliang committed
31
32
33
            'add_dark': bool,
        },
        'bright_fatter': {},
Wei Chengliang's avatar
Wei Chengliang committed
34
        'detector_defects': {
Wei Chengliang's avatar
Wei Chengliang committed
35
36
37
38
39
40
            'hot_pixels': bool,
            'dead_pixels': bool,
            'bad_columns': bool,
        },
        'nonlinearity': {},
        'blooming': {},
Wei Chengliang's avatar
Wei Chengliang committed
41
        'prescan_overscan': {
Wei Chengliang's avatar
Wei Chengliang committed
42
43
            'add_dark': bool,
        },
Wei Chengliang's avatar
Wei Chengliang committed
44
        'bias': {
Wei Chengliang's avatar
Wei Chengliang committed
45
46
47
            'bias_16channel': bool,
        },
        'readout_noise': {},
Wei Chengliang's avatar
Wei Chengliang committed
48
        'gain': {
Wei Chengliang's avatar
Wei Chengliang committed
49
50
            'gain_16channel': bool,
        },
Wei Chengliang's avatar
Wei Chengliang committed
51
        'quantization_and_output': {
Wei Chengliang's avatar
Wei Chengliang committed
52
53
54
55
            'format_output': bool,
        },
    },
}
Wei Chengliang's avatar
Wei Chengliang committed
56
57


Wei Chengliang's avatar
Wei Chengliang committed
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
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)
            if key_type_map[key] is None:
                d[key] = None


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

Wei Chengliang's avatar
Wei Chengliang committed
84

Wei Chengliang's avatar
Wei Chengliang committed
85
86
87
88
89
def save_yaml(data):
    convert_dict_values(data, key_type_map)
    with open('config_reset/obs_config_SCI_reset.yaml', 'w') as file:
        yaml.dump(data, file, default_flow_style=False, sort_keys=False)

Wei Chengliang's avatar
Wei Chengliang committed
90

Wei Chengliang's avatar
Wei Chengliang committed
91
92
93
94
95
96
97
98
99
100
101
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

Wei Chengliang's avatar
Wei Chengliang committed
102

Wei Chengliang's avatar
Wei Chengliang committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@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_obs.html', form_html=form_html)

Wei Chengliang's avatar
Wei Chengliang committed
120

Wei Chengliang's avatar
Wei Chengliang committed
121
122
if __name__ == '__main__':
    app.run(debug=True)