test_Straylight.py 9.14 KB
Newer Older
Zhang Xin's avatar
Zhang Xin committed
1
#
Fang Yuedong's avatar
Fang Yuedong committed
2
3
4
# need add environment parameter  UNIT_TEST_DATA_ROOT, link to "testData/"
# linx and mac can run as follow, need modify the name of file directory
# export UNIT_TEST_DATA_ROOT=/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData
Zhang Xin's avatar
Zhang Xin committed
5
#
6
import unittest
Fang Yuedong's avatar
Fang Yuedong committed
7
from observation_sim.sky_background import Straylight
8
9
10
11
12
13
14
15
16
17

import numpy as np
import math
import astropy.constants as cons
import galsim
from astropy.table import Table
from scipy import interpolate

import matplotlib.pyplot as plt

18
19
import os

Fang Yuedong's avatar
Fang Yuedong committed
20
21
22
23
hubbleAverZodiacal = {'nuv': 0.0035, 'u': 0.0163, 'g': 0.1109,
                      'r': 0.1471, 'i': 0.1568, 'z': 0.0953, 'y': 0.0283}
hubbleAverEarthShine = {'nuv': 0.00024, 'u': 0.0051, 'g': 0.0506,
                        'r': 0.0591, 'i': 0.0568, 'z': 0.0315, 'y': 0.0090}
24

25
26
27
28
29
# def transRaDec2D(ra, dec):
#     x1 = np.cos(dec / 57.2957795) * np.cos(ra / 57.2957795);
#     y1 = np.cos(dec / 57.2957795) * np.sin(ra / 57.2957795);
#     z1 = np.sin(dec / 57.2957795);
#     return np.array([x1, y1, z1])
30
31
32


def getAngle132(x1=0, y1=0, z1=0, x2=0, y2=0, z2=0, x3=0, y3=0, z3=0):
Fang Yuedong's avatar
Fang Yuedong committed
33
34
    cosValue = 0
    angle = 0
35

Fang Yuedong's avatar
Fang Yuedong committed
36
37
38
    x11 = x1 - x3
    y11 = y1 - y3
    z11 = z1 - z3
39

Fang Yuedong's avatar
Fang Yuedong committed
40
41
42
    x22 = x2 - x3
    y22 = y2 - y3
    z22 = z2 - z3
43

Fang Yuedong's avatar
Fang Yuedong committed
44
45
    tt = np.sqrt((x11 * x11 + y11 * y11 + z11 * z11)
                 * (x22 * x22 + y22 * y22 + z22 * z22))
46
    if (tt == 0):
Fang Yuedong's avatar
Fang Yuedong committed
47
        return 0
48

Fang Yuedong's avatar
Fang Yuedong committed
49
    cosValue = (x11 * x22 + y11 * y22 + z11 * z22) / tt
50
51

    if (cosValue > 1):
Fang Yuedong's avatar
Fang Yuedong committed
52
        cosValue = 1
53
    if (cosValue < -1):
Fang Yuedong's avatar
Fang Yuedong committed
54
55
56
        cosValue = -1
    angle = math.acos(cosValue)
    return angle * 360 / (2 * math.pi)
57

Fang Yuedong's avatar
Fang Yuedong committed
58
59

def calculateAnglePwithEarth(sat=np.array([0, 0, 0]), pointing=np.array([0, 0, 0]), sun=np.array([0, 0, 0])):
60
    modSat = np.sqrt(sat[0]*sat[0] + sat[1]*sat[1]+sat[2]*sat[2])
Fang Yuedong's avatar
Fang Yuedong committed
61
62
63
64
    modPoint = np.sqrt(pointing[0]*pointing[0] +
                       pointing[1]*pointing[1] + pointing[2]*pointing[2])
    withLocalZenithAngle = (
        pointing[0] * sat[0] + pointing[1] * sat[1] + pointing[2] * sat[2]) / (modPoint*modSat)
65
66
67
68

    innerM_sat_sun = sat[0] * sun[0] + sat[1] * sun[1] + sat[2] * sun[2]
    cosAngle = innerM_sat_sun / (modSat * cons.au.value/1000)
    isInSunSide = 1
Fang Yuedong's avatar
Fang Yuedong committed
69
70
    if (cosAngle < -0.3385737):  # cos109.79
        isInSunSide = -1
71
    elif cosAngle >= -0.3385737 and cosAngle <= 0.3385737:
Fang Yuedong's avatar
Fang Yuedong committed
72
73
74
        isInSunSide = 0

    return math.acos(withLocalZenithAngle)*180/math.pi, isInSunSide
75
76
77
78


