Commit 0a0fde28 authored by chenwei@shao.ac.cn's avatar chenwei@shao.ac.cn
Browse files

update

parent 7041526a
Loading
Loading
Loading
Loading

csst_shao_common/__init__.py

deleted100644 → 0
+0 −0

Empty file deleted.

csst_shao_common/common.py

deleted100644 → 0
+0 −190
Original line number Diff line number Diff line
"""
Identifier:     KSC-SJ6-csst_ifs_common/common.py
Name:           Common.py
Description:    CSST IFS COMMON
Author:         Wei Chen
"""

from .ifs_status import CsstIFSStatus
from .exit_code_list import IFSExitCode
from .logger import get_logger
# from .ifs_status import IFSError
from .config import IFSConfig
import os


class CsstIFSInit:

    def __init__(self):
        self.exit_code = "100"
        self.status = CsstIFSStatus
        self.fun_exit_code = IFSExitCode()
        self._env_init()
        self._dm_init()
        self.config_ifs_rss_env()
        self.config_ifs_cube_env()
        self.logger = get_logger()

    def _env_init(self):
        # code 100: "l1 normal exit"
        exit_code = "100"
        # self.logger.info("IFS Env Setting...")
        try:
            self.config = IFSConfig()
            self.config.set_ifs_config("dir_log", self.config.get_ifs_config("dir_local"))
            path_log = os.path.join(self.config.get_ifs_config("dir_log"), "./log.txt")
            self.config.set_ifs_config("path_log", path_log)
            self.logger = get_logger("CSST IFS log", self.config.get_ifs_config("path_log"))
            # self.logger.info("IFS Env Set Finish!")
        except Exception as e:
            # code 101: "l1 Env Set Error",
            exit_code = "101"
            print("{}".format(e))
            self.logger.error(self.fun_exit_code.get_ifs_exit_code('101'))
            self.logger.error(e)
            status = 2
            assert status in [CsstIFSStatus.PERFECT, CsstIFSStatus.WARNING]
        finally:
            return exit_code

    def _dm_init(self):
        # code 100: "l1 normal exit"
        exit_code = "100"
        try:
            from .data_manager import CsstIFSDataManager
            self.dm = CsstIFSDataManager()
        except Exception as e:
            # code 102: "Not found DFS system",
            # exit_code = "102"
            exit_code = "103"
            print(self.fun_exit_code.get_ifs_exit_code('103'))
            # print("{}".format(e))
            # self.logger.error(self.fun_exit_code.get_ifs_exit_code('102'))
            # self.logger.error(e)
            # status = 2
            # assert status in [CsstIFSStatus.PERFECT, CsstIFSStatus.WARNING]
        finally:
            return exit_code

    def config_ifs_rss_env(self):
        # /L1Pipeline/test_shao/test_data/ this dir is cdrs dir
        # self.config.set_ifs_config("path_RSSlog",
        #                            "/L1Pipeline/integration/ifs/csst_ifs_rss/proc_data/path_RSSlog.txt")
        # self.config.set_ifs_config("dir_rss_proc", "/L1Pipeline/integration/ifs/csst_ifs_rss/proc_data/")
        # self.config.set_ifs_config("dir_aux", "/L1Pipeline/integration/ifs/csst_ifs_rss/cal_data/")
        # self._config_ifs_rss_env_crds()
        # # self.config.set_ifs_config("path_l0_rss", self.dm.get_data_path_l0_sci(self.config.get_ifs_config("obsid")))
        # path_list = self.get_all_file_path("/L1Pipeline/integration/ifs/csst_ifs_rss/raw_data/")
        # self.config.set_ifs_config("path_l0_rss", path_list)
        #
        # self.config.set_ifs_config("path_l1_wcs_output",
        #                            "/L1Pipeline/test_shao/test_data/ifs/csst_ifs_wcs/temp_for_cube")

        self.config.set_ifs_config(
            "path_RSSlog",
            "/CSST_test/l1_all/workspace/test_data/ifs/sim/L0/IFS_simData_2023-06-26/proc_data/path_RSSlog.txt")
        # self.config.set_ifs_config(
        #     "dir_rss_proc", "/CSST_test/l1_all/workspace/test_data/ifs/sim/L0/IFS_simData_2023-06-26/proc_data/")
        self.config.set_ifs_config(
            "dir_rss_proc", "/datapool/CSST_PIPELINE/workspace/temp_data/ifs/sim/L1/sim_ver230725/proc_l1_rss/")
        self.config.set_ifs_config(
            "dir_aux",
            "/CSST_test/l1_all/workspace/test_data/ifs/sim/L0/IFS_CRDS_20230725/")
        self._config_ifs_rss_env_crds()
        # self.config.set_ifs_config("path_l0_rss", self.dm.get_data_path_l0_sci(self.config.get_ifs_config("obsid")))
        path_list = self.get_all_file_path(
            "/CSST_test/l1_all/workspace/test_data/ifs/sim/L0/IFS_simData_2023-07-25/sky_Data_temp/")
        self.config.set_ifs_config("path_l0_rss", path_list)

        self.config.set_ifs_config(
            "path_l1_wcs_output",
            "/CSST_test/l1_all/workspace/test_data/ifs/sim/L0/IFS_simData_2023-07-25/temp_for_cube")

        return

    @staticmethod
    def get_all_file_path(dir_temp):
        path_list = list()
        for root, dirs, names in os.walk(dir_temp):
            for filename in names:
                path_list.append(os.path.join(root, filename))
        return path_list

    @staticmethod
    def get_file_path_include_keywords(dir_temp, keywords):
        path_list = list()
        for root, dirs, names in os.walk(dir_temp):
            for filename in names:
                if keywords in filename:
                    path_list.append(os.path.join(root, filename))
        return path_list

    def _config_ifs_rss_env_crds(self):

        path = os.path.join(self.config.get_ifs_config("dir_aux"), self.dm.get_data_name_rss_bias_b())
        self.config.set_ifs_config("path_rss_biasm_b", path)
        path = os.path.join(self.config.get_ifs_config("dir_aux"), self.dm.get_data_name_rss_bias_r())
        self.config.set_ifs_config("path_rss_biasm_r", path)

        path = os.path.join(self.config.get_ifs_config("dir_aux"), self.dm.get_data_name_rss_flat_b())
        self.config.set_ifs_config("path_rss_flatm_b", path)
        path = os.path.join(self.config.get_ifs_config("dir_aux"), self.dm.get_data_name_rss_flat_r())
        self.config.set_ifs_config("path_rss_flatm_r", path)

        self.config.set_ifs_config("path_rss_dark_b", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_dark_b())
        self.config.set_ifs_config("path_rss_dark_r", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_dark_r())

        self.config.set_ifs_config("path_rss_flatnorm_b", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_flatnorm_b())
        self.config.set_ifs_config("path_rss_flatnorm_r", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_flatnorm_r())

        self.config.set_ifs_config("path_rss_trace_b", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_trace_b())
        self.config.set_ifs_config("path_rss_trace_r", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_trace_r())

        self.config.set_ifs_config("path_rss_ptow_b", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_ptow_b())
        self.config.set_ifs_config("path_rss_ptow_r", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_ptow_r())

        self.config.set_ifs_config("path_rss_starfile_b", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_starfile_b())
        self.config.set_ifs_config("path_rss_starfile_r", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_starfile_r())

        self.config.set_ifs_config("path_rss_arclab_b", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_arclab_b())
        self.config.set_ifs_config("path_rss_arclab_r", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_arclab_r())

        self.config.set_ifs_config("path_rss_starlab", self.config.get_ifs_config(
            "dir_aux") + self.dm.get_data_name_rss_starlab())
        return

    def config_ifs_cube_env(self):
        # self.config.set_ifs_config("dir_tmp",
        # "/nfsdata/users/csstpipeline/L1Pipeline/integration/ifs/csst_ifs_cube/aimdir")
        self.config.set_ifs_config("objid", "NGC6397")
        # self.config.set_ifs_config("path_l0_cube", self.dm.get_data_path_l1_sci(self.config.get_ifs_config("obsid")))

        # hdir = '/nfsdata/users/csstpipeline/L1Pipeline/integration/ifs/csst_ifs_cube/test_file/'
        # aimdir = '/nfsdata/users/csstpipeline/L1Pipeline/integration/ifs/csst_ifs_cube/aimdir/'
        target = 'NGC6397'
        hdir = '/nfsdata/users/csstpipeline/L1Pipeline/integration/ifs/csst_ifs_cube/test_file/'
        aimdir = '/nfsdata/users/csstpipeline/L1Pipeline/integration/ifs/csst_ifs_cube/aimdir/'
        softver = 'R7201'
        ver = 1
        lamr = hdir + "CSST_IFS_LAMP_MASTER_20220610_R_L1_R7201_bew_____.fits"
        lamb = hdir + "CSST_IFS_LAMP_MASTER_20220610_B_L1_R7201_bew_____.fits"
        self.config.set_ifs_config('objid', target)
        self.config.set_ifs_config('hdir', hdir)
        self.config.set_ifs_config('aimdir', aimdir)
        self.config.set_ifs_config('softver', softver)
        self.config.set_ifs_config('ver', ver)
        self.config.set_ifs_config('lamr', lamr)
        self.config.set_ifs_config('lamb', lamb)
        return

