Completed
Push — develop ( f92b69...710c13 )
by Adam
55s
created

TestRsrReader.setUp()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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