class TestStraylight(unittest.TestCase):

Fang Yuedong's avatar
Fang Yuedong committed
79
80
    def __init__(self, methodName='runTest', filter='i', grating="GI"):
        super(TestStraylight, self).__init__(methodName)
Zhang Xin's avatar
Zhang Xin committed
81
82
83
        # print(file_name)
        # fn = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), file_name)
        # self.pointingData = np.loadtxt(os.path.join(fn, 'Straylight_test.dat'), dtype=np.double)
Zhang Xin's avatar
Zhang Xin committed
84
        self.filePath('csst_msc_sim/test_sls_and_straylight')
85
86
        self.filter = filter
        self.grating = grating
Fang Yuedong's avatar
Fang Yuedong committed
87

Zhang Xin's avatar
Zhang Xin committed
88
89
    def filePath(self, file_name):
        fn = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), file_name)
Fang Yuedong's avatar
Fang Yuedong committed
90
91
        self.pointingData = np.loadtxt(os.path.join(
            fn, 'Straylight_test.dat'), dtype=np.double)
92
93
94

    def test_EarthShineFilter(self):
        d_sh = self.pointingData.shape
Fang Yuedong's avatar
Fang Yuedong committed
95
        sl_e_pix = np.zeros([d_sh[0], 3], dtype=np.double)
96
97
98
99
100
101
102

        for i in np.arange(d_sh[0]):
            # if i > 50:
            #     continue
            ju = self.pointingData[i, 5]
            # pointing = transRaDec2D(self.pointingData[i, 0], self.pointingData[i, 1])
            # print(ju, pointing, surveylist[i,3:9])
