Пример #1
0
    def test_interpolate_irregular_lat_lon(self):
        """ irregular interpolation """

        source = np.random.rand(5, 5)
        coords_src = Coordinates(
            [clinspace(0, 10, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
        coords_dst = Coordinates([[[0, 2, 4, 6, 8, 10], [0, 2, 4, 5, 6, 10]]],
                                 dims=["lat_lon"])

        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "nearest",
                                       "interpolators": [ScipyGrid]
                                   })
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert "lat_lon" in output.dims
        np.testing.assert_array_equal(output["lat"].values,
                                      coords_dst["lat"].coordinates)
        np.testing.assert_array_equal(output["lon"].values,
                                      coords_dst["lon"].coordinates)
        assert output.values[0] == source[0, 0]
        assert output.values[1] == source[1, 1]
        assert output.values[-1] == source[-1, -1]
Пример #2
0
    def test_interpolate_irregular_arbitrary_2dims(self):
        """ irregular interpolation """

        # Note, this test also tests the looper helper

        # try >2 dims
        source = np.random.rand(5, 5, 3)
        coords_src = Coordinates(
            [clinspace(0, 10, 5),
             clinspace(0, 10, 5), [2, 3, 5]],
            dims=["lat", "lon", "time"])
        coords_dst = Coordinates(
            [clinspace(1, 11, 5),
             clinspace(1, 11, 5), [2, 3, 4]],
            dims=["lat", "lon", "time"])

        node = MockArrayDataSource(
            data=source,
            coordinates=coords_src,
            interpolation=[{
                "method": "nearest",
                "interpolators": [ScipyGrid]
            }, {
                "method": "linear",
                "dims": ["time"]
            }],
        )
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert np.all(output.lon.values == coords_dst["lon"].coordinates)
        assert np.all(output.time.values == coords_dst["time"].coordinates)
Пример #3
0
        def test_interpolate_irregular_arbitrary_2dims(self):
            """ irregular interpolation """

            # try >2 dims
            source = np.random.rand(5, 5, 3)
            coords_src = Coordinates(
                [clinspace(0, 10, 5),
                 clinspace(0, 10, 5), [2, 3, 5]],
                dims=['lat', 'lon', 'time'])
            coords_dst = Coordinates(
                [clinspace(1, 11, 5),
                 clinspace(1, 11, 5), [2, 3, 5]],
                dims=['lat', 'lon', 'time'])

            node = MockArrayDataSource(source=source,
                                       native_coordinates=coords_src,
                                       interpolation={
                                           'method': 'nearest',
                                           'interpolators': [ScipyGrid]
                                       })
            output = node.eval(coords_dst)

            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert np.all(output.lon.values == coords_dst.coords['lon'])
            assert np.all(output.time.values == coords_dst.coords['time'])
Пример #4
0
        def test_time_tolerance(self):

            # unstacked 1D
            source = np.random.rand(5, 5)
            coords_src = Coordinates([
                np.linspace(0, 10, 5),
                clinspace('2018-01-01', '2018-01-09', 5)
            ],
                                     dims=['lat', 'time'])
            node = MockArrayDataSource(source=source,
                                       native_coordinates=coords_src,
                                       interpolation={
                                           'default': {
                                               'method': 'nearest',
                                               'params': {
                                                   'spatial_tolerance':
                                                   1.1,
                                                   'time_tolerance':
                                                   np.timedelta64(1, 'D')
                                               }
                                           }
                                       })

            coords_dst = Coordinates(
                [[1, 1.2, 1.5, 5, 9],
                 clinspace('2018-01-01', '2018-01-09', 3)],
                dims=['lat', 'time'])
            output = node.eval(coords_dst)

            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert output.values[0, 0] == source[0, 0] and output.values[0, 1] == source[0, 2] and \
                   np.isnan(output.values[1, 0]) and np.isnan(output.values[1, 1]) and \
                   output.values[2, 0] == source[1, 0] and  output.values[2, 1] == source[1, 2]
Пример #5
0
    def test_evaluate_debug_attributes_no_overlap(self):
        with podpac.settings:
            podpac.settings["DEBUG"] = True

            node = MockDataSource()

            assert node._evaluated_coordinates is None
            assert node._requested_coordinates is None
            assert node._requested_source_coordinates is None
            assert node._requested_source_coordinates_index is None
            assert node._requested_source_boundary is None
            assert node._requested_source_data is None

            coords = Coordinates(
                [clinspace(-55, -45, 20),
                 clinspace(-55, -45, 20)],
                dims=["lat", "lon"])
            node.eval(coords)

            assert node._evaluated_coordinates is not None
            assert node._requested_coordinates is not None
            assert node._requested_source_coordinates is not None
            assert node._requested_source_coordinates_index is not None
            assert node._requested_source_boundary is None  # still none in this case
            assert node._requested_source_data is None  # still none in this case
Пример #6
0
    def test_nearest_selector_negative_time_step(self):
        selector = Selector("nearest")
        request1 = Coordinates([clinspace("2020-01-01", "2020-01-11", 11)],
                               ["time"])
        request2 = Coordinates([clinspace("2020-01-11", "2020-01-01", 11)],
                               ["time"])
        source1 = Coordinates(
            [clinspace("2020-01-22T00", "2020-01-01T00", 126)], ["time"])
        source2 = Coordinates(
            [clinspace("2020-01-01T00", "2020-01-22T00", 126)], ["time"])
        c11, ci11 = selector.select(source1, request1)
        assert len(c11["time"]) == 11
        assert len(ci11[0]) == 11

        c12, ci12 = selector.select(source1, request2)
        assert len(c12["time"]) == 11
        assert len(ci12[0]) == 11

        c21, ci21 = selector.select(source2, request1)
        assert len(c21["time"]) == 11
        assert len(ci21[0]) == 11

        c22, ci22 = selector.select(source2, request2)
        assert len(c22["time"]) == 11
        assert len(ci22[0]) == 11

        np.testing.assert_equal(ci11[0], ci12[0])
        np.testing.assert_equal(ci21[0], ci22[0])
Пример #7
0
    def test_time_tolerance(self):

        # unstacked 1D
        source = np.random.rand(5, 5)
        coords_src = Coordinates(
            [np.linspace(0, 10, 5),
             clinspace("2018-01-01", "2018-01-09", 5)],
            dims=["lat", "time"])
        node = MockArrayDataSource(
            data=source,
            coordinates=coords_src,
            interpolation={
                "method": "nearest",
                "params": {
                    "spatial_tolerance": 1.1,
                    "time_tolerance": np.timedelta64(1, "D")
                },
            },
        )

        coords_dst = Coordinates([[1, 1.2, 1.5, 5, 9],
                                  clinspace("2018-01-01", "2018-01-09", 3)],
                                 dims=["lat", "time"])
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert (output.values[0, 0] == source[0, 0]
                and output.values[0, 1] == source[0, 2]
                and np.isnan(output.values[1, 0])
                and np.isnan(output.values[1, 1])
                and output.values[2, 0] == source[1, 0]
                and output.values[2, 1] == source[1, 2])
Пример #8
0
 def test_evaluate_no_overlap_with_output(self):
     # there is a shortcut if there is no intersect, so we test that here
     node = MockDataSource()
     coords = Coordinates([clinspace(30, 40, 10), clinspace(30, 40, 10)], dims=["lat", "lon"])
     output = UnitsDataArray.create(coords, data=1)
     node.eval(coords, output=output)
     np.testing.assert_equal(output.data, np.full(output.shape, np.nan))
Пример #9
0
        def setup_method(self, method):
            self.coord_src = Coordinates([
                clinspace(45, 0, 16),
                clinspace(-70., -65., 16),
                clinspace(0, 1, 2)
            ],
                                         dims=['lat', 'lon', 'time'])

            LON, LAT, TIME = np.meshgrid(self.coord_src['lon'].coordinates,
                                         self.coord_src['lat'].coordinates,
                                         self.coord_src['time'].coordinates)

            self.latSource = LAT
            self.lonSource = LON
            self.timeSource = TIME

            self.nasLat = Array(source=LAT.astype(float),
                                native_coordinates=self.coord_src,
                                interpolation='bilinear')

            self.nasLon = Array(source=LON.astype(float),
                                native_coordinates=self.coord_src,
                                interpolation='bilinear')

            self.nasTime = Array(source=TIME.astype(float),
                                 native_coordinates=self.coord_src,
                                 interpolation='bilinear')
Пример #10
0
    def test_evaluate_no_overlap(self):
        """evaluate node with coordinates that do not overlap"""

        node = MockDataSource()
        coords = Coordinates([clinspace(-55, -45, 20), clinspace(-55, -45, 20)], dims=["lat", "lon"])
        output = node.eval(coords)

        assert np.all(np.isnan(output))
Пример #11
0
    def test_interpolate_rasterio(self):
        """ regular interpolation using rasterio"""

        assert rasterio is not None

        source = np.arange(0, 15)
        source.resize((3, 5))

        coords_src = Coordinates(
            [clinspace(0, 10, 3), clinspace(0, 10, 5)], dims=["lat", "lon"])
        coords_dst = Coordinates(
            [clinspace(1, 11, 3), clinspace(1, 11, 5)], dims=["lat", "lon"])

        # try one specific rasterio case to measure output
        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "min",
                                       "interpolators": [Rasterio]
                                   })
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert output.data[0, 3] == 3.0
        assert output.data[0, 4] == 4.0

        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "max",
                                       "interpolators": [Rasterio]
                                   })
        output = node.eval(coords_dst)
        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert output.data[0, 3] == 9.0
        assert output.data[0, 4] == 9.0

        # TODO boundary should be able to use a default
        node = MockArrayDataSource(
            data=source,
            coordinates=coords_src,
            interpolation={
                "method": "bilinear",
                "interpolators": [Rasterio]
            },
            boundary={
                "lat": 2.5,
                "lon": 1.25
            },
        )
        output = node.eval(coords_dst)
        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        np.testing.assert_allclose(
            output, [[1.4, 2.4, 3.4, 4.4, 5.0], [6.4, 7.4, 8.4, 9.4, 10.0],
                     [10.4, 11.4, 12.4, 13.4, 14.0]])
