ch08_csst_common.rst 13 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
Using ``csst_common``
=====================

BO ZHANG's avatar
tweaks    
BO ZHANG committed
4
5
6
7
In this chapter, we introduce the basic usages of some IMPORTANT functions / classes in ``csst_common``.
Package source code and installation guide can be found in the link below.

- https://csst-tb.bao.ac.cn/code/csst-l1/csst_common
BO ZHANG's avatar
BO ZHANG committed
8

BO ZHANG's avatar
tweaks    
BO ZHANG committed
9
10
``csst_common.data_manager.CsstMsDataManager``
----------------------------------------------
BO ZHANG's avatar
BO ZHANG committed
11

BO ZHANG's avatar
BO ZHANG committed
12
13
14
This class provides an interface to switch between local files and DFS.

To initilize from local directory:
BO ZHANG's avatar
BO ZHANG committed
15
16
17

.. code-block:: python

BO ZHANG's avatar
tweaks    
BO ZHANG committed
18
    from csst_common.data_manager import CsstMsDataManager
BO ZHANG's avatar
BO ZHANG committed
19
    # for full basic initialization
BO ZHANG's avatar
BO ZHANG committed
20
    dm = CsstMsDataManager.from_dir(
BO ZHANG's avatar
BO ZHANG committed
21
        ver_sim="C5.2",                  # version of simulation, "C5.2" is the latest
BO ZHANG's avatar
BO ZHANG committed
22
23
24
        datatype="mbi",                  # data type, "mbi", "sls" or "all"
        dir_l0=".",                      # the L0/input directory
        dir_l1=".",                      # the L1/output directory
BO ZHANG's avatar
BO ZHANG committed
25
        path_aux="",                     # aux file paths (master bias, dark, flat)
BO ZHANG's avatar
BO ZHANG committed
26
27
        dfs_mode=False,                  # if True, use DFS in pipeline modules
        dfs_node='kmust',                # DFS server, chosen from {"kmust", "pml"}
BO ZHANG's avatar
BO ZHANG committed
28
    )
BO ZHANG's avatar
BO ZHANG committed
29
    dm.target_detectors = None           # use all available detectors
BO ZHANG's avatar
tweaks    
BO ZHANG committed
30

BO ZHANG's avatar
BO ZHANG committed
31
32

In particular, if you are on ``dandelion`` or ``PML node``, you can use a ``quickstart`` method.
BO ZHANG's avatar
BO ZHANG committed
33

BO ZHANG's avatar
tweaks    
BO ZHANG committed
34
35
36
.. code-block:: python

    # for a quick start ()
BO ZHANG's avatar
BO ZHANG committed
37
38
39
40
41
42
43
    dm = CsstMsDataManager.quickstart(
        ver_sim='C5.2',                  # version of simulation, only "C5.2" is supported
        datatype='mbi',                  # data type, "mbi", "sls" or "all"
        dir_l1='.',                      # the L1/output directory
        exposure_id=100,                 # the exposure ID
        dfs_mode=False,                  # if True, use DFS in pipeline modules
        dfs_node='kmust',                # DFS server, chosen from {"kmust", "pml"}
BO ZHANG's avatar
BO ZHANG committed
44
    )
BO ZHANG's avatar
BO ZHANG committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    dm

You will get the outputs:

.. code-block::

    <CsstMsDataManager>
    Data type = mbi
    Valid detectors = [6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25]
    Available detectors = [6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25]
    Target detectors = [6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25]
    dir_l0 = /nfsdata/share/csst_simulation_data/Cycle-5-SimuData/multipleBandsImaging/NGP_AstrometryON_shearOFF/MSC_0000100/
    dir_l1 = .
    dfs_mode = False
    dfs_node = kmust

BO ZHANG's avatar
BO ZHANG committed
61
62
63

.. note::
    This ``quickstart`` method is ONLY available on
BO ZHANG's avatar
BO ZHANG committed
64
    ``dandelion`` or the ``PML node``.
BO ZHANG's avatar
BO ZHANG committed
65
    In your local development environment, you have to use
BO ZHANG's avatar
BO ZHANG committed
66
    ``CsstMsDataManager.from_dir(...)`` to initialize it.
BO ZHANG's avatar
BO ZHANG committed
67

BO ZHANG's avatar
BO ZHANG committed
68
69
70
Attributes
^^^^^^^^^^

BO ZHANG's avatar
BO ZHANG committed
71
Some attributes can be accessed as below.
BO ZHANG's avatar
tweaks    
BO ZHANG committed
72
73
74

.. code-block:: python

BO ZHANG's avatar
BO ZHANG committed
75
    # access L0 directory
