Debian Codegraph @

geotiepoints/tests/test_modis.py

raw ◀ ▶
1 Debian patch touch this file
1 """Unit tests for python-geotiepoints: MODIS examples."""
2
3 import numpy as np
4 import h5py
5 import os
•6 import unittest
7
8 import pytest
9
10 FILENAME_250M_RESULT = os.path.join(
11 os.path.dirname(__file__), '../../testdata/250m_lonlat_section_result.h5')
12 FILENAME_250M_INPUT = os.path.join(
13 os.path.dirname(__file__), '../../testdata/250m_lonlat_section_input.h5')
14
15 FILENAME_FULL = os.path.join(
16 os.path.dirname(__file__), '../../testdata/test_5_to_1_geoloc_full.h5')
17 FILENAME_5KM = os.path.join(
18 os.path.dirname(__file__), '../../testdata/test_5_to_1_geoloc_5km.h5')
19
20 from geotiepoints import (modis5kmto1km, modis1kmto250m)
21
22 from geotiepoints import get_scene_splits
23
24
25 class TestUtils:
26 """Class for unit testing the ancillary interpolation functions."""
27
28 def test_get_numof_subscene_lines(self):
29 """Test getting the number of sub-scene lines.
30
31 Function is dependent on the number of CPUs and for various number of
32 lines in a scan.
33 """
34 ncpus = 3
35 scene_splits = get_scene_splits(1060, 10, ncpus)
36 assert list(scene_splits) == [350, 700, 1050]
37
38
39 class TestMODIS:
40 """Class for system testing the MODIS interpolation."""
41
•42 @unittest.skipIf(not os.path.isfile(FILENAME_FULL), 'deta file not available')
43 @unittest.skipIf(not os.path.isfile(FILENAME_5KM), 'deta file not available')
44 def test_5_to_1(self):
45 """Test the 5km to 1km interpolation facility."""
46
47 with h5py.File(FILENAME_FULL) as h5f:
48 glons = h5f['longitude'][:] / 1000.
49 glats = h5f['latitude'][:] / 1000.
50
51 with h5py.File(FILENAME_5KM) as h5f:
52 lons = h5f['longitude'][:] / 1000.
53 lats = h5f['latitude'][:] / 1000.
54
55 tlons, tlats = modis5kmto1km(lons, lats)
56
57 np.testing.assert_allclose(tlons, glons, atol=0.05)
58 np.testing.assert_allclose(tlats, glats, atol=0.05)
59
•60 @unittest.skipIf(not os.path.isfile(FILENAME_250M_RESULT), 'deta file not available')
61 @unittest.skipIf(not os.path.isfile(FILENAME_250M_INPUT), 'deta file not available')
62 @pytest.mark.parametrize("ncores", [None, 4])
63 def test_1000m_to_250m(self, ncores):
64 """Test the 1 km to 250 meter interpolation facility."""
65 if ncores:
66 import multiprocessing as mp
67 mp.set_start_method("spawn", force=True)
68
69 with h5py.File(FILENAME_250M_RESULT) as h5f:
70 glons = h5f['longitude'][:] / 1000.
71 glats = h5f['latitude'][:] / 1000.
72
73 with h5py.File(FILENAME_250M_INPUT) as h5f:
74 lons = h5f['longitude'][:] / 1000.
75 lats = h5f['latitude'][:] / 1000.
76
77 kwargs = {"cores": ncores} if ncores is not None else {}
78 tlons, tlats = modis1kmto250m(lons, lats, **kwargs)
79 np.testing.assert_allclose(tlons, glons, atol=0.05)
80 np.testing.assert_allclose(tlats, glats, atol=0.05)