Пример #12
0
class TestArray(object):
    """Test Array datasource class (formerly Array)"""

    data = np.random.rand(11, 11)
    coordinates = Coordinates([clinspace(-25, 25, 11), clinspace(-25, 25, 11)], dims=['lat', 'lon'])

    def test_source_trait(self):
        """ must be an ndarray """
        
        node = Array(source=self.data, native_coordinates=self.coordinates)

        # list is coercable to array
        node = Array(source=[0, 1, 1], native_coordinates=self.coordinates)

        # this list is not coercable to array
        # Starting with numpy 0.16, this is now allowed!
        #with pytest.raises(TraitError):
            #node = Array(source=[0, [0, 1]], native_coordinates=self.coordinates)

    def test_get_data(self):
        """ defined get_data function"""
        
        source = self.data
        node = Array(source=source, native_coordinates=self.coordinates)
        output = node.eval(self.coordinates)

        assert isinstance(output, UnitsDataArray)
        assert output.values[0, 0] == source[0, 0]
        assert output.values[4, 5] == source[4, 5]

    def test_native_coordinates(self):
        """test that native coordinates get defined"""
        
        node = Array(source=self.data)
        with pytest.raises(NotImplementedError):
            node.get_native_coordinates()

        node = Array(source=self.data, native_coordinates=self.coordinates)
        assert node.native_coordinates

        node = Array(source=self.data, native_coordinates=self.coordinates)
        native_coordinates = node.native_coordinates
        get_native_coordinates = node.get_native_coordinates()
        assert native_coordinates
        assert get_native_coordinates
        assert native_coordinates == get_native_coordinates

    def test_base_definition(self):
        node = Array(source=self.data)
        d = node.base_definition
        source = np.array(d['source'])
        np.testing.assert_array_equal(source, self.data)

    def test_definition(self):
        node = Array(source=self.data)
        pipeline = podpac.pipeline.Pipeline(definition=node.definition)
        np.testing.assert_array_equal(pipeline.node.source, self.data)
