data_manager.py 10.1 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
import glob
import re
BO ZHANG's avatar
BO ZHANG committed
3

BO ZHANG's avatar
BO ZHANG committed
4
from astropy.io import fits
BO ZHANG's avatar
BO ZHANG committed
5

BO ZHANG's avatar
BO ZHANG committed
6
from .backbone import VER_SIMS, CCD_FILTER_MAPPING
BO ZHANG's avatar
BO ZHANG committed
7
8


BO ZHANG's avatar
BO ZHANG committed
9
class CsstMscDataManager:
BO ZHANG's avatar
BO ZHANG committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    """ this class defines the file format of the input & output of CSST MSC L1 pipeline

    C3:
        MSC_MS_210525220000_100000020_06_raw.fits
        MSC_CRS_210525220000_100000020_06_raw.fits
        MSC_210525120000_0000020_06.cat

    C5.1:
        CSST_MSC_MS_SCI_20270810081950_20270810082220_100000100_06_L0_1.fits
        CSST_MSC_MS_CRS_20270810081950_20270810082220_100000100_06_L0_1.fits
        MSC_10000100_chip_06_filt_y.cat
        MSC_10000100_chip_06_filt_y.log

    """

BO ZHANG's avatar
BO ZHANG committed
25
    def __init__(self, ver_sim="C5.1", dir_l0="", dir_l1="", dir_pcref="", path_aux="", force_all_ccds=False):
BO ZHANG's avatar
BO ZHANG committed
26
27
28
29
        assert ver_sim in VER_SIMS

        self.dir_l0 = dir_l0
        self.dir_l1 = dir_l1
BO ZHANG's avatar
BO ZHANG committed
30
        self.dir_pcref = dir_pcref
BO ZHANG's avatar
BO ZHANG committed
31
        self.path_aux = path_aux
BO ZHANG's avatar
BO ZHANG committed
32
33
        self.ver_sim = ver_sim

BO ZHANG's avatar
BO ZHANG committed
34
35
36
        fps_img = self.glob_image(dir_l0, ver_sim=ver_sim)
        fps_cat = self.glob_cat(dir_l0, ver_sim=ver_sim)

BO ZHANG's avatar
BO ZHANG committed
37
        if force_all_ccds:
BO ZHANG's avatar
BO ZHANG committed
38
            assert len(fps_img) == len(CCD_ID_LIST)
BO ZHANG's avatar
BO ZHANG committed
39
        else:
BO ZHANG's avatar
BO ZHANG committed
40
            assert len(fps_img) > 0
BO ZHANG's avatar
BO ZHANG committed
41
42
43
44
45
46

        if ver_sim == "C3":
            # get info
            # print(re.split(r"[_.]", fps[0]))
            self._instrument, self._survey, \
                self._exp_start, self._exp_id, \
BO ZHANG's avatar
BO ZHANG committed
47
                _ccd_id, self._l0_suffix, _ext = re.split(r"[_.]", fps_img[0])
BO ZHANG's avatar
tweaks    
BO ZHANG committed
48
            self._cat_id = re.split(r"[_.]", fps_cat[0])[1]
BO ZHANG's avatar
BO ZHANG committed
49
50
51
52
53

            self._exp_start = int(self._exp_start)
            self._exp_id = int(self._exp_id)

            # available CCD IDs
BO ZHANG's avatar
BO ZHANG committed
54
            self.available_ccd_ids = [int(re.split(r"[_.]", fp)[4]) for fp in fps_img]
BO ZHANG's avatar
BO ZHANG committed
55
56
57
58
            self.available_ccd_ids.sort()

        elif ver_sim == "C5.1":
            # get info
BO ZHANG's avatar
BO ZHANG committed
59
            # print(re.split(r"[_.]", fps[0]))
BO ZHANG's avatar
BO ZHANG committed
60
61
            self._telescope, self._instrument, self._survey, self._imagetype, \
                self._exp_start, self._exp_stop, self._exp_id, \
BO ZHANG's avatar
BO ZHANG committed
62
                _ccd_id, self._l0_suffix, self._version, _ext = re.split(r"[_.]", fps_img[0])
BO ZHANG's avatar
tweaks    
BO ZHANG committed
63
            self._cat_id = re.split(r"[_.]", fps_cat[0])[1]
BO ZHANG's avatar
BO ZHANG committed
64

BO ZHANG's avatar
BO ZHANG committed
65
66
67
            self._exp_start = int(self._exp_start)
            self._exp_stop = int(self._exp_stop)
            self._exp_id = int(self._exp_id)
BO ZHANG's avatar
BO ZHANG committed
68

BO ZHANG's avatar
BO ZHANG committed
69
            # available CCD IDs
