File: test_modis.py

package info (click to toggle)
python-geotiepoints 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,824 kB
  • sloc: python: 3,124; makefile: 111; sh: 15
file content (80 lines) | stat: -rw-r--r-- 2,884 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
"""Unit tests for python-geotiepoints: MODIS examples."""

import numpy as np
import h5py
import os
import unittest

import pytest

FILENAME_250M_RESULT = os.path.join(
    os.path.dirname(__file__), '../../testdata/250m_lonlat_section_result.h5')
FILENAME_250M_INPUT = os.path.join(
    os.path.dirname(__file__), '../../testdata/250m_lonlat_section_input.h5')

FILENAME_FULL = os.path.join(
    os.path.dirname(__file__), '../../testdata/test_5_to_1_geoloc_full.h5')
FILENAME_5KM = os.path.join(
    os.path.dirname(__file__), '../../testdata/test_5_to_1_geoloc_5km.h5')

from geotiepoints import (modis5kmto1km, modis1kmto250m)

from geotiepoints import get_scene_splits


class TestUtils:
    """Class for unit testing the ancillary interpolation functions."""

    def test_get_numof_subscene_lines(self):
        """Test getting the number of sub-scene lines.

        Function is dependent on the number of CPUs and for various number of
        lines in a scan.
        """
        ncpus = 3
        scene_splits = get_scene_splits(1060, 10, ncpus)
        assert list(scene_splits) == [350, 700, 1050]


class TestMODIS:
    """Class for system testing the MODIS interpolation."""

    @unittest.skipIf(not os.path.isfile(FILENAME_FULL), 'deta file not available')
    @unittest.skipIf(not os.path.isfile(FILENAME_5KM), 'deta file not available')
    def test_5_to_1(self):
        """Test the 5km to 1km interpolation facility."""

        with h5py.File(FILENAME_FULL) as h5f:
            glons = h5f['longitude'][:] / 1000.
            glats = h5f['latitude'][:] / 1000.

        with h5py.File(FILENAME_5KM) as h5f:
            lons = h5f['longitude'][:] / 1000.
            lats = h5f['latitude'][:] / 1000.

        tlons, tlats = modis5kmto1km(lons, lats)

        np.testing.assert_allclose(tlons, glons, atol=0.05)
        np.testing.assert_allclose(tlats, glats, atol=0.05)

    @unittest.skipIf(not os.path.isfile(FILENAME_250M_RESULT), 'deta file not available')
    @unittest.skipIf(not os.path.isfile(FILENAME_250M_INPUT), 'deta file not available')
    @pytest.mark.parametrize("ncores", [None, 4])
    def test_1000m_to_250m(self, ncores):
        """Test the 1 km to 250 meter interpolation facility."""
        if ncores:
            import multiprocessing as mp
            mp.set_start_method("spawn", force=True)

        with h5py.File(FILENAME_250M_RESULT) as h5f:
            glons = h5f['longitude'][:] / 1000.
            glats = h5f['latitude'][:] / 1000.

        with h5py.File(FILENAME_250M_INPUT) as h5f:
            lons = h5f['longitude'][:] / 1000.
            lats = h5f['latitude'][:] / 1000.

        kwargs = {"cores": ncores} if ncores is not None else {}
        tlons, tlats = modis1kmto250m(lons, lats, **kwargs)
        np.testing.assert_allclose(tlons, glons, atol=0.05)
        np.testing.assert_allclose(tlats, glats, atol=0.05)