Пример #13
0
 def make_square_array(self, order=1, bands=1):
     node = Array(
         source=np.arange(8 * bands).reshape(3 - order, 3 + order, bands),
         coordinates=Coordinates(
             [clinspace(4, 0, 2, "lat"),
              clinspace(1, 4, 4, "lon")][::order],
             crs="EPSG:4326"),
         outputs=[str(s) for s in list(range(bands))],
     )
     return node
Пример #14
0
 def test_evaluate_with_output_no_intersect(self):
     # there is a shortcut if there is no intersect, so we test that here
     node = MockDataSource()
     coords = Coordinates([clinspace(30, 40, 10),
                           clinspace(30, 40, 10)],
                          dims=['lat', 'lon'])
     output = UnitsDataArray(np.ones(coords.shape),
                             coords=coords.coords,
                             dims=coords.dims)
     node.eval(coords, output=output)
     np.testing.assert_equal(output.data, np.full(output.shape, np.nan))
Пример #15
0
    def test_set_native_coordinates(self):
        nc = Coordinates([clinspace(0, 50, 101),
                          clinspace(0, 50, 101)],
                         dims=['lat', 'lon'])
        node = DataSource(source='test', native_coordinates=nc)
        assert node.native_coordinates is not None

        with pytest.raises(tl.TraitError):
            DataSource(source='test', native_coordinates='not a coordinate')

        with pytest.raises(NotImplementedError):
            DataSource(source='test').native_coordinates
