observation.py 5.05 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
from .dict import DotDict
from .instrument import csst
3
from astropy.table import Table, vstack
BO ZHANG's avatar
BO ZHANG committed
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
import numpy as np

# from csst_dag.dict import DotDict
# from csst_dag.instrument import csst


class CsstPlanObsid(DotDict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @property
    def detectors(self):
        if self.instrument in ("MSC", "MCI", "IFS", "CPIC"):
            return csst[self.instrument].effective_detectors
        elif self.instrument == "HSTDM":
            if self.params.detector == "SIS12":
                return csst[self.instrument].effective_detectors
            else:
                return [csst[self.instrument][self.params.detector]]
        else:
            raise ValueError(f"Unknown instrument: {self.instrument}")

    @property
    def n_detector(self):
        return len(self.detectors)

    @property
    def n_file_expected(self):
        if self.instrument in ("MSC", "MCI", "IFS", "CPIC"):
            return self.n_detector
        elif self.instrument == "HSTDM":
            return self.n_detector * self.params.num_epec_frame
        else:
            raise ValueError(f"Unknown instrument: {self.instrument}")

    @staticmethod
    def from_plan(plan_data: dict) -> "CsstPlanObsid":
        return CsstPlanObsid(**plan_data)

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
    @property
    def expected_data_table(self):
        contents = []
        for detector in self.detectors:
            n_frame = self.params.num_epec_frame if self.instrument == "HSTDM" else 1
            for _ in range(n_frame):
                contents.append(
                    dict(
                        dataset=self.dataset,
                        instrument=self.instrument,
                        obs_type=self.obs_type,
                        obs_group=self.obs_group,
                        obs_id=self.obs_id,
                        detector=detector.name,
                    )
                )
        # post processing
        t = Table(contents)
        t.sort(
            keys=[
                "dataset",
                "instrument",
                "obs_type",
                "obs_group",
                "obs_id",
                "detector",
            ]
        )
        return t

BO ZHANG's avatar
BO ZHANG committed
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

class CsstPlanObsgroup(DotDict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # construct plan table
        self._plan_table = Table(list(self.values()))

        # assert unique obs_group
        u_keys = np.unique(
            self._plan_table["dataset", "instrument", "obs_type", "obs_group"]
        )
        assert len(u_keys) == 1, "Multiple `instruments/obs_types/datasets` found."

        # no duplicated obs_id's
        u_obsid = np.unique(self._plan_table["obs_id"])
        assert len(u_obsid) == len(
            self
        ), f"n_obsid {len(u_obsid)} != n_plan {len(self)}"

        # assign parameters
        self._dataset = str(u_keys[0]["dataset"])
        self._instrument = str(u_keys[0]["instrument"])
        self._obs_type = str(u_keys[0]["obs_type"])
        self._obs_group = str(u_keys[0]["obs_group"])

    @property
    def dataset(self):
        return self._dataset

    @property
    def instrument(self):
        return self._instrument

    @property
    def obs_type(self):
        return self._obs_type

    @property
    def obs_group(self):
        return self._obs_group

    @property
    def obs_id_list(self):
        return list(self.keys())

    @property
    def n_file_expected(self):
        return sum([self[_].n_file_expected for _ in self.obs_id_list])

    def __repr__(self):
        return (
            f"<CsstPlanObsgroup '{self.obs_group}' (n_obs_id={len(self)}): "
            f"instrument='{self.instrument}' "
            f"dataset='{self.dataset}' "
            f"obs_type='{self.obs_type}'>"
        )

    @staticmethod
    def from_plan(plan_data: list[dict]) -> "CsstPlanObsgroup":
        return CsstPlanObsgroup(
            **{_["obs_id"]: CsstPlanObsid.from_plan(_) for _ in plan_data}
        )
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

    @property
    def expected_data_table(self):
        t = vstack([self[k].expected_data_table for k in self])
        t.sort(
            keys=[
                "dataset",
                "instrument",
                "obs_type",
                "obs_group",
                "obs_id",
                "detector",
            ]
        )
        return t


class CsstLevel0(DotDict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @property
    def n_file_expected(self):
        return sum([self[_].n_file_expected for _ in self.obs_id_list])

    @staticmethod
    def extract_data_table(level0_data: list[dict]) -> Table:
        d_list = [
            dict(
                dataset=d["dataset"],
                instrument=d["instrument"],
                obs_type=d["obs_type"],
                obs_group=d["obs_group"],
                obs_id=d["obs_id"],
                detector=d["detector"],
            )
            for d in level0_data
        ]

        return CsstPlanObsgroup(
            **{_["obs_id"]: CsstPlanObsid.from_plan(_) for _ in plan_data}
        )