BO ZHANG's avatar
BO ZHANG committed
70
            self.available_ccd_ids = [int(re.split(r"[_.]", fp)[7]) for fp in fps_img]
BO ZHANG's avatar
BO ZHANG committed
71
72
73
            self.available_ccd_ids.sort()

    @staticmethod
BO ZHANG's avatar
BO ZHANG committed
74
    def glob_image(dir_l0, ver_sim="C5"):
BO ZHANG's avatar
BO ZHANG committed
75
        """ glob files in L0 data directory """
BO ZHANG's avatar
BO ZHANG committed
76
        if ver_sim == "C3":
BO ZHANG's avatar
BO ZHANG committed
77
            pattern = os.path.join(dir_l0, "MSC_MS_*_raw.fits")
BO ZHANG's avatar
BO ZHANG committed
78
        elif ver_sim == "C5.1":
BO ZHANG's avatar
BO ZHANG committed
79
            pattern = os.path.join(dir_l0, "CSST_MSC_MS_SCI_*.fits")
BO ZHANG's avatar
BO ZHANG committed
80
81
82
        fps = glob.glob(pattern)
        fps = [os.path.basename(fp) for fp in fps]
        fps.sort()
BO ZHANG's avatar
tweaks    
BO ZHANG committed
83

BO ZHANG's avatar
BO ZHANG committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        print("@DM.glob_dir: {} files found with pattern: {}".format(len(fps), pattern))
        return fps

    @staticmethod
    def glob_cat(dir_l0, ver_sim="C5"):
        """ glob input catalogs in L0 data directory """
        if ver_sim == "C3":
            pattern = os.path.join(dir_l0, "MSC_*.cat")
        elif ver_sim == "C5.1":
            pattern = os.path.join(dir_l0, "MSC_*.cat")
        fps = glob.glob(pattern)
        fps = [os.path.basename(fp) for fp in fps]
        fps.sort()

        print("@DM.glob_dir: {} files found with pattern: {}".format(len(fps), pattern))