Пример #16
0
    def test_interpolate_scipy_grid(self):

        source = np.arange(0, 25)
        source.resize((5, 5))

        coords_src = Coordinates(
            [clinspace(0, 10, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
        coords_dst = Coordinates(
            [clinspace(1, 11, 5), clinspace(1, 11, 5)], dims=["lat", "lon"])

        # try one specific rasterio case to measure output
        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "nearest",
                                       "interpolators": [ScipyGrid]
                                   })
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        print(output)
        assert output.data[0, 0] == 0.0
        assert output.data[0, 3] == 3.0
        assert output.data[1, 3] == 8.0
        assert np.isnan(output.data[0,
                                    4])  # TODO: how to handle outside bounds

        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "cubic_spline",
                                       "interpolators": [ScipyGrid]
                                   })
        output = node.eval(coords_dst)
        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert int(output.data[0, 0]) == 2
        assert int(output.data[2, 4]) == 16

        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "bilinear",
                                       "interpolators": [ScipyGrid]
                                   })
        output = node.eval(coords_dst)
        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert int(output.data[0, 0]) == 2
        assert int(output.data[3, 3]) == 20
        assert np.isnan(output.data[4,
                                    4])  # TODO: how to handle outside bounds
Пример #17
0
    def test_interpolate_alt(self):
        """ for now alt uses nearest neighbor """

        source = np.random.rand(5)
        coords_src = Coordinates([clinspace(0, 10, 5)], dims=['alt'])
        coords_dst = Coordinates([clinspace(1, 11, 5)], dims=['alt'])

        node = MockArrayDataSource(source=source,
                                   native_coordinates=coords_src)
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.alt.values == coords_dst.coords['alt'])
Пример #18
0
    def test_evaluate_no_overlap_with_output_extract_output(self):
        class MockMultipleDataSource(DataSource):
            outputs = ["a", "b", "c"]
            coordinates = Coordinates([[0, 1, 2, 3], [10, 11]], dims=["lat", "lon"])

            def get_data(self, coordinates, coordinates_index):
                return self.create_output_array(coordinates, data=1)

        node = MockMultipleDataSource(output="a")
        coords = Coordinates([clinspace(-55, -45, 20), clinspace(-55, -45, 20)], dims=["lat", "lon"])
        output = node.eval(coords)

        assert np.all(np.isnan(output))
