File: test_data_reduce.py

package info (click to toggle)
pyresample 1.35.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,880 kB
  • sloc: python: 20,340; cpp: 463; makefile: 105
file content (160 lines) | stat: -rw-r--r-- 6,997 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 Pyresample Developers
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Testing the data_reduce module."""

import unittest

import numpy as np

from pyresample import geometry
from pyresample.data_reduce import (
    get_valid_index_from_cartesian_grid,
    get_valid_index_from_lonlat_grid,
    swath_from_cartesian_grid,
    swath_from_lonlat_boundaries,
    swath_from_lonlat_grid,
)


class Test(unittest.TestCase):
    """Unit testing the data_reduce module."""

    @classmethod
    def setUpClass(cls):
        """Get ready for testing."""
        cls.area_def = geometry.AreaDefinition('areaD',
                                               'Europe (3km, HRV, VTC)',
                                               'areaD',
                                               {'a': '6378144.0',
                                                'b': '6356759.0',
                                                'lat_0': '50.00',
                                                'lat_ts': '50.00',
                                                'lon_0': '8.00',
                                                'proj': 'stere'},
                                               800,
                                               800,
                                               [-1370912.72,
                                                   -909968.64000000001,
                                                   1029087.28,
                                                   1490031.3600000001])

    def test_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        grid_lons, grid_lats = self.area_def.get_lonlats()
        lons, lats, data = swath_from_lonlat_grid(grid_lons, grid_lats,
                                                  lons, lats, data,
                                                  7000)
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected)

    def test_reduce_boundary(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        boundary_lonlats = self.area_def.get_boundary_lonlats()
        lons, lats, data = swath_from_lonlat_boundaries(boundary_lonlats[0],
                                                        boundary_lonlats[1],
                                                        lons, lats, data, 7000)
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected)

    def test_cartesian_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        grid = self.area_def.get_cartesian_coords()
        lons, lats, data = swath_from_cartesian_grid(grid, lons, lats, data,
                                                     7000)
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected)

    def test_area_con_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        grid_lons, grid_lats = self.area_def.get_lonlats()
        valid_index = get_valid_index_from_lonlat_grid(grid_lons, grid_lats,
                                                       lons, lats, 7000)
        data = data[valid_index]
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected)

    def test_area_con_cartesian_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        cart_grid = self.area_def.get_cartesian_coords()
        valid_index = get_valid_index_from_cartesian_grid(cart_grid,
                                                          lons, lats, 7000)
        data = data[valid_index]
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected)

    def test_reduce_north_pole(self):
        """Test reducing around the poles."""
        from pyresample import utils
        area_id = 'ease_sh'
        description = 'Antarctic EASE grid'
        proj_id = 'ease_sh'
        projection = '+proj=laea +lat_0=-90 +lon_0=0 +a=6371228.0 +units=m'
        x_size = 425
        y_size = 425
        area_extent = (-5326849.0625, -5326849.0625,
                       5326849.0625, 5326849.0625)
        area_def = utils.get_area_def(area_id, description, proj_id,
                                      projection, x_size, y_size, area_extent)

        grid_lons, grid_lats = area_def.get_lonlats()

        area_id = 'ease_sh'
        description = 'Antarctic EASE grid'
        proj_id = 'ease_sh'
        projection = '+proj=laea +lat_0=-90 +lon_0=0 +a=6371228.0 +units=m'
        x_size = 1000
        y_size = 1000
        area_extent = (-532684.0625, -532684.0625, 532684.0625, 532684.0625)
        smaller_area_def = utils.get_area_def(area_id, description, proj_id,
                                              projection, x_size, y_size,
                                              area_extent)

        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons, lats = smaller_area_def.get_lonlats()

        lons, lats, data = swath_from_lonlat_grid(grid_lons, grid_lats,
                                                  lons, lats, data, 7000)

        cross_sum = data.sum()
        expected = 999000000.0
        self.assertAlmostEqual(cross_sum, expected)