File: test_seviri.py

package info (click to toggle)
python-mpop 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,516 kB
  • ctags: 1,877
  • sloc: python: 15,374; xml: 820; makefile: 90; sh: 8
file content (282 lines) | stat: -rw-r--r-- 8,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
72
73
74
75
76
77
78
79
80
81
82
83
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2010, 2011.

# Author(s):
 
#   Martin Raspaud <martin.raspaud@smhi.se>

# This file is part of mpop.

# mpop is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.

# mpop is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with
# mpop.  If not, see <http://www.gnu.org/licenses/>.

"""Module for testing the :mod:`mpop.instruments.seviri` module.
"""
import random
import unittest

import numpy as np

import mpop.instruments.seviri
from mpop.imageo import geo_image


def patch_scene_mask():
    """Patch the :mod:`mpop.scene` module to avoid using it in these tests.
    """
    class FakeChannel(object):
        """FakeChannel class.
        """
        def __init__(self, val):
            del val
            self.data = np.ma.array((random.random(),))

        def check_range(self, *args):
            """Dummy check_range function.
            """
            del args
            return self.data

    
    class FakeSatscene(object):
        """Fake SatelliteInstrumentScene.
        """
        __version__ = "fake"
        def __init__(self):
            self.channels = {}
            self.area = None
            self.time_slot = None
            self.error = []
        def check_channels(self, *args):
            """Dummy check_channels function.
            """
            for chn in args:
                if chn in self.error:
                    raise RuntimeError()

        def __getitem__(self, key):
            if key not in self.channels:
                self.channels[key] = FakeChannel(key)
            return self.channels[key]
        
    mpop.instruments.visir.OldVisirCompositer = mpop.instruments.visir.VisirCompositer
    mpop.instruments.visir.VisirCompositer = FakeSatscene
    reload(mpop)
    reload(mpop.instruments)
    reload(mpop.instruments.seviri)

def patch_scene():
    """Patch the :mod:`mpop.scene` module to avoid using it in these tests.
    """
    class FakeChannel(object):
        """FakeChannel class.
        """
        def __init__(self, val):
            self.data = val

        def check_range(self, *args):
            """Dummy check_range function.
            """
            del args
            return self.data

    
    class FakeSatscene(object):
        """Fake SatelliteInstrumentScene.
        """
        __version__ = "fake"
        def __init__(self):
            self.channels = None
            self.area = None
            self.time_slot = None
            self._data_holder = self
            
        def check_channels(self, *args):
            """Dummy check_channels function.
            """
            self.channels = args

        def __contains__(self, point):
            return True
            

        def __getitem__(self, key):
            if key == "_IR39Corr":
                return FakeChannel(3.75)
            elif key == "HRV":
                return FakeChannel(0.7)
            return FakeChannel(key)

    mpop.instruments.visir.OldVisirCompositer = mpop.instruments.visir.VisirCompositer
    mpop.instruments.visir.VisirCompositer = FakeSatscene
    reload(mpop)
    reload(mpop.instruments)
    reload(mpop.instruments.seviri)

def unpatch_scene():
    """Unpatch the :mod:`mpop.scene` module.
    """
    mpop.instruments.visir.VisirCompositer = mpop.instruments.visir.OldVisirCompositer
    delattr(mpop.instruments.visir, "OldVisirCompositer")
    reload(mpop)
    reload(mpop.instruments)
    reload(mpop.instruments.visir)
    reload(mpop.instruments.seviri)

def patch_geo_image():
    """Patch the :mod:`imageo.geo_image` module to avoid using it in these
    tests.
    """
    class FakeGeoImage:
        """FakeGeoImage class.
        """
        def __init__(self, *args, **kwargs):
            self.args = args
            self.kwargs = kwargs
            self.lum = None
            self.channels = [self]
            
        def enhance(self, **kwargs):
            """Dummy enhance function.
            """
            self.kwargs.update(kwargs)

        def replace_luminance(self, lum):
            """Dummy remplace_luminance.
            """
            self.lum = lum
        

    geo_image.OldGeoImage = geo_image.GeoImage
    geo_image.GeoImage = FakeGeoImage

def unpatch_geo_image():
    """Unpatch the :mod:`imageo.geo_image` module.
    """
    geo_image.GeoImage = geo_image.OldGeoImage
    delattr(geo_image, "OldGeoImage")