BO ZHANG's avatar
BO ZHANG committed
76
    dm.dir_l0
BO ZHANG's avatar
BO ZHANG committed
77
    # access L1 directory
BO ZHANG's avatar
BO ZHANG committed
78
    dm.dir_l1
BO ZHANG's avatar
BO ZHANG committed
79
    # access dir_pcref
BO ZHANG's avatar
BO ZHANG committed
80
    dm.dir_pcref
BO ZHANG's avatar
BO ZHANG committed
81
    # access path_aux
BO ZHANG's avatar
BO ZHANG committed
82
    dm.path_aux
BO ZHANG's avatar
BO ZHANG committed
83
    # access ver_sim
BO ZHANG's avatar
BO ZHANG committed
84
    dm.ver_sim
BO ZHANG's avatar
BO ZHANG committed
85
    # access target detectors
BO ZHANG's avatar
BO ZHANG committed
86
    dm.target_detectors
BO ZHANG's avatar
BO ZHANG committed
87
    # access available detectors
BO ZHANG's avatar
BO ZHANG committed
88
    dm.available_detectors
BO ZHANG's avatar
BO ZHANG committed
89
    # define an L1 file (detector-specified)
BO ZHANG's avatar
BO ZHANG committed
90
    dm.l1_detector(detector=6)
BO ZHANG's avatar
BO ZHANG committed
91
    # define an L1 file (non-detector-specified)
BO ZHANG's avatar
BO ZHANG committed
92
    dm.l1_file("flipped_image.fits")
BO ZHANG's avatar
BO ZHANG committed
93

BO ZHANG's avatar
BO ZHANG committed
94
95
To get master files:

BO ZHANG's avatar
tweaks    
BO ZHANG committed
96
97
.. code-block:: python

BO ZHANG's avatar
BO ZHANG committed
98
    # to get bias
BO ZHANG's avatar
BO ZHANG committed
99
    dm.get_bias()
BO ZHANG's avatar
BO ZHANG committed
100
    # to get dark
BO ZHANG's avatar
BO ZHANG committed
101
    dm.get_dark()
BO ZHANG's avatar
BO ZHANG committed
102
    # to get flat
BO ZHANG's avatar
BO ZHANG committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    dm.get_flat()

Query Reference Catalog
^^^^^^^^^^^^^^^^^^^^^^^

``CsstMsDataManager`` provides a method ``query_rc`` to query reference catalog from ``DFS``.

.. code-block:: python

    cat = dm.query_rc(ra=180, dec=0, radius=2, min_mag=0, max_mag=30)
    if cat is None:
        print("The query failed!")
    else:
        print("The query succeeded!")
BO ZHANG's avatar
BO ZHANG committed
117

BO ZHANG's avatar
BO ZHANG committed
118

BO ZHANG's avatar
BO ZHANG committed
119
120
121
122
123
124
125
126
127
128
129
130
131
Access code to data products
----------------------------

``Multi-Band Imaging``
^^^^^^^^^^^^^^^^^^^^^^

========================== ===== ========================================================== ==================
Data source                Level File name / access code                                     Notes
========================== ===== ========================================================== ==================
raw                        0     ``dm.l0_detector(detector=detector)``                      sci image
raw                        0     ``dm.l0_crs(detector=detector)``                           cosmic ray image
raw                        0     ``dm.l0_cat(detector=detector)``                           input catalog
raw                        0     ``dm.l0_log(detector=detector)``                           log file
BO ZHANG's avatar
BO ZHANG committed
132
133
134
135
``csst_ms_mbi_instrument`` --    ``dm.l1_detector(detector=detector, post="img.fits")``     corrected image
``csst_ms_mbi_instrument`` --    ``dm.l1_detector(detector=detector, post="flg.fits")``     flag image
``csst_ms_mbi_instrument`` --    ``dm.l1_detector(detector=detector, post="wht.fits")``     weight image
``csst_ms_mbi_instrument`` --    ``dm.l1_detector(detector=detector, post="img.head")``     L0 fits header
BO ZHANG's avatar
BO ZHANG committed
136
137
``DFS``                    --    ``dm.l1_file(name="gaia_dfs.fits")``                       reference catalog
``csst_ms_mbi_distortion`` --    ``dm.l1_detector(detector=detector, post="wcs.head")``     fits head with WCS
BO ZHANG's avatar
BO ZHANG committed
138
139
``csst_ms_mbi_position``   --    ``dm.l1_detector(detector=detector, post="img.acat")``     photometry catalog
``csst_ms_mbi_position``   --    ``dm.l1_detector(detector=detector, post="img.rcat")``     reference catalog
140
141
142
143
144
``csst_ms_mbi_flux``       1     ``dm.l1_detector(detector=detector, post="img_L1.fits")``  sci image
``csst_ms_mbi_flux``       1     ``dm.l1_detector(detector=detector, post="flg_L1.fits")``  flag image
``csst_ms_mbi_flux``       1     ``dm.l1_detector(detector=detector, post="wht_L1.fits")``  weight image
``csst_ms_mbi_photometry`` 1     ``dm.l1_detector(detector=detector, post="cat.fits")``     source catalog
``csst_ms_mbi_photometry`` 1     ``dm.l1_detector(detector=detector, post="psf.fits")``     source catalog
BO ZHANG's avatar
BO ZHANG committed
145
146
147
========================== ===== ========================================================== ==================


