Commit 26a0eb7d authored by Wei Chengliang's avatar Wei Chengliang
Browse files

reset config.yaml with html

parent 60a424bf
Loading
Loading
Loading
Loading
Loading
+8 KiB

File added.

No diff preview for this file type.

+118 −0
Original line number Diff line number Diff line
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,
    'call_sequence':{
        'scie_obs':{
          'shutter_effect': bool,
          'flat_fielding': bool,
          'field_dist': bool,
        },
        'sky_background':{
            'shutter_effect': bool,
            'flat_fielding': bool,
            'enable_straylight_model': bool,
            'flat_level': None,
            'flat_level_filt': None,
        },
        'PRNU_effect': {},
        'cosmic_rays':{
            'save_cosmic_img':  bool,
        },
        'poisson_and_dark':{
            'add_dark': bool,
        },
        'bright_fatter': {},
        'detector_defects':{
            'hot_pixels': bool,
            'dead_pixels': bool,
            'bad_columns': bool,
        },
        'nonlinearity': {},
        'blooming': {},
        'prescan_overscan':{
            'add_dark': bool,
        },
        'bias':{
            'bias_16channel': bool,
        },
        'readout_noise': {},
        'gain':{
            'gain_16channel': bool,
        },
        'quantization_and_output':{
            'format_output': bool,
        },
    },
}
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)

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)

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_obs.html', form_html=form_html)

if __name__ == '__main__':
    app.run(debug=True)
+127 −0
Original line number Diff line number Diff line
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)
+145 −0
Original line number Diff line number Diff line
---
###############################################
#
#  Configuration file for CSST simulation
#           Overall settings
#  CSST-Sim Group, 2024/01/08
#
###############################################

# Base diretories and naming setup
# can add some of the command-line arguments here as well;
# ok to pass either way or both, as long as they are consistent
work_dir: "/public/home/fangyuedong/project/workplace/"
run_name: "ext_on"

# Project cycle and run counter are used to name the outputs
project_cycle: 9
run_counter: 1

# Run options
run_option:
  # Output catalog only?
  # If yes, no imaging simulation will be run. Only the catalogs
  # of corresponding footprints will be generated.
  out_cat_only: NO

###############################################
# Catalog setting
###############################################
# Configure the input catalog: options should be implemented
# in the corresponding (user defined) 'Catalog' class
catalog_options:
  input_path:
    cat_dir: "/public/share/yangxuliu/CSSOSDataProductsSims/data_50sqDeg/"
    star_cat: "starcat_C9/"
    galaxy_cat: "qsocat/cat2CSSTSim_bundle-50sqDeg/"

  SED_templates_path:
    star_SED: "/public/share/yangxuliu/CSSOSDataProductsSims/data_50sqDeg/starcat_C9/"
    galaxy_SED: "/public/share/yangxuliu/CSSOSDataProductsSims/data_50sqDeg/sedlibs/"
    AGN_SED: "/public/share/yangxuliu/CSSOSDataProductsSims/data_50sqDeg/qsocat/qsosed/"

  # Only simulate stars?
  star_only: NO

  # Only simulate galaxies?
  galaxy_only: NO

  # rotate galaxy ellipticity
  rotateEll: 0. # [degree]

  # Whether to apply milky way extinction to galaxies
  enable_mw_ext_gal: YES
  planck_ebv_map: "/public/home/fangyuedong/project/ext_maps/planck/HFI_CompMap_ThermalDustModel_2048_R1.20.fits"

###############################################
# Observation setting
###############################################
obs_setting:
  # (Optional) a file of point list 
  # if you just want to run default pointing:
  # - pointing_dir: null
  # - pointing_file: null
  pointing_file: "/public/share/yangxuliu/CSSOSDataProductsSims/data_50sqDeg/pointing50_C9/pointing_50_1_n.dat"

  obs_config_file: "/public/home/fangyuedong/project/csst_msc_sim/config/obs_config_SCI.yaml"

  # Run specific pointing(s):
  # - give a list of indexes of pointings: [ip_1, ip_2...]
  # - run all pointings: null
  # Note: only valid when a pointing list is specified
  run_pointings: [0, 1, 2, 3, 4]

  # Whether to enable astrometric modeling
  enable_astrometric_model: YES

  # Cut by saturation magnitude in which band?
  cut_in_band: "z"

  # saturation magnitude margin
  mag_sat_margin: -2.5
  # mag_sat_margin: -15.

  # limiting magnitude margin
  mag_lim_margin: +1.0