Пример #19
0
        def test_interpolate_scipy_grid(self):

            source = np.arange(0, 25)
            source.resize((5, 5))

            coords_src = Coordinates(
                [clinspace(0, 10, 5), clinspace(0, 10, 5)],
                dims=['lat', 'lon'])
            coords_dst = Coordinates(
                [clinspace(1, 11, 5), clinspace(1, 11, 5)],
                dims=['lat', 'lon'])

            # try one specific rasterio case to measure output
            node = MockArrayDataSource(source=source,
                                       native_coordinates=coords_src)
            node.interpolation = {
                'method': 'nearest',
                'interpolators': [ScipyGrid]
            }
            output = node.eval(coords_dst)

            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            print(output)
            assert output.data[0, 0] == 0.
            assert output.data[0, 3] == 3.
            assert output.data[1, 3] == 8.
            assert np.isnan(
                output.data[0, 4])  # TODO: how to handle outside bounds

            node.interpolation = {
                'method': 'cubic_spline',
                'interpolators': [ScipyGrid]
            }
            output = node.eval(coords_dst)
            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert int(output.data[0, 0]) == 2
            assert int(output.data[2, 4]) == 16

            node.interpolation = {
                'method': 'bilinear',
                'interpolators': [ScipyGrid]
            }
            output = node.eval(coords_dst)
            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert int(output.data[0, 0]) == 2
            assert int(output.data[3, 3]) == 20
            assert np.isnan(
                output.data[4, 4])  # TODO: how to handle outside bounds
Пример #20
0
        def test_raster_to_raster(self):
            coord_dst = Coordinates(
                [clinspace(5., 40., 50),
                 clinspace(-68., -66., 100)],
                dims=['lat', 'lon'])

            oLat = self.nasLat.eval(coord_dst)
            oLon = self.nasLon.eval(coord_dst)

            LON, LAT = np.meshgrid(coord_dst['lon'].coordinates,
                                   coord_dst['lat'].coordinates)

            np.testing.assert_array_almost_equal(oLat.data[..., 0], LAT)
            np.testing.assert_array_almost_equal(oLon.data[..., 0], LON)
Пример #21
0
    def test_interpolators(self):
        node = MockDataSource()
        node.eval(node.native_coordinates)

        assert isinstance(node.interpolators, OrderedDict)

        # when no interpolation happens, this returns as an empty ordered dict
        assert not node.interpolators

        # when interpolation happens, this is filled
        node.eval(
            Coordinates([clinspace(-11, 11, 7),
                         clinspace(-11, 11, 7)],
                        dims=['lat', 'lon']))
        assert 'lat' in list(node.interpolators.keys())[0]
        assert 'lon' in list(node.interpolators.keys())[0]
        assert isinstance(list(node.interpolators.values())[0], Interpolator)
Пример #22
0
    def test_interpolate_looper_helper(self):
        """ irregular interpolation """

        # Note, this test also tests the looper helper

        # try >2 dims
        source = np.random.rand(5, 5, 3, 2)
        result = source.copy()
        result[:, :, 2, :] = (result[:, :, 1, :] + result[:, :, 2, :]) / 2
        result = (result[..., 0:1] + result[..., 1:]) / 2
        result = result[[0, 1, 2, 3, 4]]
        result = result[:, [0, 1, 2, 3, 4]]
        result[-1] = np.nan
        result[:, -1] = np.nan
        coords_src = Coordinates(
            [clinspace(0, 10, 5),
             clinspace(0, 10, 5), [2, 3, 5], [0, 2]],
            dims=["lat", "lon", "time", "alt"])
        coords_dst = Coordinates(
            [clinspace(1, 11, 5),
             clinspace(1, 11, 5), [2, 3, 4], [1]],
            dims=["lat", "lon", "time", "alt"])

        node = MockArrayDataSource(
            data=source,
            coordinates=coords_src,
            interpolation=[
                {
                    "method": "nearest",
                    "interpolators": [ScipyGrid]
                },
                {
                    "method": "linear",
                    "dims": ["time", "alt"]
                },
            ],
        )
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert np.all(output.lon.values == coords_dst["lon"].coordinates)
        assert np.all(output.time.values == coords_dst["time"].coordinates)
        assert np.all(output.alt.values == coords_dst["alt"].coordinates)
        np.testing.assert_array_almost_equal(result, output.data)