148
149
150
``Slit-Less Spectra 2D``
^^^^^^^^^^^^^^^^^^^^^^^^
========================== ===== =========================================================== ==================
BO ZHANG's avatar
BO ZHANG committed
151
Data source                Level File name / access code                                     Notes
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
178
========================== ===== =========================================================== ==================
raw                        0     ``dm.l0_detector(detector=detector)``                       sci image
raw                        0     ``dm.l0_crs(detector=detector)``                            cosmic ray image
raw                        0     ``dm.l0_cat(detector=detector)``                            input catalog
raw                        0     ``dm.l0_log(detector=detector)``                            log file
``csst_ms_sls_instrument`` 1     ``dm.l1_detector(detector=detector, post="L1_1_flt.fits")`` corrected image
``csst_ms_sls_position``   --    ``dm.l1_detector(detector=detector, post="L1_1.fits")``     WCS determined
========================== ===== =========================================================== ==================


``Slit-Less Spectra 1D``
^^^^^^^^^^^^^^^^^^^^^^^^
=========================== ===== =============================================================== ==================
Data source                 Level File name / access code                                         Notes
=========================== ===== =============================================================== ==================
``csst_ms_sls_position``    --    ``dm.l1_detector(detector=detector, post="L1_1.fits")``         L1 SLS image
``csst_common``             --    ``dm.target_detectors[0]``                                      detector number
``csst_ms_sls_mosaic``      --    ``dm.l1_detector(detector=detector, post="coadd.fits")``        coadded image
``csst_ms_sls_mosaic``      --    ``dm.l1_detector(detector=detector, post="coadd.weight.fits")`` coadded weight
``csst_ms_sls_directimage`` --    ``dm.l1_detector(detector=detector, post="dir_img.fits")``      cropped image
``csst_ms_sls_directimage`` --    ``dm.l1_detector(detector=detector, post="dir_wht.fits")``      cropped weight
``csst_ms_sls_objextract``  --    ``dm.l1_detector(detector=detector, post="comb.cat")``          combined catalog
``csst_ms_sls_objextract``  --    ``dm.l1_detector(detector=detector, post="segmentation.fits")`` segmentation file
``csst_ms_sls_sky``         --    ``dm.l1_detector(detector=detector, post="L1_1_sky.fits")``     sky background
``csst_ms_sls_axe``         --    ``dm.l1_detector(detector=detector, post="L1_1_axe.fits")``     aXe spectra
``csst_ms_sls_cde``         --    ``dm.l1_detector(detector=detector, post="L1_1_cde.fits")``     CDE spectra
=========================== ===== =============================================================== ==================
BO ZHANG's avatar
BO ZHANG committed
179

BO ZHANG's avatar
BO ZHANG committed
180

BO ZHANG's avatar
BO ZHANG committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
``csst_common.file_recorder.FileRecorder``
------------------------------------------

Get an empty ``FileRecorder``.
This is initially a ``list``-like object.
Use ``FileRecorder.add_record()`` to add file records.

.. code-block::
    :linenos:

    >>> from csst_common.file_recorder import FileRecorder
    >>> fr = FileRecorder()
    >>> for i in range(3):
    >>>     fr.add_record(filepath="test{:03d}.txt".format(i),
    >>>                   db=True,
    >>>                   comment="Test file {:d}".format(i))
    >>> fr.pprint_all()

    <FileRecorder length=3>
      filepath   db    comment   existence
    ----------- ---- ----------- ---------
    test000.txt True Test file 0     False
    test001.txt True Test file 1     False
    test002.txt True Test file 2     False

BO ZHANG's avatar
tweaks    
BO ZHANG committed
206
``FileRecorder`` inherits from ``list``.
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
Therefore, if you are collecting records from subprocesses,
here is the way to combine records:

