Commit a45b0e9c authored by Zhao Bowei's avatar Zhao Bowei
Browse files

Replace slsconf.py

parent c4c1e88f
Pipeline #2676 failed with stage
"""
Identifier: csst_common/ccds/slsconf.py
Name: slsconf.py
Description: csst_common package
Author: Bo Zhang
Created: 2022-09-13
Modified-History:
2023-12-15, Bo Zhang, created
2023-12-15, Bo Zhang, add module header
"""
import json
import os
import re
import astropy.io.fits as pyfits
from ccds import client
# chipid: [01, 02, 03, 04, 05, 10, 21, 26, 27, 28, 29, 30]
# filter: [GI, GV, GU, GU, GV, GI, GI, GV, GU, GU, GV, GI]
# clabel: [GI-1, GV-1, GU-1, GU-2, GV-2, GI-2, GI-3, GV-3, GU-3, GU-4, GV-4, GI-4]
def get_version():
observatory = client.get_default_observatory()
operational_context = client.get_default_context(observatory)
ver = re.split(r"[_.]", operational_context)[1]
return ver
def resave_sensitivity(dir_save, chipid, flt):
ver = get_version()
h = pyfits.open(
dir_save + "CSST_MSC_MS_SENSITIVITY_" + chipid + "_" + ver + ".fits"
)
for extname, order in zip(
["L0ST", "LP1ST", "LM1ST", "LP2ST", "LM2ST"],
["0st", "1st", "-1st", "2st", "-2st"],
):
hdu0 = pyfits.PrimaryHDU()
hdu1 = pyfits.BinTableHDU(h[extname].data)
hdul = pyfits.HDUList([hdu0, hdu1])
hdul.writeto(
dir_save + "CSST_MSC_MS_SENSITIVITY_" + flt + "_" + order + ".fits",
overwrite=True,
checksum=True,
)
def readjson(dir_save, fjsoname):
with open(dir_save + fjsoname) as f:
def readjson(file_json_path):
with open(file_json_path) as f:
d = json.load(f)
return d
......@@ -64,45 +24,15 @@ def readjson(dir_save, fjsoname):
# 'DLDP_D_0', 'DLDP_D_1', 'BEAME', 'MMAG_EXTRACT_E', 'MMAG_MARK_E', 'DYDX_ORDER_E', 'DYDX_E_0', 'DYDX_E_1',
# 'XOFF_E', 'YOFF_E', 'DISP_ORDER_E', 'DLDP_E_0', 'DLDP_E_1']
GL = [
"GI2",
"GV4",
"GU2",
"GU4",
"GV2",
"GI4",
"GI6",
"GV8",
"GU6",
"GU8",
"GV6",
"GI8",
]
GR = [
"GI1",
"GV3",
"GU1",
"GU3",
"GV1",
"GI3",
"GI5",
"GV7",
"GU5",
"GU7",
"GV5",
"GI7",
]
SEN = ["GI", "GV", "GU", "GU", "GV", "GI", "GI", "GV", "GU", "GU", "GV", "GI"]
def fwriteKEY(fsx, i):
def fwriteKEY(fsx, flt):
fsx.write("INSTRUMENT CSSTSLS" + "\n")
fsx.write("CAMERA " + SEN[i] + "\n")
if SEN[i] == "GI":
fsx.write("CAMERA " + flt + "\n")
if flt == "GI":
fsx.write("WAVELENGTH 6200 10000" + "\n")
elif SEN[i] == "GV":
elif flt == "GV":
fsx.write("WAVELENGTH 4000 6200" + "\n")
elif SEN[i] == "GU":
elif flt == "GU":
fsx.write("WAVELENGTH 2550 4000" + "\n")
fsx.write("\n" + "SCIENCE_EXT SCI ; Science extension" + "\n")
fsx.write("DQ_EXT DQ ; DQ extension" + "\n")
......@@ -116,11 +46,12 @@ def fwriteKEY(fsx, i):
def fwriteBEAM(
dir_save,
fsx,
i,
extract1d_json_path,
sensitivity_order_name,
GRATINGLR,
BEAMX,
SENSITIVITY_X,
MMAG_EXTRACT_X,
MMAG_MARK_X,
DYDX_ORDER_X,
......@@ -132,130 +63,66 @@ def fwriteBEAM(
DLDP_X_0,
DLDP_X_1,
):
ver = get_version()
[
d01_GI21,
d02_GV43,
d03_GU21,
d04_GU43,
d05_GV21,
d10_GI43,
d21_GI65,
d26_GV87,
d27_GU65,
d28_GU87,
d29_GV65,
d30_GI87,
] = [
readjson(dir_save, "CSST_MSC_MS_EXTRACT1D_" + chipid + "_" + ver + ".json")
for chipid in [
"01",
"02",
"03",
"04",
"05",
"10",
"21",
"26",
"27",
"28",
"29",
"30",
]
]
d = [
d01_GI21,
d02_GV43,
d03_GU21,
d04_GU43,
d05_GV21,
d10_GI43,
d21_GI65,
d26_GV87,
d27_GU65,
d28_GU87,
d29_GV65,
d30_GI87,
]
d = readjson(extract1d_json_path)
fsx.write(BEAMX), [
fsx.write(" " + str(d[i][GRATINGLR][BEAMX][j]))
for j in range(len(d[i][GRATINGLR][BEAMX]))
fsx.write(" " + str(d[GRATINGLR][BEAMX][j]))
for j in range(len(d[GRATINGLR][BEAMX]))
], fsx.write("\n")
fsx.write(MMAG_EXTRACT_X + " " + str(d[i][GRATINGLR][MMAG_EXTRACT_X]) + "\n")
fsx.write(MMAG_MARK_X + " " + str(d[i][GRATINGLR][MMAG_MARK_X]) + "\n")
fsx.write(MMAG_EXTRACT_X + " " + str(d[GRATINGLR][MMAG_EXTRACT_X]) + "\n")
fsx.write(MMAG_MARK_X + " " + str(d[GRATINGLR][MMAG_MARK_X]) + "\n")
fsx.write("# " + "\n")
fsx.write("# Trace description " + "\n")
fsx.write("# " + "\n")
fsx.write(DYDX_ORDER_X + " " + str(d[i][GRATINGLR][DYDX_ORDER_X]) + "\n")
fsx.write(DYDX_ORDER_X + " " + str(d[GRATINGLR][DYDX_ORDER_X]) + "\n")
fsx.write(DYDX_X_0), [
fsx.write(" " + str(d[i][GRATINGLR][DYDX_X_0][j]))
for j in range(len(d[i][GRATINGLR][DYDX_X_0]))
fsx.write(" " + str(d[GRATINGLR][DYDX_X_0][j]))
for j in range(len(d[GRATINGLR][DYDX_X_0]))
], fsx.write("\n")
if BEAMX == "BEAMB":
pass
else:
fsx.write(DYDX_X_1), [
fsx.write(" " + str(d[i][GRATINGLR][DYDX_X_1][j]))
for j in range(len(d[i][GRATINGLR][DYDX_X_1]))
fsx.write(" " + str(d[GRATINGLR][DYDX_X_1][j]))
for j in range(len(d[GRATINGLR][DYDX_X_1]))
], fsx.write("\n")
fsx.write("# " + "\n")
fsx.write("# X and Y Offsets " + "\n")
fsx.write("# " + "\n")
fsx.write(XOFF_X + " " + str(d[i][GRATINGLR][XOFF_X]) + "\n")
fsx.write(YOFF_X + " " + str(d[i][GRATINGLR][YOFF_X]) + "\n")
fsx.write(XOFF_X + " " + str(d[GRATINGLR][XOFF_X]) + "\n")
fsx.write(YOFF_X + " " + str(d[GRATINGLR][YOFF_X]) + "\n")
fsx.write("# " + "\n")
fsx.write("# Dispersion solution " + "\n")
fsx.write("# " + "\n")
fsx.write(DISP_ORDER_X + " " + str(d[i][GRATINGLR][DISP_ORDER_X]) + "\n")
fsx.write(DISP_ORDER_X + " " + str(d[GRATINGLR][DISP_ORDER_X]) + "\n")
fsx.write(DLDP_X_0), [
fsx.write(" " + str(d[i][GRATINGLR][DLDP_X_0][j]))
for j in range(len(d[i][GRATINGLR][DLDP_X_0]))
fsx.write(" " + str(d[GRATINGLR][DLDP_X_0][j]))
for j in range(len(d[GRATINGLR][DLDP_X_0]))
], fsx.write("\n")
fsx.write(DLDP_X_1), [
fsx.write(" " + str(d[i][GRATINGLR][DLDP_X_1][j]))
for j in range(len(d[i][GRATINGLR][DLDP_X_1]))
fsx.write(" " + str(d[GRATINGLR][DLDP_X_1][j]))
for j in range(len(d[GRATINGLR][DLDP_X_1]))
], fsx.write("\n")
fsx.write("# " + "\n")
if BEAMX == "BEAMA":
ordername = "1st"
fsx.write(
"SENSITIVITY_A CSST_MSC_MS_SENSITIVITY_" + SEN[i] + "_1st.fits" + "\n"
)
elif BEAMX == "BEAMB":
ordername = "0st"
fsx.write(
"SENSITIVITY_B CSST_MSC_MS_SENSITIVITY_" + SEN[i] + "_0st.fits" + "\n"
)
elif BEAMX == "BEAMC":
ordername = "-1st"
fsx.write(
"SENSITIVITY_C CSST_MSC_MS_SENSITIVITY_" + SEN[i] + "_-1st.fits" + "\n"
)
elif BEAMX == "BEAMD":
ordername = "2st"
fsx.write(
"SENSITIVITY_D CSST_MSC_MS_SENSITIVITY_" + SEN[i] + "_2st.fits" + "\n"
)
elif BEAMX == "BEAME":
ordername = "-2st"
fsx.write(
"SENSITIVITY_E CSST_MSC_MS_SENSITIVITY_" + SEN[i] + "_-2st.fits" + "\n"
)
fsx.write(SENSITIVITY_X + " " + sensitivity_order_name + '\n')
fsx.write("# " + "\n" + "\n")
def fsave_conf(dir_save, GLR, GRATINGLR, i):
c = dir_save + "CSST_MSC_MS_" + GLR[i] + ".conf"
def fsave_conf(extract1d_json_path, extract1d_conf_path, sensitivity_order_names, GRATINGLR):
c = extract1d_conf_path
flt = readjson(extract1d_json_path)['FILTER']
os.system("> " + c)
fs = open(c, "a")
fwriteKEY(fs, i)
fwriteKEY(fs, flt)
fs.write("# 1 order (BEAM A) *******************" + "\n")
fwriteBEAM(
dir_save,
fs,
i,
extract1d_json_path,
sensitivity_order_names[0],
GRATINGLR,
"BEAMA",
"SENSITIVITY_A",
"MMAG_EXTRACT_A",
"MMAG_MARK_A",
"DYDX_ORDER_A",
......@@ -267,13 +134,15 @@ def fsave_conf(dir_save, GLR, GRATINGLR, i):
"DLDP_A_0",
"DLDP_A_1",
)
fs.write("\n# 0 order (BEAM B) *******************" + "\n")
fwriteBEAM(
dir_save,
fs,
i,
extract1d_json_path,
sensitivity_order_names[1],
GRATINGLR,
"BEAMB",
"SENSITIVITY_B",
"MMAG_EXTRACT_B",
"MMAG_MARK_B",
"DYDX_ORDER_B",
......@@ -287,11 +156,12 @@ def fsave_conf(dir_save, GLR, GRATINGLR, i):
)
fs.write("\n# -1 order (BEAM C) *******************" + "\n")
fwriteBEAM(
dir_save,
fs,
i,
extract1d_json_path,
sensitivity_order_names[2],
GRATINGLR,
"BEAMC",
"SENSITIVITY_C",
"MMAG_EXTRACT_C",
"MMAG_MARK_C",
"DYDX_ORDER_C",
......@@ -305,11 +175,12 @@ def fsave_conf(dir_save, GLR, GRATINGLR, i):
)
fs.write("\n# 2 order (BEAM D) *******************" + "\n")
fwriteBEAM(
dir_save,
fs,
i,
extract1d_json_path,
sensitivity_order_names[3],
GRATINGLR,
"BEAMD",
"SENSITIVITY_D",
"MMAG_EXTRACT_D",
"MMAG_MARK_D",
"DYDX_ORDER_D",
......@@ -323,11 +194,12 @@ def fsave_conf(dir_save, GLR, GRATINGLR, i):
)
fs.write("\n# -2 order (BEAM E) *******************" + "\n")
fwriteBEAM(
dir_save,
fs,
i,
extract1d_json_path,
sensitivity_order_names[4],
GRATINGLR,
"BEAME",
"SENSITIVITY_E",
"MMAG_EXTRACT_E",
"MMAG_MARK_E",
"DYDX_ORDER_E",
......@@ -342,23 +214,39 @@ def fsave_conf(dir_save, GLR, GRATINGLR, i):
fs.close()
def get_slsconf(dir_save=".", **kwargs):
"""save SLS conf files to `dir_save`"""
# resave the sensitivity.fits
for chipid, flt in zip(["01", "02", "03"], ["GI", "GV", "GU"]):
resave_sensitivity(dir_save, chipid, flt)
# save CSST_MSC_MS_*.conf
for i in range(0, 12):
fsave_conf(dir_save, GL, "GRATINGL", i)
fsave_conf(dir_save, GR, "GRATINGR", i)
# TODO
def convert_slsconf(
extract1d_json_path: str = "/ccds_root/references/msc/csst_msc_ms_extract1d_01_000040.json",
config_left: str = "./csst_msc_ms_extract1d_01_000040_left.conf",
config_right: str = "./csst_msc_ms_extract1d_01_000040_right.conf",
):
pass
def resave_sensitivity(sensitivity_fits_path, sensitivity_order_path):
h = pyfits.open(sensitivity_fits_path)
for extname in ['L0st', 'LP1st', 'LM1st', 'LP2st', 'LM2st']:
hdu0 = pyfits.PrimaryHDU()
hdu1 = pyfits.BinTableHDU(h[extname].data)
hdul = pyfits.HDUList([hdu0, hdu1])
hdul.writeto(sensitivity_order_path, overwrite=True, checksum=True)
hdul.close()
h.close()
def convert_slsconf(extract1d_json_path, sensitivity_fits_path, dir_output):
extract1d_name = os.path.basename(extract1d_json_path)
sensitivity_name = os.path.basename(sensitivity_fits_path)
conf_names = []
conf_paths = []
sensitivity_order_names = []
sensitivity_order_paths = []
# save CSST_MSC_MS_SENSITIVITY_CHIPID_VERSION_ORDER.conf
for order in ['+1st', '0st', '-1st', '+2st', '-2st']:
sensitivity_order_name = sensitivity_name.replace(".fits", ''.join(["_", order, ".fits"]))
sensitivity_order_path = os.path.join(dir_output, sensitivity_order_name)
resave_sensitivity(sensitivity_fits_path, sensitivity_order_path)
sensitivity_order_names.append(sensitivity_order_name)
sensitivity_order_paths.append(sensitivity_order_path)
# save CSST_MSC_MS_EXTRACT1D_CHIPID_VERSION_DIRECTION.conf
for direction, GRATINGLR in zip(['left', 'right'], ['GRATINGL', 'GRATINGR']):
conf_name = extract1d_name.replace(".json", ''.join(["_", direction, ".conf"]))
conf_path = os.path.join(dir_output, conf_name)
fsave_conf(extract1d_json_path, conf_path, sensitivity_order_names, GRATINGLR)
conf_names.append(conf_name)
conf_paths.append(conf_path)
return dict(zip(conf_names, conf_paths)), dict(zip(sensitivity_order_names, sensitivity_order_paths))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment