Catalog_example.py 8.29 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong 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
import os
import numpy as np
import astropy.constants as cons
from astropy.table import Table
from scipy import interpolate

from observation_sim.mock_objects import CatalogBase, Star, Galaxy, Quasar


class Catalog(CatalogBase):
    """An user customizable class for reading in catalog(s) of objects and SEDs.

    NOTE: must inherit the "CatalogBase" abstract class

    ...

    Attributes
    ----------
    cat_dir : str
        a directory that contains the catalog file(s)
    star_path : str
        path to the star catalog file
    star_SED_path : str
        path to the star SED data
    objs : list
26
        a list of observation_sim.mock_objects (Star, Galaxy, or Quasar)
Fang Yuedong's avatar
Fang Yuedong committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        NOTE: must have "obj" list when implement your own Catalog

    Methods
    ----------
    load_sed(obj, **kwargs):
        load the corresponding SED data for one object
    load_norm_filt(obj):
        load the filter throughput for the input catalog's photometric system.
    """

    def __init__(self, config, chip, **kwargs):
        """Constructor method.

        Parameters
        ----------
        config : dict
            configuration dictionary which is parsed from the input YAML file
44
45
        chip: observation_sim.instruments.Chip
            an observation_sim.instruments.Chip instance, can be used to identify the band etc.
Fang Yuedong's avatar
Fang Yuedong committed
46
        **kwargs : dict
47
            any other needed input parameters (in key-value pairs), please modify corresponding
Fang Yuedong's avatar
Fang Yuedong committed
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
            initialization call in "ObservationSim.py" as you need.

        Returns
        ----------
        None
        """

        super().__init__()
        self.cat_dir = os.path.join(
            config["data_dir"], config["catalog_options"]["input_path"]["cat_dir"])
        self.chip = chip
        if "star_cat" in config["catalog_options"]["input_path"] and config["catalog_options"]["input_path"]["star_cat"]:
            star_file = config["catalog_options"]["input_path"]["star_cat"]
            star_SED_file = config["catalog_options"]["SED_templates_path"]["star_SED"]
            self.star_path = os.path.join(self.cat_dir, star_file)
            self.star_SED_path = os.path.join(
                config["data_dir"], star_SED_file)
        # NOTE: must call _load() method here to read in all objects
        self.objs = []
        self._load()

    def _load(self, **kwargs):
        """Read in all objects in from the catalog file(s).

        This is a must implemented method which is used to read in all objects, and
73
        then convert them to observation_sim.mock_objects (Star, Galaxy, or Quasar).
Fang Yuedong's avatar
Fang Yuedong committed
74

Wei Chengliang's avatar
Wei Chengliang committed
75
        Currently,
76
        the model of observation_sim.mock_objects.Star class requires:
Fang Yuedong's avatar
Fang Yuedong committed
77
78
79
80
81
82
83
84
85
86
            param["star"] : int
                specify the object type: 0: galaxy, 1: star, 2: quasar
            param["id"] : int
                ID number of the object
            param["ra"] : float
                Right ascension (in degrees)
            param["dec"] : float
                Declination (in degrees)
            param["mag_use_normal"]: float
                the absolute magnitude in a particular filter
Wei Chengliang's avatar
Wei Chengliang committed
87
                NOTE: if that filter is not the corresponding CSST filter, the
Fang Yuedong's avatar
Fang Yuedong committed
88
89
90
                load_norm_filt(obj) function must be implemented to load the filter
                throughput of that particular photometric system

91
        the model of observation_sim.mock_objects.Galaxy class requires:
Fang Yuedong's avatar
Fang Yuedong committed
92
93
94
95
96
97
98
99
100
101
            param["star"] : int
                specify the object type: 0: galaxy, 1: star, 2: quasar
            param["id"] : int
                ID number of the object
            param["ra"] : float
                Right ascension (in degrees)
            param["dec"] : float
                Declination (in degrees)
            param["mag_use_normal"]: float
                the absolute magnitude in a particular filter
Wei Chengliang's avatar
Wei Chengliang committed
102
                NOTE: if that filter is not the corresponding CSST filter, the
Fang Yuedong's avatar
Fang Yuedong committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                load_norm_filt(obj) function must be implemented to load the filter
                throughput of that particular photometric system
            param["bfrac"] : float
                the bulge fraction
            param["hlr_bulge"] : float
                the half-light-radius of the bulge
            param["hlr_disk"] : float
                the half-light-radius of the disk
            param["e1_bulge"], param["e2_bulge"] : float
                the ellipticity of the bulge components
            param["e1_disk"], param["e2_disk"] : float
                the ellipticity of the disk components
            (Optional parameters):
            param['disk_sersic_idx']: float
                Sersic index for galaxy disk component
            param['bulge_sersic_idx']: float
                Sersic index for galaxy bulge component
            param['g1'], param['g2']: float
                Reduced weak lensing shear components (valid for shear type: catalog)
122
        the model of observation_sim.mock_objects.Galaxy class requires:
Fang Yuedong's avatar
Fang Yuedong committed
123
124
125
126
127
128
129
130
131
132
133
134
135
            Currently a Quasar is modeled as a point source, just like a Star.

        NOTE: To construct an object, according to its type, just call:
            Star(param), Galaxy(param), or Quasar(param)

        NOTE: All constructed objects should be appened to "self.objs".

        NOTE: Any other parameters can also be set within "param" dict:
            Used to calculate required quantities and/or SEDs etc.

        Parameters
        ----------
        **kwargs : dict
136
            any other needed input parameters (in key-value pairs), please modify corresponding
Fang Yuedong's avatar
Fang Yuedong committed
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
            initialization call in "ObservationSim.py" as you need.

        Returns
        ----------
        None
        """

        stars = Table.read(self.star_path)
        nstars = stars['sourceID'].size
        for istars in range(nstars):
            param = self.initialize_param()
            param['id'] = istars + 1
            param['ra'] = stars['RA'][istars]
            param['dec'] = stars['Dec'][istars]
            param['sed_type'] = stars['sourceID'][istars]
            param['model_tag'] = stars['model_tag'][istars]
            param['z'] = 0.0
            param['star'] = 1   # Star
            param['mag_use_normal'] = stars['app_sdss_g'][istars]
            obj = Star(param)
            self.objs.append(obj)

    def load_sed(self, obj, **kwargs):
        """Load the corresponding SED data for a particular obj.

        Parameters
        ----------
164
        obj : instance of a particular class in observation_sim.mock_objects
Fang Yuedong's avatar
Fang Yuedong committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
            the object to get SED data for
        **kwargs : dict
            other needed input parameters (in key-value pairs), please modify corresponding
            initialization call in "ObservationSim.py" as you need.

        Returns
        ----------
        sed : Astropy.Table
            the SED Table with two columns (namely, "WAVELENGTH", "FLUX"):
                sed["WAVELENGTH"] : wavelength in Angstroms
                sed["FLUX"] : fluxes in photons/s/m^2/A
            NOTE: the range of wavelengthes must at least cover [2450 - 11000] Angstorms
        """

        if obj.type == 'star':
            wave = Table.read(self.star_SED_path,
                              path=f"/SED/wave_{obj.model_tag}")
            flux = Table.read(self.star_SED_path, path=f"/SED/{obj.sed_type}")
            wave, flux = wave['col0'].data, flux['col0'].data
        else:
            raise ValueError("Object type not known")
        speci = interpolate.interp1d(wave, flux)
        lamb = np.arange(2400, 11001 + 0.5, 0.5)
        y = speci(lamb)
        # erg/s/cm^2/A --> photons/s/m^2/A
        all_sed = y * lamb / (cons.h.value * cons.c.value) * 1e-13
        sed = Table(np.array([lamb, all_sed]).T, names=('WAVELENGTH', 'FLUX'))
        return sed

    def load_norm_filt(self, obj):
        """Load the corresponding thourghput for the input magnitude "param["mag_use_normal"]".

        NOTE: if the input magnitude is already in CSST magnitude, simply return None

        Parameters
        ----------
201
        obj : instance of a particular class in observation_sim.mock_objects
Fang Yuedong's avatar
Fang Yuedong committed
202
203
204
205
206
207
208
209
210
211
            the object to get thourghput data for

        Returns
        ----------
        norm_filt : Astropy.Table
            the throughput Table with two columns (namely, "WAVELENGTH", "SENSITIVITY"):
                norm_filt["WAVELENGTH"] : wavelengthes in Angstroms
                norm_filt["SENSITIVITY"] : efficiencies
        """
        return None