.. code-block:: python
    :linenos:

    >>> from csst_common.file_recorder import FileRecorder
    >>> fr1 = FileRecorder()
    >>> fr1.add_record(filepath="test1.txt", db=False, comment="test file 1")
    >>> fr2 = FileRecorder()
    >>> fr2.add_record(filepath="test2.txt", db=False, comment="test file 2")
    >>> fr_all = FileRecorder([*fr1, *fr2])
    >>> fr_all.pprint_all()
    <FileRecorder length=2>
     filepath   db    comment   existence
    --------- ----- ----------- ---------
    test1.txt False test file 1     False
    test2.txt False test file 2     False

BO ZHANG's avatar
tweaks    
BO ZHANG committed
226
227
228
229
230
231
232
233
234
    >>> fr_all.extend(fr1)
    >>> fr_all.pprint_all()
    <FileRecorder length=3>
     filepath   db    comment   existence
    --------- ----- ----------- ---------
    test1.txt False test file 1     False
    test2.txt False test file 2     False
    test1.txt False test file 1     False

BO ZHANG's avatar
BO ZHANG committed
235

BO ZHANG's avatar
BO ZHANG committed
236
237
238
239
240
241
242
``csst_common.logger.get_logger()``
-----------------------------------

Get the default configured ``logging.Logger``.


.. code-block:: python
BO ZHANG's avatar
BO ZHANG committed
243
    :linenos:
BO ZHANG's avatar
BO ZHANG committed
244
245
246
247

    from csst_common.logger import get_logger
    logger = get_logger()

BO ZHANG's avatar
BO ZHANG committed
248
.. warning::
BO ZHANG's avatar
BO ZHANG committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    Developers should NOT use ``print`` function extensively in code.
    Generally ``print`` can be replaced with ``logger.debug("msg")``.
    For important information, use ``logger.info("msg")``.
    For warnings, use ``logger.warning("msg")``.
    For errors, use ``logger.error("msg")``.
    Conf https://docs.python.org/3/library/logging.html for more information
    about the usage of ``logging.Logger``.


``csst_common.status.CsstStatus``
---------------------------------

Developers should use ``csst_common.status.CsstStatus`` to return the
status of their interfaces.

.. code-block:: python
BO ZHANG's avatar
BO ZHANG committed
265
    :linenos:
BO ZHANG's avatar
BO ZHANG committed
266
267
268
269
270
271
272
273
274
275
276

    from csst_common.status import CsstStatus
    # presently 3 kinds of status are available, they are
    CsstStatus.PERFECT
    CsstStatus.WARNING
    CsstStatus.ERROR


An example interface
--------------------

BO ZHANG's avatar
tweaks    
BO ZHANG committed
277
278
279
We recommend our developers to use the code structure used in the example interfaces,
e.g., ``process_single_image`` and ``process_multiple_images``.
The example code is shown below.
BO ZHANG's avatar
BO ZHANG committed
280

BO ZHANG's avatar
BO ZHANG committed
281
282
283
Source code
^^^^^^^^^^^

BO ZHANG's avatar
BO ZHANG committed
284
.. literalinclude:: csst_common/example_interface.py
BO ZHANG's avatar
BO ZHANG committed
285
    :caption: ``example_interface.py``
286
    :emphasize-lines: 7-11,36-41,85,87-88,91-92,95-96,99-101,111-116,148,155-165,167-173,178-179,182-183,186-187,190-191
BO ZHANG's avatar
BO ZHANG committed
287
288
    :linenos:
    :language: python
BO ZHANG's avatar
BO ZHANG committed
289
290
291
292
293
294
295


Rendered ``docstring``
^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: example_interface
    :members:
BO ZHANG's avatar
tweaks    
BO ZHANG committed
296
297
298
299
300
301
302
303
304
305


``csst_common.params``
----------------------

to be updated

Module Identifier
-----------------

BO ZHANG's avatar
BO ZHANG committed
306
to be updated
307
308
309
310
311
312
313
314
315
316
317
318

DFS interfaces
--------------

==================================== ====================== ==========================================
Method                               Function               Original DFS interface
==================================== ====================== ==========================================
``CsstMsDataManager.dfs_query_rc()`` query catalog from DFS ``csst_dfs_api.common.catalog.CatalogApi``
``CsstMsDataManager.dfs_query_l0()`` query L0 data from DFS --
``CsstMsDataManager.dfs_query_l1()`` query L1 data from DFS --
``CsstMsDataManager.dfs_push_l1()``  push L1 data to DFS    ``csst_dfs_api.msc.level1.Level1DataApi``
==================================== ====================== ==========================================