class TestComposites(unittest.TestCase):
    """Class for testing the composites.
    """

    def setUp(self):
        """Setup stuff.
        """
        patch_geo_image()
        patch_scene()
        self.scene = mpop.instruments.seviri.SeviriCompositer()


    # def test_cloudtop(self):
    #     """Test cloudtop.
    #     """
    #     img = self.scene.cloudtop()
    #     self.assertEquals(img.kwargs["mode"], "RGB")
    #     self.assertEquals(img.kwargs["fill_value"], (0, 0, 0))
    #     self.assertEquals(img.args[0], (-3.75, -10.8, -12.0))
    #     self.assertEquals(img.kwargs["stretch"], (0.005, 0.005))
    #     self.assertTrue("crange" not in img.kwargs)
    #     self.assertTrue("gamma" not in img.kwargs)

    # def test_night_fog(self):
    #     """Test night_fog.
    #     """
    #     img = self.scene.night_fog()
    #     self.assertEquals(img.kwargs["mode"], "RGB")
    #     self.assertEquals(img.kwargs["fill_value"], (0, 0, 0))
    #     self.assertEquals(img.args[0], (12.0 - 10.8, 10.8 - 3.75, 10.8))
    #     self.assertEquals(img.kwargs["crange"], ((-4, 2),
    #                                              (0, 6),
    #                                              (243, 293)))
    #     self.assertEquals(img.kwargs["gamma"], (1.0, 2.0, 1.0))
    #     self.assertTrue("stretch" not in img.kwargs)

#     def test_hr_overview(self):
#         """Test hr_overview.
#         """
#         img = self.scene.hr_overview()
#         self.assertEquals(img.kwargs["mode"], "RGB")
#         self.assertEquals(img.kwargs["fill_value"], (0, 0, 0))
#         self.assertEquals(img.args[0], (0.635, 0.85, -10.8))
#         self.assertEquals(img.kwargs["stretch"], "crude")
#         self.assertEquals(list(img.kwargs["gamma"]), list((1.6, 1.6, 1.1)))
#         self.assertTrue("crange" not in img.kwargs)

#         self.assertEquals(img.lum.kwargs["mode"], "L")
#         self.assertEquals(img.lum.kwargs["crange"], (0, 100))
#         self.assertEquals(img.lum.kwargs["gamma"], 2.0)
#         self.assertTrue("stretch" not in img.lum.kwargs)
#         self.assertTrue("fill_value" not in img.lum.kwargs)

#     def test_hr_visual(self):
#         """Test hr_visual.
#         """
#         img = self.scene.hr_visual()
#         self.assertEquals(img.kwargs["mode"], "L")
#         self.assertEquals(img.kwargs["fill_value"], 0)
#         self.assertEquals(img.args[0], 0.7)
#         self.assertEquals(img.kwargs["stretch"], "crude")
#         self.assertTrue("crange" not in img.kwargs)
#         self.assertTrue("gamma" not in img.kwargs)
        
        

    def tearDown(self):
        unpatch_scene()
        unpatch_geo_image()


class TestCo2Corr(unittest.TestCase):
    """Class for testing the composites.
    """

    def setUp(self):
        """Setup stuff.
        """
        patch_geo_image()
        patch_scene_mask()
        self.scene = mpop.instruments.seviri.SeviriCompositer()


    def test_co2corr(self):
        """Test CO2 correction.
        """
        res = self.scene.co2corr()
        bt039 = self.scene[3.9].data
        bt108 = self.scene[10.8].data
        bt134 = self.scene[13.4].data
        
        dt_co2 = (bt108-bt134)/4.0
        rcorr = bt108 ** 4 - (bt108-dt_co2) ** 4
        
        
        t4_co2corr = bt039 ** 4 + rcorr
        if t4_co2corr < 0.0:
            t4_co2corr = 0
        solution = t4_co2corr ** 0.25
        self.assertEquals(res, solution)

        self.scene.error = [3.75]
        res = self.scene.co2corr()

        self.assertTrue(res is None)


    def tearDown(self):
        unpatch_scene()
        unpatch_geo_image()


if __name__ == '__main__':
    unittest.main()