def test_eval_multiple_outputs(self): lat = clinspace(45, 66, 30, name="lat") lon = clinspace(-80, 70, 40, name="lon") kernel = [[1, 2, 1]] coords = Coordinates([lat, lon]) multi = Array(source=np.random.random(coords.shape + (2, )), coordinates=coords, outputs=["a", "b"]) node = Convolution(source=multi, kernel=kernel, kernel_dims=["lat", "lon"]) o1 = node.eval(Coordinates([lat, lon])) kernel = [[[1, 2]]] coords = Coordinates([lat, lon]) multi = Array(source=np.random.random(coords.shape + (2, )), coordinates=coords, outputs=["a", "b"]) node1 = Convolution(source=multi, kernel=kernel, kernel_dims=["lat", "lon", "output"], force_eval=True) node2 = Convolution(source=multi, kernel=kernel[0], kernel_dims=["lat", "lon"], force_eval=True) o1 = node1.eval(Coordinates([lat, lon])) o2 = node2.eval(Coordinates([lat, lon])) assert np.any(o2.data != o1.data)
def test_extra_coord_dims(self): lat = clinspace(-0.25, 1.25, 7, name="lat") lon = clinspace(-0.125, 1.125, 11, name="lon") time = ["2012-05-19", "2016-01-31", "2018-06-20"] coords = Coordinates([lat, lon, time], dims=["lat", "lon", "time"]) source = Array(source=np.random.random(coords.drop("time").shape), coordinates=coords.drop("time")) node = Convolution(source=source, kernel=[[-1, 2, -1]], kernel_dims=["lat", "lon"], force_eval=True) o = node.eval(coords) assert np.all([d in ["lat", "lon"] for d in o.dims])
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
def test_eval_nan(self): lat = clinspace(45, 66, 30, name="lat") lon = clinspace(-80, 70, 40, name="lon") coords = Coordinates([lat, lon]) data = np.ones(coords.shape) data[10, 10] = np.nan source = Array(source=data, coordinates=coords) node = Convolution(source=source, kernel=[[1, 2, 1]], kernel_dims=["lat", "lon"]) o = node.eval(coords[8:12, 7:13])
def test_open_after_eval(self): # mock node data = np.random.rand(5, 5) lat = np.linspace(-10, 10, 5) lon = np.linspace(-10, 10, 5) native_coords = Coordinates([lat, lon], ["lat", "lon"]) node = Array(source=data, coordinates=native_coords) uda = node.eval(node.coordinates) ncdf = uda.to_netcdf() uda_2 = UnitsDataArray.open(ncdf) assert isinstance(uda_2, UnitsDataArray) assert np.all(uda_2.data == uda.data) assert "layer_style" in uda_2.attrs assert uda_2.attrs.get("layer_style").json == uda.attrs.get( "layer_style").json assert "crs" in uda_2.attrs assert uda_2.attrs.get("crs") == uda.attrs.get("crs")
def test_coords_order(self): lat = clinspace(-0.25, 1.25, 7, name="lat") lon = clinspace(-0.125, 1.125, 11, name="lon") coords = Coordinates([lat, lon]) lat = clinspace(0, 1, 5, name="lat") lon = clinspace(0, 1, 9, name="lon") coords1 = Coordinates([lat, lon]) coords2 = Coordinates([lon, lat]) source = Array(source=np.random.random(coords.shape), coordinates=coords) node = Convolution(source=source, kernel=[[-1, 2, -1]], kernel_dims=["lat", "lon"], force_eval=True) o1 = node.eval(coords1) o2 = node.eval(coords2) assert np.all(o2.data == o1.data.T)
def make_rot_array(self, order=1, bands=1): # order = -1 # bands = 3 rc = RotatedCoordinates(shape=(2, 4), theta=np.pi / 8, origin=[10, 20], step=[-2.0, 1.0], dims=["lat", "lon"][::order]) c = Coordinates([rc]) node = Array( source=np.arange(8 * bands).reshape(3 - order, 3 + order, bands), coordinates=c, outputs=[str(s) for s in list(range(bands))], ) return node
def make_rot_array(self, order=1, bands=1): if order == 1: geotransform = (10.0, 1.879, -1.026, 20.0, 0.684, 2.819) else: # I think this requires changing the geotransform? Not yet supported raise NotImplementedError("TODO") rc = AffineCoordinates(geotransform=geotransform, shape=(2, 4)) c = Coordinates([rc], crs="EPSG:4326") node = Array( source=np.arange(8 * bands).reshape(3 - order, 3 + order, bands), coordinates=c, outputs=[str(s) for s in list(range(bands))], ) return node
def test_partial_source_convolution(self): lat = clinspace(-0.25, 1.25, 7, name="lat") lon = clinspace(-0.125, 1.125, 11, name="lon") time = ["2012-05-19", "2016-01-31", "2018-06-20"] coords = Coordinates([lat, lon, time], dims=["lat", "lon", "time"]) source = Array(source=np.random.random(coords.shape), coordinates=coords) node = Convolution(source=source, kernel=[[-1, 2, -1]], kernel_dims=["lat", "lon"], force_eval=True) o = node.eval(coords[:, 1:-1, :]) expected = source.source[:, 1: -1] * 2 - source.source[:, 2:] - source.source[:, : -2] assert np.abs(o.data - expected).max() < 1e-14
def test_missing_source_dims(self): """ When the kernel has more dimensions than the source, sum out the kernel for the missing dim""" lat = clinspace(-0.25, 1.25, 7, name="lat") lon = clinspace(-0.125, 1.125, 11, name="lon") time = ["2012-05-19", "2016-01-31", "2018-06-20"] coords = Coordinates([lat, lon, time], dims=["lat", "lon", "time"]) coords2 = Coordinates([lat[[1, 2, 4]], lon, time], dims=["lat", "lon", "time"]) source = Array(source=np.random.random(coords.drop("time").shape), coordinates=coords.drop("time")) node = Convolution(source=source, kernel=[[[-1], [2], [-1]]], kernel_dims=["lat", "lon", "time"], force_eval=True) o = node.eval(coords[:, 1:-1, :]) expected = source.source[:, 1: -1] * 2 - source.source[:, 2:] - source.source[:, : -2] assert np.abs(o.data - expected).max() < 1e-14 # Check when request has an ArrayCoordinates1d node = Convolution(source=source, kernel_type="mean,3", kernel_dims=["lat", "lon", "time"], force_eval=True) o = node.eval(coords2[:, 1:-1]) expected = ( source.source[[1, 2, 4], 1:-1] + source.source[[0, 1, 2], 1:-1] + source.source[[2, 4, 6], 1:-1] + source.source[[1, 2, 4], :-2] + source.source[[0, 1, 2], :-2] + source.source[[2, 4, 6], :-2] + source.source[[1, 2, 4], 2:] + source.source[[0, 1, 2], 2:] + source.source[[2, 4, 6], 2:]) / 9 assert np.abs(o.data - expected).max() < 1e-14 # Check to make sure array coordinates for a single coordinate is ok... o = node.eval(coords2[0, 1:-1])
class TestReprojection(object): source_coords = Coordinates( [clinspace(0, 8, 9, "lat"), clinspace(0, 8, 9, "lon")]) coarse_coords = Coordinates( [clinspace(0, 8, 3, "lat"), clinspace(0, 8, 3, "lon")]) source = Array(source=np.arange(81).reshape(9, 9), coordinates=source_coords, interpolation="nearest") source_coarse = Array(source=[[0, 4, 8], [36, 40, 44], [72, 76, 80]], coordinates=coarse_coords, interpolation="bilinear") source_coarse2 = Array( source=[[0, 4, 8], [36, 40, 44], [72, 76, 80]], coordinates=coarse_coords.transform("EPSG:3857"), interpolation="bilinear", ) def test_reprojection_Coordinates(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords) o1 = reproject.eval(self.source_coords) o2 = self.source_coarse.eval(self.source_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.source_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_source_coords(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.source_coarse) o1 = reproject.eval(self.coarse_coords) o2 = self.source_coarse.eval(self.coarse_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.coarse_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_source_dict(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords.definition) o1 = reproject.eval(self.coarse_coords) o2 = self.source_coarse.eval(self.coarse_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.coarse_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_source_str(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords.json) o1 = reproject.eval(self.coarse_coords) o2 = self.source_coarse.eval(self.coarse_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.coarse_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_Coordinates_crs(self): # same eval and source but different reproject reproject = Reproject( source=self.source, interpolation={ "method": "bilinear", "params": { "fill_value": "extrapolate" } }, coordinates=self.coarse_coords.transform("EPSG:3857"), ) o1 = reproject.eval(self.source_coords) # We have to use a second source here because the reprojected source # gets interpreted as having it's source coordinates in EPSG:3857 # and when being subsampled, there's a warping effect... o2 = self.source_coarse2.eval(self.source_coords) assert_almost_equal(o1.data, o2.data, decimal=13) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.source_coords) assert_array_equal(o1.data, o3.data) # same eval and reproject but different source o1 = reproject.eval(self.source_coords.transform("EPSG:3857")) o2 = self.source_coarse2.eval( self.source_coords.transform("EPSG:3857")) assert_almost_equal(o1.data, o2.data, decimal=13) # same source and reproject but different eval reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords) o1 = reproject.eval(self.source_coords.transform("EPSG:3857")) o2 = self.source_coarse.eval(self.source_coords.transform("EPSG:3857")) assert_almost_equal(o1.data, o2.data, decimal=13)
class TestReprojection(object): source_coords = Coordinates( [clinspace(0, 8, 9, "lat"), clinspace(0, 8, 9, "lon")]) coarse_coords = Coordinates( [clinspace(0, 8, 3, "lat"), clinspace(0, 8, 3, "lon")]) source = Array(source=np.arange(81).reshape(9, 9), coordinates=source_coords, interpolation="nearest") source_coarse = Array(source=[[0, 4, 8], [36, 40, 44], [72, 76, 80]], coordinates=coarse_coords, interpolation="bilinear") def test_reprojection_Coordinates(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords) o1 = reproject.eval(self.source_coords) o2 = self.source_coarse.eval(self.source_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.source_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_source_coords(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.source_coarse) o1 = reproject.eval(self.coarse_coords) o2 = self.source_coarse.eval(self.coarse_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.coarse_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_source_dict(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords.definition) o1 = reproject.eval(self.coarse_coords) o2 = self.source_coarse.eval(self.coarse_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.coarse_coords) assert_array_equal(o1.data, o3.data) def test_reprojection_source_str(self): reproject = Reproject(source=self.source, interpolation="bilinear", coordinates=self.coarse_coords.json) o1 = reproject.eval(self.coarse_coords) o2 = self.source_coarse.eval(self.coarse_coords) assert_array_equal(o1.data, o2.data) node = podpac.Node.from_json(reproject.json) o3 = node.eval(self.coarse_coords) assert_array_equal(o1.data, o3.data)