csst_shao_common/config.py

deleted100644 → 0
+0 −87
Original line number Diff line number Diff line
"""
Identifier:     KSC-SJ6-csst_ifs_common/config.py
Name:           config.py
Description:    CSST IFS CONFIG
Author:         Wei Chen
"""

import os
import threading
from .ifs_status import CsstIFSStatus


class IFSConfig(object):
    """
    IFS Config as public parameters.
    """
    _instance_lock = threading.Lock()

    def __init__(self):
        self._set_ifs_config_init()

    def __new__(cls, *args, **kwargs):
        if not hasattr(IFSConfig, "_instance"):
            with IFSConfig._instance_lock:
                if not hasattr(IFSConfig, "_instance"):
                    IFSConfig._instance = object.__new__(cls)
                    cls.config_dict = dict()
                    cls.public_variable_dict = dict()
        return IFSConfig._instance

    def _set_ifs_config_init(self):
        local_path = os.getcwd()
        config_dict = {
            "dir_l0": ".",
            "dir_l1": ".",
            "dir_log": ".",
            "dir_rss": ".",
            "dir_cube": ".",
            "dir_local": local_path,
            "dir_rss_proc": "/L1Pipeline/integration/ifs/csst_ifs_rss/proc_data/"
        }
        if self.config_dict == {}:
            self.config_dict = config_dict

    def get_ifs_config(self, ifs_config_key):
        """
            get ifs config.

            Parameters
            ----------
            ifs_config_key : str
                The string of dict key

            Returns
            -------
            dict_value : string
                The string of dict_value.
        """
        if ifs_config_key in self.config_dict:
            ifs_config_value = self.config_dict[ifs_config_key]
            return ifs_config_value
        else:
            print("Error: key "+ifs_config_key+" not exist")
            status = 2
            assert status in [CsstIFSStatus.PERFECT, CsstIFSStatus.WARNING]

    def set_ifs_config(self, ifs_config_key, ifs_config_value):
        """
            get ifs config.

            Parameters
            ----------
            ifs_config_key : str
                The string of dict key

            ifs_config_value : anytype
                The string of dict value

            Returns
            -------
            None
        """
        # if ifs_config_key in self.config_dict:
        #     print("Warning: key "+ifs_config_key+" has exist, overwrite!")
        #     status = 1
        #     assert status in [CsstIFSStatus.PERFECT, CsstIFSStatus.WARNING]
        self.config_dict[ifs_config_key] = ifs_config_value
