def test_band_dimension_set_labels(): bdim = BandDimension(name="bs", bands=[Band('some_name',None,None)]) metadata = CollectionMetadata({},dimensions=[bdim]) newdim = metadata.rename_labels("bs",target=['1','2','3']).band_dimension assert metadata.band_dimension.band_names == ['some_name'] assert newdim.band_names == ['1','2','3']
def test_band_dimension_rename_labels(): b02 = Band("B02", "blue", 0.490) b03 = Band("B03", "green", 0.560) b04 = Band("B04", "red", 0.665) bdim = BandDimension(name="bs", bands=[b02, b03, b04]) metadata = CollectionMetadata({},dimensions=[bdim]) newdim = metadata.rename_labels("bs",target=['1','2','3']).band_dimension assert metadata.band_dimension.band_names == ['B02','B03','B04'] assert newdim.band_names == ['1','2','3']
def load_collection(self, collection_id: str, load_params: LoadParameters, env: EvalEnv) -> DummyDataCube: _register_load_collection_call(collection_id, load_params) if collection_id in _collections: return _collections[collection_id] metadata = CollectionMetadata( metadata=self.get_collection_metadata(collection_id)) if load_params.bands: metadata = metadata.filter_bands(load_params.bands) image_collection = DummyDataCube(metadata=metadata) _collections[collection_id] = image_collection return image_collection
def test_get_dimensions_cube_dimensions_eo_bands(): dims = CollectionMetadata._parse_dimensions({ "cube:dimensions": { "x": {"type": "spatial", "extent": [-10, 10]}, "y": {"type": "spatial", "extent": [-56, 83], "reference_system": 123}, "t": {"type": "temporal", "extent": ["2020-02-20", None]}, "spectral": {"type": "bands", "values": ["r", "g", "b"]}, }, "summaries": { "eo:bands": [ {"name": "r", "common_name": "red", "center_wavelength": 5}, {"name": "g", "center_wavelength": 8}, {"name": "b", "common_name": "blue"}, ] } }) assert_same_dimensions(dims, [ SpatialDimension(name="x", extent=[-10, 10]), SpatialDimension(name="y", extent=[-56, 83], crs=123), TemporalDimension(name="t", extent=["2020-02-20", None]), BandDimension(name="spectral", bands=[ Band("r", "red", 5), Band("g", None, 8), Band("b", "blue", None), ]) ])
def load_collection(self, collection_id: str, arguments: dict, metadata: dict = None) -> 'DryRunDataCube': """Create a DryRunDataCube from a `load_collection` process.""" # TODO: avoid VITO/Terrascope specific handling here? properties = { **CollectionMetadata(metadata).get("_vito", "properties", default={}), **arguments.get("properties", {}) } trace = DataSource.load_collection(collection_id=collection_id, properties=properties) self.add_trace(trace) cube = DryRunDataCube(traces=[trace], data_tracer=self, metadata=metadata) if "temporal_extent" in arguments: cube = cube.filter_temporal(*arguments["temporal_extent"]) if "spatial_extent" in arguments: cube = cube.filter_bbox(**arguments["spatial_extent"]) if "bands" in arguments: cube = cube.filter_bands(arguments["bands"]) if properties: cube = cube.filter_properties(properties) return cube
def __init__(self, node_id: str, builder: GraphBuilder, session: 'Connection', metadata: CollectionMetadata = None): self.node_id = node_id self.builder = builder self.session = session self.graph = builder.processes self.metadata = CollectionMetadata.get_or_create(metadata)
def test_get_dimensions_cube_dimensions_no_band_names(): logs = [] dims = CollectionMetadata._parse_dimensions({ "cube:dimensions": { "spectral": {"type": "bands"}, }, }, complain=logs.append) assert_same_dimensions(dims, [ BandDimension(name="spectral", bands=[]) ]) assert logs == ["No band names in dimension 'spectral'"]
def test_get_dimensions_cube_dimensions_non_standard_type(): logs = [] dims = CollectionMetadata._parse_dimensions({ "cube:dimensions": { "bar": {"type": "foo"}, }, }, complain=logs.append) assert_same_dimensions(dims, [ Dimension(type="foo", name="bar") ]) assert logs == ["Unknown dimension type 'foo'"]
def test_get_dimensions_cube_dimensions_spatial_xyt(): dims = CollectionMetadata._parse_dimensions({ "cube:dimensions": { "xx": {"type": "spatial", "extent": [-10, 10]}, "yy": {"type": "spatial", "extent": [-56, 83], "reference_system": 123}, "tt": {"type": "temporal", "extent": ["2020-02-20", None]}, } }) assert_same_dimensions(dims, [ SpatialDimension(name="xx", extent=[-10, 10]), SpatialDimension(name="yy", extent=[-56, 83], crs=123), TemporalDimension(name="tt", extent=["2020-02-20", None]), ])
def test_aggregate_polygon_result_basic(tmp_path): timeseries = { "2019-11-11T01:11:11Z": [[7, 8, 9], [10, 11, 12]], "2019-10-15T08:15:45Z": [[1, 2, 3], [4, 5, 6]], } regions = GeometryCollection( [Polygon([(0, 0), (5, 1), (1, 4)]), Polygon([(6, 1), (1, 7), (9, 9)])]) metadata = CollectionMetadata({ "cube:dimensions": { "x": { "type": "spatial" }, "b": { "type": "bands", "values": ["red", "green", "blue"] } } }) result = AggregatePolygonResult(timeseries, regions=regions, metadata=metadata) result.set_format("netcdf") assets = result.write_assets(tmp_path) theAsset = assets.popitem()[1] filename = theAsset['href'] assert 'application/x-netcdf' == theAsset['type'] assert ["red", "green", "blue"] == [b['name'] for b in theAsset['bands']] timeseries_ds = xr.open_dataset(filename) print(timeseries_ds) assert_array_equal( timeseries_ds.band_0.coords['t'].data, np.asarray([ np.datetime64('2019-10-15T08:15:45'), np.datetime64('2019-11-11T01:11:11') ])) timeseries_ds.band_0.sel(feature=1) timeseries_ds.band_0.sel(t='2019-10-16') print(timeseries_ds) assert_array_equal( 4, timeseries_ds.band_0.sel(feature=1).sel(t="2019-10-15T08:15:45Z").data)
def test_metadata_bands_dimension_no_band_dimensions(): metadata = CollectionMetadata({ "cube:dimensions": { "x": {"type": "spatial", "axis": "x"}, } }) with pytest.raises(MetadataException, match="No band dimension"): metadata.band_dimension with pytest.raises(MetadataException, match="No band dimension"): metadata.bands with pytest.raises(MetadataException, match="No band dimension"): metadata.band_common_names with pytest.raises(MetadataException, match="No band dimension"): metadata.get_band_index("red") with pytest.raises(MetadataException, match="No band dimension"): metadata.filter_bands(["red"])
def collection_metadata(self, name) -> CollectionMetadata: return CollectionMetadata(metadata=self.describe_collection(name))