###############################################
# PSF setting
###############################################
psf_setting:
  
  # Which PSF model to use:
  # "Gauss": simple gaussian profile
  # "Interp": Interpolated PSF from sampled ray-tracing data
  psf_model: "Interp"

  # PSF size [arcseconds]
  # radius of 80% energy encircled
  # NOTE: only valid for "Gauss" PSF
  # psf_rcont: 0.15

  # path to PSF data
  # NOTE: only valid for "Interp" PSF
  # PSF models for photometry survey simulation
  psf_pho_dir: "/public/share/yangxuliu/CSSOSDataProductsSims/dataC6/psfCube1"
  # PSF models for slitless spectrum survey simulation
  psf_sls_dir: "/public/share/yangxuliu/CSSOSDataProductsSims/data_50sqDeg/SLS_PSF_PCA_fp/"

###############################################
# Shear setting
###############################################

shear_setting:
  # Options to generate mock shear field:
  # "constant": all galaxies are assigned a constant reduced shear
  # "catalog": get shear values from catalog
  shear_type: "constant"

  # For constant shear field
  reduced_g1: 0.
  reduced_g2: 0.

###############################################
# Output options
###############################################
output_setting:
  output_format:  "channels" # Whether to export as 16 channels (subimages) with pre- and over-scan ("image"/"channels")
  shutter_output: NO # Whether to export shutter effect 16-bit image
  prnu_output:    NO # Whether to export the PRNU (pixel-to-pixel flat-fielding) files

###############################################
# Random seeds
###############################################
random_seeds:
  seed_poisson:         20210601  # Seed for Poisson noise
  seed_CR:              20210317  # Seed for generating random cosmic ray maps
  seed_flat:            20210101  # Seed for generating random flat fields
  seed_prnu:            20210102  # Seed for photo-response non-uniformity
  seed_gainNonUniform:  20210202  # Seed for gain nonuniformity
  seed_biasNonUniform:  20210203  # Seed for bias nonuniformity
  seed_rnNonUniform:    20210204  # Seed for readout-noise nonuniformity
  seed_badcolumns:      20240309  # Seed for bad columns
  seed_defective:       20210304  # Seed for defective (bad) pixels
  seed_readout:         20210601  # Seed for read-out gaussian noise
...
 No newline at end of file
