example_interface.py 4.34 KB
Newer Older
BO ZHANG's avatar
BO ZHANG 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import logging
import os
from typing import Union

import numpy as np
from astropy.io import fits
from csst_common.data_manager import CsstMsDataManager
from csst_common.logger import get_logger
from csst_common.status import CsstStatus


def read_image(filename_input: str) -> np.ndarray:
    """ Read image. """
    return fits.getdata(filename_input)


def process_data(data: np.ndarray) -> np.ndarray:
    """ Process data. """
    return np.fliplr(np.flipud(data))


def check_results(dm: CsstMsDataManager, logger: logging.Logger) -> bool:
    """ Check whether processed data are generated. """
    existence = [
        os.path.exists(dm.l1_detector(detector=detector, post="L1_processed.fits"))
        for detector in dm.target_detectors
    ]
    if all(existence):
        return True
    else:
        return False


# process a single image
def process_single_image(
        filename_input: str,
        filename_output: str,
        logger: Union[None, logging.Logger] = None
) -> CsstStatus:
    """
    Flip a single image.

    Flip a single image with ``numpy.fliplr`` and ``numpy.flipud``.

    Parameters
    ----------
    filename_input : str
        The input filename.
    filename_output : str
        The output filename.
    logger : logging.Logger
        The logger.

    Returns
    -------
    CsstStatus
        The final status.

    Examples
    --------
    >>> process_single_image(
    >>>     filename_input="input_image.fits",
    >>>     filename_output="output_image.fits",
    >>>     logger=None
    >>> )
    """
    # set default logger
    if logger is None:
        logger = get_logger()

    # process data
    try:
        # this will NOT be written into the log file
        logger.debug("Reading the image {}".format(filename_input))
        # start processing
        data = read_image(filename_input)
        data_processed = process_data(data)
        np.save(filename_output, data_processed)
        # this will be written into the log file
        logger.info("Processed image saved to {}".format(filename_output))
        return CsstStatus.PERFECT
    except DeprecationWarning:
        # this will be written into the log file
        logger.warning("Suffered DeprecationWarning!")
        return CsstStatus.WARNING
    except IOError:
        # this will be written into the log file
        logger.error("Suffered IOError!")
        return CsstStatus.ERROR


# process an exposure (MBI or SLS)
def process_multiple_images(
        dm: CsstMsDataManager,
        logger: Union[None, logging.Logger] = None
) -> CsstStatus:
    """
    Flip all images.

    Flip all images in an exposure in a for-loop (not in parallel).

    Parameters
    ----------
    dm : CsstMsDataManager
        The data manager of the specified exposure.
    logger : {None, logging.Logger}
        The logger. If None, use the default logger.

    Returns
    -------
    CsstStatus
        The final status.

    Examples
    --------
    >>> dm = CsstMsDataManager.quickstart(
    >>>     ver_sim="C5.2", dir_l1=".", datatype="sls", exposure_id=100)
    >>> logger = get_logger()
    >>> process_multiple_images(dm, logger)
    """

    # set default logger
    if logger is None:
        logger = get_logger()

    # process data
    try:
        # dm.target_detectors is a list of detector number that should be processed
        # start processing
        for detector in dm.target_detectors:
            # this will NOT be written into the log file
            logger.debug("Processing for detector {}".format(detector))
            data = read_image(dm.l0_detector(detector=detector))
            data_processed = process_data(data)
            np.save(
                dm.l1_detector(detector=detector, post="L1_processed.fits"),
                data_processed
            )
        # check results
        if check_results(dm=dm, logger=logger):
            # this will be written into the log file
            logger.info("Processed all images")
            return CsstStatus.PERFECT
        else:
            # not all images are properly processed
            return CsstStatus.WARNING
    except DeprecationWarning:
        # this will be written into the log file
        logger.warning("Suffered DeprecationWarning!")
        return CsstStatus.WARNING
    except IOError:
        # this will be written into the log file
        logger.error("Suffered IOError!")
        return CsstStatus.ERROR