Пример #23
0
    def test_get_native_coordinates(self):
        # get_native_coordinates should set the native_coordinates by default
        node = MockDataSource()
        assert node.native_coordinates is not None
        np.testing.assert_equal(node.native_coordinates['lat'].coordinates,
                                np.linspace(-25, 25, 101))
        np.testing.assert_equal(node.native_coordinates['lon'].coordinates,
                                np.linspace(-25, 25, 101))

        # but don't call get_native_coordinates if the native_coordinates are set explicitly
        nc = Coordinates([clinspace(0, 50, 101),
                          clinspace(0, 50, 101)],
                         dims=['lat', 'lon'])
        node = MockDataSource(native_coordinates=nc)
        assert node.native_coordinates is not None
        np.testing.assert_equal(node.native_coordinates['lat'].coordinates,
                                nc['lat'].coordinates)
        np.testing.assert_equal(node.native_coordinates['lat'].coordinates,
                                nc['lat'].coordinates)
Пример #24
0
        def test_interpolate_rasterio(self):
            """ regular interpolation using rasterio"""

            assert rasterio is not None

            source = np.arange(0, 15)
            source.resize((3, 5))

            coords_src = Coordinates(
                [clinspace(0, 10, 3), clinspace(0, 10, 5)],
                dims=['lat', 'lon'])
            coords_dst = Coordinates(
                [clinspace(1, 11, 3), clinspace(1, 11, 5)],
                dims=['lat', 'lon'])

            # try one specific rasterio case to measure output
            node = MockArrayDataSource(source=source,
                                       native_coordinates=coords_src)
            node.interpolation = {'method': 'min', 'interpolators': [Rasterio]}
            output = node.eval(coords_dst)

            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert output.data[0, 3] == 3.
            assert output.data[0, 4] == 4.

            node.interpolation = {'method': 'max', 'interpolators': [Rasterio]}
            output = node.eval(coords_dst)
            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert output.data[0, 3] == 9.
            assert output.data[0, 4] == 9.

            node.interpolation = {
                'method': 'bilinear',
                'interpolators': [Rasterio]
            }
            output = node.eval(coords_dst)
            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert int(output.data[0, 0]) == 1
            assert int(output.data[0, 4]) == 5
Пример #25
0
    def test_evaluate_with_output_transpose(self):
        # initialize coords with dims=[lon, lat]
        lat = clinspace(10, 20, 11)
        lon = clinspace(10, 15, 6)
        coords = Coordinates([lat, lon], dims=['lat', 'lon'])

        # evaluate with dims=[lat, lon], passing in the output
        node = MockDataSource()
        output = node.create_output_array(coords.transpose('lon', 'lat'))
        returned_output = node.eval(coords, output=output)

        # returned output sohuld match the requested coordinates
        assert returned_output.dims == ('lat', 'lon')

        # dims should stay in the order of the output, rather than the order of the requested coordinates
        assert output.dims == ('lon', 'lat')

        # output data and returned output data should match
        np.testing.assert_equal(
            output.transpose('lat', 'lon').data, returned_output.data)