+0 −84
Original line number Diff line number Diff line
"""
Identifier:     KSC-SJ6-csst_ifs_common/exit_code_list.py
Name:           exit_code_list.py
Description:    CSST IFS Exit Code List
Author:         Wei Chen
"""

import threading


class IFSExitCode(object):
    """
    IFS Exit Code as public.
    """
    _instance_lock = threading.Lock()

    def __init__(self):
        self.config_dict = {}
        self._set_ifs_exit_code_init()

    def __new__(cls, *args, **kwargs):
        if not hasattr(IFSExitCode, "_instance"):
            with IFSExitCode._instance_lock:
                if not hasattr(IFSExitCode, "_instance"):
                    IFSExitCode._instance = object.__new__(cls)
        return IFSExitCode._instance

    # modify exit code here!
    def _set_ifs_exit_code_init(self):
        config_dict = {
            "100": "l1 normal exit",
            "101": "l1 Env Set Error",
            "102": "Not found DFS system",
            "103": "CSST Dummy DFS system Initialize",
            "199": "l1 error exit",
            "200": "rss normal exit",
            "201": "rss error exit",
            "300": "cube normal exit",
            "301": "cube error exit",
            "400": "qc normal exit",
            "401": "qc error exit",
        }
        self.config_dict = config_dict

    def get_ifs_exit_code(self, ifs_exit_code_key):
        """
            get ifs config.

            Parameters
            ----------
            ifs_exit_code_key : str
                The string of dict key

            Returns
            -------
            dict_value : string
                The exit_code of program status.
            """
        if ifs_exit_code_key in self.config_dict:
            ifs_exit_code_value = self.config_dict[ifs_exit_code_key]
            return ifs_exit_code_value
        else:
            print("Error: key "+ifs_exit_code_key+" not exist")

    def set_ifs_exit_code(self, ifs_exit_code_key, ifs_exit_code_value):
        """
            get ifs config.

            Parameters
            ----------
            ifs_exit_code_key : str
                The string of dict key

            ifs_exit_code_value : str
                The string of dict value

            Returns
            -------
            None
            """
        if ifs_exit_code_key in self.config_dict:
            print("Warning: key "+ifs_exit_code_key+" has existed, overwrite!")

        self.config_dict[ifs_exit_code_key] = ifs_exit_code_value

csst_shao_common/ifs_status.py

deleted100644 → 0
+0 −36
Original line number Diff line number Diff line
"""
Identifier:     KSC-SJ6-csst_ifs_common/ifs_status.py
Name:           ifs_status.py
Description:    CSST IFS Status
Author:         Wei Chen
"""

from enum import IntEnum


class CsstIFSStatus(IntEnum):
    """
    The CSST IFS Status class.

    This class provides a set of status which should be returned
    from each CSST L1 IFS functional module.

    Examples
    --------
    >>> def f(num):
    >>>     if num > 0:
    >>>         return CsstIFSStatus.PERFECT
    >>>     return CsstIFSStatus.ERROR
    """
    PERFECT = 0
    WARNING = 1
    ERROR = 2
    # status list to be completed

#
# class IFSError(Exception):
#     def __init__(self, msg):
#         self.msg = msg
#
#     def __str__(self):
#         return self.msg
Loading