test_Straylight.py 8.87 KB
Newer Older
Zhang Xin's avatar
Zhang Xin committed
1
#
2
#need add environment parameter  UNIT_TEST_DATA_ROOT, link to "testData/"
Zhang Xin's avatar
Zhang Xin committed
3
#linx and mac can run as follow, need modify the name of file directory
4
#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
7
8
9
10
11
12
13
14
15
16
17
import unittest
from ObservationSim.Straylight import Straylight

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

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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}

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])


def getAngle132(x1=0, y1=0, z1=0, x2=0, y2=0, z2=0, x3=0, y3=0, z3=0):
    cosValue = 0;
    angle = 0;

    x11 = x1 - x3;
    y11 = y1 - y3;
    z11 = z1 - z3;

    x22 = x2 - x3;
    y22 = y2 - y3;
    z22 = z2 - z3;

    tt = np.sqrt((x11 * x11 + y11 * y11 + z11 * z11) * (x22 * x22 + y22 * y22 + z22 * z22));
    if (tt == 0):
        return 0;

    cosValue = (x11 * x22 + y11 * y22 + z11 * z22) / tt;

    if (cosValue > 1):
        cosValue = 1;
    if (cosValue < -1):
        cosValue = -1;
    angle = math.acos(cosValue);
    return angle * 360 / (2 * math.pi);

def calculateAnglePwithEarth(sat = np.array([0,0,0]), pointing = np.array([0,0,0]), sun = np.array([0,0,0])):
    modSat = np.sqrt(sat[0]*sat[0] + sat[1]*sat[1]+sat[2]*sat[2])
    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)

    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
    if (cosAngle < -0.3385737): #cos109.79
        isInSunSide = -1;
    elif cosAngle >= -0.3385737 and cosAngle <= 0.3385737:
        isInSunSide = 0;

    return math.acos(withLocalZenithAngle)*180/math.pi,isInSunSide

class TestStraylight(unittest.TestCase):

Zhang Xin's avatar
Zhang Xin committed
72
    def __init__(self,methodName='runTest', filter = 'i', grating = "GI"):
73
        super(TestStraylight,self).__init__(methodName)
Zhang Xin's avatar
Zhang Xin committed
74
75
76
        # 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
77
        self.filePath('csst_msc_sim/test_sls_and_straylight')
78
79
        self.filter = filter
        self.grating = grating
Zhang Xin's avatar
Zhang Xin committed
80
81
82
83
        
    def filePath(self, 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)
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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

    def test_EarthShineFilter(self):
        d_sh = self.pointingData.shape
        sl_e_pix = np.zeros([d_sh[0],3],dtype=np.double)

        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])
            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, py = sl.calculateEarthShineFilter(filter=self.filter)
            earthZenithAngle, isInSunSide = calculateAnglePwithEarth(sat=self.pointingData[i, 6:9], pointing= sl.pointing, sun=self.pointingData[i,9:12])
            # 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')
            sl_e_pix[i,0] = e1
            sl_e_pix[i, 1] = earthZenithAngle
            sl_e_pix[i, 2] = isInSunSide
        median  = np.median(sl_e_pix[:,0])
        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
        sl_e_pix = np.zeros([d_sh[0],2],dtype=np.double)

        for i in np.arange(d_sh[0]):
            ju = self.pointingData[i, 5]
            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)
        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()
        median  = np.median(sl_e_pix[:,0])
        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
        sl_e_pix = np.zeros(d_sh[0],dtype=np.double)

        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])
            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, 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
        median  = np.median(sl_e_pix[0:tnum])
        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
        sl_e_pix = np.zeros(d_sh[0],dtype=np.double)

        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])
            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, 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)')
        plt.xlim(2000,10000)
        plt.show()
        median  = np.median(sl_e_pix[0:tnum])
        print(' average Earthshine %s: %e' % (self.grating, median))
        self.assertTrue(median < 0.8)





if __name__ == '__main__':
193
    os.environ['UNIT_TEST_DATA_ROOT']="/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData"
194
195

    suit = unittest.TestSuite()
196
197
198
199
200
201
202
    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')
203
204
    suit.addTest(case4)
    unittest.TextTestRunner(verbosity=2).run(suit)