Completed
Pull Request — develop (#19)
by Adam
25s
created

TestReflectance.setUp()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# Copyright (c) 2013-2018 Adam.Dybbroe
5
6
# Author(s):
7
8
#   Adam.Dybbroe <[email protected]>
9
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation, either version 3 of the License, or
13
# (at your option) any later version.
14
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19
20
# You should have received a copy of the GNU General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23
"""Unit testing the 3.7 micron reflectance calculations"""
24
25
from pyspectral.near_infrared_reflectance import Calculator
26
from mock import patch
27
28
import unittest
29
import numpy as np
30
31
TEST_RSR = {'20': {}}
32
TEST_RSR['20']['det-1'] = {}
33
TEST_RSR['20']['det-1']['central_wavelength'] = 3.780281935
34
TEST_RSR['20']['det-1']['wavelength'] = np.array([
35
    3.6123999, 3.6163599, 3.6264927, 3.6363862, 3.646468,
36
    3.6564937, 3.6664478, 3.6765388, 3.6865413, 3.6964585,
37
    3.7065142, 3.716509, 3.7264658, 3.7364102, 3.7463682,
38
    3.7563652, 3.7664226, 3.7763396, 3.7863384, 3.7964207,
39
    3.8063589, 3.8163606, 3.8264089, 3.8364836, 3.8463381,
40
    3.8563975, 3.8664163, 3.8763755, 3.8864797, 3.8964978,
41
    3.9064275, 3.9164873, 3.9264729, 3.9364026, 3.9465107,
42
    3.9535347], dtype='double')
43
44
TEST_RSR['20']['det-1']['response'] = np.array([
45
    0.01, 0.0118, 0.01987, 0.03226, 0.05028, 0.0849,
46
    0.16645, 0.33792, 0.59106, 0.81815, 0.96077, 0.92855,
47
    0.86008, 0.8661, 0.87697, 0.85412, 0.88922, 0.9541,
48
    0.95687, 0.91037, 0.91058, 0.94256, 0.94719, 0.94808,
49
    1., 0.92676, 0.67429, 0.44715, 0.27762, 0.14852,
50
    0.07141, 0.04151, 0.02925, 0.02085, 0.01414, 0.01], dtype='double')
51
52
TEST_RSR_WN = {'20': {}}
53
TEST_RSR_WN['20']['det-1'] = {}
54
WVN = np.array([2529.38208008, 2533.8840332, 2540.390625, 2546.81494141,
55
                2553.30883789, 2559.88354492, 2566.40722656, 2573.02270508,
56
                2579.72949219, 2586.37451172, 2593.09375, 2599.87548828,
57
                2606.55371094, 2613.41674805, 2620.29760742, 2627.18286133,
58
                2634.06005859, 2641.07421875, 2648.06713867, 2655.03930664,
59
                2662.14794922, 2669.25170898, 2676.36572266, 2683.50805664,
60
                2690.69702148, 2697.95288086, 2705.29199219, 2712.56982422,
61
                2719.94970703, 2727.43554688, 2734.8605957, 2742.38012695,
62
                2749.9831543, 2757.48510742, 2765.21142578, 2768.24291992],
63
               dtype='float32')
64
RESP = np.array([0.01, 0.01414, 0.02085, 0.02925, 0.04151,
65
                 0.07141, 0.14851999, 0.27761999, 0.44714999, 0.67429,
66
                 0.92676002, 1., 0.94808, 0.94718999, 0.94256002,
67
                 0.91057998, 0.91036999, 0.95687002, 0.95410001, 0.88922,
68
                 0.85412002, 0.87696999, 0.86610001, 0.86008, 0.92855,
69
                 0.96077001, 0.81814998, 0.59105998, 0.33792001, 0.16644999,
70
                 0.0849, 0.05028, 0.03226, 0.01987, 0.0118,
71
                 0.01], dtype='float32')
72
73
TEST_RSR_WN['20']['det-1']['central_wavelength'] = 3.780281935
74
TEST_RSR_WN['20']['det-1']['wavenumber'] = WVN
75
TEST_RSR_WN['20']['det-1']['response'] = RESP
76
77
78
class TestReflectance(unittest.TestCase):
79
80
    """Unit testing the reflectance calculations"""
81
82
    def setUp(self):
83
        """Set up"""
84
        pass
85
86
    def test_rsr_integral(self):
87
        """Test calculating the integral of the relative spectral response
88
        function. 
89
        """
90
        with patch('pyspectral.radiance_tb_conversion.RelativeSpectralResponse') as mymock:
91
            instance = mymock.return_value
92
            instance.rsr = TEST_RSR
93
            instance.unit = '1e-6 m'
94
            instance.si_scale = 1e-6
95
96
            refl37 = Calculator('EOS-Aqua', 'modis', '20')
97
98
        expected = 1.8563451e-07  # unit = 'm' (meter)
99
        self.assertAlmostEqual(refl37.rsr_integral, expected)
100
101
        with patch('pyspectral.radiance_tb_conversion.RelativeSpectralResponse') as mymock:
102
            instance = mymock.return_value
103
            instance.rsr = TEST_RSR_WN
104
            instance.unit = 'cm-1'
105
            instance.si_scale = 100.
106
107
            refl37 = Calculator('EOS-Aqua', 'modis', '20', wavespace='wavenumber')
108
109
        expected = 13000.385  # SI units = 'm-1' (1/meter)
110
        res = refl37.rsr_integral
111
        self.assertAlmostEqual(res / expected, 1.0, 6)
112
113
    def test_reflectance(self):
114
        """Test the derivation of the reflective part of a 3.7 micron band"""
115
116
        with patch('pyspectral.radiance_tb_conversion.RelativeSpectralResponse') as mymock:
117
            instance = mymock.return_value
118
            instance.rsr = TEST_RSR
119
            instance.unit = '1e-6 m'
120
            instance.si_scale = 1e-6
121
122
            refl37 = Calculator('EOS-Aqua', 'modis', '20')
123
124
        sunz = 80.
125
        tb3 = 290.
126
        tb4 = 282.
127
        refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
128
        self.assertAlmostEqual(refl, 0.251245010648, 6)
129
130
        tb3x = refl37.emissive_part_3x()
131
        self.assertAlmostEqual(tb3x, 276.213054, 6)
132
133
        sunz = 80.
134
        tb3 = 295.
135
        tb4 = 282.
136
        refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
137
        self.assertAlmostEqual(refl, 0.452497961, 6)
138
139
        tb3x = refl37.emissive_part_3x()
140
        self.assertAlmostEqual(tb3x, 270.077268, 6)
141
142
        sunz = 50.
143
        tb3 = 300.
144
        tb4 = 285.
145
        refl = refl37.reflectance_from_tbs(sunz, tb3, tb4)
146
        self.assertAlmostEqual(refl, 0.1189217, 6)
147
148
        tb3x = refl37.emissive_part_3x()
149
        self.assertAlmostEqual(tb3x, 282.455426, 6)
150
151
    def tearDown(self):
152
        """Clean up"""
153
        pass
154
155
156
def suite():
157
    """The suite for test_reflectance."""
158
    loader = unittest.TestLoader()
159
    mysuite = unittest.TestSuite()
160
    mysuite.addTest(loader.loadTestsFromTestCase(TestReflectance))
161
162
    return mysuite
163