Completed
Push — develop ( f0e27d...451801 )
by Adam
44s
created

TestRsrReader.test_integral()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# Copyright (c) 2017, 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 generic rsr hdf5 reader
24
"""
25
import sys
26
from pyspectral.rsr_reader import RelativeSpectralResponse
27
from pyspectral.utils import WAVE_NUMBER
28
from pyspectral.utils import WAVE_LENGTH
29
from pyspectral.utils import RSR_DATA_VERSION
30
31
from mock import patch
32
if sys.version_info < (2, 7):
33
    import unittest2 as unittest
34
else:
35
    import unittest
36
37
import numpy as np
38
39
TEST_RSR = {'20': {}, }
40
TEST_RSR['20']['det-1'] = {}
41
TEST_RSR['20']['det-1']['central_wavelength'] = 3.75
42
TEST_RSR['20']['det-1']['wavelength'] = np.array([
43
    3.6123999, 3.6163599, 3.6264927, 3.6363862, 3.646468,
44
    3.6564937, 3.6664478, 3.6765388, 3.6865413, 3.6964585,
45
    3.7065142, 3.716509, 3.7264658, 3.7364102, 3.7463682,
46
    3.7563652, 3.7664226, 3.7763396, 3.7863384, 3.7964207,
47
    3.8063589, 3.8163606, 3.8264089, 3.8364836, 3.8463381,
48
    3.8563975, 3.8664163, 3.8763755, 3.8864797, 3.8964978,
49
    3.9064275, 3.9164873, 3.9264729, 3.9364026, 3.9465107,
50
    3.9535347], dtype='float32')
51
52
TEST_RSR['20']['det-1']['response'] = np.array([
53
    0.01, 0.0118, 0.01987, 0.03226, 0.05028, 0.0849,
54
    0.16645, 0.33792, 0.59106, 0.81815, 0.96077, 0.92855,
55
    0.86008, 0.8661, 0.87697, 0.85412, 0.88922, 0.9541,
56
    0.95687, 0.91037, 0.91058, 0.94256, 0.94719, 0.94808,
57
    1., 0.92676, 0.67429, 0.44715, 0.27762, 0.14852,
58
    0.07141, 0.04151, 0.02925, 0.02085, 0.01414, 0.01], dtype='float32')
59
60
TEST_RSR2 = {'20': {}, }
61
TEST_RSR2['20']['det-1'] = {}
62
TEST_RSR2['20']['det-1']['central_wavelength'] = 3.75
63
TEST_RSR2['20']['det-1']['wavelength'] = TEST_RSR['20']['det-1']['wavelength'].copy()
64
TEST_RSR2['20']['det-1']['response'] = TEST_RSR['20']['det-1']['response'].copy()
65
66
RESULT_WVN_RSR = np.array([2529.38232422,  2533.8840332,  2540.390625,  2546.81494141,
67
                           2553.30859375,  2559.88378906,  2566.40722656,  2573.02270508,
68
                           2579.72949219,  2586.37451172,  2593.09375,  2599.87548828,
69
                           2606.55371094,  2613.41674805,  2620.29760742,  2627.18286133,
70
                           2634.06005859,  2641.07421875,  2648.06713867,  2655.03930664,
71
                           2662.14794922,  2669.25170898,  2676.36572266,  2683.50805664,
72
                           2690.69702148,  2697.95288086,  2705.29199219,  2712.56982422,
73
                           2719.94970703,  2727.43554688,  2734.8605957,  2742.37988281,
74
                           2749.9831543,  2757.48510742,  2765.21142578,  2768.24291992], dtype=np.float32)
75
76
DIR_PATH_ITEMS = ['test', 'path', 'to', 'rsr', 'data']
77
TEST_CONFIG = {}
78
import os.path
79
TEST_RSR_DIR = os.path.join(*DIR_PATH_ITEMS)
80
TEST_CONFIG['rsr_dir'] = TEST_RSR_DIR
81
82
83
class TestRsrReader(unittest.TestCase):
84
85
    """Class for testing pyspectral.rsr_reader"""
86
87
    def setUp(self):
88
        """Setup the test"""
89
        pass
90
91
    @patch('os.path.exists')
92
    @patch('os.path.isfile')
93
    @patch('pyspectral.rsr_reader.RelativeSpectralResponse.load')
94
    @patch('pyspectral.rsr_reader.download_rsr')
95
    @patch('pyspectral.rsr_reader.RelativeSpectralResponse._get_rsr_data_version')
96
    def test_rsr_response(self, get_rsr_version, download_rsr, load, isfile, exists):
97
        """Test the RelativeSpectralResponse class initialisation"""
98
        load.return_code = None
99
        download_rsr.return_code = None
100
        isfile.return_code = True
101
        exists.return_code = True
102
        get_rsr_version.return_code = RSR_DATA_VERSION
103
104
        with patch('pyspectral.rsr_reader.get_config', return_value=TEST_CONFIG):
105
            with self.assertRaises(AttributeError):
106
                test_rsr = RelativeSpectralResponse('GOES-16')
107
                test_rsr = RelativeSpectralResponse(instrument='ABI')
108
109
            test_rsr = RelativeSpectralResponse('GOES-16', 'AbI')
110
            self.assertEqual(test_rsr.platform_name, 'GOES-16')
111
            self.assertEqual(test_rsr.instrument, 'abi')
112
            test_rsr = RelativeSpectralResponse('GOES-16', 'ABI')
113
            self.assertEqual(test_rsr.instrument, 'abi')
114
115
        with patch('pyspectral.rsr_reader.get_config', return_value=TEST_CONFIG):
116
            test_rsr = RelativeSpectralResponse('GOES-16', 'abi')
117
118
        self.assertEqual(test_rsr.platform_name, 'GOES-16')
119
        self.assertEqual(test_rsr.instrument, 'abi')
120
        self.assertEqual(
121
            test_rsr.filename, os.path.join(TEST_RSR_DIR, 'rsr_abi_GOES-16.h5'))
122
123
        with patch('pyspectral.rsr_reader.get_config', return_value=TEST_CONFIG):
124
            test_rsr = RelativeSpectralResponse(
125
                filename=os.path.join(TEST_RSR_DIR, 'rsr_abi_GOES-16.h5'))
126
127
        self.assertEqual(
128
            test_rsr.filename, os.path.join(TEST_RSR_DIR, 'rsr_abi_GOES-16.h5'))
129
130
    @patch('os.path.exists')
131
    @patch('os.path.isfile')
132
    @patch('pyspectral.rsr_reader.RelativeSpectralResponse.load')
133
    @patch('pyspectral.rsr_reader.download_rsr')
134
    @patch('pyspectral.rsr_reader.RelativeSpectralResponse._get_rsr_data_version')
135
    def test_convert(self, get_rsr_version, download_rsr, load, isfile, exists):
136
        """Test the conversion method"""
137
        load.return_code = None
138
        download_rsr.return_code = None
139
        isfile.return_code = True
140
        exists.return_code = True
141
        get_rsr_version.return_code = RSR_DATA_VERSION
142
143
        with patch('pyspectral.rsr_reader.get_config', return_value=TEST_CONFIG):
144
            test_rsr = RelativeSpectralResponse('EOS-Aqua', 'modis')
145
            test_rsr.rsr = TEST_RSR
146
            test_rsr.convert()
147
            self.assertAlmostEqual(test_rsr.rsr['20']['det-1']['central_wavenumber'], 2647.397, 3)
148
            self.assertTrue(np.allclose(test_rsr.rsr['20']['det-1'][WAVE_NUMBER], RESULT_WVN_RSR, 5))
149
            self.assertEqual(test_rsr._wavespace, WAVE_NUMBER)
150
151
            with self.assertRaises(NotImplementedError):
152
                test_rsr.convert()
153
154
    @patch('os.path.exists')
155
    @patch('os.path.isfile')
156
    @patch('pyspectral.rsr_reader.RelativeSpectralResponse.load')
157
    @patch('pyspectral.rsr_reader.download_rsr')
158
    @patch('pyspectral.rsr_reader.RelativeSpectralResponse._get_rsr_data_version')
159
    def test_integral(self, get_rsr_version, download_rsr, load, isfile, exists):
160
        """Test the calculation of the integral of the spectral responses"""
161
        load.return_code = None
162
        download_rsr.return_code = None
163
        isfile.return_code = True
164
        exists.return_code = True
165
        get_rsr_version.return_code = RSR_DATA_VERSION
166
167
        with patch('pyspectral.rsr_reader.get_config', return_value=TEST_CONFIG):
168
            test_rsr = RelativeSpectralResponse('EOS-Aqua', 'modis')
169
            test_rsr.rsr = TEST_RSR2
170
            res = test_rsr.integral('20')
171
            self.assertAlmostEqual(res['det-1'], 0.185634, 6)
172
173
    def tearDown(self):
174
        """Clean up"""
175
        pass
176
177
178
def suite():
179
    """The test suite for test_rsr_reader.
180
    """
181
    loader = unittest.TestLoader()
182
    mysuite = unittest.TestSuite()
183
    mysuite.addTest(loader.loadTestsFromTestCase(TestRsrReader))
184
185
    return mysuite
186