BO ZHANG's avatar
BO ZHANG committed
99
100
101
        return fps

    def l0_cat(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
102
        """ the L0 cat file path"""
BO ZHANG's avatar
BO ZHANG committed
103
        if self.ver_sim == "C3":
BO ZHANG's avatar
BO ZHANG committed
104
            fn = "{}_{}_{:07d}_{:02d}.cat".format(
BO ZHANG's avatar
BO ZHANG committed
105
                self._instrument, self._cat_id, self._exp_id - 100000000, ccd_id)
BO ZHANG's avatar
BO ZHANG committed
106
        elif self.ver_sim == "C5.1":
BO ZHANG's avatar
BO ZHANG committed
107
            fn = "{}_{}_chip_{:02d}_filt_{}.cat".format(
BO ZHANG's avatar
BO ZHANG committed
108
                self._instrument, self._exp_id - 90000000, ccd_id, CCD_FILTER_MAPPING[ccd_id])
BO ZHANG's avatar
BO ZHANG committed
109
110
111
        return os.path.join(self.dir_l0, fn)

    def l0_log(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
112
        """ L0 log file path """
BO ZHANG's avatar
BO ZHANG committed
113
        if self.ver_sim == "C5.1":
BO ZHANG's avatar
BO ZHANG committed
114
115
            fn = "{}_{}_chip_{:02d}_filt_{}.log".format(
                self._instrument, self._exp_id - 90000000, ccd_id, CCD_FILTER_MAPPING[ccd_id])
BO ZHANG's avatar
BO ZHANG committed
116
117
118
            return os.path.join(self.dir_l0, fn)

    def l0_sci(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
119
        """ L0 image file path """
BO ZHANG's avatar
BO ZHANG committed
120
121
122
123
124
125
126
127
128
129
        if self.ver_sim == "C3":
            fn = "{}_{}_{}_{}_{:02d}_raw.fits".format(
                self._instrument, self._survey, self._exp_start, self._exp_id, ccd_id)
        elif self.ver_sim == "C5.1":
            fn = "{}_{}_{}_SCI_{}_{}_{}_{:02d}_L0_1.fits".format(
                self._telescope, self._instrument, self._survey,
                self._exp_start, self._exp_stop, self._exp_id, ccd_id)
        return os.path.join(self.dir_l0, fn)

    def l0_crs(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
130
        """ L0 cosmic ray file path """
BO ZHANG's avatar
BO ZHANG committed
131
132
133
134
135
136
137
138
139
        if self.ver_sim == "C3":
            fn = "{}_CRS_{}_{}_{:02d}_raw.fits".format(
                self._instrument, self._exp_start, self._exp_id, ccd_id)
        elif self.ver_sim == "C5.1":
            fn = "{}_{}_{}_CRS_{}_{}_{}_{:02d}_L0_1.fits".format(
                self._telescope, self._instrument, self._survey,
                self._exp_start, self._exp_stop, self._exp_id, ccd_id)
        return os.path.join(self.dir_l0, fn)

BO ZHANG's avatar
BO ZHANG committed
140
141
142
143
144
145
146
147
148
149
    def l1_sci(self, ccd_id=6, suffix="img_whead", ext="fits"):
        """ generate L1 file path

        Parameters
        ----------
        ccd_id:
            CCD ID
        suffix:
            {"img", "wht", "flg", "img_L1", "wht_L1", "flg_L1", "whead"}
        ext:
BO ZHANG's avatar
BO ZHANG committed
150
            {"fits", "cat", "rcat"}
BO ZHANG's avatar
BO ZHANG committed
151
152
153
154
155
156

        Returns
        -------
        L1 file path

        """
BO ZHANG's avatar
BO ZHANG committed
157
        if self.ver_sim == "C3":
BO ZHANG's avatar
BO ZHANG committed
158
            fn = "{}_{}_{}_{}_{:02d}_{}.{}".format(
BO ZHANG's avatar
BO ZHANG committed
159
160
                self._instrument, self._survey,
                self._exp_start, self._exp_id, ccd_id, suffix, ext)
BO ZHANG's avatar
BO ZHANG committed
161
        elif self.ver_sim == "C5.1":
BO ZHANG's avatar
BO ZHANG committed
162
            fn = "{}_{}_{}_SCI_{}_{}_{}_{:02d}_{}.{}".format(
BO ZHANG's avatar
BO ZHANG committed
163
                self._telescope, self._instrument, self._survey,
BO ZHANG's avatar
BO ZHANG committed
164
                self._exp_start, self._exp_stop, self._exp_id, ccd_id, suffix, ext)
BO ZHANG's avatar
BO ZHANG committed
165
166
        return os.path.join(self.dir_l1, fn)

niejuzi's avatar
modify    
niejuzi committed
167
    def pc_combined_file(self, suffix="img", ext="fits"):
BO ZHANG's avatar
BO ZHANG committed
168
169
170
171
        """ combined images

        Parameters
        ----------
BO ZHANG's avatar
BO ZHANG committed
172
        suffix:
niejuzi's avatar
modify    
niejuzi committed
173
            {"img", "wht", "flg", "cat", "head"}
BO ZHANG's avatar
BO ZHANG committed
174
        ext:
niejuzi's avatar
modify    
niejuzi committed
175
            {"fits", "head", "ahead" }
BO ZHANG's avatar
BO ZHANG committed
176
177
178
179
180

        Returns
        -------
        combined image path

BO ZHANG's avatar
BO ZHANG committed
181
        """
niejuzi's avatar
modify    
niejuzi committed
182
183
184
185
186
187
188
189
        if self.ver_sim == "C3":
            fn = "{}_{}_{}_{}_{}.{}".format(
                self._instrument, self._survey,
                self._exp_start, self._exp_id, suffix, ext)
        elif self.ver_sim == "C5.1":
            fn = "{}_{}_{}_SCI_{}_{}_{}_{}.{}".format(
                self._telescope, self._instrument, self._survey,
                self._exp_start, self._exp_stop, self._exp_id, suffix, ext)
BO ZHANG's avatar
BO ZHANG committed
190
        return os.path.join(self.dir_l1, fn)
BO ZHANG's avatar
BO ZHANG committed
191
192
193
194
195
196

    @property
    def pc_ref_cat(self):
        """ reference catalog """
        return os.path.join(self.dir_l1, "ref.cat")

niejuzi's avatar
modify    
niejuzi committed
197
198
199
200
201
    @property
    def pc_radecoff(self):
        """ plot coordinate diff between obs and ref """
        return os.path.join(self.dir_l1, "radec_off.png")

BO ZHANG's avatar
BO ZHANG committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    @property
    def pc_check_fits(self):
        """ check fits """
        return os.path.join(self.dir_l1, "check.fits")

    @property
    def pc_combined_head_fits(self):
        """ combined head (converted to) fits """
        return os.path.join(self.dir_l1, "combined_cat.head.fits")

    @property
    def pc_scamp_coord(self):
        """ SCAMP coord """
        return os.path.join(self.dir_l1, "scamp_coord.txt")
BO ZHANG's avatar
BO ZHANG committed
216

BO ZHANG's avatar
BO ZHANG committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    def get_ccd_ids(self, ccd_ids=None):
        """  """
        if ccd_ids is None:
            # default ccd_ids
            ccd_ids = self.available_ccd_ids
        else:
            try:
                # assert ccd_ids is a subset of available ccd_ids
                assert set(ccd_ids).issubset(set(self.available_ccd_ids))
            except AssertionError as ae:
                print("@DM: available CCD IDs are ", self.available_ccd_ids)
                print("@DM: target CCD IDs are ", ccd_ids)
                raise ae
        return ccd_ids

    def get_bias(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
233
234
        fp = glob.glob(self.path_aux.format("CLB", ccd_id))[0]
        return fits.getdata(fp)
BO ZHANG's avatar
BO ZHANG committed
235
236

    def get_dark(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
237
238
        fp = glob.glob(self.path_aux.format("CLD", ccd_id))[0]
        return fits.getdata(fp)
BO ZHANG's avatar
BO ZHANG committed
239
240

    def get_flat(self, ccd_id=6):
BO ZHANG's avatar
BO ZHANG committed
241
242
        fp = glob.glob(self.path_aux.format("CLF", ccd_id))[0]
        return fits.getdata(fp)
BO ZHANG's avatar
BO ZHANG committed
243

BO ZHANG's avatar
BO ZHANG committed
244
245

if __name__ == "__main__":
BO ZHANG's avatar
BO ZHANG committed
246
247
248
    # test C3
    import os
    from csst.msc.data_manager import CsstMscDataManager
BO ZHANG's avatar
BO ZHANG committed
249

BO ZHANG's avatar
BO ZHANG committed
250
    dm = CsstMscDataManager(
BO ZHANG's avatar
BO ZHANG committed
251
        ver_sim="C3", dir_l0="/data/L1Pipeline/msc/MSC_0000020", dir_l1="/data/L1Pipeline/msc/work")
BO ZHANG's avatar
BO ZHANG committed
252
253
254
255
256
257
258
259
260
261
262
263
264
    print("----- L0 images -----")
    print(dm.l0_sci(ccd_id=6))
    print(os.path.exists(dm.l0_sci(ccd_id=6)))
    print("----- L0 crs -----")
    print(dm.l0_crs(ccd_id=6))
    print(os.path.exists(dm.l0_sci(ccd_id=8)))
    print("----- L0 input cat -----")
    print(dm.l0_cat(8))
    print(os.path.exists(dm.l0_cat(ccd_id=8)))
    print("----- available ccd_ids -----")
    print(dm.available_ccd_ids)
    print("----- L1 images -----")
    print(dm.l1_sci(25, "img", "fits"))
BO ZHANG's avatar
BO ZHANG committed
265
266
267
268
269

    # test C5.1
    import os
    from csst.msc.data_manager import CsstMscDataManager
    from csst.msc.backbone import CCD_ID_LIST
BO ZHANG's avatar
BO ZHANG committed
270

BO ZHANG's avatar
BO ZHANG committed
271
272
    dm = CsstMscDataManager(
        ver_sim="C5.1", dir_l0="/data/sim_data/MSC_0000100", dir_l1="/home/user/L1Pipeline/msc/work")
BO ZHANG's avatar
BO ZHANG committed
273
    print("----- available ccd_ids -----")
BO ZHANG's avatar
BO ZHANG committed
274
    print(dm.available_ccd_ids)
BO ZHANG's avatar
BO ZHANG committed
275
276
    for ccd_id in dm.available_ccd_ids[:2]:
        print("----- L0 images -----")
BO ZHANG's avatar
BO ZHANG committed
277
278
        print(dm.l0_sci(ccd_id=ccd_id))
        print(os.path.exists(dm.l0_sci(ccd_id=ccd_id)))
BO ZHANG's avatar
BO ZHANG committed
279
        print("----- L0 crs -----")
BO ZHANG's avatar
BO ZHANG committed
280
281
        print(dm.l0_crs(ccd_id=ccd_id))
        print(os.path.exists(dm.l0_sci(ccd_id=ccd_id)))
BO ZHANG's avatar
BO ZHANG committed
282
        print("----- L0 input cat -----")
BO ZHANG's avatar
BO ZHANG committed
283
284
        print(dm.l0_cat(ccd_id=ccd_id))
        print(os.path.exists(dm.l0_cat(ccd_id=ccd_id)))
BO ZHANG's avatar
BO ZHANG committed
285
        print("----- L0 input log -----")
BO ZHANG's avatar
BO ZHANG committed
286
287
        print(dm.l0_log(ccd_id=ccd_id))
        print(os.path.exists(dm.l0_log(ccd_id=ccd_id)))
BO ZHANG's avatar
BO ZHANG committed
288
289
        print("----- L1 images -----")
        print(dm.l1_sci(ccd_id, "img", "fits"))