Пример #26
0
    def test_interpolate_rasterio_descending(self):
        """should handle descending"""

        source = np.random.rand(5, 5)
        coords_src = Coordinates(
            [clinspace(10, 0, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
        coords_dst = Coordinates(
            [clinspace(2, 12, 5), clinspace(2, 12, 5)], dims=["lat", "lon"])

        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "nearest",
                                       "interpolators": [Rasterio]
                                   })
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        assert np.all(output.lat.values == coords_dst["lat"].coordinates)
        assert np.all(output.lon.values == coords_dst["lon"].coordinates)
Пример #27
0
    def test_dim_in(self):
        interpolator = Interpolator(methods_supported=["test"], method="test")

        coords = Coordinates(
            [clinspace(0, 10, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
        assert interpolator._dim_in("lat", coords)
        assert interpolator._dim_in("lat", coords, unstacked=True)
        assert not interpolator._dim_in("time", coords)

        coords_two = Coordinates([clinspace(0, 10, 5)], dims=["lat"])
        assert interpolator._dim_in("lat", coords, coords_two)
        assert not interpolator._dim_in("lon", coords, coords_two)

        coords_three = Coordinates(
            [(np.linspace(0, 10, 5), np.linspace(0, 10, 5))], dims=["lat_lon"])
        assert not interpolator._dim_in("lat", coords, coords_two,
                                        coords_three)
        assert interpolator._dim_in("lat",
                                    coords,
                                    coords_two,
                                    coords_three,
                                    unstacked=True)
Пример #28
0
    def test_interpolate_irregular_arbitrary_swap(self):
        """should handle descending"""

        source = np.random.rand(5, 5)
        coords_src = Coordinates(
            [clinspace(0, 10, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
        coords_dst = Coordinates(
            [clinspace(2, 12, 5), clinspace(2, 12, 5)], dims=["lat", "lon"])

        node = MockArrayDataSource(data=source,
                                   coordinates=coords_src,
                                   interpolation={
                                       "method": "nearest",
                                       "interpolators": [ScipyGrid]
                                   })
        output = node.eval(coords_dst)

        assert isinstance(output, UnitsDataArray)
        np.testing.assert_array_equal(output.lat.values,
                                      coords_dst["lat"].coordinates)
        np.testing.assert_array_equal(output.lon.values,
                                      coords_dst["lon"].coordinates)
Пример #29
0
        def test_interpolate_irregular_arbitrary_swap(self):
            """should handle descending"""

            source = np.random.rand(5, 5)
            coords_src = Coordinates(
                [clinspace(0, 10, 5), clinspace(0, 10, 5)],
                dims=['lat', 'lon'])
            coords_dst = Coordinates(
                [clinspace(2, 12, 5), clinspace(2, 12, 5)],
                dims=['lat', 'lon'])

            node = MockArrayDataSource(source=source,
                                       native_coordinates=coords_src,
                                       interpolation={
                                           'method': 'nearest',
                                           'interpolators': [ScipyGrid]
                                       })
            output = node.eval(coords_dst)

            assert isinstance(output, UnitsDataArray)
            assert np.all(output.lat.values == coords_dst.coords['lat'])
            assert np.all(output.lon.values == coords_dst.coords['lon'])
Пример #30
0
    def test_get_data(self):
        """get data from wcs server"""

        self.mock_requests()
        node = WCS(source=self.source)
        lat = node.native_coordinates['lat'].coordinates
        lon = node.native_coordinates['lon'].coordinates
        time = node.native_coordinates['time'].coordinates

        # no time
        notime_coordinates = Coordinates([
            clinspace(lat[0], lat[-2], 10),
            clinspace(lon[0], lon[-2], 10),
            '2006-06-14T17:00:00'],
            dims=['lat', 'lon', 'time'])

        with pytest.raises(ValueError):
            output = node.eval(notime_coordinates)
            assert isinstance(output, UnitsDataArray)
            assert output.native_coordinates['lat'][0] == node.native_coordinates['lat'][0]

        # time
        time_coordinates = Coordinates([
            clinspace(lat[0], lat[-2], 10),
            clinspace(lon[0], lon[-2], 10),
            clinspace(time[0], time[-1], len(time))],
            dims=['lat', 'lon', 'time'])

        with pytest.raises(ValueError):
            output = node.eval(time_coordinates)
            assert isinstance(output, UnitsDataArray)

        # requests exceptions
        self.mock_requests(data_status_code=400)
        with pytest.raises(Exception):
            output = node.eval(time_coordinates)
        with pytest.raises(Exception):
            output = node.eval(time_coordinates)