Fang Yuedong's avatar
Fang Yuedong committed
103
104
            sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
                [self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
105
            e1, py = sl.calculateEarthShineFilter(filter=self.filter)
Fang Yuedong's avatar
Fang Yuedong committed
106
107
            earthZenithAngle, isInSunSide = calculateAnglePwithEarth(
                sat=self.pointingData[i, 6:9], pointing=sl.pointing, sun=self.pointingData[i, 9:12])
108
109
110
111
            # e2, _ = sl.calculateZodiacalFilter2(filter='i', sun_pos=sl.sun_pos)
            # e3 = sl.calculateStarLightFilter(filter='i', pointYaxis=py)
            # e_all = sl.calculateStrayLightFilter(filter='i')
            # s_pix, spec = sl.calculateStrayLightGrating(grating='GI')
Fang Yuedong's avatar
Fang Yuedong committed
112
            sl_e_pix[i, 0] = e1
113
114
            sl_e_pix[i, 1] = earthZenithAngle
            sl_e_pix[i, 2] = isInSunSide
Fang Yuedong's avatar
Fang Yuedong committed
115
        median = np.median(sl_e_pix[:, 0])
116
117
118
119
120
121
122
123
124
125
126
127
128
129
        print(' average Earthshine %s: %e' % (self.filter, median))
        self.assertTrue(median-hubbleAverEarthShine[self.filter] < 0.1)
        plt.figure()
        ids1 = sl_e_pix[:, 2] == 1
        ids2 = sl_e_pix[:, 2] != 1
        plt.plot(sl_e_pix[ids1, 0], sl_e_pix[ids1, 1], 'r.')
        plt.plot(sl_e_pix[ids2, 0], sl_e_pix[ids2, 1], 'b.')
        plt.legend(['In Sun Side', 'In Earths shadow'])
        plt.xlabel('straylight-earthshine(e-/pixel/s)')
        plt.ylabel('Angle with local zenith(degree)')
        plt.show()

    def test_ZodiacalFilter(self):
        d_sh = self.pointingData.shape
Fang Yuedong's avatar
Fang Yuedong committed
130
        sl_e_pix = np.zeros([d_sh[0], 2], dtype=np.double)
131
132
133

        for i in np.arange(d_sh[0]):
            ju = self.pointingData[i, 5]
Fang Yuedong's avatar
Fang Yuedong committed
134
135
136
137
138
139
140
            sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
                [self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
            e1, _ = sl.calculateZodiacalFilter2(
                filter=self.filter, sun_pos=sl.sun_pos)
            sl_e_pix[i, 0] = e1
            sl_e_pix[i, 1] = getAngle132(x1=self.pointingData[i, 9], y1=self.pointingData[i, 10], z1=self.pointingData[i, 11], x2=sl.pointing[0],
                                         y2=sl.pointing[1], z2=sl.pointing[2], x3=0, y3=0, z3=0)
141
142
143
144
145
        plt.figure()
        plt.plot(sl_e_pix[:, 0], sl_e_pix[:, 1], 'r.')
        plt.xlabel('straylight-zodiacal(e-/pixel/s)')
        plt.ylabel('Angle between pointing and sun(degree)')
        plt.show()
Fang Yuedong's avatar
Fang Yuedong committed
146
        median = np.median(sl_e_pix[:, 0])
147
148
149
150
151
        print(' average Zodiacal %s: %f' % (self.filter, median))
        self.assertTrue(median-hubbleAverZodiacal[self.filter] < 0.1)

    def test_StarFilter(self):
        d_sh = self.pointingData.shape
Fang Yuedong's avatar
Fang Yuedong committed
152
        sl_e_pix = np.zeros(d_sh[0], dtype=np.double)
153
154
155
156
157
158
159
160

        tnum = 10
        for i in np.arange(tnum):
            # if i > 50:
            #     continue
            ju = self.pointingData[i, 5]
            # pointing = transRaDec2D(self.pointingData[i, 0], self.pointingData[i, 1])
            # print(ju, pointing, surveylist[i,3:9])
Fang Yuedong's avatar
Fang Yuedong committed
161
162
            sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
                [self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
163
164
165
166
167
168
            e1, py = sl.calculateEarthShineFilter(filter=self.filter)
            # e2, _ = sl.calculateZodiacalFilter2(filter='i', sun_pos=sl.sun_pos)
            e3 = sl.calculateStarLightFilter(filter=self.filter, pointYaxis=py)
            # e_all = sl.calculateStrayLightFilter(filter='i')
            # s_pix, spec = sl.calculateStrayLightGrating(grating='GI')
            sl_e_pix[i] = e3
Fang Yuedong's avatar
Fang Yuedong committed
169
        median = np.median(sl_e_pix[0:tnum])
170
171
172
173
174
        print(' average Earthshine %s: %e' % (self.filter, median))
        self.assertTrue(median-hubbleAverEarthShine[self.filter] < 0.2)

    def test_GratingStraylight(self):
        d_sh = self.pointingData.shape
Fang Yuedong's avatar
Fang Yuedong committed
175
        sl_e_pix = np.zeros(d_sh[0], dtype=np.double)
176
177
178
179
180
181
182
183

        tnum = 10
        for i in np.arange(tnum):
            # if i > 50:
            #     continue
            ju = self.pointingData[i, 5]
            # pointing = transRaDec2D(self.pointingData[i, 0], self.pointingData[i, 1])
            # print(ju, pointing, surveylist[i,3:9])
Fang Yuedong's avatar
Fang Yuedong committed
184
185
            sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
                [self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
186
187
188
189
190
191
192
193
194
195
            # e1, py = sl.calculateEarthShineFilter(filter=self.filter)
            # e2, _ = sl.calculateZodiacalFilter2(filter='i', sun_pos=sl.sun_pos)
            # e3 = sl.calculateStarLightFilter(filter=self.filter, pointYaxis=py)
            # e_all = sl.calculateStrayLightFilter(filter='i')
            s_pix, spec = sl.calculateStrayLightGrating(grating=self.grating)
            sl_e_pix[i] = s_pix
        plt.figure()
        plt.plot(spec['WAVELENGTH'], spec['FLUX'], 'r')
        plt.xlabel('WAVELENGTH')
        plt.ylabel('F$\lambda$(erg/s/cm2/A/arcsec2)')
Fang Yuedong's avatar
Fang Yuedong committed
196
        plt.xlim(2000, 10000)
197
        plt.show()
Fang Yuedong's avatar
Fang Yuedong committed
198
        median = np.median(sl_e_pix[0:tnum])
199
200
201
202
203
        print(' average Earthshine %s: %e' % (self.grating, median))
        self.assertTrue(median < 0.8)


if __name__ == '__main__':
Fang Yuedong's avatar
Fang Yuedong committed
204
    os.environ['UNIT_TEST_DATA_ROOT'] = "/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData"
205

206
207
208
209
210
211
212
213
214
    # suit = unittest.TestSuite()
    # case1 = TestStraylight('test_EarthShineFilter', filter = 'i')
    # suit.addTest(case1)
    # case2 = TestStraylight('test_ZodiacalFilter',filter = 'i')
    # suit.addTest(case2)
    # case3 = TestStraylight('test_StarFilter', filter='i')
    # suit.addTest(case3)
    # case4 = TestStraylight('test_GratingStraylight', grating = 'GI')
    # suit.addTest(case4)
Fang Yuedong's avatar
Fang Yuedong committed
215
    # unittest.TextTestRunner(verbosity=2).run(suit)