+246 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>csst_msc_sim_CONF</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
            color: #333;
        }
        h1 {
            text-align: center;
            color: #4CAF50;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .block {
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 10px;
            background-color: #fafafa;
        }
        h2 {
            color: #4CAF50;
            font-size: 1.2em;
            margin-bottom: 10px;
        }
        label {
            display: block;
            margin: 5px 0;
        }
        input[type="text"] {
            width: calc(100% - 22px);
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin-bottom: 10px;
            transition: border 0.3s;
        }
        input[type="text"]:focus {
            border-color: #4CAF50;
            outline: none;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        table {
            width: 100%;
            border: 1px solid #ddd;
            border-radius: 8px; /* 圆角 */
            overflow: hidden; /* 隐藏超出表格的内容 */
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* 添加阴影 */
        }

        th, td {
            border-bottom: 1px solid #ddd; /* 单元格底部边框 */
            padding: 12px; /* 内边距 */
            text-align: left;
        }
        tr:hover {
            background-color: #ADD8E6; /* 悬停时背景颜色 */
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>配置obs_config_SCI.yaml</h1>
        <p><strong>说明:</strong>
        1,该脚本可用于生成CSST主巡天成像仿真的参数文件,相关参数说明见本页脚注;2,用户必须修改相关路径参数,其他参数可参考默认值
.
        </p>
        <hr style="border: 2px solid green;">
        <form method="POST">
            {{ form_html | safe }}
            <input type="submit" value="保存">
        </form>
    </div>

    <footer class="mt-4">
        <p class="text-center">这是一个有用的网页</p>
        <table>
            <tr>
                <th>KEYS</th>
                <th>VALUES</th>
                <th>COMMENTS</th>
            </tr>
            <tr>
                <th>obs_type</th>
                <td>"SCI", [str]</td>
                <td>仿真图像类型</td>
            </tr>
            <tr>
                <th>obs_type_code</th>
                <td>"101", [str]</td>
                <td>数据系统内部标号</td>
            </tr>
            <tr>
                <th>obs_id</th>
                <td>"00000001", [str]</td>
                <td>this setting will only be used if pointing list file is not given</td>
            </tr>
            <tr>
                <th>run_chips</th>
                <td>[7,8,9], [list]</td>
                <td>Define list of chips</td>
            </tr>
            <tr>
                <th>scie_obs:<span style="font-weight: normal;">shutter_effect</span></th>
                <td>YES, [bool]</td>
                <td>是否应用快门效应</td>
            </tr>
            <tr>
                <th>scie_obs:<span style="font-weight: normal;">flat_fielding</span></th>
                <td>YES, [bool]</td>
                <td>是否应用平场模块</td>
            </tr>
            <tr>
                <th>scie_obs:<span style="font-weight: normal;">field_dist</span></th>
                <td>YES, [bool]</td>
                <td> </td>
            </tr>
            <tr>
                <th>sky_background:<span style="font-weight: normal;">shutter_effect</span></th>
                <td>YES, [bool]</td>
                <td>是否应用快门效应</td>
            </tr>
            <tr>
                <th>sky_background:<span style="font-weight: normal;">flat_fielding</span></th>
                <td>YES, [bool]</td>
                <td>是否应用平场模块</td>
            </tr>
            <tr>
                <th>sky_background:<span style="font-weight: normal;">enable_straylight_model</span></th>
                <td>YES, [bool]</td>
                <td>是否应用杂散光模块</td>
            </tr>
            <tr>
                <th>sky_background:<span style="font-weight: normal;">flat_level</span></th>
                <td>null [null or int]</td>
                <td>set the total skybackground value (e-) in the exptime,if none,set null, or delete the key</td>
            </tr>
            <tr>
                <th>sky_background:<span style="font-weight: normal;">flat_level_filt</span></th>
                <td>null, [str]</td>
                <td>the vale of "flat_level" is in the filter "flat_level_filt", can set NUV, u, g, r, i, z, y, if none,set null,or delete the key</td>
            </tr>
            <tr>
                <th>PRNU_effect<span style="font-weight: normal;"></span></th>
                <td>---</td>
                <td>应用像素间不均匀响应模块;如果需关闭,请在生成的yaml文件中直接注释</td>
            </tr>
            <tr>
                <th>cosmic_rays:<span style="font-weight: normal;">save_cosmic_img</span></th>
                <td>YES, [bool]</td>
                <td>是否保存宇宙线图像</td>
            </tr>
            <tr>
                <th>poisson_and_dark:<span style="font-weight: normal;">add_dark</span></th>
                <td>YES, [bool]</td>
                <td>是否添加暗电流</td>
            </tr>
            <tr>
                <th>bright_fatter<span style="font-weight: normal;"></span></th>
                <td>YES, [bool]</td>
                <td>应用亮胖效应模块;如果需关闭,请在生成的yaml文件中直接注释</td>
            </tr>
            <tr>
                <th>detector_defects:<span style="font-weight: normal;">hot_pixels</span></th>
                <td>YES, [bool]</td>
                <td>是否添加热像元</td>
            </tr>
            <tr>
                <th>detector_defects:<span style="font-weight: normal;">dead_pixels</span></th>
                <td>YES, [bool]</td>
                <td>是否添加坏像元</td>
            </tr>
            <tr>
                <th>detector_defects:<span style="font-weight: normal;">bad_columns</span></th>
                <td>YES, [bool]</td>
                <td>是否添加坏像列</td>
            </tr>
            <tr>
                <th>nonlinearity<span style="font-weight: normal;"></span></th>
                <td>---</td>
                <td>应用非线性响应模块;如果需关闭,请在生成的yaml文件中直接注释</td>
            </tr>
            <tr>
                <th>blooming<span style="font-weight: normal;"></span></th>
                <td>---</td>
                <td>是否应用饱和溢出模块;如果需关闭,请在生成的yaml文件中直接注释</td>
            </tr>
            <tr>
                <th>prescan_overscan:<span style="font-weight: normal;">add_dark</span></th>
                <td>YES, [bool]</td>
                <td> 是否在pre/over-scan区域添加暗电流/</td>
            </tr>
            <tr>
                <th>bias:<span style="font-weight: normal;">bias_16channel</span></th>
                <td>YES, [bool]</td>
                <td>是否添加16通道的偏置电压</td>
            </tr>
            <tr>
                <th>readout_noise<span style="font-weight: normal;"></span></th>
                <td>---</td>
                <td>添加读出噪声;如果需关闭,请在生成的yaml文件中直接注释</td>
            </tr>
            <tr>
                <th>gain:<span style="font-weight: normal;">gain_16channel</span></th>
                <td>YES, [bool]</td>
                <td>是否应用增益</td>
            </tr>
            <tr>
                <th>quantization_and_output:<span style="font-weight: normal;">format_output</span></th>
                <td>YES, [bool]</td>
                <td>是否按0级数据格式定义输出图像</td>
            </tr>



        </table>
    </footer>
</body>
</html>
Loading